WeatherStatusController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\ResponseDefinitions;
  26. use OCA\WeatherStatus\Service\WeatherStatusService;
  27. use OCP\AppFramework\Http;
  28. use OCP\AppFramework\Http\DataResponse;
  29. use OCP\AppFramework\OCSController;
  30. use OCP\ILogger;
  31. use OCP\IRequest;
  32. /**
  33. * @psalm-import-type WeatherStatusForecast from ResponseDefinitions
  34. */
  35. class WeatherStatusController extends OCSController {
  36. /** @var string */
  37. private $userId;
  38. /** @var ILogger */
  39. private $logger;
  40. /** @var WeatherStatusService */
  41. private $service;
  42. public function __construct(string $appName,
  43. IRequest $request,
  44. ILogger $logger,
  45. WeatherStatusService $service,
  46. ?string $userId) {
  47. parent::__construct($appName, $request);
  48. $this->userId = $userId;
  49. $this->logger = $logger;
  50. $this->service = $service;
  51. }
  52. /**
  53. * @NoAdminRequired
  54. *
  55. * Try to use the address set in user personal settings as weather location
  56. *
  57. * @return DataResponse<Http::STATUS_OK, array{success: bool, lat: ?float, lon: ?float, address: ?string}, array{}>
  58. */
  59. public function usePersonalAddress(): DataResponse {
  60. return new DataResponse($this->service->usePersonalAddress());
  61. }
  62. /**
  63. * @NoAdminRequired
  64. *
  65. * Change the weather status mode. There are currently 2 modes:
  66. * - ask the browser
  67. * - use the user defined address
  68. *
  69. * @param int $mode New mode
  70. * @return DataResponse<Http::STATUS_OK, array{success: bool}, array{}>
  71. */
  72. public function setMode(int $mode): DataResponse {
  73. return new DataResponse($this->service->setMode($mode));
  74. }
  75. /**
  76. * @NoAdminRequired
  77. *
  78. * Set address and resolve it to get coordinates
  79. * or directly set coordinates and get address with reverse geocoding
  80. *
  81. * @param string|null $address Any approximative or exact address
  82. * @param float|null $lat Latitude in decimal degree format
  83. * @param float|null $lon Longitude in decimal degree format
  84. * @return DataResponse<Http::STATUS_OK, array{success: bool, lat: ?float, lon: ?float, address: ?string}, array{}>
  85. */
  86. public function setLocation(?string $address, ?float $lat, ?float $lon): DataResponse {
  87. $currentWeather = $this->service->setLocation($address, $lat, $lon);
  88. return new DataResponse($currentWeather);
  89. }
  90. /**
  91. * @NoAdminRequired
  92. *
  93. * Get stored user location
  94. *
  95. * @return DataResponse<Http::STATUS_OK, array{lat: float, lon: float, address: string, mode: int}, array{}>
  96. */
  97. public function getLocation(): DataResponse {
  98. $location = $this->service->getLocation();
  99. return new DataResponse($location);
  100. }
  101. /**
  102. * @NoAdminRequired
  103. *
  104. * Get forecast for current location
  105. *
  106. * @return DataResponse<Http::STATUS_OK, WeatherStatusForecast[], array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{success: bool}, array{}>
  107. *
  108. * 200: Forecast returned
  109. * 404: Forecast not found
  110. */
  111. public function getForecast(): DataResponse {
  112. $forecast = $this->service->getForecast();
  113. if (isset($forecast['success']) && $forecast['success'] === false) {
  114. return new DataResponse($forecast, Http::STATUS_NOT_FOUND);
  115. } else {
  116. return new DataResponse($forecast);
  117. }
  118. }
  119. /**
  120. * @NoAdminRequired
  121. *
  122. * Get favorites list
  123. *
  124. * @return DataResponse<Http::STATUS_OK, string[], array{}>
  125. */
  126. public function getFavorites(): DataResponse {
  127. return new DataResponse($this->service->getFavorites());
  128. }
  129. /**
  130. * @NoAdminRequired
  131. *
  132. * Set favorites list
  133. *
  134. * @param string[] $favorites Favorite addresses
  135. * @return DataResponse<Http::STATUS_OK, array{success: bool}, array{}>
  136. */
  137. public function setFavorites(array $favorites): DataResponse {
  138. return new DataResponse($this->service->setFavorites($favorites));
  139. }
  140. }