source: webapp/webservice/index.php@ 3121b3b

0.5 app-cordova debian/bullseye debian/jessie
Last change on this file since 3121b3b was 3121b3b, checked in by Joachim Langenbach <joachim.langenbach@…>, 10 years ago
  • Webservice is now able to connect to GPSD
    • Improved the display section in webclient

Signed-off-by: Joachim Langenbach <joachim.langenbach@…>

  • Property mode set to 100644
File size: 2.6 KB
Line 
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
19/**
20 * The GPSD parts are adepted from the gpsd.php script deployed with GPSD package!
21 */
22
23$gpsdHost = "localhost";
24$gpsdPort = "2947";
25
26set_time_limit(3);
27ini_set('max_execution_time', 3);
28
29function readGpsd(){
30 global $gpsdHost, $gpsdPort;
31
32
33 $speedOverGround = "-";
34 $gpsfix = "-";
35 $errno = "";
36 $errstr = "";
37
38 $sock = @fsockopen($gpsdHost, $gpsdPort, $errno, $errstr, 2);
39 if(!$sock){
40 $speedOverGround = "-";
41 $gpsfix = -1;
42 }
43 @fwrite($sock, "?WATCH={\"enable\":true}\n");
44 usleep(1000);
45 @fwrite($sock, "?POLL;\n");
46 usleep(1000);
47 for($tries = 0; $tries < 10; $tries++){
48 $response = @fread($sock, 2000); # SKY can be pretty big
49 if (preg_match('/{"class":"POLL".+}/i', $response, $m)){
50 $response = $m[0];
51 break;
52 }
53 }
54 @fclose($sock);
55 if(!$response){
56 $speedOverGround = "-";
57 $gpsfix = -2;
58 }
59 else{
60 $data = json_decode($response, true);
61 if ($data['class'] != 'POLL'){
62 $speedOverGround = "-";
63 $gpsfix = -3;
64 }
65 if(!array_key_exists('tpv', $data) && is_array($data['tpv'])){
66 $speedOverGround = "-";
67 $gpsfix = -4;
68 }
69 $tpv = $data['tpv'][0];
70
71 if(!array_key_exists('mode', $tpv)){
72 $gpsfix = -5;
73 }
74 else{
75 switch($tpv['mode']){
76 case 1:
77 $gpsfix = "2D";
78 break;
79 case 2:
80 $gpsfix = "3D";
81 break;
82 default:
83 $gpsfix = "-";
84 }
85 }
86
87 if(!array_key_exists('speed', $tpv)){
88 $speedOverGround = "-1";
89 }
90 else{
91 $speedOverGround = $tpv['speed'];
92 }
93 }
94
95 return array("speedOverGround" => $speedOverGround, "gpsfix" => $gpsfix);
96}
97
98
99
100mt_srand((double) microtime() * 1000000);
101
102$result = array();
103
104$gpsdResult = readGpsd();
105
106$result["speedthroughwater"] = mt_rand(0, 10);
107$result["speedoverground"] = $gpsdResult["speedOverGround"];
108$result["waterdepth"] = mt_rand(1, 30) + 0.5;
109$result["watertemperature"] = mt_rand(15, 25) + 0.5;
110$result["gpsfix"] = $gpsdResult["gpsfix"];
111
112echo json_encode($result);
113
114?>
Note: See TracBrowser for help on using the repository browser.