2020-10-21 17:35:52 +00:00
|
|
|
<?php
|
|
|
|
class AirQuality extends VirtualDeviceManager
|
|
|
|
{
|
|
|
|
private $city_sluig = "prague";
|
|
|
|
private $app_id = "53ccbc353bb0bd0b05515169a593b96c38d57c48";
|
|
|
|
private $api_uri = 'http://api.waqi.info/feed/%s/?token=%s'; // Your redirect uri
|
|
|
|
private $virtual_device_name = "Air Quality";
|
|
|
|
private $subdevice_type = "air-quality";
|
|
|
|
|
2020-10-22 14:42:49 +00:00
|
|
|
function make()
|
2020-10-21 17:35:52 +00:00
|
|
|
{
|
2020-12-23 14:57:12 +00:00
|
|
|
//Register the settings
|
|
|
|
$settingMng = new SettingsManager();
|
|
|
|
if (!($settingField = $settingMng->getByName("airquality"))) {
|
|
|
|
$settingMng->create("token", "", "airquality");
|
|
|
|
} else {
|
|
|
|
$app_id = $settingField['value'];
|
|
|
|
}
|
|
|
|
|
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'];
|
|
|
|
if (!$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, $this->subdevice_type)) {
|
|
|
|
SubDeviceManager::create($deviceId, $this->subdevice_type, '');
|
|
|
|
sleep(1);
|
2020-10-25 10:51:44 +00:00
|
|
|
$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($this->subdevice_type));
|
2020-10-22 14:42:49 +00:00
|
|
|
}
|
2020-12-23 14:57:12 +00:00
|
|
|
|
2020-10-22 14:42:49 +00:00
|
|
|
//if (!$this->fetchEnabled($deviceId,$subDevice['subdevice_id'])) die();
|
2020-12-23 14:57:12 +00:00
|
|
|
|
2020-10-22 14:42:49 +00:00
|
|
|
$finalUrl = sprintf($this->api_uri, $this->city_sluig, $this->app_id);
|
|
|
|
$json = json_decode(Utilities::CallAPI('GET', $finalUrl, ''), true);
|
2021-02-10 15:13:22 +00:00
|
|
|
RecordManager::create($deviceId, $this->subdevice_type, $json['data']['aqi'], 'plugin');
|
2020-10-22 14:42:49 +00:00
|
|
|
} else {
|
2020-10-22 19:28:08 +00:00
|
|
|
DeviceManager::create($this->virtual_device_name, $this->virtual_device_name, 'senzore-virtual');
|
2020-10-22 14:42:49 +00:00
|
|
|
DeviceManager::approved($this->virtual_device_name);
|
2020-10-21 17:35:52 +00:00
|
|
|
}
|
2020-10-22 14:42:49 +00:00
|
|
|
return 'sucessful';
|
|
|
|
} catch(Exception $e) {
|
|
|
|
return 'exception: ' . $e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
2020-10-21 17:35:52 +00:00
|
|
|
|
2020-10-22 14:42:49 +00:00
|
|
|
function translate($value){
|
|
|
|
if ($value < 50) {
|
|
|
|
return 'Good';
|
|
|
|
} else if ($value > 51 && $value < 100) {
|
|
|
|
return 'Moderate';
|
|
|
|
} else if ($value > 101 && $value < 150) {
|
|
|
|
return 'Normal';
|
|
|
|
} else if ($value > 151 && $value < 200) {
|
|
|
|
return 'Unhealthy';
|
|
|
|
} else if ($value > 201 && $value < 300) {
|
|
|
|
return 'Very Unhealthy';
|
|
|
|
} else if ($value > 301 ) {
|
|
|
|
return 'Hazardous';
|
2020-10-21 17:35:52 +00:00
|
|
|
}
|
2020-10-22 14:42:49 +00:00
|
|
|
return '';
|
2020-10-21 17:35:52 +00:00
|
|
|
}
|
|
|
|
}
|