weather.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 searchCityForCoordinates($query)
  119. {
  120. try {
  121. $query = $query ?? false;
  122. if (!$query) {
  123. $this->setAPIResponse('error', 'Query was not supplied', 422);
  124. return false;
  125. }
  126. $url = $this->qualifyURL('https://api.mapbox.com/geocoding/v5/mapbox.places/' . urlencode($query) . '.json?access_token=pk.eyJ1IjoiY2F1c2VmeCIsImEiOiJjazhyeGxqeXgwMWd2M2ZydWQ4YmdjdGlzIn0.R50iYuMewh1CnUZ7sFPdHA&limit=5&fuzzyMatch=true');
  127. $options = array('verify' => false);
  128. $response = Requests::get($url, array(), $options);
  129. if ($response->success) {
  130. $this->setAPIResponse('success', null, 200, json_decode($response->body));
  131. return json_decode($response->body);
  132. }
  133. } catch (Requests_Exception $e) {
  134. $this->setAPIResponse('error', $e->getMessage(), 500);
  135. return false;
  136. };
  137. }
  138. public function getWeatherAndAirData()
  139. {
  140. if (!$this->config['homepageWeatherAndAirEnabled']) {
  141. $this->setAPIResponse('error', 'Weather homepage item is not enabled', 409);
  142. return false;
  143. }
  144. if (!$this->qualifyRequest($this->config['homepageWeatherAndAirAuth'])) {
  145. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  146. return false;
  147. }
  148. if (empty($this->config['homepageWeatherAndAirLatitude']) && empty($this->config['homepageWeatherAndAirLongitude'])) {
  149. $this->setAPIResponse('error', 'Weather Latitude and/or Longitude were not defined', 422);
  150. return false;
  151. }
  152. $api['content'] = array(
  153. 'weather' => false,
  154. 'air' => false,
  155. 'pollen' => false
  156. );
  157. $apiURL = $this->qualifyURL('https://api.breezometer.com/');
  158. $info = '&lat=' . $this->config['homepageWeatherAndAirLatitude'] . '&lon=' . $this->config['homepageWeatherAndAirLongitude'] . '&units=' . $this->config['homepageWeatherAndAirUnits'] . '&key=b7401295888443538a7ebe04719c8394';
  159. try {
  160. $headers = array();
  161. $options = array();
  162. if ($this->config['homepageWeatherAndAirWeatherEnabled']) {
  163. $endpoint = '/weather/v1/forecast/hourly?hours=120&metadata=true';
  164. $response = Requests::get($apiURL . $endpoint . $info, $headers, $options);
  165. if ($response->success) {
  166. $apiData = json_decode($response->body, true);
  167. $api['content']['weather'] = ($apiData['error'] === null) ? $apiData : false;
  168. unset($apiData);
  169. }
  170. }
  171. if ($this->config['homepageWeatherAndAirAirQualityEnabled']) {
  172. $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';
  173. $response = Requests::get($apiURL . $endpoint . $info, $headers, $options);
  174. if ($response->success) {
  175. $apiData = json_decode($response->body, true);
  176. $api['content']['air'] = ($apiData['error'] === null) ? $apiData : false;
  177. unset($apiData);
  178. }
  179. }
  180. if ($this->config['homepageWeatherAndAirPollenEnabled']) {
  181. $endpoint = '/pollen/v2/forecast/daily?features=plants_information,types_information&days=1&metadata=true';
  182. $response = Requests::get($apiURL . $endpoint . $info, $headers, $options);
  183. if ($response->success) {
  184. $apiData = json_decode($response->body, true);
  185. $api['content']['pollen'] = ($apiData['error'] === null) ? $apiData : false;
  186. unset($apiData);
  187. }
  188. }
  189. } catch (Requests_Exception $e) {
  190. $this->writeLog('error', 'Weather And Air Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  191. $this->setAPIResponse('error', $e->getMessage(), 500);
  192. return false;
  193. };
  194. $api['content'] = isset($api['content']) ? $api['content'] : false;
  195. $this->setAPIResponse('success', null, 200, $api);
  196. return $api;
  197. }
  198. }