PHP_SMART_HOME_V3/app/plugins/CovidV2.php

53 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2020-10-08 17:41:38 +00:00
<?php
class CovidV2 extends VirtualDeviceManager
2020-10-14 16:33:34 +00:00
{
private $api_uri = 'https://onemocneni-aktualne.mzcr.cz/api/v2/covid-19/nakazeni-vyleceni-umrti-testy.json'; // Your redirect uri
private $virtual_device_name = "Covid-V2";
private $name_index = [
"Active" => "kumulativni_pocet_nakazenych",
"Recovered" => "kumulativni_pocet_vylecenych",
"Tested" => "kumulativni_pocet_testu",
"Deaths" => "kumulativni_pocet_umrti",
2020-10-08 17:41:38 +00:00
];
public 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 = ['Tested', 'Deaths', 'Recovered', 'Active'];
2020-10-22 14:42:49 +00:00
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
$finalUrl = $this->api_uri;
$json = json_decode(Utilities::CallAPI('GET', $finalUrl, ''), true)['data'];
2020-10-14 13:53:26 +00:00
2020-10-22 14:42:49 +00:00
foreach ($dataItems as $dataItem) {
2021-02-10 15:13:22 +00:00
RecordManager::create($deviceId, strtolower($dataItem), end($json)[$this->name_index[$dataItem]], 'plugin');
2020-10-22 14:42:49 +00:00
}
} else {
DeviceManager::create($this->virtual_device_name, $this->virtual_device_name, strtolower($this->virtual_device_name));
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
}
}
public function translate($value){
$outcome = $value / 1000;
if ($outcome < 1){
return $value;
}
return round($outcome) . 'K';
}
}