weather.php 7.5 KB

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