blueweather/api/json.php

108 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2019-07-13 20:02:47 +02:00
<?php
/**
* BlueWeather
*
* PHP version 7.2
*
* @param locId
* @param range (range[from]: from UNIX time; range[to]: to UNIX time)
2019-07-16 17:42:33 +02:00
* @param maxVals (maximum measvals to be transmitted; if more are present
* in the timespan, the avarage will be calculated)
2019-07-13 20:02:47 +02:00
*
* @category Tools
* @package BlueWeather
* @author Dorian Zedler <dorian@itsblue.de>
* @license GPLV3 gpl.com
* @link itsblue.de
*/
require_once './config.php';
2019-07-15 20:29:19 +02:00
require_once './blueweather.php';
2019-07-13 20:02:47 +02:00
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
2019-07-15 20:29:19 +02:00
$blueweather = new BlueWeather($config);
2019-07-13 20:02:47 +02:00
2019-08-07 23:03:59 +02:00
//$_GET['command'] = '{"header":1001,"body":"undefined"}';
2019-07-13 20:02:47 +02:00
if (isset($_GET['locId'])) {
// get data of given location
2019-07-16 17:42:33 +02:00
$data = $blueweather->getLocationData(
$_GET['locId'], $_GET['range'], $_GET['maxVals']
);
} elseif (isset($_POST['submitSensorData'])) {
// sensor data is being submited
$sensorData = json_decode($_POST['submitSensorData'], true);
$ret = $blueweather->processSensorData($sensorData);
$data = $ret;
if ($ret['status'] !== 200) {
2019-08-08 15:27:30 +02:00
$log = "\n[".date("M,d,Y h:i:s A")."] ret: ".
json_encode($ret)." request: ".json_encode($_POST['submitSensorData']);
//echo $log;
2019-08-08 15:27:30 +02:00
file_put_contents("./error.log", $log, FILE_APPEND);
}
2019-08-07 23:03:59 +02:00
} elseif (isset($_GET['command'])) {
// some user-data api request
$request = json_decode($_GET['command'], true);
if (!$request || !isset($request['header'])) {
$data = array("header"=>400);
} else {
switch ($request['header']) {
case 1000:
// user login
2019-08-08 15:27:30 +02:00
if (!isset($request['body']['username'])
|| !isset($request['body']['password'])
) {
2019-08-07 23:03:59 +02:00
$data = array("header"=>400);
} else {
$token = $blueweather->loginUser(
$request['body']['username'],
$request['body']['password']
);
if ($token !== '') {
$data = array("header"=>200, "body"=>$token);
} else {
$data = array("header"=>401);
}
}
break;
case 1001:
// check session
if (!isset($request['body'])) {
$data = array("header"=>400);
} else {
$ret = $blueweather->getUserInfo(
$blueweather->checkSession($request['body'])
);
if ($ret === -1) {
$data = array("header"=>401);
} else {
$data = array("header"=>200, "data"=>$ret);
}
}
2019-08-08 15:11:45 +02:00
break;
case 1002:
// destroy session
if (!isset($request['body'])) {
$data = array("header"=>400);
} else {
$data = array(
"header"=>$blueweather->destroySession($request['body'])
);
}
2019-08-07 23:03:59 +02:00
}
}
2019-07-13 20:02:47 +02:00
} else {
2019-07-15 20:29:19 +02:00
$data = $blueweather->getAllLocations();
2019-07-13 20:02:47 +02:00
}
http_response_code(200);
echo json_encode($data);