blueweather/api/json.php

88 lines
2.5 KiB
PHP
Executable File

<?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';
require_once './blueweather.php';
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
$blueweather = new BlueWeather($config);
//$_GET['command'] = '{"header":1001,"body":"undefined"}';
if (isset($_GET['locId'])) {
// get data of given location
$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;
} 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
if (!isset($request['body']['username']) || !isset($request['body']['password'])) {
$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);
}
}
}
}
} else {
$data = $blueweather->getAllLocations();
}
http_response_code(200);
echo json_encode($data);