WeatherStatusController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Julien Veyssier
  5. *
  6. * @author Julien Veyssier <eneiluj@posteo.net>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\WeatherStatus\Controller;
  25. use OCA\WeatherStatus\Service\WeatherStatusService;
  26. use OCP\AppFramework\Http;
  27. use OCP\AppFramework\Http\DataResponse;
  28. use OCP\AppFramework\OCSController;
  29. use OCP\ILogger;
  30. use OCP\IRequest;
  31. class WeatherStatusController extends OCSController {
  32. /** @var string */
  33. private $userId;
  34. /** @var ILogger */
  35. private $logger;
  36. /** @var WeatherStatusService */
  37. private $service;
  38. public function __construct(string $appName,
  39. IRequest $request,
  40. ILogger $logger,
  41. WeatherStatusService $service,
  42. ?string $userId) {
  43. parent::__construct($appName, $request);
  44. $this->userId = $userId;
  45. $this->logger = $logger;
  46. $this->service = $service;
  47. }
  48. /**
  49. * @NoAdminRequired
  50. *
  51. * Try to use the address set in user personal settings as weather location
  52. *
  53. * @return DataResponse with success state and address information
  54. */
  55. public function usePersonalAddress(): DataResponse {
  56. return new DataResponse($this->service->usePersonalAddress());
  57. }
  58. /**
  59. * @NoAdminRequired
  60. *
  61. * Change the weather status mode. There are currently 2 modes:
  62. * - ask the browser
  63. * - use the user defined address
  64. *
  65. * @param int $mode New mode
  66. * @return DataResponse success state
  67. */
  68. public function setMode(int $mode): DataResponse {
  69. return new DataResponse($this->service->setMode($mode));
  70. }
  71. /**
  72. * @NoAdminRequired
  73. *
  74. * Set address and resolve it to get coordinates
  75. * or directly set coordinates and get address with reverse geocoding
  76. *
  77. * @param string|null $address Any approximative or exact address
  78. * @param float|null $lat Latitude in decimal degree format
  79. * @param float|null $lon Longitude in decimal degree format
  80. * @return DataResponse with success state and address information
  81. */
  82. public function setLocation(?string $address, ?float $lat, ?float $lon): DataResponse {
  83. $currentWeather = $this->service->setLocation($address, $lat, $lon);
  84. return new DataResponse($currentWeather);
  85. }
  86. /**
  87. * @NoAdminRequired
  88. *
  89. * Get stored user location
  90. *
  91. * @return DataResponse which contains coordinates, formatted address and current weather status mode
  92. */
  93. public function getLocation(): DataResponse {
  94. $location = $this->service->getLocation();
  95. return new DataResponse($location);
  96. }
  97. /**
  98. * @NoAdminRequired
  99. *
  100. * Get forecast for current location
  101. *
  102. * @return DataResponse which contains success state and filtered forecast data
  103. */
  104. public function getForecast(): DataResponse {
  105. $forecast = $this->service->getForecast();
  106. if (isset($forecast['success']) && $forecast['success'] === false) {
  107. return new DataResponse($forecast, Http::STATUS_NOT_FOUND);
  108. } else {
  109. return new DataResponse($forecast);
  110. }
  111. }
  112. /**
  113. * @NoAdminRequired
  114. *
  115. * Get favorites list
  116. *
  117. * @return DataResponse which contains the favorite list
  118. */
  119. public function getFavorites(): DataResponse {
  120. return new DataResponse($this->service->getFavorites());
  121. }
  122. /**
  123. * @NoAdminRequired
  124. *
  125. * Set favorites list
  126. *
  127. * @param array $favorites
  128. * @return DataResponse success state
  129. */
  130. public function setFavorites(array $favorites): DataResponse {
  131. return new DataResponse($this->service->setFavorites($favorites));
  132. }
  133. }