Better Graphs #4

This commit is contained in:
JonatanRek 2019-09-29 09:23:42 +02:00
parent 7e047ee0a3
commit fdf94f8b8d
3 changed files with 121 additions and 127 deletions

View File

@ -1,4 +1,10 @@
<?php
class ChartScale{
const HOUR = 'info';
const DAY = 'warning';
const MONTH = 'warning';
const YEAR = 'error';
}
class ChartManager{
function generateChart($data, $min = 0, $max = 100)
{

View File

@ -18,7 +18,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="./app/templates/css/main.css">
<link rel="stylesheet" href="./app/templates/css/main.css?v2">
<link rel="stylesheet" href="./app/templates/css/font-awesome.min.css">
<link rel="stylesheet" href="./app/templates/css/modal.css">
<link rel="stylesheet" href="./app/templates/css/pre.css">

View File

@ -50,44 +50,132 @@ class Ajax extends Template
} else if (isset($_POST['subDevice']) && isset($_POST['action']) && $_POST['action'] == "chart") {
//TODO lepe rozstrukturovat
$subDeviceId = $_POST['subDevice'];
$period = $_POST['period'];
$groupBy = $_POST['group'];
$subDevice = SubDeviceManager::getSubDevice($subDeviceId);
$records = RecordManager::getAllRecordForGraph($subDeviceId);
$records = RecordManager::getAllRecordForGraph($subDeviceId, $period, $groupBy);
$array = array_column($records, 'value');
$arrayTime = array_column($records, 'time');
$output = [];
foreach ($array as $key => $value) {
/*if ($value == 1 || $value == 0)
{
$output[$key]['y'] = $value;
if ($subDevice['type'] == 'light'){
if ($value > 810){
$output[$key]['y'] = 1;
} else {
$output[$key]['y'] = 0;
}
}
$timeStamp = new DateTime($arrayTime[$key]);
$output[$key]['t'] = $timeStamp->format("Y-m-d") . 'T' . $timeStamp->format("H:i:s") . 'Z';
}
if ($value > 810){
$output[$key]['y'] = 1;
} else {
$output[$key]['y'] = 0;
}*/
$output[$key]['y'] = $value;
$timeStamp = new DateTime($arrayTime[$key]);
$output[$key]['t'] = $timeStamp->format("Y-m-d") . 'T' . $timeStamp->format("H:i:s") . 'Z';
$data = json_encode($output);
$arrayTimeStamps = array_column($records, 'time');
foreach ($arrayTimeStamps as $key => $value) {
$arrayTimeStamps[$key] = (new DateTime($value))->format(TIMEFORMAT);
}
$labels = json_encode($arrayTimeStamps);
$range = RANGES[$subDevice['type']];
$graphType = $range['graph'];
header('Content-Type: application/json');
$JSON = '
{
"type": "' . $graphType . '",
"data": {
"datasets": [{
"data": ' . $data . '
}]
},
"options": {
"scales": {
"xAxes": [{
"type": "time",
"distribution": "linear"
}],
"yAxes": [{
"ticks": {
"min": ' . $range['min'] . ',
"max": ' . $range['max'] . ',
"steps": ' . $range['scale'] . '
}
}]
},
"legend": {
"display": false
},
"tooltips": {
"enabled": true
},
"hover": {
"mode": true
}
}
}';
echo $JSON;
die();
} else if (isset($_POST['action']) && $_POST['action'] == "getState") {
//State Update
$roomsData = RoomManager::getAllRooms();
$subDevices = [];
foreach ($roomsData as $roomKey => $roomsData) {
$devicesData = DeviceManager::getAllDevicesInRoom($roomsData['room_id']);
foreach ($devicesData as $deviceKey => $deviceData) {
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceData['device_id']);
foreach ($subDevicesData as $subDeviceKey => $subDeviceData) {
$lastRecord = RecordManager::getLastRecord($subDeviceData['subdevice_id']);
$parsedValue = round($lastRecord['value']);
//TODO: Předelat na switch snažší přidávání
/*Value Parsing*/
if ($subDeviceData['type'] == "on/off") {
$parsedValue = ($parsedValue == 1 ? 'ON' : 'OFF');
}
if ($subDeviceData['type'] == "light") {
$replacementTrue = 'Light';
$replacementFalse = 'Dark';
if ($parsedValue != 1){
//Analog Reading
$parsedValue = ($parsedValue <= 810 ? $replacementTrue : $replacementFalse);
} else {
//Digital Reading
$parsedValue = ($parsedValue == 0 ? $replacementTrue : $replacementFalse);
}
}
if ($subDeviceData['type'] == "door") {
$replacementTrue = 'Closed';
$replacementFalse = 'Opened';
$parsedValue = ($parsedValue == 1 ? $replacementTrue : $replacementFalse);
}
$subDevices[$subDeviceData['subdevice_id']] = [
'value' => $parsedValue .$subDeviceData['unit'],
'time' => $lastRecord['time'],
];
}
}
}
echo json_encode($subDevices);
die();
} else if (isset($_POST['scene_id'])) {
$sceneId = $_POST['scene_id'];
if (isset($_POST['action']) && $_POST['action'] == 'delete') {
SceneManager::delete($sceneId);
}else {
echo SceneManager::execScene($sceneId);
}
}
die();
}
}
$data = json_encode($output);
$arrayTimeStamps = array_column($records, 'time');
foreach ($arrayTimeStamps as $key => $value) {
$arrayTimeStamps[$key] = (new DateTime($value))->format(TIMEFORMAT);
}
$labels = json_encode($arrayTimeStamps);
$range = RANGES[$subDevice['type']];
header('Content-Type: application/json');
/*$JSON = '{
"type": "line",
"data": {
@ -126,103 +214,3 @@ header('Content-Type: application/json');
}
}
}';*/
$JSON = '
{
"type": "bar",
"data": {
"datasets": [{
"data": ' . $data . '
}]
},
"options": {
"scales": {
"xAxes": [{
"type": "time",
"distribution": "linear"
}],
"yAxes": [{
"ticks": {
"min": ' . $range['min'] . ',
"max": ' . $range['max'] . ',
"steps": ' . $range['scale'] . '
}
}]
},
"legend": {
"display": false
},
"tooltips": {
"enabled": true
},
"hover": {
"mode": true
}
}
}
';
/*
*/
echo $JSON;
die();
} else if (isset($_POST['action']) && $_POST['action'] == "getState") {
//State Update
$roomsData = RoomManager::getAllRooms();
$subDevices = [];
foreach ($roomsData as $roomKey => $roomsData) {
$devicesData = DeviceManager::getAllDevicesInRoom($roomsData['room_id']);
foreach ($devicesData as $deviceKey => $deviceData) {
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceData['device_id']);
foreach ($subDevicesData as $subDeviceKey => $subDeviceData) {
$lastRecord = RecordManager::getLastRecord($subDeviceData['subdevice_id']);
$parsedValue = round($lastRecord['value']);
//TODO: Předelat na switch snažší přidávání
/*Value Parsing*/
if ($subDeviceData['type'] == "on/off") {
$parsedValue = ($parsedValue == 1 ? 'ON' : 'OFF');
}
if ($subDeviceData['type'] == "light") {
$replacementTrue = 'Light';
$replacementFalse = 'Dark';
if ($parsedValue != 1){
//Analog Reading
$parsedValue = ($parsedValue <= 810 ? $replacementTrue : $replacementFalse);
} else {
//Digital Reading
$parsedValue = ($parsedValue == 0 ? $replacementTrue : $replacementFalse);
}
}
if ($subDeviceData['type'] == "door") {
$replacementTrue = 'Closed';
$replacementFalse = 'Opened';
$parsedValue = ($parsedValue == 1 ? $replacementTrue : $replacementFalse);
}
$subDevices[$subDeviceData['subdevice_id']] = [
'value' => $parsedValue .$subDeviceData['unit'],
'time' => $lastRecord['time'],
];
}
}
}
echo json_encode($subDevices);
die();
} else if (isset($_POST['scene_id'])) {
$sceneId = $_POST['scene_id'];
if (isset($_POST['action']) && $_POST['action'] == 'delete') {
SceneManager::delete($sceneId);
}else {
echo SceneManager::execScene($sceneId);
}
}
die();
}
}