From fdf94f8b8da3dfbe76043e074f12b99829cc2d2b Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Sun, 29 Sep 2019 09:23:42 +0200 Subject: [PATCH] Better Graphs #4 --- app/class/ChartManager.php | 6 + app/templates/part/head.phtml | 2 +- app/views/Ajax.php | 240 ++++++++++++++++------------------ 3 files changed, 121 insertions(+), 127 deletions(-) diff --git a/app/class/ChartManager.php b/app/class/ChartManager.php index 56435cf..f1d2c96 100644 --- a/app/class/ChartManager.php +++ b/app/class/ChartManager.php @@ -1,4 +1,10 @@ - + diff --git a/app/views/Ajax.php b/app/views/Ajax.php index 50c9fc6..485e4d1 100644 --- a/app/views/Ajax.php +++ b/app/views/Ajax.php @@ -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(); - -} -}