weather.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 = [
  17. 'debug' => true,
  18. 'settings' => [
  19. 'Enable' => [
  20. $this->settingsOption('enable', 'homepageWeatherAndAirEnabled'),
  21. $this->settingsOption('auth', 'homepageWeatherAndAirAuth'),
  22. ],
  23. 'Connection' => [
  24. $this->settingsOption('input', 'homepageWeatherAndAirLatitude', ['label' => 'Latitude', 'help' => 'Please enter full latitude including minus if needed']),
  25. $this->settingsOption('input', 'homepageWeatherAndAirLongitude', ['label' => 'Longitude', 'help' => 'Please enter full longitude including minus if needed']),
  26. $this->settingsOption('blank'),
  27. $this->settingsOption('button', null, ['type' => 'button', 'label' => '', 'icon' => 'fa fa-search', 'class' => 'pull-right', 'text' => 'Need Help With Coordinates?', 'attr' => 'onclick="showLookupCoordinatesModal()"']),
  28. ],
  29. 'Options' => [
  30. $this->settingsOption('title', 'homepageWeatherAndAirWeatherHeader'),
  31. $this->settingsOption('toggle-title', 'homepageWeatherAndAirWeatherHeaderToggle'),
  32. $this->settingsOption('enable', 'homepageWeatherAndAirWeatherEnabled', ['label' => 'Enable Weather', 'help' => 'Toggles the view module for Weather']),
  33. $this->settingsOption('enable', 'homepageWeatherAndAirAirQualityEnabled', ['label' => 'Enable Air Quality', 'help' => 'Toggles the view module for Air Quality']),
  34. $this->settingsOption('enable', 'homepageWeatherAndAirPollenEnabled', ['label' => 'Enable Pollen', 'help' => 'Toggles the view module for Pollen']),
  35. $this->settingsOption('select', 'homepageWeatherAndAirUnits', ['label' => 'Unit of Measurement', 'options' => [['name' => 'Imperial', 'value' => 'imperial'], ['name' => 'Metric', 'value' => 'metric']]]),
  36. $this->settingsOption('refresh', 'homepageWeatherAndAirRefresh'),
  37. ],
  38. ]
  39. ];
  40. return array_merge($homepageInformation, $homepageSettings);
  41. }
  42. public function weatherHomepagePermissions($key = null)
  43. {
  44. $permissions = [
  45. 'main' => [
  46. 'enabled' => [
  47. 'homepageWeatherAndAirEnabled'
  48. ],
  49. 'auth' => [
  50. 'homepageWeatherAndAirAuth'
  51. ],
  52. 'not_empty' => [
  53. 'homepageWeatherAndAirLatitude',
  54. 'homepageWeatherAndAirLongitude'
  55. ]
  56. ]
  57. ];
  58. return $this->homepageCheckKeyPermissions($key, $permissions);
  59. }
  60. public function homepageOrderWeatherAndAir()
  61. {
  62. if ($this->homepageItemPermissions($this->weatherHomepagePermissions('main'))) {
  63. return '
  64. <div id="' . __FUNCTION__ . '">
  65. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Weather...</h2></div>
  66. <script>
  67. // Weather And Air
  68. homepageWeatherAndAir("' . $this->config['homepageWeatherAndAirRefresh'] . '");
  69. // End Weather And Air
  70. </script>
  71. </div>
  72. ';
  73. }
  74. }
  75. public function searchCityForCoordinates($query)
  76. {
  77. try {
  78. $query = $query ?? false;
  79. if (!$query) {
  80. $this->setAPIResponse('error', 'Query was not supplied', 422);
  81. return false;
  82. }
  83. $url = $this->qualifyURL('https://api.mapbox.com/geocoding/v5/mapbox.places/' . urlencode($query) . '.json?access_token=pk.eyJ1IjoiY2F1c2VmeCIsImEiOiJjazhyeGxqeXgwMWd2M2ZydWQ4YmdjdGlzIn0.R50iYuMewh1CnUZ7sFPdHA&limit=5&fuzzyMatch=true');
  84. $response = Requests::get($url);
  85. if ($response->success) {
  86. $this->setAPIResponse('success', null, 200, json_decode($response->body));
  87. return json_decode($response->body);
  88. }
  89. } catch (Requests_Exception $e) {
  90. $this->setResponse(500, $e->getMessage());
  91. return false;
  92. };
  93. }
  94. public function getWeatherAndAirData()
  95. {
  96. if (!$this->homepageItemPermissions($this->weatherHomepagePermissions('main'), true)) {
  97. return false;
  98. }
  99. $api['content'] = array(
  100. 'weather' => false,
  101. 'air' => false,
  102. 'pollen' => false
  103. );
  104. $apiURL = $this->qualifyURL('https://api.breezometer.com/');
  105. $info = '&lat=' . $this->config['homepageWeatherAndAirLatitude'] . '&lon=' . $this->config['homepageWeatherAndAirLongitude'] . '&units=' . $this->config['homepageWeatherAndAirUnits'] . '&key=' . $this->config['breezometerToken'];
  106. try {
  107. if ($this->config['homepageWeatherAndAirWeatherEnabled']) {
  108. $endpoint = '/weather/v1/forecast/hourly?hours=120&metadata=true';
  109. $options = $this->requestOptions($apiURL, $this->config['homepageWeatherAndAirRefresh']);
  110. $response = Requests::get($apiURL . $endpoint . $info, [], $options);
  111. if ($response->success) {
  112. $apiData = json_decode($response->body, true);
  113. $api['content']['weather'] = ($apiData['error'] === null) ? $apiData : false;
  114. unset($apiData);
  115. }
  116. }
  117. if ($this->config['homepageWeatherAndAirAirQualityEnabled']) {
  118. $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';
  119. $response = Requests::get($apiURL . $endpoint . $info);
  120. if ($response->success) {
  121. $apiData = json_decode($response->body, true);
  122. $api['content']['air'] = ($apiData['error'] === null) ? $apiData : false;
  123. unset($apiData);
  124. }
  125. }
  126. if ($this->config['homepageWeatherAndAirPollenEnabled']) {
  127. $endpoint = '/pollen/v2/forecast/daily?features=plants_information,types_information&days=1&metadata=true';
  128. $response = Requests::get($apiURL . $endpoint . $info);
  129. if ($response->success) {
  130. $apiData = json_decode($response->body, true);
  131. $api['content']['pollen'] = ($apiData['error'] === null) ? $apiData : false;
  132. unset($apiData);
  133. }
  134. }
  135. } catch (Requests_Exception $e) {
  136. $this->setLoggerChannel('Weather & Air')->error($e);
  137. $this->setResponse(500, $e->getMessage());
  138. return false;
  139. };
  140. $api['content'] = isset($api['content']) ? $api['content'] : false;
  141. $this->setAPIResponse('success', null, 200, $api);
  142. return $api;
  143. }
  144. }