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";
@ -162,6 +165,10 @@ class BlueWeather
if (!isset($range['from']) || $range['from'] === "") { if (!isset($range['from']) || $range['from'] === "") {
$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)
$takenValuesForNextSum = 0; );
$tmpMeasvalueSum = 0; break;
$tmpTimestampSum = 0; case "oldest":
$finalMeasvalsOfThisSensor = array_slice(
for ($i = 0; $i < count($rawMeasvalsOfThisSensor); $i++) { $rawMeasvalsOfThisSensor, 0, $maxVals['count']
// loop through all measvals of the sensor );
$measvalue = $rawMeasvalsOfThisSensor[$i]; break;
case "avg":
if ($measvalue["sensorid"] === $sensor["id"]) { // always sum up the same amount of values to get a new array
$tmpMeasvalueSum += $measvalue["measvalue"]; // which doesn't have more than $maxVals values
$tmpTimestampSum += $measvalue["timestamp"]; $countOfValuesForAvarage = intval(
$takenValuesForNextSum += 1; round(count($rawMeasvalsOfThisSensor) / $maxVals['count'], 0)
} );
if ($takenValuesForNextSum === $countOfValuesForAvarage || $i === count($rawMeasvalsOfThisSensor) - 1) {
array_push( $takenValuesForNextSum = 0;
$finalMeasvalsOfThisSensor, $tmpMeasvalueSum = 0;
array( $tmpTimestampSum = 0;
"measvalue" => round(
$tmpMeasvalueSum / $takenValuesForNextSum, 2 for ($i = 0; $i < count($rawMeasvalsOfThisSensor); $i++) {
), // loop through all measvals of the sensor
"timestamp" => round( $measvalue = $rawMeasvalsOfThisSensor[$i];
$tmpTimestampSum / $takenValuesForNextSum, 0
), if ($measvalue["sensorid"] === $sensor["id"]) {
"sensorid" => $sensor["id"] $tmpMeasvalueSum += $measvalue["measvalue"];
) $tmpTimestampSum += $measvalue["timestamp"];
); $takenValuesForNextSum += 1;
}
$takenValuesForNextSum = 0; if ($takenValuesForNextSum === $countOfValuesForAvarage
$tmpMeasvalueSum = 0; || $i === count($rawMeasvalsOfThisSensor) - 1
$tmpTimestampSum = 0; ) {
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 // insert the new vals of this sensor into the global new vals