Bether Graph generation & covid version 2

This commit is contained in:
GamerClassN7 2020-10-25 11:51:44 +01:00
parent e2c9c31d2f
commit 6b08b2785f
8 changed files with 91 additions and 19 deletions

View File

@ -23,6 +23,9 @@ class CronApi extends ApiController
$pluginsFiles = array_diff(scandir($dir), ['..', '.']);
foreach ($pluginsFiles as $key => $pluginFile) {
$className = str_replace(".php", "", $pluginFile);
if (strpos($pluginFile, '_') === true) {
continue;
}
if (!class_exists($className)) {
continue;
}

View File

@ -19,22 +19,29 @@ class RoomsApi extends ApiController
$subDevicesData = SubDeviceManager::getSubdevicesByRoomIds($roomIds);
foreach ($subDevicesData as $subDeviceKey => $subDevice) {
foreach ($subDevice as $key => $value) {
$type = null;
if (strpos($subDevicesData[$subDeviceKey][$key]['type'], '-') !== false) {
$type = "";
foreach (explode('-', $subDevicesData[$subDeviceKey][$key]['type']) as $word) {
$type .= ucfirst($word);
}
if (!class_exists($type)) {
$type = $subDevicesData[$subDeviceKey][$key]['type'];
} else if (strpos(SubDeviceManager::getSubDeviceMaster($subDevicesData[$subDeviceKey][$key]['subdevice_id'])['type'], '-') !== false) {
$type = SubDeviceManager::getSubDeviceMaster($subDevicesData[$subDeviceKey][$key]['subdevice_id'])['type'];
} else {
continue;
}
$deviceClass = new $type;
$cammelCaseClass = "";
foreach (explode('-', $type) as $word) {
$cammelCaseClass .= ucfirst($word);
}
if (!class_exists($cammelCaseClass)) {
continue;
}
$deviceClass = new $cammelCaseClass;
if (!method_exists($deviceClass, 'translate')) {
continue;
}
$subDevicesData[$subDeviceKey][$key]['value'] = $deviceClass->translate($subDevicesData[$subDeviceKey][$key]['value']);
}
}
}
foreach ($roomsData as $roomKey => $roomData) {
if ($roomData['device_count'] == 0) continue;

View File

@ -96,13 +96,7 @@ class WidgetApi extends ApiController
'type' => 'time',
'distribution' => 'linear',
]],
'yAxes' => [[
'ticks' => [
'min' => $this->getDeviceConfig($subDeviceData['type'])['min'],
'max' => $this->getDeviceConfig($subDeviceData['type'])['max'],
'steps' => $this->getDeviceConfig($subDeviceData['type'])['scale'],
]
]]
],
'legend' => [
'display' => false
@ -119,6 +113,17 @@ class WidgetApi extends ApiController
'lastConnectionTime' => (empty($niceTime) ? "00:00" : $niceTime),
];
//TODO: Make Cleaner
if (isset(RANGES[$subDeviceData['type']])){
$response['graph']['options']['options']['scales']['yAxes'] = [[
'ticks' => [
'min' => RANGES[$subDeviceData['type']]['min'],
'max' => RANGES[$subDeviceData['type']]['max'],
'steps' => RANGES[$subDeviceData['type']]['scale'],
]
]];
}
$this->response($response);
}

View File

@ -15,6 +15,7 @@ class AirQuality extends VirtualDeviceManager
if (!$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, $this->subdevice_type)) {
SubDeviceManager::create($deviceId, $this->subdevice_type, '');
sleep(1);
$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($this->subdevice_type));
}
//if (!$this->fetchEnabled($deviceId,$subDevice['subdevice_id'])) die();

53
app/plugins/CovidV2.php Normal file
View File

@ -0,0 +1,53 @@
<?php
class CovidV2 extends VirtualDeviceManager
{
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",
];
public function make()
{
try {
if (DeviceManager::registeret($this->virtual_device_name)) {
$deviceId = DeviceManager::getDeviceByToken($this->virtual_device_name)['device_id'];
$dataItems = ['Tested', '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));
}
}
if (!$this->fetchEnabled($deviceId, $subDevice['subdevice_id'])) die();
$finalUrl = $this->api_uri;
$json = json_decode(Utilities::CallAPI('GET', $finalUrl, ''), true)['data'];
foreach ($dataItems as $dataItem) {
RecordManager::create($deviceId, strtolower($dataItem), end($json)[$this->name_index[$dataItem]]);
}
} else {
DeviceManager::create($this->virtual_device_name, $this->virtual_device_name, strtolower($this->virtual_device_name));
DeviceManager::approved($this->virtual_device_name);
}
return 'sucessful';
} catch (Exception $e) {
return 'exception: ' . $e->getMessage();
}
}
public function translate($value){
$outcome = $value / 1000;
if ($outcome < 1){
return $value;
}
return round($outcome) . 'K';
}
}

View File

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

View File

@ -15,6 +15,7 @@ class UsaElection extends VirtualDeviceManager
if (!$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($dataItem))) {
SubDeviceManager::create($deviceId, strtolower($dataItem), '% ' . $dataItem);
sleep(1);
$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($this->subdevice_type));
}
}

View File

@ -15,6 +15,7 @@ class Covid extends VirtualDeviceManager
if (!$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($dataItem))) {
SubDeviceManager::create($deviceId, strtolower($dataItem), $dataItem);
sleep(1);
$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($dataItem));
}
}