diff --git a/config/sample.ini b/config/sample.ini index 091d3d3..7adca84 100644 --- a/config/sample.ini +++ b/config/sample.ini @@ -61,6 +61,13 @@ base = 0:BEBEBE ;; Can be overridden at sensor level! ;lowbatt = user@example.net +;; (OPTIONAL) +;; Use min/avg/max values from last 5 minutes. +;; Default value for this option is "latest" +;; Workaround for slippy and shaky sensors. +;; Can be overridden at sensor level! +;align = latest|min|avg|max + ; Text between [] is an unique key for one sensor/measurement. ; It's impossible to combine two values from one wireless sensor @@ -100,6 +107,11 @@ channel = 1 ;; Disable or change recipient. ;LOWBATT = +;; (OPTIONAL) +;; Override global configuration. +;; Reset or change recalculation. +;ALIGN = latest + ;; (OPTIONAL) ;; By default processing of groups will stop after the first match. ;; You can change this behavior for sensors with multiple values. diff --git a/ism-catcher b/ism-catcher index 6fd4962..71d2d6e 100755 --- a/ism-catcher +++ b/ism-catcher @@ -380,17 +380,74 @@ abstract class ISM if($db->getPacketCount()) { - $item = $db->getPacket(-1); + $time = time(); + $align = null; - if($item[ISMDB::FIELD_TIME] < (intval(time() / self::$ini['interval']) - 1) * self::$ini['interval']) + if(isset(self::$ini[$section]['ALIGN'])) { - continue; + $align = strtolower(self::$ini[$section]['ALIGN']); } + else if(isset(self::$ini['align'])) + { + $align = strtolower(self::$ini['align']); + } + + if($align !== null && in_array($align, ['min', 'avg', 'max'], true)) + { + $values = []; + $c = -$db->getPacketCount(); + for($i = -1; $i >= $c; $i--) + { + $item = $db->getPacket($i); + + if($item[ISMDB::FIELD_TIME] < (intval($time / self::$ini['interval']) - 1) * self::$ini['interval']) + { + break; + } + + $values[] = $item[ISMDB::FIELD_VALUE]; + } + + if(!($c = count($values))) + { + continue; + } + + switch($align) + { + case 'min': + $value = min($values); + break; - self::outputLine('value', $item[ISMDB::FIELD_VALUE], $section); + case 'avg': + $value = array_sum($values) / $c; + break; + + case 'max': + $value = max($values); + break; + + default: + throw new InvalidArgumentException($align); + } + unset($values); + } + else + { + $item = $db->getPacket(-1); + + if($item[ISMDB::FIELD_TIME] < (intval($time / self::$ini['interval']) - 1) * self::$ini['interval']) + { + continue; + } + + $value = $item[ISMDB::FIELD_VALUE]; + + // TODO: Implement INI switch to output last update time? + #self::outputLine('extinfo', date('c', $item[ISMDB::FIELD_TIME]), $section); + } - // TODO: Implement INI switch to output last update time? - #self::outputLine('extinfo', date('c', $item[ISMDB::FIELD_TIME]), $section); + self::outputLine('value', $value, $section); if($item[ISMDB::FIELD_FLAGS] & self::FLAG_LOWBATT) {