Widget Detail no data Fix

This commit is contained in:
JonatanRek 2020-10-19 16:22:54 +02:00
parent 9dfaeb554d
commit ce05533d29

View File

@ -40,9 +40,14 @@ class WidgetApi extends ApiController
$subDeviceData = SubDeviceManager::getSubDevice($subDeviceId); $subDeviceData = SubDeviceManager::getSubDevice($subDeviceId);
$deviceData = DeviceManager::getDeviceById($subDeviceData['device_id']); $deviceData = DeviceManager::getDeviceById($subDeviceData['device_id']);
$events = RecordManager::getLastRecord($subDeviceId, 10);
$LastRecordTime = new DateTime($events[4]['time']); //TODO: zeptat se @Patrik Je Graf Dobře Seřazený na DESC ?
$events = RecordManager::getAllRecordForGraph($subDeviceId);
if ( count($events) == 0){
throw new Exception("No Records", 404);
}
$LastRecordTime = new DateTime(reset($events)['time']);
$niceTime = Utilities::ago($LastRecordTime); $niceTime = Utilities::ago($LastRecordTime);
$interval = $LastRecordTime->diff(new DateTime()); $interval = $LastRecordTime->diff(new DateTime());
@ -63,7 +68,11 @@ class WidgetApi extends ApiController
$values = []; $values = [];
foreach ($events as $key => $event) { foreach ($events as $key => $event) {
$recordDatetime = new DateTime($event['time']); $recordDatetime = new DateTime($event['time']);
if ($key == 0){
$labels[] = 'now';
} else {
$labels[] = $recordDatetime->format('H:i'); $labels[] = $recordDatetime->format('H:i');
}
$values[] = [ $values[] = [
'y' => $event['value'], 'y' => $event['value'],
't' => $recordDatetime->getTimestamp() * 1000, 't' => $recordDatetime->getTimestamp() * 1000,
@ -73,6 +82,7 @@ class WidgetApi extends ApiController
$response = [ $response = [
'records' => $events, 'records' => $events,
'graph' => [ 'graph' => [
'type' => $this->getDeviceConfig($subDeviceData['type'])['graph'],
'data' => [ 'data' => [
'labels' => $labels, 'labels' => $labels,
'datasets' => [[ 'datasets' => [[
@ -82,17 +92,17 @@ class WidgetApi extends ApiController
], ],
'options' => [ 'options' => [
'scales' => [ 'scales' => [
'xAxis' => [ 'xAxis' => [[
'type' => 'time', 'type' => 'time',
'distribution' => 'linear', 'distribution' => 'linear',
], ]],
'yAxes' => [ 'yAxes' => [[
'ticks' => [ 'ticks' => [
'min' => $this->getDeviceConfig($subDeviceData['type'])['min'], 'min' => $this->getDeviceConfig($subDeviceData['type'])['min'],
'max' => $this->getDeviceConfig($subDeviceData['type'])['max'], 'max' => $this->getDeviceConfig($subDeviceData['type'])['max'],
'steps' => $this->getDeviceConfig($subDeviceData['type'])['scale'], 'steps' => $this->getDeviceConfig($subDeviceData['type'])['scale'],
] ]
] ]]
], ],
'legend' => [ 'legend' => [
'display' => false 'display' => false