| [1c92d58] | 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 |
|
|---|
| [8c356c1] | 21 | require(__DIR__ ."/inc/envsetup.php");
|
|---|
| 22 |
|
|---|
| 23 | $settings = new SiWeather\Webservice\WebserviceConfig();
|
|---|
| [1c92d58] | 24 |
|
|---|
| [92c9c7c] | 25 | /**
|
|---|
| 26 | * @brief Handles the stations (list) REST requests
|
|---|
| 27 | */
|
|---|
| [1c92d58] | 28 | function stations(){
|
|---|
| 29 | $stations = Array();
|
|---|
| 30 | // $station = Array();
|
|---|
| 31 | // $station["id"] = "123456";
|
|---|
| 32 | // $station["name"] = "Station Name";
|
|---|
| 33 | // $station["longitude"] = 52.320;
|
|---|
| 34 | // $station["latitude"] = 48;
|
|---|
| 35 | // $station["city"] = "Krefeld";
|
|---|
| 36 | // $station["values"] = Array();
|
|---|
| 37 | // $station["values"][] = "temperature";
|
|---|
| 38 | // $station["values"][] = "humidity";
|
|---|
| 39 | // $station["url"] = "stations/". $station["id"];
|
|---|
| 40 | // $stations[] = $station;
|
|---|
| 41 | echo json_encode($stations);
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| [b858e6f] | 44 | /**
|
|---|
| 45 | * @brief Registers a new weather station
|
|---|
| 46 | */
|
|---|
| 47 | function stationsRegister(){
|
|---|
| 48 | try{
|
|---|
| 49 | $app = \Slim\Slim::getInstance("siweather");
|
|---|
| 50 | $body = $app->request->getBody();
|
|---|
| 51 | $json = json_decode($body);
|
|---|
| 52 |
|
|---|
| 53 | $location = new IpGeoLocator($app->request->getIp());
|
|---|
| 54 | if($location->city() != $json["city"]){
|
|---|
| 55 | return json_encode(["result" => "unprovenlocation", "message" => "You send "]);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | $app->response()->header("Content-Type", "application/json");
|
|---|
| 59 | echo json_encode($json);
|
|---|
| 60 | }
|
|---|
| 61 | catch(Exception $exception){
|
|---|
| 62 | error_log("Caught exception handling request stationsRegister: ". $exception->getMessage());
|
|---|
| 63 | $app->response()->status(400);
|
|---|
| 64 | $app->response()->header("X-Status-Reason", $exception->getMessage());
|
|---|
| 65 | }
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| [1c92d58] | 68 | $app = new \Slim\Slim
|
|---|
| 69 | (
|
|---|
| 70 | array
|
|---|
| 71 | (
|
|---|
| [1e37648] | 72 | "mode" => $settings->getConfig("mode"),
|
|---|
| [1c92d58] | 73 | )
|
|---|
| 74 | );
|
|---|
| [b858e6f] | 75 | $app->setName("siweather");
|
|---|
| [1c92d58] | 76 |
|
|---|
| 77 | // Only invoked if mode is "production"
|
|---|
| 78 | $app->configureMode('production', function () use ($app) {
|
|---|
| 79 | $app->config(array(
|
|---|
| [1e37648] | 80 | "log.enable" => true,
|
|---|
| 81 | "log.level" => \Slim\Log::WARN,
|
|---|
| 82 | "debug" => false,
|
|---|
| [1c92d58] | 83 | ));
|
|---|
| 84 | });
|
|---|
| 85 | // Only invoked if mode is "development"
|
|---|
| 86 | $app->configureMode('development', function () use ($app) {
|
|---|
| 87 | $app->config(array(
|
|---|
| [1e37648] | 88 | "log.enable" => true,
|
|---|
| 89 | "log.level" => \Slim\Log::DEBUG,
|
|---|
| 90 | "debug" => true
|
|---|
| [1c92d58] | 91 | ));
|
|---|
| 92 | });
|
|---|
| 93 |
|
|---|
| [1e37648] | 94 | $app->notFound(function () use ($app) {
|
|---|
| 95 | $html = "";
|
|---|
| 96 | if($app->config("debug")){
|
|---|
| 97 | ob_start();
|
|---|
| 98 | phpinfo( INFO_VARIABLES ^ INFO_ENVIRONMENT );
|
|---|
| 99 | $html = ob_get_contents();
|
|---|
| 100 | ob_end_clean();
|
|---|
| 101 | }
|
|---|
| 102 | $app->render("404.php", array("dump" => $html));
|
|---|
| 103 | });
|
|---|
| 104 |
|
|---|
| 105 | $app->get("/setup", function () use ($app){
|
|---|
| 106 | $app->render("setup.php");
|
|---|
| 107 | });
|
|---|
| [1c92d58] | 108 |
|
|---|
| 109 | $app->get("/stations", "stations");
|
|---|
| [b858e6f] | 110 | $app->post("/stations/register", "stationsRegister");
|
|---|
| [1e37648] | 111 |
|
|---|
| [1c92d58] | 112 | $app->run();
|
|---|
| 113 |
|
|---|
| 114 | ?>
|
|---|