Some Tweeks

This commit is contained in:
JonatanRek 2020-10-14 18:33:34 +02:00
parent c410df1bd6
commit 5d32c2bfa4
3 changed files with 41 additions and 34 deletions

View File

@ -1,7 +1,9 @@
<?php <?php
class WidgetApi extends ApiController{ class WidgetApi extends ApiController
{
public function run($subDeviceId){ public function run($subDeviceId)
{
//$this->requireAuth(); //$this->requireAuth();
$response = null; $response = null;
@ -10,7 +12,7 @@ class WidgetApi extends ApiController{
} }
$subDeviceData = SubDeviceManager::getSubDevice($subDeviceId); $subDeviceData = SubDeviceManager::getSubDevice($subDeviceId);
if ($subDeviceData['type'] == 'on/off'){ if ($subDeviceData['type'] == 'on/off') {
$lastValue = RecordManager::getLastRecord($subDeviceData['subdevice_id'])['value']; $lastValue = RecordManager::getLastRecord($subDeviceData['subdevice_id'])['value'];
RecordManager::create($subDeviceData['device_id'], 'on/off', (int) !$lastValue); RecordManager::create($subDeviceData['device_id'], 'on/off', (int) !$lastValue);
$response = !$lastValue; $response = !$lastValue;
@ -20,7 +22,7 @@ class WidgetApi extends ApiController{
$i = 0; $i = 0;
$timeout = 20; $timeout = 20;
while (RecordManager::getLastRecord($subDeviceId)['execuded'] == 0){ while (RecordManager::getLastRecord($subDeviceId)['execuded'] == 0) {
if ($i == $timeout) { if ($i == $timeout) {
throw new Exception("Timeout", 444); throw new Exception("Timeout", 444);
} }
@ -30,7 +32,8 @@ class WidgetApi extends ApiController{
$this->response(['value' => $response]); $this->response(['value' => $response]);
} }
public function detail($subDeviceId){ public function detail($subDeviceId)
{
//$this->requireAuth(); //$this->requireAuth();
$response = null; $response = null;
$connectionError = true; $connectionError = true;
@ -63,17 +66,18 @@ class WidgetApi extends ApiController{
$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,
]; ];
} }
$response = [ $response = [
'records'=> $events, 'records' => $events,
'graph'=> [ 'graph' => [
'labels' => $labels,
'data' => [ 'data' => [
'labels' => $labels,
'dataset' => $values 'dataset' => $values
], ],
'options' => [
'scales' => [ 'scales' => [
'xAxis' => [ 'xAxis' => [
'type' => 'time', 'type' => 'time',
@ -97,10 +101,11 @@ class WidgetApi extends ApiController{
'mode' => true 'mode' => true
], ],
], ],
],
'comError' => $connectionError, 'comError' => $connectionError,
'lastConnectionTime' => (empty($niceTime) ? "00:00" : $niceTime), 'lastConnectionTime' => (empty($niceTime) ? "00:00" : $niceTime),
]; ];
$this->response($response); $this->response($response);
} }
} }

View File

@ -1,22 +1,23 @@
<?php <?php
class Covid extends VirtualDeviceManager { class Covid extends VirtualDeviceManager
{
private $country_sluig = "czech-republic"; private $country_sluig = "czech-republic";
private $api_uri = 'https://api.covid19api.com/live/country/%s/status/confirmed'; // Your redirect uri private $api_uri = 'https://api.covid19api.com/live/country/%s/status/confirmed'; // Your redirect uri
private $virtual_device_name = "Covid"; private $virtual_device_name = "Covid";
function fetch($url = 'true') function fetch($url = 'true')
{ {
if (DeviceManager::registeret($this->virtual_device_name)) { if (DeviceManager::registeret($this->virtual_device_name)) {
$deviceId = DeviceManager::getDeviceByToken($this->virtual_device_name)['device_id']; $deviceId = DeviceManager::getDeviceByToken($this->virtual_device_name)['device_id'];
$dataItems = ['Confirmed','Deaths','Recovered','Active']; $dataItems = ['Confirmed', 'Deaths', 'Recovered', 'Active'];
foreach ($dataItems as $dataItem) { foreach ($dataItems as $dataItem) {
if (!$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($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); $finalUrl = sprintf($this->api_uri, $this->country_sluig);
$json = json_decode(Utilities::CallAPI('GET', $finalUrl, ''), true); $json = json_decode(Utilities::CallAPI('GET', $finalUrl, ''), true);

View File

@ -13,6 +13,7 @@ class OpenWeatherMap extends VirtualDeviceManager
$deviceId = DeviceManager::getDeviceByToken($this->virtual_device_name)['device_id']; $deviceId = DeviceManager::getDeviceByToken($this->virtual_device_name)['device_id'];
if (!$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, $this->subdevice_type)) { if (!$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, $this->subdevice_type)) {
SubDeviceManager::create($deviceId, $this->subdevice_type, ''); SubDeviceManager::create($deviceId, $this->subdevice_type, '');
sleep(1);
} }
if (!$this->fetchEnabled($deviceId,$subDevice['subdevice_id'])) die(); if (!$this->fetchEnabled($deviceId,$subDevice['subdevice_id'])) die();