PHP_SMART_HOME_V3/app/plugins/OpenWeatherMap.php

73 lines
2.8 KiB
PHP
Raw Normal View History

2020-10-14 12:34:46 +00:00
<?php
2020-10-14 13:53:26 +00:00
class OpenWeatherMap extends VirtualDeviceManager
2020-10-14 12:34:46 +00:00
{
private $city_sluig = "prague";
2020-10-14 13:53:26 +00:00
private $app_id = "1ee609f2fcf8048e84f1d2fb1d1d72b5";
2020-10-14 12:34:46 +00:00
private $api_uri = 'api.openweathermap.org/data/2.5/weather?q=%s&appid=%s'; // Your redirect uri
private $virtual_device_name = "Weather";
2020-10-14 13:53:26 +00:00
private $subdevice_type = "weather";
2020-10-14 12:34:46 +00:00
2020-10-22 14:42:49 +00:00
function make()
2020-10-14 13:53:26 +00:00
{
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);
$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($this->subdevice_type));
2020-10-22 14:42:49 +00:00
}
2020-10-14 13:53:26 +00:00
2020-10-22 14:42:49 +00:00
if (!$this->fetchEnabled($deviceId, $subDevice['subdevice_id'])) die();
2020-10-14 13:53:26 +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);
2020-10-14 13:53:26 +00:00
2021-01-07 17:02:01 +00:00
//Notification data setup
$notificationMng = new NotificationManager;
if ($json['weather'][0]['id'] >= 500 && $json['weather'][0]['id'] < 600) {
// $notificationData = [
// 'title' => 'Weather',
// 'body' => 'It Will be rainy outhere, Take Umbrela :)',
// 'icon' => 'http://dev.steelants.cz/projekty/simplehome-client/img/icons/favicon-16x16.png',
// ];
// //Notification for newly added Device
// if ($notificationData != []) {
// $subscribers = $notificationMng::getSubscription();
// foreach ($subscribers as $key => $subscriber) {
// $notificationMng::sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
// }
// }
} else if ($json['weather'][0]['id'] >= 600 && $json['weather'][0]['id'] < 700) {
// $notificationData = [
// 'title' => 'Weather',
// 'body' => 'It is white out there :)',
// 'icon' => 'http://dev.steelants.cz/projekty/simplehome-client/img/icons/favicon-16x16.png',
// ];
// //Notification for newly added Device
// if ($notificationData != []) {
// $subscribers = $notificationMng::getSubscription();
// foreach ($subscribers as $key => $subscriber) {
// $notificationMng::sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
// }
// }
}
2021-02-10 15:13:22 +00:00
RecordManager::create($deviceId, $this->subdevice_type, $json['weather'][0]['id'], '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);
}
return 'sucessful';
} catch (Exception $e) {
return 'exception: ' . $e->getMessage();
2020-10-14 12:34:46 +00:00
}
2020-10-14 13:53:26 +00:00
}
2020-12-17 18:42:29 +00:00
function enable(){
(new SettingsManager)->create('open_weather_api_token', '', 'open_weather');
}
2020-10-14 12:34:46 +00:00
}