weather.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. trait WeatherHomepageItem
  3. {
  4. public function weatherSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'Weather-Air',
  8. 'enabled' => true,
  9. 'image' => 'plugins/images/tabs/wind.png',
  10. 'category' => 'Monitor',
  11. 'settingsArray' => __FUNCTION__
  12. ];
  13. if ($infoOnly) {
  14. return $homepageInformation;
  15. }
  16. $homepageSettings = array(
  17. 'debug' => true,
  18. 'settings' => array(
  19. 'Enable' => array(
  20. array(
  21. 'type' => 'switch',
  22. 'name' => 'homepageWeatherAndAirEnabled',
  23. 'label' => 'Enable',
  24. 'value' => $this->config['homepageWeatherAndAirEnabled']
  25. ),
  26. array(
  27. 'type' => 'select',
  28. 'name' => 'homepageWeatherAndAirAuth',
  29. 'label' => 'Minimum Authentication',
  30. 'value' => $this->config['homepageWeatherAndAirAuth'],
  31. 'options' => $this->groupOptions
  32. )
  33. ),
  34. 'Connection' => array(
  35. array(
  36. 'type' => 'input',
  37. 'name' => 'homepageWeatherAndAirLatitude',
  38. 'label' => 'Latitude',
  39. 'value' => $this->config['homepageWeatherAndAirLatitude'],
  40. 'help' => 'Please enter full latitude including minus if needed'
  41. ),
  42. array(
  43. 'type' => 'input',
  44. 'name' => 'homepageWeatherAndAirLongitude',
  45. 'label' => 'Longitude',
  46. 'value' => $this->config['homepageWeatherAndAirLongitude'],
  47. 'help' => 'Please enter full longitude including minus if needed'
  48. ),
  49. array(
  50. 'type' => 'blank',
  51. 'label' => ''
  52. ),
  53. array(
  54. 'type' => 'button',
  55. 'label' => '',
  56. 'icon' => 'fa fa-search',
  57. 'class' => 'pull-right',
  58. 'text' => 'Need Help With Coordinates?',
  59. 'attr' => 'onclick="showLookupCoordinatesModal()"'
  60. ),
  61. ),
  62. 'Options' => array(
  63. array(
  64. 'type' => 'input',
  65. 'name' => 'homepageWeatherAndAirWeatherHeader',
  66. 'label' => 'Title',
  67. 'value' => $this->config['homepageWeatherAndAirWeatherHeader'],
  68. 'help' => 'Sets the title of this homepage module',
  69. ),
  70. array(
  71. 'type' => 'switch',
  72. 'name' => 'homepageWeatherAndAirWeatherHeaderToggle',
  73. 'label' => 'Toggle Title',
  74. 'value' => $this->config['homepageWeatherAndAirWeatherHeaderToggle'],
  75. 'help' => 'Shows/hides the title of this homepage module'
  76. ),
  77. array(
  78. 'type' => 'switch',
  79. 'name' => 'homepageWeatherAndAirWeatherEnabled',
  80. 'label' => 'Enable Weather',
  81. 'value' => $this->config['homepageWeatherAndAirWeatherEnabled'],
  82. 'help' => 'Toggles the view module for Weather'
  83. ),
  84. array(
  85. 'type' => 'switch',
  86. 'name' => 'homepageWeatherAndAirAirQualityEnabled',
  87. 'label' => 'Enable Air Quality',
  88. 'value' => $this->config['homepageWeatherAndAirAirQualityEnabled'],
  89. 'help' => 'Toggles the view module for Air Quality'
  90. ),
  91. array(
  92. 'type' => 'switch',
  93. 'name' => 'homepageWeatherAndAirPollenEnabled',
  94. 'label' => 'Enable Pollen',
  95. 'value' => $this->config['homepageWeatherAndAirPollenEnabled'],
  96. 'help' => 'Toggles the view module for Pollen'
  97. ),
  98. array(
  99. 'type' => 'select',
  100. 'name' => 'homepageWeatherAndAirUnits',
  101. 'label' => 'Unit of Measurement',
  102. 'value' => $this->config['homepageWeatherAndAirUnits'],
  103. 'options' => array(
  104. array(
  105. 'name' => 'Imperial',
  106. 'value' => 'imperial'
  107. ),
  108. array(
  109. 'name' => 'Metric',
  110. 'value' => 'metric'
  111. )
  112. )
  113. ),
  114. array(
  115. 'type' => 'select',
  116. 'name' => 'homepageWeatherAndAirRefresh',
  117. 'label' => 'Refresh Seconds',
  118. 'value' => $this->config['homepageWeatherAndAirRefresh'],
  119. 'options' => $this->timeOptions()
  120. ),
  121. ),
  122. )
  123. );
  124. return array_merge($homepageInformation, $homepageSettings);
  125. }
  126. public function weatherHomepagePermissions($key = null)
  127. {
  128. $permissions = [
  129. 'main' => [
  130. 'enabled' => [
  131. 'homepageWeatherAndAirEnabled'
  132. ],
  133. 'auth' => [
  134. 'homepageWeatherAndAirAuth'
  135. ],
  136. 'not_empty' => [
  137. 'homepageWeatherAndAirLatitude',
  138. 'homepageWeatherAndAirLongitude'
  139. ]
  140. ]
  141. ];
  142. if (array_key_exists($key, $permissions)) {
  143. return $permissions[$key];
  144. } elseif ($key == 'all') {
  145. return $permissions;
  146. } else {
  147. return [];
  148. }
  149. }
  150. public function homepageOrderWeatherAndAir()
  151. {
  152. if ($this->homepageItemPermissions($this->weatherHomepagePermissions('main'))) {
  153. return '
  154. <div id="' . __FUNCTION__ . '">
  155. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Weather...</h2></div>
  156. <script>
  157. // Weather And Air
  158. homepageWeatherAndAir("' . $this->config['homepageWeatherAndAirRefresh'] . '");
  159. // End Weather And Air
  160. </script>
  161. </div>
  162. ';
  163. }
  164. }
  165. public function searchCityForCoordinates($query)
  166. {
  167. try {
  168. $query = $query ?? false;
  169. if (!$query) {
  170. $this->setAPIResponse('error', 'Query was not supplied', 422);
  171. return false;
  172. }
  173. $url = $this->qualifyURL('https://api.mapbox.com/geocoding/v5/mapbox.places/' . urlencode($query) . '.json?access_token=pk.eyJ1IjoiY2F1c2VmeCIsImEiOiJjazhyeGxqeXgwMWd2M2ZydWQ4YmdjdGlzIn0.R50iYuMewh1CnUZ7sFPdHA&limit=5&fuzzyMatch=true');
  174. $options = array('verify' => false);
  175. $response = Requests::get($url, array(), $options);
  176. if ($response->success) {
  177. $this->setAPIResponse('success', null, 200, json_decode($response->body));
  178. return json_decode($response->body);
  179. }
  180. } catch (Requests_Exception $e) {
  181. $this->setAPIResponse('error', $e->getMessage(), 500);
  182. return false;
  183. };
  184. }
  185. public function getWeatherAndAirData()
  186. {
  187. if (!$this->homepageItemPermissions($this->weatherHomepagePermissions('main'), true)) {
  188. return false;
  189. }
  190. $api['content'] = array(
  191. 'weather' => false,
  192. 'air' => false,
  193. 'pollen' => false
  194. );
  195. $apiURL = $this->qualifyURL('https://api.breezometer.com/');
  196. $info = '&lat=' . $this->config['homepageWeatherAndAirLatitude'] . '&lon=' . $this->config['homepageWeatherAndAirLongitude'] . '&units=' . $this->config['homepageWeatherAndAirUnits'] . '&key=' . $this->config['breezometerToken'];
  197. try {
  198. $headers = array();
  199. $options = array();
  200. if ($this->config['homepageWeatherAndAirWeatherEnabled']) {
  201. $endpoint = '/weather/v1/forecast/hourly?hours=120&metadata=true';
  202. $response = Requests::get($apiURL . $endpoint . $info, $headers, $options);
  203. if ($response->success) {
  204. $apiData = json_decode($response->body, true);
  205. $api['content']['weather'] = ($apiData['error'] === null) ? $apiData : false;
  206. unset($apiData);
  207. }
  208. }
  209. if ($this->config['homepageWeatherAndAirAirQualityEnabled']) {
  210. $endpoint = '/air-quality/v2/current-conditions?features=breezometer_aqi,local_aqi,health_recommendations,sources_and_effects,dominant_pollutant_concentrations,pollutants_concentrations,pollutants_aqi_information&metadata=true';
  211. $response = Requests::get($apiURL . $endpoint . $info, $headers, $options);
  212. if ($response->success) {
  213. $apiData = json_decode($response->body, true);
  214. $api['content']['air'] = ($apiData['error'] === null) ? $apiData : false;
  215. unset($apiData);
  216. }
  217. }
  218. if ($this->config['homepageWeatherAndAirPollenEnabled']) {
  219. $endpoint = '/pollen/v2/forecast/daily?features=plants_information,types_information&days=1&metadata=true';
  220. $response = Requests::get($apiURL . $endpoint . $info, $headers, $options);
  221. if ($response->success) {
  222. $apiData = json_decode($response->body, true);
  223. $api['content']['pollen'] = ($apiData['error'] === null) ? $apiData : false;
  224. unset($apiData);
  225. }
  226. }
  227. } catch (Requests_Exception $e) {
  228. $this->writeLog('error', 'Weather And Air Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  229. $this->setAPIResponse('error', $e->getMessage(), 500);
  230. return false;
  231. };
  232. $api['content'] = isset($api['content']) ? $api['content'] : false;
  233. $this->setAPIResponse('success', null, 200, $api);
  234. return $api;
  235. }
  236. }