Bether Graph generation & covid version 2
This commit is contained in:
		@@ -23,6 +23,9 @@ class CronApi extends ApiController
 | 
				
			|||||||
		$pluginsFiles = array_diff(scandir($dir), ['..', '.']);
 | 
							$pluginsFiles = array_diff(scandir($dir), ['..', '.']);
 | 
				
			||||||
		foreach ($pluginsFiles as $key => $pluginFile) {
 | 
							foreach ($pluginsFiles as $key => $pluginFile) {
 | 
				
			||||||
			$className = str_replace(".php", "", $pluginFile);
 | 
								$className = str_replace(".php", "", $pluginFile);
 | 
				
			||||||
 | 
								if (strpos($pluginFile, '_') === true) {
 | 
				
			||||||
 | 
									continue;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
			if (!class_exists($className)) {
 | 
								if (!class_exists($className)) {
 | 
				
			||||||
				continue;
 | 
									continue;
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -19,22 +19,29 @@ class RoomsApi extends ApiController
 | 
				
			|||||||
		$subDevicesData = SubDeviceManager::getSubdevicesByRoomIds($roomIds);
 | 
							$subDevicesData = SubDeviceManager::getSubdevicesByRoomIds($roomIds);
 | 
				
			||||||
		foreach ($subDevicesData as $subDeviceKey => $subDevice) {
 | 
							foreach ($subDevicesData as $subDeviceKey => $subDevice) {
 | 
				
			||||||
			foreach ($subDevice as $key => $value) {
 | 
								foreach ($subDevice as $key => $value) {
 | 
				
			||||||
 | 
									$type = null;
 | 
				
			||||||
				if (strpos($subDevicesData[$subDeviceKey][$key]['type'], '-') !== false) {
 | 
									if (strpos($subDevicesData[$subDeviceKey][$key]['type'], '-') !== false) {
 | 
				
			||||||
					$type = "";
 | 
										$type = $subDevicesData[$subDeviceKey][$key]['type'];
 | 
				
			||||||
					foreach (explode('-', $subDevicesData[$subDeviceKey][$key]['type']) as $word) {
 | 
									} else if (strpos(SubDeviceManager::getSubDeviceMaster($subDevicesData[$subDeviceKey][$key]['subdevice_id'])['type'], '-') !== false) {
 | 
				
			||||||
						$type .= ucfirst($word);
 | 
										$type = SubDeviceManager::getSubDeviceMaster($subDevicesData[$subDeviceKey][$key]['subdevice_id'])['type'];
 | 
				
			||||||
					}
 | 
									} else {
 | 
				
			||||||
					if (!class_exists($type)) {
 | 
					 | 
				
			||||||
					continue;
 | 
										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')) {
 | 
									if (!method_exists($deviceClass, 'translate')) {
 | 
				
			||||||
					continue;
 | 
										continue;
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				$subDevicesData[$subDeviceKey][$key]['value'] = $deviceClass->translate($subDevicesData[$subDeviceKey][$key]['value']);
 | 
									$subDevicesData[$subDeviceKey][$key]['value'] = $deviceClass->translate($subDevicesData[$subDeviceKey][$key]['value']);
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		foreach ($roomsData as $roomKey => $roomData) {
 | 
							foreach ($roomsData as $roomKey => $roomData) {
 | 
				
			||||||
			if ($roomData['device_count'] == 0) continue;
 | 
								if ($roomData['device_count'] == 0) continue;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -96,13 +96,7 @@ class WidgetApi extends ApiController
 | 
				
			|||||||
							'type' => 'time',
 | 
												'type' => 'time',
 | 
				
			||||||
							'distribution' => 'linear',
 | 
												'distribution' => 'linear',
 | 
				
			||||||
						]],
 | 
											]],
 | 
				
			||||||
						'yAxes' => [[
 | 
					
 | 
				
			||||||
							'ticks' => [
 | 
					 | 
				
			||||||
								'min' => $this->getDeviceConfig($subDeviceData['type'])['min'],
 | 
					 | 
				
			||||||
								'max' => $this->getDeviceConfig($subDeviceData['type'])['max'],
 | 
					 | 
				
			||||||
								'steps' => $this->getDeviceConfig($subDeviceData['type'])['scale'],
 | 
					 | 
				
			||||||
							]
 | 
					 | 
				
			||||||
						]]
 | 
					 | 
				
			||||||
					],
 | 
										],
 | 
				
			||||||
					'legend' => [
 | 
										'legend' => [
 | 
				
			||||||
						'display' => false
 | 
											'display' => false
 | 
				
			||||||
@@ -119,6 +113,17 @@ class WidgetApi extends ApiController
 | 
				
			|||||||
			'lastConnectionTime' => (empty($niceTime) ? "00:00" : $niceTime),
 | 
								'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);
 | 
							$this->response($response);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -15,6 +15,7 @@ class AirQuality extends VirtualDeviceManager
 | 
				
			|||||||
				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);
 | 
										sleep(1);
 | 
				
			||||||
 | 
										$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($this->subdevice_type));
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				
 | 
									
 | 
				
			||||||
				//if (!$this->fetchEnabled($deviceId,$subDevice['subdevice_id'])) die();
 | 
									//if (!$this->fetchEnabled($deviceId,$subDevice['subdevice_id'])) die();
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										53
									
								
								app/plugins/CovidV2.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								app/plugins/CovidV2.php
									
									
									
									
									
										Normal 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';
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -15,6 +15,7 @@ class OpenWeatherMap extends VirtualDeviceManager
 | 
				
			|||||||
				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);
 | 
										sleep(1);
 | 
				
			||||||
 | 
										$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($this->subdevice_type));
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				if (!$this->fetchEnabled($deviceId, $subDevice['subdevice_id'])) die();
 | 
									if (!$this->fetchEnabled($deviceId, $subDevice['subdevice_id'])) die();
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -15,6 +15,7 @@ class UsaElection extends VirtualDeviceManager
 | 
				
			|||||||
					if (!$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($dataItem))) {
 | 
										if (!$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($dataItem))) {
 | 
				
			||||||
						SubDeviceManager::create($deviceId, strtolower($dataItem), '% ' . $dataItem);
 | 
											SubDeviceManager::create($deviceId, strtolower($dataItem), '% ' . $dataItem);
 | 
				
			||||||
						sleep(1);
 | 
											sleep(1);
 | 
				
			||||||
 | 
											$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($this->subdevice_type));
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -15,6 +15,7 @@ class Covid extends VirtualDeviceManager
 | 
				
			|||||||
					if (!$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($dataItem))) {
 | 
										if (!$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($dataItem))) {
 | 
				
			||||||
						SubDeviceManager::create($deviceId, strtolower($dataItem), $dataItem);
 | 
											SubDeviceManager::create($deviceId, strtolower($dataItem), $dataItem);
 | 
				
			||||||
						sleep(1);
 | 
											sleep(1);
 | 
				
			||||||
 | 
											$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($dataItem));
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		Reference in New Issue
	
	Block a user