PHP_SMART_HOME_V3/app/plugins/Covid.php

34 lines
1.2 KiB
PHP
Raw Normal View History

2020-10-08 17:41:38 +00:00
<?php
2020-10-14 16:33:34 +00:00
class Covid extends VirtualDeviceManager
{
2020-10-08 17:41:38 +00:00
private $country_sluig = "czech-republic";
private $api_uri = 'https://api.covid19api.com/live/country/%s/status/confirmed'; // Your redirect uri
private $virtual_device_name = "Covid";
function fetch($url = 'true')
{
if (DeviceManager::registeret($this->virtual_device_name)) {
$deviceId = DeviceManager::getDeviceByToken($this->virtual_device_name)['device_id'];
2020-10-14 16:33:34 +00:00
$dataItems = ['Confirmed', 'Deaths', 'Recovered', 'Active'];
2020-10-08 17:41:38 +00:00
foreach ($dataItems as $dataItem) {
2020-10-14 13:53:26 +00:00
if (!$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($dataItem))) {
2020-10-14 16:33:34 +00:00
SubDeviceManager::create($deviceId, strtolower($dataItem), $dataItem);
sleep(1);
2020-10-08 17:41:38 +00:00
}
2020-10-14 13:53:26 +00:00
}
2020-10-14 16:33:34 +00:00
if (!$this->fetchEnabled($deviceId, $subDevice['subdevice_id'])) die();
2020-10-14 13:53:26 +00:00
$finalUrl = sprintf($this->api_uri, $this->country_sluig);
$json = json_decode(Utilities::CallAPI('GET', $finalUrl, ''), true);
foreach ($dataItems as $dataItem) {
2020-10-08 17:41:38 +00:00
RecordManager::create($deviceId, strtolower($dataItem), $json[0][$dataItem]);
}
} else {
DeviceManager::create($this->virtual_device_name, $this->virtual_device_name);
DeviceManager::approved($this->virtual_device_name);
}
}
}