PHP_SMART_HOME_V3/app/plugins/_Covid.php

40 lines
1.4 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";
2020-10-22 14:42:49 +00:00
function make()
2020-10-08 17:41:38 +00:00
{
2020-10-22 14:42:49 +00:00
try {
if (DeviceManager::registeret($this->virtual_device_name)) {
$deviceId = DeviceManager::getDeviceByToken($this->virtual_device_name)['device_id'];
$dataItems = ['Confirmed', 'Deaths', 'Recovered', 'Active'];
foreach ($dataItems as $dataItem) {
if (!$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($dataItem))) {
SubDeviceManager::create($deviceId, strtolower($dataItem), $dataItem);
sleep(1);
$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($dataItem));
2020-10-22 14:42:49 +00:00
}
2020-10-08 17:41:38 +00:00
}
2020-10-14 13:53:26 +00:00
2020-10-22 14:42:49 +00:00
if (!$this->fetchEnabled($deviceId, $subDevice['subdevice_id'])) die();
2020-10-14 13:53:26 +00:00
2020-10-22 14:42:49 +00:00
$finalUrl = sprintf($this->api_uri, $this->country_sluig);
$json = json_decode(Utilities::CallAPI('GET', $finalUrl, ''), true);
2020-10-14 13:53:26 +00:00
2020-10-22 14:42:49 +00:00
foreach ($dataItems as $dataItem) {
RecordManager::create($deviceId, strtolower($dataItem), end($json)[$dataItem]);
}
} else {
2020-10-22 19:28:08 +00:00
DeviceManager::create($this->virtual_device_name, $this->virtual_device_name, 'senzore-virtual');
2020-10-22 14:42:49 +00:00
DeviceManager::approved($this->virtual_device_name);
2020-10-08 17:41:38 +00:00
}
2020-10-22 14:42:49 +00:00
return 'sucessful';
} catch (Exception $e) {
return 'exception: ' . $e->getMessage();
2020-10-08 17:41:38 +00:00
}
}
}