added new maxVals modes (see README.md)

This commit is contained in:
dorian 2019-07-30 13:07:49 +02:00
parent 142d7fc81b
commit 9981696d91

View file

@ -139,8 +139,12 @@ class BlueWeather
* *
* @param int $locId id of the location to return data of * @param int $locId id of the location to return data of
* @param mixed $range contains 'from' and 'to' as unix timestamps * @param mixed $range contains 'from' and 'to' as unix timestamps
* @param int $maxVals maximum measvals to be transmitted; if more are * @param mixed $maxVals array containing:
* present in the timespan, the avarage will be calculated * 'count': maximum measvals to be transmitted
* 'mode':
* - 'newest': will return the newest <maxVals> values
* - 'oldest': will return the oldest <maxVals> values
* - 'avg' : <maxVals> sub avarages will be calculated
* *
* @return mixed object with all information about the location (see docs) * @return mixed object with all information about the location (see docs)
*/ */
@ -150,7 +154,6 @@ class BlueWeather
$locId = $this->_con->real_escape_string($locId); $locId = $this->_con->real_escape_string($locId);
$range["from"] = $this->_con->real_escape_string($range["from"]); $range["from"] = $this->_con->real_escape_string($range["from"]);
$range["to"] = $this->_con->real_escape_string($range["to"]); $range["to"] = $this->_con->real_escape_string($range["to"]);
$maxVals = $this->_con->real_escape_string($maxVals);
$sql = "SELECT * FROM `locations` $sql = "SELECT * FROM `locations`
WHERE`id`=$locId"; WHERE`id`=$locId";
@ -163,6 +166,10 @@ class BlueWeather
$range['from'] = time() - 24 * 60 * 60; $range['from'] = time() - 24 * 60 * 60;
} }
if ($range['from'] < 0) {
$range['from'] = time() + $range['from'];
}
if (!isset($range['to']) || $range['to'] === "") { if (!isset($range['to']) || $range['to'] === "") {
$range['to'] = time(); $range['to'] = time();
} }
@ -198,7 +205,7 @@ class BlueWeather
$result = $this->_con->query($sql); $result = $this->_con->query($sql);
//loop through the returned data // get all necessaray valuetypes
while ($row = $result->fetch_assoc()) { while ($row = $result->fetch_assoc()) {
foreach ($sensors as $sensor) { foreach ($sensors as $sensor) {
if ($sensor['valuetypeid'] == $row['id']) { if ($sensor['valuetypeid'] == $row['id']) {
@ -208,67 +215,86 @@ class BlueWeather
} }
} }
if (isset($maxVals) && $maxVals > 0) { if (isset($maxVals) && isset($maxVals['count']) && isset($maxVals['mode'])) {
// build the new measvalues array with respect to maxVals for each sensor // build the new measvalues array with respect to maxVals for each sensor
$finalMeasvals = array(); $finalMeasvals = array();
foreach ($sensors as $sensor) { foreach ($sensors as $sensor) {
// get all measvalues of the current sensor
$rawMeasvalsOfThisSensor = array(); $rawMeasvalsOfThisSensor = array();
$finalMeasvalsOfThisSensor = array(); $finalMeasvalsOfThisSensor = array();
// get all measvalues of the current sensor
foreach ($measvalues as $measvalue) { foreach ($measvalues as $measvalue) {
if ($measvalue["sensorid"] === $sensor["id"]) { if ($measvalue["sensorid"] === $sensor["id"]) {
array_push($rawMeasvalsOfThisSensor, $measvalue); array_push($rawMeasvalsOfThisSensor, $measvalue);
} }
} }
if (count($rawMeasvalsOfThisSensor) <= $maxVals) { if (count($rawMeasvalsOfThisSensor) <= $maxVals['count']) {
// measvls don't exceed maxVals -> nothng needs to be done
$finalMeasvals = array_merge( $finalMeasvals = array_merge(
$finalMeasvals, $rawMeasvalsOfThisSensor $finalMeasvals, $rawMeasvalsOfThisSensor
); );
continue; continue;
} }
// always sum up the same amount of values to get a new array switch($maxVals['mode']) {
// which doesn't have more than $maxVals values case "newest":
$countOfValuesForAvarage = intval( $finalMeasvalsOfThisSensor = array_slice(
round(count($rawMeasvalsOfThisSensor) / $maxVals, 0) $rawMeasvalsOfThisSensor,
); count($rawMeasvalsOfThisSensor) - $maxVals['count'],
count($rawMeasvalsOfThisSensor)
);
break;
case "oldest":
$finalMeasvalsOfThisSensor = array_slice(
$rawMeasvalsOfThisSensor, 0, $maxVals['count']
);
break;
case "avg":
// always sum up the same amount of values to get a new array
// which doesn't have more than $maxVals values
$countOfValuesForAvarage = intval(
round(count($rawMeasvalsOfThisSensor) / $maxVals['count'], 0)
);
$takenValuesForNextSum = 0; $takenValuesForNextSum = 0;
$tmpMeasvalueSum = 0; $tmpMeasvalueSum = 0;
$tmpTimestampSum = 0; $tmpTimestampSum = 0;
for ($i = 0; $i < count($rawMeasvalsOfThisSensor); $i++) { for ($i = 0; $i < count($rawMeasvalsOfThisSensor); $i++) {
// loop through all measvals of the sensor // loop through all measvals of the sensor
$measvalue = $rawMeasvalsOfThisSensor[$i]; $measvalue = $rawMeasvalsOfThisSensor[$i];
if ($measvalue["sensorid"] === $sensor["id"]) { if ($measvalue["sensorid"] === $sensor["id"]) {
$tmpMeasvalueSum += $measvalue["measvalue"]; $tmpMeasvalueSum += $measvalue["measvalue"];
$tmpTimestampSum += $measvalue["timestamp"]; $tmpTimestampSum += $measvalue["timestamp"];
$takenValuesForNextSum += 1; $takenValuesForNextSum += 1;
} }
if ($takenValuesForNextSum === $countOfValuesForAvarage || $i === count($rawMeasvalsOfThisSensor) - 1) { if ($takenValuesForNextSum === $countOfValuesForAvarage
array_push( || $i === count($rawMeasvalsOfThisSensor) - 1
$finalMeasvalsOfThisSensor, ) {
array( array_push(
"measvalue" => round( $finalMeasvalsOfThisSensor,
$tmpMeasvalueSum / $takenValuesForNextSum, 2 array(
), "measvalue" => round(
"timestamp" => round( $tmpMeasvalueSum / $takenValuesForNextSum, 2
$tmpTimestampSum / $takenValuesForNextSum, 0 ),
), "timestamp" => round(
"sensorid" => $sensor["id"] $tmpTimestampSum / $takenValuesForNextSum, 0
) ),
); "sensorid" => $sensor["id"]
)
$takenValuesForNextSum = 0; );
$tmpMeasvalueSum = 0;
$tmpTimestampSum = 0; $takenValuesForNextSum = 0;
$tmpMeasvalueSum = 0;
$tmpTimestampSum = 0;
}
} }
break;
} }
// insert the new vals of this sensor into the global new vals // insert the new vals of this sensor into the global new vals