PHP_SMART_HOME_V3/app/api/WidgetApi.php

138 lines
3.2 KiB
PHP
Raw Normal View History

2020-05-24 17:35:19 +00:00
<?php
2020-10-14 16:33:34 +00:00
class WidgetApi extends ApiController
{
2020-05-24 17:35:19 +00:00
2020-10-14 16:33:34 +00:00
public function run($subDeviceId)
{
2020-05-24 17:35:19 +00:00
//$this->requireAuth();
2020-07-20 09:07:32 +00:00
2020-05-24 17:35:19 +00:00
$response = null;
2020-07-20 09:07:32 +00:00
if (RecordManager::getLastRecord($subDeviceId)['execuded'] === 0) {
throw new Exception("Unreachable", 409);
}
2020-05-24 17:35:19 +00:00
$subDeviceData = SubDeviceManager::getSubDevice($subDeviceId);
2020-10-14 16:33:34 +00:00
if ($subDeviceData['type'] == 'on/off') {
2020-05-24 17:35:19 +00:00
$lastValue = RecordManager::getLastRecord($subDeviceData['subdevice_id'])['value'];
RecordManager::create($subDeviceData['device_id'], 'on/off', (int) !$lastValue);
2020-05-24 17:35:19 +00:00
$response = !$lastValue;
2020-07-20 09:07:32 +00:00
} else {
throw new Exception("Bad Request", 403);
2020-05-24 17:35:19 +00:00
}
2020-07-20 09:07:32 +00:00
$i = 0;
$timeout = 20;
2020-10-14 16:33:34 +00:00
while (RecordManager::getLastRecord($subDeviceId)['execuded'] == 0) {
2020-07-20 09:07:32 +00:00
if ($i == $timeout) {
throw new Exception("Timeout", 444);
}
$i++;
usleep(250000);
}
$this->response(['value' => $response]);
2020-05-26 19:42:39 +00:00
}
2020-05-26 19:47:36 +00:00
2020-10-31 20:37:29 +00:00
public function detail($subDeviceId, $period = "day")
2020-10-14 16:33:34 +00:00
{
2020-05-26 19:47:36 +00:00
//$this->requireAuth();
2020-10-31 20:37:29 +00:00
$groupBy = [
"year" => "month",
"month" => "day",
"day" => "hour",
"hout" => "minute",
];
2020-05-26 19:47:36 +00:00
$response = null;
2020-12-07 19:21:27 +00:00
$subDeviceData = SubDeviceManager::getSubdeviceDetailById($subDeviceId);
2020-05-27 19:37:35 +00:00
2020-12-07 19:21:27 +00:00
2020-10-19 14:22:54 +00:00
//TODO: zeptat se @Patrik Je Graf Dobře Seřazený na DESC ?
2020-10-31 20:37:29 +00:00
$events = RecordManager::getAllRecordForGraph($subDeviceId, $period, $groupBy[$period]);
2020-10-19 14:22:54 +00:00
if ( count($events) == 0){
throw new Exception("No Records", 404);
}
2020-10-31 20:37:29 +00:00
//Striping executed value from dataset if pasiv device such as Senzor ETC
if ($subDeviceData['type'] != "on/off") {
foreach ($events as $key => $event) {
unset($events[$key]['execuded']);
}
}
2020-10-19 14:22:54 +00:00
$LastRecordTime = new DateTime(reset($events)['time']);
2020-05-27 19:37:35 +00:00
$niceTime = Utilities::ago($LastRecordTime);
2020-10-14 16:33:34 +00:00
$labels = [];
$values = [];
foreach ($events as $key => $event) {
$recordDatetime = new DateTime($event['time']);
2020-10-19 14:22:54 +00:00
if ($key == 0){
$labels[] = 'now';
} else {
$labels[] = $recordDatetime->format('H:i');
}
2020-10-14 16:33:34 +00:00
$values[] = [
'y' => $event['value'],
't' => $recordDatetime->getTimestamp() * 1000,
];
}
2020-10-05 19:11:44 +00:00
2020-10-14 16:33:34 +00:00
$response = [
2020-12-07 19:21:27 +00:00
'room_id' => $subDeviceData['room_id'],
'device_id' => $subDeviceData['device_id'],
'lastConnectionTime' => (empty($niceTime) ? "00:00" : $niceTime),
2020-10-14 16:33:34 +00:00
'records' => $events,
'graph' => [
2020-10-19 14:22:54 +00:00
'type' => $this->getDeviceConfig($subDeviceData['type'])['graph'],
2020-10-14 16:33:34 +00:00
'data' => [
2020-10-05 19:32:23 +00:00
'labels' => $labels,
2020-10-15 16:55:16 +00:00
'datasets' => [[
//'label' => 'FUCK you',
'data' => $values,
]],
2020-10-14 16:33:34 +00:00
],
'options' => [
2020-10-14 12:31:22 +00:00
'scales' => [
2020-10-19 14:22:54 +00:00
'xAxis' => [[
2020-10-14 12:31:22 +00:00
'type' => 'time',
'distribution' => 'linear',
2020-10-19 14:22:54 +00:00
]],
2020-10-14 12:31:22 +00:00
],
'legend' => [
'display' => false
],
'tooltips' => [
'enabled' => true
],
'hover' => [
'mode' => true
],
2020-10-05 19:11:44 +00:00
],
2020-10-14 16:33:34 +00:00
],
2020-12-07 19:21:27 +00:00
2020-10-14 16:33:34 +00:00
];
2020-05-26 19:47:36 +00:00
//TODO: Make Cleaner
if (isset(RANGES[$subDeviceData['type']])){
2020-10-31 20:37:29 +00:00
$response['graph']['options']['scales']['yAxes'] = [[
'ticks' => [
'min' => RANGES[$subDeviceData['type']]['min'],
'max' => RANGES[$subDeviceData['type']]['max'],
'steps' => RANGES[$subDeviceData['type']]['scale'],
]
]];
}
2020-10-14 16:33:34 +00:00
$this->response($response);
2020-05-26 19:47:36 +00:00
}
2020-10-15 16:55:16 +00:00
private function getDeviceConfig($type){
if (isset(RANGES[$type])){
return RANGES[$type];
}
return RANGES[''];
}
2020-10-14 16:33:34 +00:00
}