source: src/webapp/webservice/index.php@ 5519ced

0.5 app-cordova debian/bullseye debian/jessie
Last change on this file since 5519ced was 5519ced, checked in by Joachim Langenbach <joachim.langenbach@…>, 10 years ago
  • Started to implement shutdown functionality
    • Tried to fix webservice rrd_fetch with to many values
  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[d7e6e9c]1<?php
2/*
3 Copyright (C) 2016 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program 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
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
[4ab1c0a]19require_once(__DIR__ ."/inc/Config.php");
20$config = new Config();
21$dbFile = $config->databaseFile();
22
[5519ced]23define("REQUEST_DATA", "data");
24define("REQUEST_SHUTDOWN", "shutdown");
25
26
[4ab1c0a]27// configure roundings per value (key) and precision (value)
28$configRound = array
29(
30 "latitude" => 5,
31 "longitude" => 5,
32 "speedthroughwater" => 1,
33 "speedoverground" => 1,
34 "waterdepth" => 1,
35 "watertemperature" => 1,
36);
37
38$debug = 0;
39if(array_key_exists("debug", $_GET))
40 $debug = $_GET["debug"];
41
[5519ced]42$request = REQUEST_DATA;
43if(array_key_exists("request", $_GET)){
44 $request = $_GET["request"];
45}
46
[4ab1c0a]47if($debug == 0)
48 header('Content-Type: application/json');
49
[5519ced]50$result = array();
51if($request == REQUEST_SHUTDOWN){
52 $handle = @fopen("/var/spool/gnuboat/gnuboat_shutdown", "w");
53 if($handle === false){
54 $message = "Could not create file to indicate shutdown to the daemon (Timezone: ". date_default_timezone_get() .")";
55 if($debug > 0)
56 echo $message;
[4ab1c0a]57 error_log($message);
[5519ced]58 $result["shutdownstart"] = -1;
59 $shutdownTime = new DateTime('NOW');
60 $shutdownTime->modify("+60 seconds");
61 $result["shutdownstart"] = $shutdownTime->format(DateTime::ISO8601);
62 }
63 else{
64 $shutdownTime = new DateTime('NOW');
65 $shutdownTime>modify("+15 seconds");
66 fwrite($handle, $shutdownTime->format(DateTime::ISO8601));
67 $result["shutdownstart"] = $shutdownTime->format(DateTime::ISO8601);
68 }
[4ab1c0a]69}
[5519ced]70elseif($request == REQUEST_DATA){
71 $result["lastvalid"] = array();
72 $result["track"] = array();
[3121b3b]73
[5519ced]74 if(!file_exists($dbFile)){
75 echo json_encode("");
76 if($debug > 0)
77 echo "Database file ". $dbFile ." does not exist!";
78 error_log("Database file ". $dbFile ." does not exist");
79 exit;
80 }
81 // Fetch entries of the last 12hours
82 $data = rrd_fetch($dbFile, array( "AVERAGE", "--resolution", "5", "--start", "-12h"));
83 if($data === false){
84 echo json_encode($result);
85 $message = "Could not open rrd database ". $dbFile;
86 if($debug > 0)
87 echo $message;
88 else
89 error_log($message);
90 exit;
91 }
[3121b3b]92
[5519ced]93 if($debug > 2){
94 echo "<pre>";
95 var_dump($data);
96 echo "</pre>";
97 }
[3121b3b]98
[5519ced]99 $data = $data["data"];
100
101 // transform them into json format
102 $i = 0;
103 $lastTime = -1;
104 $keys = array_keys($data);
105 if(count($keys) < 0 ){
106 error_log("No keys in fetched rrd data from ". $dbFile);
107 echo json_encode("");
108 exit;
109 }
110 for($i = 0; $i < count($data[$keys[0]]); $i++){
111 $time = array_keys($data[$keys[0]])[$i];
112 $allValid = true;
113 $tmp = array();
114 foreach($keys as $key){
115 if($key == "gpsfix"){
116 switch($data[$key][$time]){
117 case 1:
118 $tmp[$key] = "2D";
119 break;
120 case 2:
121 $tmp[$key] = "3D";
122 break;
123 default:
124 $tmp[$key] = "-";
125 }
126 }
127 else{
128 $tmp[$key] = $data[$key][$time];
129 if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
130 $allValid = false;
131 }
132 elseif(array_key_exists($key, $configRound) && is_integer($configRound[$key]))
133 $tmp[$key] = round($tmp[$key], $configRound[$key]);
[3121b3b]134 }
135 }
[5519ced]136 // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
137 if($allValid && $tmp["gpsfix"] != "-"){
138 $result["track"][$time] = $tmp;
139 $lastTime = $time;
140 }
[3121b3b]141 else{
[5519ced]142 if($debug > 0){
143 echo "Failed <pre>";
144 var_dump($tmp);
145 echo "</pre>\n";
[4ab1c0a]146 }
[f8dfbe7]147 }
[4ab1c0a]148 }
[5519ced]149 if($lastTime > -1){
150 $result["lastvalid"] = $result["track"][$lastTime];
[4ab1c0a]151 }
[3121b3b]152}
153
[4ab1c0a]154if($debug > 0){
155 echo json_encode($result, JSON_PRETTY_PRINT);
156}
157else{
158 echo json_encode($result);
159}
[d7e6e9c]160
161?>
Note: See TracBrowser for help on using the repository browser.