initial working status (overview for each device)
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/*
|
||||
* dbQuery.php
|
||||
*
|
||||
* This file is part of the VE.Direct web UI
|
||||
* https://git.schauaus.at/VE-Tools/VE.Direct-webui
|
||||
* It is licensed under the GPL3.0 or later.
|
||||
* You should have received a copy of the license with this software
|
||||
*/
|
||||
|
||||
$config = json_decode( file_get_contents('../config.json') );
|
||||
$dbPath = (str_starts_with($config->database, '/')) ? $config->database : dirname(__FILE__) . '/../' . $config->database;
|
||||
try {
|
||||
$db = new SQLite3($dbPath, SQLITE3_OPEN_READONLY);
|
||||
}
|
||||
catch (Exception $e) {
|
||||
returnJson(['error'=>'Cannot conect to database! ' . $e->getMessage()], 400);
|
||||
}
|
||||
switch ($_REQUEST['q']) {
|
||||
case 'deviceData':
|
||||
getDeviceData();
|
||||
break;
|
||||
case 'devices':
|
||||
getDevices();
|
||||
break;
|
||||
case 'metadata':
|
||||
getMetaData();
|
||||
break;
|
||||
default:
|
||||
returnJson(['error'=>'No request provided!'], 400);
|
||||
}
|
||||
|
||||
function getDeviceData() {
|
||||
if(!isset($_REQUEST['device'])) returnJson(['error'=>'No device provided'], 400);
|
||||
$sql = "SELECT date, time, data FROM '" . $_REQUEST['device'] . "' ORDER BY date ASC, time ASC;";
|
||||
$data = queryDb($sql);
|
||||
if(!$data) returnJson(['error'=>'No data found'], 400);
|
||||
returnJson($data);
|
||||
}
|
||||
|
||||
function getDevices () {
|
||||
$sql = "SELECT name FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%' AND name <> 'meta' ORDER BY name ASC;";
|
||||
$data = queryDb($sql);
|
||||
if(!$data) returnJson(['error'=>'No devices found'], 400);
|
||||
returnJson($data);
|
||||
}
|
||||
|
||||
function getMetaData() {
|
||||
$sql = "SELECT * FROM meta;";
|
||||
$data = queryDb($sql);
|
||||
if(!$data) returnJson(['error'=>'No metadata found in the database!'], 400);
|
||||
returnJson($data);
|
||||
}
|
||||
|
||||
function queryDb($sql) {
|
||||
global $db;
|
||||
$res = $db->query($sql);
|
||||
if(!$res) return false;
|
||||
$data = [];
|
||||
while ( $resLine = $res->fetchArray(SQLITE3_NUM) )
|
||||
array_push($data, $resLine);
|
||||
if(count($data) == 0) return false;
|
||||
return $data;
|
||||
}
|
||||
|
||||
function returnJson($data, $status = 200) {
|
||||
http_response_code($status); //HTTP status code
|
||||
header('Content-Type: application/json; charset=utf-8'); //HTTP headers
|
||||
echo json_encode($data, JSON_UNESCAPED_UNICODE); //send data
|
||||
exit();
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user