weather.php 7.8 KB

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