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 mixed $range contains 'from' and 'to' as unix timestamps
* @param int $maxVals maximum measvals to be transmitted; if more are
* present in the timespan, the avarage will be calculated
* @param mixed $maxVals array containing:
* '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)
*/
@ -150,7 +154,6 @@ class BlueWeather
$locId = $this->_con->real_escape_string($locId);
$range["from"] = $this->_con->real_escape_string($range["from"]);
$range["to"] = $this->_con->real_escape_string($range["to"]);
$maxVals = $this->_con->real_escape_string($maxVals);
$sql = "SELECT * FROM `locations`
WHERE`id`=$locId";
@ -163,6 +166,10 @@ class BlueWeather
$range['from'] = time() - 24 * 60 * 60;
}
if ($range['from'] < 0) {
$range['from'] = time() + $range['from'];
}
if (!isset($range['to']) || $range['to'] === "") {
$range['to'] = time();
}
@ -198,7 +205,7 @@ class BlueWeather
$result = $this->_con->query($sql);
//loop through the returned data
// get all necessaray valuetypes
while ($row = $result->fetch_assoc()) {
foreach ($sensors as $sensor) {
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
$finalMeasvals = array();
foreach ($sensors as $sensor) {
// get all measvalues of the current sensor
$rawMeasvalsOfThisSensor = array();
$finalMeasvalsOfThisSensor = array();
// get all measvalues of the current sensor
foreach ($measvalues as $measvalue) {
if ($measvalue["sensorid"] === $sensor["id"]) {
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, $rawMeasvalsOfThisSensor
);
continue;
}
// 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, 0)
);
switch($maxVals['mode']) {
case "newest":
$finalMeasvalsOfThisSensor = array_slice(
$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;
$tmpMeasvalueSum = 0;
$tmpTimestampSum = 0;
$takenValuesForNextSum = 0;
$tmpMeasvalueSum = 0;
$tmpTimestampSum = 0;
for ($i = 0; $i < count($rawMeasvalsOfThisSensor); $i++) {
// loop through all measvals of the sensor
$measvalue = $rawMeasvalsOfThisSensor[$i];
for ($i = 0; $i < count($rawMeasvalsOfThisSensor); $i++) {
// loop through all measvals of the sensor
$measvalue = $rawMeasvalsOfThisSensor[$i];
if ($measvalue["sensorid"] === $sensor["id"]) {
$tmpMeasvalueSum += $measvalue["measvalue"];
$tmpTimestampSum += $measvalue["timestamp"];
$takenValuesForNextSum += 1;
}
if ($takenValuesForNextSum === $countOfValuesForAvarage || $i === count($rawMeasvalsOfThisSensor) - 1) {
array_push(
$finalMeasvalsOfThisSensor,
array(
"measvalue" => round(
$tmpMeasvalueSum / $takenValuesForNextSum, 2
),
"timestamp" => round(
$tmpTimestampSum / $takenValuesForNextSum, 0
),
"sensorid" => $sensor["id"]
)
);
$takenValuesForNextSum = 0;
$tmpMeasvalueSum = 0;
$tmpTimestampSum = 0;
if ($measvalue["sensorid"] === $sensor["id"]) {
$tmpMeasvalueSum += $measvalue["measvalue"];
$tmpTimestampSum += $measvalue["timestamp"];
$takenValuesForNextSum += 1;
}
if ($takenValuesForNextSum === $countOfValuesForAvarage
|| $i === count($rawMeasvalsOfThisSensor) - 1
) {
array_push(
$finalMeasvalsOfThisSensor,
array(
"measvalue" => round(
$tmpMeasvalueSum / $takenValuesForNextSum, 2
),
"timestamp" => round(
$tmpTimestampSum / $takenValuesForNextSum, 0
),
"sensorid" => $sensor["id"]
)
);
$takenValuesForNextSum = 0;
$tmpMeasvalueSum = 0;
$tmpTimestampSum = 0;
}
}
break;
}
// insert the new vals of this sensor into the global new vals