| [d55ec21] | 1 | <?php
|
|---|
| 2 | /*
|
|---|
| 3 | Copyright (C) 2015 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
|
|---|
| 4 |
|
|---|
| 5 | This library is free software; you can redistribute it and/or
|
|---|
| 6 | modify it under the terms of the GNU Lesser General Public
|
|---|
| 7 | License as published by the Free Software Foundation; either
|
|---|
| 8 | version 2.1 of the License, or (at your option) any later version.
|
|---|
| 9 |
|
|---|
| 10 | This library is distributed in the hope that it will be useful,
|
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 13 | Lesser General Public License for more details.
|
|---|
| 14 |
|
|---|
| 15 | You should have received a copy of the GNU Lesser General Public
|
|---|
| 16 | License along with this library; if not, write to the Free Software
|
|---|
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|---|
| 18 | MA 02110-1301 USA.
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | namespace SiWeather\Webservice;
|
|---|
| 22 |
|
|---|
| 23 | require(__DIR__ ."/../../inc/envsetup.php");
|
|---|
| 24 |
|
|---|
| 25 | /**
|
|---|
| 26 | * @brief Implementation of the Webservice API
|
|---|
| 27 | *
|
|---|
| 28 | * @module WebserviceAPI
|
|---|
| 29 | */
|
|---|
| 30 | class WebserviceApi {
|
|---|
| 31 | /**
|
|---|
| 32 | * @brief Handles the stations (list) REST requests
|
|---|
| 33 | */
|
|---|
| 34 | public static function stations(){
|
|---|
| 35 | $stations = Array();
|
|---|
| 36 |
|
|---|
| 37 | $db = new DatabaseHandler();
|
|---|
| 38 | if(!$db->connect()){
|
|---|
| 39 | echo json_encode
|
|---|
| 40 | (
|
|---|
| 41 | [
|
|---|
| 42 | "result" => "failed",
|
|---|
| 43 | "message" => "Could not connect to the database",
|
|---|
| 44 | ]
|
|---|
| 45 | );
|
|---|
| 46 | return;
|
|---|
| 47 | }
|
|---|
| 48 | $db->exec("BEGIN");
|
|---|
| 49 |
|
|---|
| 50 | $sqlStatement = $db->prepare("SELECT stations.uuid as uuid, stations.name as name, X(stations.location) as longitude, Y(stations.location) as latitude, cities.name as city FROM stations, cities WHERE stations.city=cities.id");
|
|---|
| 51 | if(!$sqlStatement){
|
|---|
| 52 | $db->exec("ROLLBACK");
|
|---|
| 53 | echo json_encode
|
|---|
| 54 | (
|
|---|
| 55 | [
|
|---|
| 56 | "result" => "failed",
|
|---|
| 57 | "message" => "Could not fetch stations from database",
|
|---|
| 58 | ]
|
|---|
| 59 | );
|
|---|
| 60 | return;
|
|---|
| 61 | }
|
|---|
| 62 | $result = $sqlStatement->execute();
|
|---|
| 63 | while($row = $result->fetchArray()){
|
|---|
| 64 | $station = Array();
|
|---|
| 65 | $station["uuid"] = $row["uuid"];
|
|---|
| 66 | $station["name"] = $row["name"];
|
|---|
| 67 | $station["longitude"] = $row["longitude"];
|
|---|
| 68 | $station["latitude"] = $row["latitude"];
|
|---|
| 69 | $station["city"] = $row["city"];
|
|---|
| 70 | // $station["values"] = Array();
|
|---|
| 71 | // $station["values"][] = "temperature";
|
|---|
| 72 | // $station["values"][] = "humidity";
|
|---|
| 73 | $station["url"] = "stations/". $station["uuid"];
|
|---|
| 74 | $stations[] = $station;
|
|---|
| 75 | }
|
|---|
| 76 | $db->exec("COMMIT");
|
|---|
| 77 |
|
|---|
| 78 | echo json_encode($stations);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| [8a5cdba] | 82 | * @brief Registers a new weather station
|
|---|
| 83 | *
|
|---|
| 84 | * @note Call this function with a big timeout > 2seconds, because we use another webservice
|
|---|
| 85 | * to fetch location data. This fetch runs already max. 2 seconds.
|
|---|
| 86 | * @note Requires php-pecl-uuid
|
|---|
| 87 | * @note Requires php-pecl-rrd
|
|---|
| 88 | */
|
|---|
| [d55ec21] | 89 | public static function stationsRegister(){
|
|---|
| 90 | $geoLocationRadius = 5;
|
|---|
| 91 |
|
|---|
| 92 | try{
|
|---|
| 93 | $app = \Slim\Slim::getInstance("siweather");
|
|---|
| 94 | $body = $app->request->getBody();
|
|---|
| 95 | $json = json_decode($body, true);
|
|---|
| 96 |
|
|---|
| 97 | $app->response()->header("Content-Type", "application/json");
|
|---|
| 98 |
|
|---|
| [7271978] | 99 | if(!function_exists("uuid_create")){
|
|---|
| [8a5cdba] | 100 | error_log("pecl uuid extension not installed!");
|
|---|
| [7271978] | 101 | echo json_encode
|
|---|
| 102 | (
|
|---|
| 103 | [
|
|---|
| 104 | "result" => "missinguuidcreate",
|
|---|
| 105 | "message" => "The function uuid_create() from pecl package http://pecl.php.net/package/uuid is not installed, but needed!",
|
|---|
| 106 | ]
|
|---|
| 107 | );
|
|---|
| 108 | return;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| [8a5cdba] | 111 | if(!function_exists("rrd_create") || !function_exists("rrd_update")){
|
|---|
| 112 | error_log("pecl rrd extension not installed!");
|
|---|
| 113 | echo json_encode
|
|---|
| 114 | (
|
|---|
| 115 | [
|
|---|
| 116 | "result" => "missingrrd",
|
|---|
| 117 | "message" => "The function rrd_create() or rrd_update() from pecl package http://pecl.php.net/package/rrd is not installed, but needed!",
|
|---|
| 118 | ]
|
|---|
| 119 | );
|
|---|
| 120 | return;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| [d55ec21] | 123 | $expectedKeys = array("name", "city", "longitude", "latitude", "email");
|
|---|
| 124 | $missingKeys = array();
|
|---|
| 125 | foreach($expectedKeys as $key){
|
|---|
| 126 | if(!array_key_exists($key, $json))
|
|---|
| 127 | $missingKeys[] = $key;
|
|---|
| 128 | }
|
|---|
| 129 | if(count($missingKeys) > 0){
|
|---|
| 130 | echo json_encode
|
|---|
| 131 | (
|
|---|
| 132 | [
|
|---|
| 133 | "result" => "missingkeys",
|
|---|
| 134 | "message" => "Your request misses the following keys: ". implode(", ", $missingKeys),
|
|---|
| 135 | ]
|
|---|
| 136 | );
|
|---|
| 137 | return;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | if(!Location::ValidLatitude($json["latitude"])){
|
|---|
| 141 | echo json_encode
|
|---|
| 142 | (
|
|---|
| 143 | [
|
|---|
| 144 | "result" => "invalid latitude",
|
|---|
| 145 | "message" => "Your specified an invalid latitude: ". $json["latitude"],
|
|---|
| 146 | ]
|
|---|
| 147 | );
|
|---|
| 148 | return;
|
|---|
| 149 | }
|
|---|
| 150 | if(!Location::ValidLongitude($json["longitude"])){
|
|---|
| 151 | echo json_encode
|
|---|
| 152 | (
|
|---|
| 153 | [
|
|---|
| 154 | "result" => "invalid longitude",
|
|---|
| 155 | "message" => "Your specified an invalid longitude: ". $json["longitude"],
|
|---|
| 156 | ]
|
|---|
| 157 | );
|
|---|
| 158 | return;
|
|---|
| 159 | }
|
|---|
| 160 | if(empty($json["email"]) || !filter_var($json["email"], FILTER_VALIDATE_EMAIL)){
|
|---|
| 161 | echo json_encode
|
|---|
| 162 | (
|
|---|
| 163 | [
|
|---|
| 164 | "result" => "invalid email",
|
|---|
| 165 | "message" => "Your specified an invalid email address: ". $json["email"],
|
|---|
| 166 | ]
|
|---|
| 167 | );
|
|---|
| 168 | return;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | $errorMessage = "";
|
|---|
| 172 | $locations = GeoLocator::locations($json["latitude"], $json["longitude"], $geoLocationRadius, $errorMessage);
|
|---|
| 173 | if(!$locations || count($locations) < 1){
|
|---|
| 174 | error_log($errorMessage);
|
|---|
| 175 | echo json_encode
|
|---|
| 176 | (
|
|---|
| 177 | [
|
|---|
| 178 | "result" => "unprovenlocation",
|
|---|
| 179 | "message" => "Could not check the city (". $json["city"] .") and location (". $json["latitude"] .", ". $json["longitude"] .") you send with help of your city and geo coordinates. Please make sure, your data is correct.",
|
|---|
| 180 | ]
|
|---|
| 181 | );
|
|---|
| 182 | return;
|
|---|
| 183 | }
|
|---|
| 184 | else{
|
|---|
| 185 | $validatedLocation = false;
|
|---|
| 186 | foreach($locations as $location){
|
|---|
| 187 | if($location->city() == $json["city"]){
|
|---|
| 188 | $validatedLocation = $location;
|
|---|
| 189 | }
|
|---|
| 190 | }
|
|---|
| 191 | if(!$validatedLocation){
|
|---|
| 192 | $message = "The city (". $json[city] .") and location (". $json["latitude"] .", ". $json["longitude"] .") you send, does not suite with locations we found at your given location (city, latitude, longitude):\n";
|
|---|
| 193 | foreach($locations as $location){
|
|---|
| 194 | $message .= " ". $location->city() .": ". $location->latitude() .", ". $location->longitude() ."\n";
|
|---|
| 195 | }
|
|---|
| 196 | $message .= " Please make sure, your data is correct.";
|
|---|
| 197 |
|
|---|
| 198 | echo json_encode
|
|---|
| 199 | (
|
|---|
| 200 | [
|
|---|
| 201 | "result" => "unprovenlocation",
|
|---|
| 202 | "message" => $message,
|
|---|
| 203 | ]
|
|---|
| 204 | );
|
|---|
| 205 | return;
|
|---|
| 206 | }
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | $db = new DatabaseHandler();
|
|---|
| 210 | if(!$db->connect()){
|
|---|
| 211 | echo json_encode
|
|---|
| 212 | (
|
|---|
| 213 | [
|
|---|
| 214 | "result" => "failed",
|
|---|
| 215 | "message" => "Could not connect to the database",
|
|---|
| 216 | ]
|
|---|
| 217 | );
|
|---|
| 218 | return;
|
|---|
| 219 | }
|
|---|
| 220 | $db->exec("BEGIN");
|
|---|
| 221 |
|
|---|
| 222 | // find out, if the city is already there
|
|---|
| 223 | $cityId = -1;
|
|---|
| 224 | $sqlStatement = $db->prepare("SELECT id FROM cities WHERE name=:name AND location=:location");
|
|---|
| 225 | if(!$sqlStatement){
|
|---|
| 226 | $db->exec("ROLLBACK");
|
|---|
| 227 | echo json_encode
|
|---|
| 228 | (
|
|---|
| 229 | [
|
|---|
| 230 | "result" => "failed",
|
|---|
| 231 | "message" => "Could not fetch city from database",
|
|---|
| 232 | ]
|
|---|
| 233 | );
|
|---|
| 234 | return;
|
|---|
| 235 | }
|
|---|
| 236 | $sqlStatement->bindValue(":name", $validatedLocation->city());
|
|---|
| 237 | $sqlStatement->bindValue(":location", "POINT(". $location->locationWkt() .")");
|
|---|
| 238 | $result = $sqlStatement->execute();
|
|---|
| 239 | if($result->numColumns() && $result->columnType(0) != SQLITE3_NULL){
|
|---|
| 240 | error_log("not cool");
|
|---|
| 241 | $row = $result->fetchArray();
|
|---|
| 242 | $cityId = $row[0];
|
|---|
| 243 | error_log($row[0]);
|
|---|
| 244 | }
|
|---|
| 245 | else{
|
|---|
| 246 | $sqlStatement = $db->prepare("INSERT INTO cities (name, location) VALUES(:name, GeomFromText(:location, 4326))");
|
|---|
| 247 | if(!$sqlStatement){
|
|---|
| 248 | $db->exec("ROLLBACK");
|
|---|
| 249 | echo json_encode
|
|---|
| 250 | (
|
|---|
| 251 | [
|
|---|
| 252 | "result" => "failed",
|
|---|
| 253 | "message" => "Could insert city into the database",
|
|---|
| 254 | ]
|
|---|
| 255 | );
|
|---|
| 256 | return;
|
|---|
| 257 | }
|
|---|
| 258 | $sqlStatement->bindValue(":name", $validatedLocation->city());
|
|---|
| 259 | $sqlStatement->bindValue(":location", "POINT(". $location->locationWkt() .")");
|
|---|
| 260 | $sqlResult = $sqlStatement->execute();
|
|---|
| 261 | if(!$sqlResult){
|
|---|
| 262 | error_log("Failed to add city to the database: ". $db->lastDatabaseErrorMessage());
|
|---|
| 263 | $db->exec("ROLLBACK");
|
|---|
| 264 | echo json_encode
|
|---|
| 265 | (
|
|---|
| 266 | [
|
|---|
| 267 | "result" => "failed",
|
|---|
| 268 | "message" => "Could not add city to the database",
|
|---|
| 269 | ]
|
|---|
| 270 | );
|
|---|
| 271 | return;
|
|---|
| 272 | }
|
|---|
| 273 | $cityId = $db->lastInsertRowId();
|
|---|
| 274 | }
|
|---|
| 275 | if($cityId < 0){
|
|---|
| 276 | error_log("Failed to select city from the database: ". $db->lastDatabaseErrorMessage());
|
|---|
| 277 | $db->exec("ROLLBACK");
|
|---|
| 278 | echo json_encode
|
|---|
| 279 | (
|
|---|
| 280 | [
|
|---|
| 281 | "result" => "failed",
|
|---|
| 282 | "message" => "Could not select city from the database",
|
|---|
| 283 | ]
|
|---|
| 284 | );
|
|---|
| 285 | return;
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | // insert station
|
|---|
| 289 | $uuid = uuid_create(4);
|
|---|
| 290 | $sqlStatement = $db->prepare("INSERT INTO stations (name, city, uuid, ownerMail, location) VALUES(:name, :city, :uuid, :ownerMail, GeomFromText(:location, 4326))");
|
|---|
| 291 | if(!$sqlStatement){
|
|---|
| 292 | $db->exec("ROLLBACK");
|
|---|
| 293 | echo json_encode
|
|---|
| 294 | (
|
|---|
| 295 | [
|
|---|
| 296 | "result" => "failed",
|
|---|
| 297 | "message" => "Could insert station into the database",
|
|---|
| 298 | ]
|
|---|
| 299 | );
|
|---|
| 300 | return;
|
|---|
| 301 | }
|
|---|
| 302 | $sqlStatement->bindValue(":name", $json["name"]);
|
|---|
| 303 | $sqlStatement->bindValue(":city", $cityId);
|
|---|
| 304 | $sqlStatement->bindValue(":uuid", $uuid);
|
|---|
| 305 | $sqlStatement->bindValue(":ownerMail",$json["email"]);
|
|---|
| 306 | $sqlStatement->bindValue(":location", "POINT(". $location->locationWkt() .")");
|
|---|
| 307 | $sqlResult = $sqlStatement->execute();
|
|---|
| 308 | if(!$sqlResult){
|
|---|
| 309 | error_log("Failed to add station to the database: ". $db->lastDatabaseErrorMessage());
|
|---|
| 310 | $db->query("ROLLBACK");
|
|---|
| 311 | echo json_encode
|
|---|
| 312 | (
|
|---|
| 313 | [
|
|---|
| 314 | "result" => "failed",
|
|---|
| 315 | "message" => "Could not add station to the database",
|
|---|
| 316 | ]
|
|---|
| 317 | );
|
|---|
| 318 | return;
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| [8a5cdba] | 321 | // creating rrd file
|
|---|
| 322 | $webserviceConfig = new WebserviceConfig();
|
|---|
| 323 | $rrdFileName = $webserviceConfig->dataDirectory() . $uuid .".rrd";
|
|---|
| 324 | $rrdCreator = new \RRDCreator($rrdFileName, "now", 900);
|
|---|
| 325 | $rrdCreator->addDataSource("Temp:GAUGE:1320:-100:100");
|
|---|
| 326 | $rrdCreator->addDataSource("Humidity:GAUGE:1320:0:100");
|
|---|
| 327 | $rrdCreator->addDataSource("Pressure:GAUGE:1320:800:1200");
|
|---|
| 328 | $rrdCreator->addDataSource("WindSpeed:GAUGE:1320:0:999");
|
|---|
| 329 | $rrdCreator->addDataSource("Winddirection:GAUGE:1320:1:360");
|
|---|
| 330 | $rrdCreator->addDataSource("Rainfall:GAUGE:1320:0:999");
|
|---|
| 331 | $rrdCreator->addArchive("AVERAGE:0.5:1:96");
|
|---|
| 332 | $rrdCreator->addArchive("AVERAGE:0.5:4:96");
|
|---|
| 333 | $rrdCreator->addArchive("AVERAGE:0.5:8:360");
|
|---|
| 334 | $rrdCreator->addArchive("AVERAGE:0.5:16:2190");
|
|---|
| 335 | $rrdCreator->addArchive("AVERAGE:0.5:24:146000");
|
|---|
| 336 | $rrdCreator->addArchive("AVERAGE:0.5:48:146000");
|
|---|
| 337 | $rrdCreator->addArchive("AVERAGE:0.5:96:146000");
|
|---|
| 338 | $rrdCreator->addArchive("MIN:0.5:4:96");
|
|---|
| 339 | $rrdCreator->addArchive("MIN:0.5:8:360");
|
|---|
| 340 | $rrdCreator->addArchive("MIN:0.5:16:2190");
|
|---|
| 341 | $rrdCreator->addArchive("MIN:0.5:24:146000");
|
|---|
| 342 | $rrdCreator->addArchive("MIN:0.5:48:146000");
|
|---|
| 343 | $rrdCreator->addArchive("MIN:0.5:96:146000");
|
|---|
| 344 | $rrdCreator->addArchive("MAX:0.5:4:96");
|
|---|
| 345 | $rrdCreator->addArchive("MAX:0.5:8:360");
|
|---|
| 346 | $rrdCreator->addArchive("MAX:0.5:16:2190");
|
|---|
| 347 | $rrdCreator->addArchive("MAX:0.5:24:146000");
|
|---|
| 348 | $rrdCreator->addArchive("MAX:0.5:48:146000");
|
|---|
| 349 | $rrdCreator->addArchive("MAX:0.5:96:146000");
|
|---|
| 350 | if(!$rrdCreator->save()){
|
|---|
| 351 | $db->query("ROLLBACK");
|
|---|
| 352 | $errorMessage = rrd_error();
|
|---|
| 353 | echo json_encode
|
|---|
| 354 | (
|
|---|
| 355 | [
|
|---|
| 356 | "result" => "failed",
|
|---|
| 357 | "message" => "Could not create rrd files for this station: ". $errorMessage,
|
|---|
| 358 | ]
|
|---|
| 359 | );
|
|---|
| 360 | return;
|
|---|
| 361 | }
|
|---|
| 362 |
|
|---|
| [d55ec21] | 363 | // finish transaction
|
|---|
| 364 | $db->exec("COMMIT");
|
|---|
| 365 |
|
|---|
| 366 | echo json_encode
|
|---|
| 367 | (
|
|---|
| 368 | [
|
|---|
| 369 | "result" => "success",
|
|---|
| 370 | "uuid" => $uuid,
|
|---|
| 371 | ]
|
|---|
| 372 | );
|
|---|
| 373 | }
|
|---|
| 374 | catch(Exception $exception){
|
|---|
| 375 | error_log("Caught exception handling request stationsRegister: ". $exception->getMessage());
|
|---|
| 376 | $app->response()->status(400);
|
|---|
| 377 | $app->response()->header("X-Status-Reason", $exception->getMessage());
|
|---|
| 378 | }
|
|---|
| 379 | }
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 | ?>
|
|---|