added json api php code
This commit is contained in:
parent
d2a66b8a7e
commit
8464808bc8
1 changed files with 173 additions and 0 deletions
173
api/json.php
Normal file
173
api/json.php
Normal file
|
@ -0,0 +1,173 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* BlueWeather
|
||||
*
|
||||
* PHP version 7.2
|
||||
*
|
||||
* @param locId
|
||||
* @param range (range[from]: from UNIX time; range[to]: to UNIX time)
|
||||
* @param maxVals (maximum measvals to be transmitted; if more are present in the timespan, the avarage will be calculated)
|
||||
*
|
||||
* @category Tools
|
||||
* @package BlueWeather
|
||||
* @author Dorian Zedler <dorian@itsblue.de>
|
||||
* @license GPLV3 gpl.com
|
||||
* @link itsblue.de
|
||||
*/
|
||||
|
||||
require_once './config.php';
|
||||
|
||||
|
||||
|
||||
header('Content-Type: application/json');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
|
||||
$con = mysqli_connect($config['dbhost'], $config['dbuser'], $config['dbpassword'], $config['dbname']);
|
||||
if (!$con) {
|
||||
echo "<h1>Fatal internal Error! :-/</h1>";
|
||||
echo "Error connecting to database: " . mysqli_connect_error();
|
||||
http_response_code(500);
|
||||
exit();
|
||||
}
|
||||
|
||||
if (isset($_GET['locId'])) {
|
||||
// get data of given location
|
||||
$locId = $con->real_escape_string($_GET['locId']);
|
||||
$range['from'] = $con->real_escape_string($_GET['range']['from']);
|
||||
$range['to'] = $con->real_escape_string($_GET['range']['to']);
|
||||
$maxVals = $con->real_escape_string($_GET['maxVals']);
|
||||
|
||||
$sql = "SELECT * FROM `locations`
|
||||
WHERE`id`=$locId";
|
||||
$result = $con->query($sql);
|
||||
|
||||
// only one row will be returned
|
||||
$data = $result->fetch_assoc();
|
||||
|
||||
if (!isset($range['from']) || $range['from'] === "") {
|
||||
$range['from'] = time() - 24 * 60 * 60;
|
||||
}
|
||||
|
||||
if (!isset($range['to']) || $range['to'] === "") {
|
||||
$range['to'] = time();
|
||||
}
|
||||
|
||||
// get all measvalues of given location
|
||||
$sql = "SELECT M.measvalue,M.sensorid,M.timestamp FROM measvalues M
|
||||
JOIN sensors S ON M.sensorid = S.id
|
||||
JOIN locations L ON S.locationid=L.id
|
||||
WHERE L.id=$locId AND M.timestamp > " . $range['from'] . " AND M.timestamp < " . $range['to'] . "
|
||||
ORDER BY timestamp ASC";
|
||||
|
||||
$result = $con->query($sql);
|
||||
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$measvalues[] = $row;
|
||||
}
|
||||
|
||||
// get all sensors of given location
|
||||
$sql = "SELECT * FROM `sensors`
|
||||
WHERE `locationid` = $locId";
|
||||
|
||||
$result = $con->query($sql);
|
||||
|
||||
//loop through the returned data
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
unset($row['locationid']); // remove locId as it is redundant
|
||||
$sensors[] = $row;
|
||||
}
|
||||
|
||||
// get all value types
|
||||
$sql = "SELECT * FROM `valuetypes`";
|
||||
|
||||
$result = $con->query($sql);
|
||||
|
||||
//loop through the returned data
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
foreach ($sensors as $sensor) {
|
||||
if ($sensor['valuetypeid'] == $row['id']) {
|
||||
$valuetypes[] = $row;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($maxVals) && $maxVals > 0 && count($measvalues) > $maxVals) {
|
||||
// build the new measvalues array with respect to maxVals for each sensor
|
||||
|
||||
$finalMeasvals = array();
|
||||
|
||||
foreach ($sensors as $sensor) {
|
||||
|
||||
|
||||
$rawMeasvalsOfThisSensor = array();
|
||||
$finalMeasvalsOfThisSensor = array();
|
||||
|
||||
foreach ($measvalues as $measvalue) {
|
||||
if ($measvalue["sensorid"] === $sensor["id"]) {
|
||||
array_push($rawMeasvalsOfThisSensor, $measvalue);
|
||||
}
|
||||
}
|
||||
|
||||
$countOfValuesForAvarage = intval(round(count($rawMeasvalsOfThisSensor) / $maxVals, 0));
|
||||
|
||||
$takenValuesForNextSum = 0;
|
||||
$tmpMeasvalueSum = 0;
|
||||
$tmpTimestampSum = 0;
|
||||
|
||||
for ($i = 0; $i < count($rawMeasvalsOfThisSensor); $i++) {
|
||||
$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" => $tmpMeasvalueSum / $takenValuesForNextSum, "timestamp" => $tmpTimestampSum / $takenValuesForNextSum, "sensorid" => $sensor["id"]));
|
||||
|
||||
$takenValuesForNextSum = 0;
|
||||
$tmpMeasvalueSum = 0;
|
||||
$tmpTimestampSum = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//print_r($finalMeasvalsOfThisSensor);
|
||||
|
||||
$finalMeasvals = array_merge($finalMeasvals, $finalMeasvalsOfThisSensor);
|
||||
}
|
||||
|
||||
$measvalues = $finalMeasvals;
|
||||
}
|
||||
|
||||
// find actual range
|
||||
$min;
|
||||
$max;
|
||||
foreach ($measvalues as $value) {
|
||||
if ($value['timestamp'] < $min || !isset($min)) {
|
||||
$min = $value['timestamp'];
|
||||
} else if ($value['timestamp'] > $max || !isset($max)) {
|
||||
$max = $value['timestamp'];
|
||||
}
|
||||
}
|
||||
|
||||
// add sensors and value types to data object
|
||||
$data['measvalues'] = $measvalues;
|
||||
$data['sensors'] = $sensors;
|
||||
$data['valuetypes'] = $valuetypes;
|
||||
$data['range'] = array('from' => $min, 'to' => $max);
|
||||
} else {
|
||||
// get all locations
|
||||
$sql = "SELECT * FROM `locations`";
|
||||
$result = $con->query($sql);
|
||||
|
||||
//loop through the returned data
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$data[] = $row;
|
||||
}
|
||||
}
|
||||
|
||||
http_response_code(200);
|
||||
|
||||
echo json_encode($data);
|
Loading…
Reference in a new issue