From 5d32c2bfa48ca5a27f6ada755b504dabef210e3f Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Wed, 14 Oct 2020 18:33:34 +0200 Subject: [PATCH] Some Tweeks --- app/api/WidgetApi.php | 63 ++++++++++++++++++---------------- app/plugins/Covid.php | 11 +++--- app/plugins/OpenWeatherMap.php | 1 + 3 files changed, 41 insertions(+), 34 deletions(-) diff --git a/app/api/WidgetApi.php b/app/api/WidgetApi.php index 36ceb70..41ad8aa 100644 --- a/app/api/WidgetApi.php +++ b/app/api/WidgetApi.php @@ -1,7 +1,9 @@ requireAuth(); $response = null; @@ -10,7 +12,7 @@ class WidgetApi extends ApiController{ } $subDeviceData = SubDeviceManager::getSubDevice($subDeviceId); - if ($subDeviceData['type'] == 'on/off'){ + if ($subDeviceData['type'] == 'on/off') { $lastValue = RecordManager::getLastRecord($subDeviceData['subdevice_id'])['value']; RecordManager::create($subDeviceData['device_id'], 'on/off', (int) !$lastValue); $response = !$lastValue; @@ -20,7 +22,7 @@ class WidgetApi extends ApiController{ $i = 0; $timeout = 20; - while (RecordManager::getLastRecord($subDeviceId)['execuded'] == 0){ + while (RecordManager::getLastRecord($subDeviceId)['execuded'] == 0) { if ($i == $timeout) { throw new Exception("Timeout", 444); } @@ -30,7 +32,8 @@ class WidgetApi extends ApiController{ $this->response(['value' => $response]); } - public function detail($subDeviceId){ + public function detail($subDeviceId) + { //$this->requireAuth(); $response = null; $connectionError = true; @@ -52,28 +55,29 @@ class WidgetApi extends ApiController{ $subDeviceData['type'] == "on/off" || $subDeviceData['type'] == "door" || $subDeviceData['type'] == "wather" - ) { - $connectionError = false; - } + ) { + $connectionError = false; + } - $labels = []; - $values = []; - foreach ($events as $key => $event) { - $recordDatetime = new DateTime($event['time']); - $labels[] = $recordDatetime->format('H:i'); - $values[] = [ - 'y' => $event['value'], - 't' => $recordDatetime->getTimestamp()*1000, - ]; - } + $labels = []; + $values = []; + foreach ($events as $key => $event) { + $recordDatetime = new DateTime($event['time']); + $labels[] = $recordDatetime->format('H:i'); + $values[] = [ + 'y' => $event['value'], + 't' => $recordDatetime->getTimestamp() * 1000, + ]; + } - $response = [ - 'records'=> $events, - 'graph'=> [ + $response = [ + 'records' => $events, + 'graph' => [ + 'data' => [ 'labels' => $labels, - 'data' => [ - 'dataset' => $values - ], + 'dataset' => $values + ], + 'options' => [ 'scales' => [ 'xAxis' => [ 'type' => 'time', @@ -97,10 +101,11 @@ class WidgetApi extends ApiController{ 'mode' => true ], ], - 'comError' => $connectionError, - 'lastConnectionTime' => (empty($niceTime) ? "00:00" : $niceTime), - ]; + ], + 'comError' => $connectionError, + 'lastConnectionTime' => (empty($niceTime) ? "00:00" : $niceTime), + ]; - $this->response($response); - } + $this->response($response); } +} diff --git a/app/plugins/Covid.php b/app/plugins/Covid.php index 99f8dc7..1ce42ec 100644 --- a/app/plugins/Covid.php +++ b/app/plugins/Covid.php @@ -1,22 +1,23 @@ virtual_device_name)) { $deviceId = DeviceManager::getDeviceByToken($this->virtual_device_name)['device_id']; - $dataItems = ['Confirmed','Deaths','Recovered','Active']; + $dataItems = ['Confirmed', 'Deaths', 'Recovered', 'Active']; foreach ($dataItems as $dataItem) { if (!$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($dataItem))) { - SubDeviceManager::create($deviceId, strtolower($dataItem), 'Cases'); + SubDeviceManager::create($deviceId, strtolower($dataItem), $dataItem); + sleep(1); } } - if (!$this->fetchEnabled($deviceId,$subDevice['subdevice_id'])) die(); + if (!$this->fetchEnabled($deviceId, $subDevice['subdevice_id'])) die(); $finalUrl = sprintf($this->api_uri, $this->country_sluig); $json = json_decode(Utilities::CallAPI('GET', $finalUrl, ''), true); diff --git a/app/plugins/OpenWeatherMap.php b/app/plugins/OpenWeatherMap.php index f195dd2..0e31182 100644 --- a/app/plugins/OpenWeatherMap.php +++ b/app/plugins/OpenWeatherMap.php @@ -13,6 +13,7 @@ class OpenWeatherMap extends VirtualDeviceManager $deviceId = DeviceManager::getDeviceByToken($this->virtual_device_name)['device_id']; if (!$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, $this->subdevice_type)) { SubDeviceManager::create($deviceId, $this->subdevice_type, ''); + sleep(1); } if (!$this->fetchEnabled($deviceId,$subDevice['subdevice_id'])) die();