Some Fixes

This commit is contained in:
JonatanRek 2020-10-14 15:53:26 +02:00
parent 3aab42d081
commit c410df1bd6
3 changed files with 36 additions and 31 deletions

View File

@ -1,21 +1,19 @@
<?php
class VirtualDeviceManager
{
function fetch($url)
{
$json = json_decode(Utilities::CallAPI('GET', 'api.openweathermap.org/data/2.5/weather?q=prague&appid=1ee609f2fcf8048e84f1d2fb1d1d72b5', ''), true);
public function fetchEnabled($deviceId = null, $subDeviceId = null){
$sleepTime = DeviceManager::getDeviceById($deviceId)['sleep_time'];
if (DeviceManager::registeret('1ee609f2fcf8048e84f1d2fb1d1d72b5')) {
$LastRecordTime = new DateTime(RecordManager::getLastRecord($subDeviceId, 1)['time']);
$interval = $LastRecordTime->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;
}
}

View File

@ -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 {

View File

@ -1,26 +1,29 @@
<?php
class OpenWeatherMap
class OpenWeatherMap extends VirtualDeviceManager
{
private $city_sluig = "prague";
private $app_id= "1ee609f2fcf8048e84f1d2fb1d1d72b5";
private $app_id = "1ee609f2fcf8048e84f1d2fb1d1d72b5";
private $api_uri = 'api.openweathermap.org/data/2.5/weather?q=%s&appid=%s'; // Your redirect uri
private $virtual_device_name = "Weather";
private $fetch_iterval = "";
private $subdevice_type = "weather";
function fetch($url)
{
//todo fetch interval limit from sleep time
$finalUrl = sprintf($this->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);
}
}
}
}