diff --git a/app/models/managers/VirtualDeviceManager.php b/app/models/managers/VirtualDeviceManager.php index 6f7029c..5a5f92f 100644 --- a/app/models/managers/VirtualDeviceManager.php +++ b/app/models/managers/VirtualDeviceManager.php @@ -1,21 +1,19 @@ diff(new DateTime()); + $hours = $interval->format('%h'); + $minutes = $interval->format('%i'); + $lastSeen = ($hours * 60 + $minutes); - $deviceId = DeviceManager::getDeviceByToken('1ee609f2fcf8048e84f1d2fb1d1d72b5')['device_id']; + if ($lastSeen > $sleepTime || $sleepTime == 0) { + return true; + } - if (!SubDeviceManager::getSubDeviceByMaster($deviceId, 'weather-nice')) { - SubDeviceManager::create($deviceId, 'weather-nice', ''); - } - - RecordManager::create($deviceId, 'weather-nice', $json['weather'][0]['main']); - } else { - $deviceId = DeviceManager::create('1ee609f2fcf8048e84f1d2fb1d1d72b5', '1ee609f2fcf8048e84f1d2fb1d1d72b5')['device_id']; - } - } + return false; + } } diff --git a/app/plugins/Covid.php b/app/plugins/Covid.php index 8bb4f15..99f8dc7 100644 --- a/app/plugins/Covid.php +++ b/app/plugins/Covid.php @@ -3,21 +3,25 @@ class Covid extends VirtualDeviceManager { 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"; - private $fetch_iterval = ""; function fetch($url = 'true') { - //todo fetch interval limit from sleep time - $finalUrl = sprintf($this->api_uri, $this->country_sluig); - $json = json_decode(Utilities::CallAPI('GET', $finalUrl, ''), true); 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 (!SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($dataItem))) { + if (!$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($dataItem))) { SubDeviceManager::create($deviceId, strtolower($dataItem), 'Cases'); } + } + + if (!$this->fetchEnabled($deviceId,$subDevice['subdevice_id'])) die(); + + $finalUrl = sprintf($this->api_uri, $this->country_sluig); + $json = json_decode(Utilities::CallAPI('GET', $finalUrl, ''), true); + + foreach ($dataItems as $dataItem) { RecordManager::create($deviceId, strtolower($dataItem), $json[0][$dataItem]); } } else { diff --git a/app/plugins/OpenWeatherMap.php b/app/plugins/OpenWeatherMap.php index 91e7d53..f195dd2 100644 --- a/app/plugins/OpenWeatherMap.php +++ b/app/plugins/OpenWeatherMap.php @@ -1,26 +1,29 @@ api_uri, $this->city_sluig, $this->app_id); - $json = json_decode(Utilities::CallAPI('GET', $finalUrl, ''), true); + function fetch($url) + { if (DeviceManager::registeret($this->virtual_device_name)) { $deviceId = DeviceManager::getDeviceByToken($this->virtual_device_name)['device_id']; - if (!SubDeviceManager::getSubDeviceByMaster($deviceId, 'weather')) { - SubDeviceManager::create($deviceId, 'weather', ''); + if (!$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, $this->subdevice_type)) { + SubDeviceManager::create($deviceId, $this->subdevice_type, ''); } - RecordManager::create($deviceId, 'weather', $json['weather'][0]['id']); + + if (!$this->fetchEnabled($deviceId,$subDevice['subdevice_id'])) die(); + + $finalUrl = sprintf($this->api_uri, $this->city_sluig, $this->app_id); + $json = json_decode(Utilities::CallAPI('GET', $finalUrl, ''), true); + + RecordManager::create($deviceId, $this->subdevice_type, $json['weather'][0]['id']); } else { DeviceManager::create($this->virtual_device_name, $this->virtual_device_name); DeviceManager::approved($this->virtual_device_name); } - } + } }