1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
#!/usr/bin/env python
# encoding: utf-8
"""webgps.py
This is a Python port of webgps.c
from http://www.wireless.org.au/~jhecker/gpsd/
by Beat Bolli <me+gps@drbeat.li>
It creates a skyview of the currently visible GPS satellites and their tracks
over a time period.
Usage:
./webgps.py [duration]
duration may be
- a number of seconds
- a number followed by a time unit ('s' for secinds, 'm' for minutes,
'h' for hours or 'd' for days, e.g. '4h' for a duration of four hours)
- the letter 'c' for continuous operation
If duration is missing, the current skyview is generated and webgps.py exits
immediately. This is the same as giving a duration of 0.
If a duration is given, webgps.py runs for this duration and generates the
tracks of the GPS satellites in view. If the duration is the letter 'c',
the script never exits and continuously updates the skyview.
webgps.py generates two files: a HTML5 file that can be browsed, and a
JavaScript file that contains the drawing commands for the skyview. The HTML5
file auto-refreshes every five minutes. The generated file names are
"gpsd-<duration>.html" and "gpsd-<duration>.js".
If webgps.py is interrupted with Ctrl-C before the duration is over, it saves
the current tracks into the file "tracks.p". This is a Python "pickle" file.
If this file is present on start of webgps.py, it is loaded. This allows to
restart webgps.py without losing accumulated satellite tracks.
"""
# This file is Copyright (c) 2010-2018 by the GPSD project
# SPDX-License-Identifier: BSD-2-clause
from __future__ import absolute_import, print_function, division
import math
import os
import pickle
import sys
import time
from gps import *
gps_version = '3.19-dev'
if gps.__version__ != gps_version:
sys.stderr.write("webgps.py: ERROR: need gps module version %s, got %s\n" %
(gps_version, gps.__version__))
sys.exit(1)
TRACKMAX = 1024
STALECOUNT = 10
DIAMETER = 200
def polartocart(el, az):
radius = DIAMETER * (1 - el / 90.0) # * math.cos(Deg2Rad(float(el)))
theta = Deg2Rad(float(az - 90))
return (
# Changed this back to normal orientation - fw
int(radius * math.cos(theta) + 0.5),
int(radius * math.sin(theta) + 0.5)
)
class Track:
'''Store the track of one satellite.'''
def __init__(self, prn):
self.prn = prn
self.stale = 0
self.posn = [] # list of (x, y) tuples
def add(self, x, y):
pos = (x, y)
self.stale = STALECOUNT
if not self.posn or self.posn[-1] != pos:
self.posn.append(pos)
if len(self.posn) > TRACKMAX:
self.posn = self.posn[-TRACKMAX:]
return 1
return 0
def track(self):
'''Return the track as canvas drawing operations.'''
return('M(%d,%d); ' % self.posn[0] + ''.join(['L(%d,%d); ' %
p for p in self.posn[1:]]))
class SatTracks(gps):
'''gpsd client writing HTML5 and <canvas> output.'''
def __init__(self):
super(SatTracks, self).__init__()
self.sattrack = {} # maps PRNs to Tracks
self.state = None
self.statetimer = time.time()
self.needsupdate = 0
def html(self, fh, jsfile):
fh.write("""<!DOCTYPE html>
<html>
<head>
\t<meta http-equiv=Refresh content=300>
\t<meta charset='utf-8'>
\t<title>GPSD Satellite Positions and Readings</title>
\t<style type='text/css'>
\t\t.num td { text-align: right; }
\t\tth { text-align: left; }
\t</style>
\t<script src='%s'></script>
</head>
<body>
\t<table border=1>
\t\t<tr>
\t\t\t<td>
\t\t\t\t<table border=0 class=num>
\t\t\t\t\t<tr><th>PRN:</th><th>Elev:</th><th>Azim:</th><th>SNR:</th>
<th>Used:</th></tr>
""" % jsfile)
sats = self.satellites[:]
sats.sort(key=lambda x: x.PRN)
for s in sats:
fh.write("\t\t\t\t\t<tr><td>%d</td><td>%d</td><td>%d</td>"
"<td>%d</td><td>%s</td></tr>\n" %
(s.PRN, s.elevation, s.azimuth, s.ss,
s.used and 'Y' or 'N'))
fh.write("\t\t\t\t</table>\n\t\t\t\t<table border=0>\n")
def row(l, v):
fh.write("\t\t\t\t\t<tr><th>%s:</th><td>%s</td></tr>\n" % (l, v))
def deg_to_str(a, hemi):
return '%.6f %c' % (abs(a), hemi[a < 0])
row('Time', self.utc or 'N/A')
if self.fix.mode >= MODE_2D:
row('Latitude', deg_to_str(self.fix.latitude, 'SN'))
row('Longitude', deg_to_str(self.fix.longitude, 'WE'))
row('Altitude', self.fix.mode == MODE_3D and "%f m" %
self.fix.altitude or 'N/A')
row('Speed', isfinite(self.fix.speed) and "%f m/s" %
self.fix.speed or 'N/A')
row('Course', isfinite(self.fix.track) and "%f°" %
self.fix.track or 'N/A')
else:
row('Latitude', 'N/A')
row('Longitude', 'N/A')
row('Altitude', 'N/A')
row('Speed', 'N/A')
row('Course', 'N/A')
row('EPX', isfinite(self.fix.epx) and "%f m" % self.fix.epx or 'N/A')
row('EPY', isfinite(self.fix.epy) and "%f m" % self.fix.epy or 'N/A')
row('EPV', isfinite(self.fix.epv) and "%f m" % self.fix.epv or 'N/A')
row('Climb', self.fix.mode == MODE_3D and isfinite(self.fix.climb) and
"%f m/s" % self.fix.climb or 'N/A')
state = "INIT"
if not (self.valid & ONLINE_SET):
newstate = 0
state = "OFFLINE"
else:
newstate = self.fix.mode
if newstate == MODE_2D:
state = "2D FIX"
elif newstate == MODE_3D:
state = "3D FIX"
else:
state = "NO FIX"
if newstate != self.state:
self.statetimer = time.time()
self.state = newstate
row('State', "%s (%d secs)" % (state, time.time() - self.statetimer))
fh.write("""\t\t\t\t</table>
\t\t\t</td>
\t\t\t<td>
\t\t\t\t<canvas id=satview width=425 height=425>
\t\t\t\t\t<p>Your browser needs HTML5 <canvas> support to display
the satellite view correctly.</p>
\t\t\t\t</canvas>
\t\t\t\t<script type='text/javascript'>draw_satview();</script>
\t\t\t</td>
\t\t</tr>
\t</table>
</body>
</html>
""")
def js(self, fh):
fh.write("""// draw the satellite view
function draw_satview() {
var c = document.getElementById('satview');
if (!c.getContext) return;
var ctx = c.getContext('2d');
if (!ctx) return;
var circle = Math.PI * 2,
M = function (x, y) { ctx.moveTo(x, y); },
L = function (x, y) { ctx.lineTo(x, y); };
ctx.save();
ctx.clearRect(0, 0, c.width, c.height);
ctx.translate(210, 210);
// grid and labels
ctx.strokeStyle = 'black';
ctx.beginPath();
ctx.arc(0, 0, 200, 0, circle, 0);
ctx.stroke();
ctx.beginPath();
ctx.strokeText('N', -4, -202);
ctx.strokeText('W', -210, 4);
ctx.strokeText('E', 202, 4);
ctx.strokeText('S', -4, 210);
ctx.strokeStyle = 'grey';
ctx.beginPath();
ctx.arc(0, 0, 100, 0, circle, 0);
M(2, 0);
ctx.arc(0, 0, 2, 0, circle, 0);
ctx.stroke();
ctx.strokeStyle = 'lightgrey';
ctx.save();
ctx.beginPath();
M(0, -200); L(0, 200); ctx.rotate(circle / 8);
M(0, -200); L(0, 200); ctx.rotate(circle / 8);
M(0, -200); L(0, 200); ctx.rotate(circle / 8);
M(0, -200); L(0, 200);
ctx.stroke();
ctx.restore();
// tracks
ctx.lineWidth = 0.6;
ctx.strokeStyle = 'red';
""")
# Draw the tracks
for t in self.sattrack.values():
if t.posn:
fh.write(" ctx.globalAlpha = %s; ctx.beginPath(); "
"%sctx.stroke();\n" %
(t.stale == 0 and '0.66' or '1', t.track()))
fh.write("""
// satellites
ctx.lineWidth = 1;
ctx.strokeStyle = 'black';
""")
# Draw the satellites
for s in self.satellites:
el, az = s.elevation, s.azimuth
if el == 0 and az == 0:
continue # Skip satellites with unknown position
x, y = polartocart(el, az)
fill = not s.used and 'lightgrey' or \
s.ss < 30 and 'red' or \
s.ss < 35 and 'yellow' or \
s.ss < 40 and 'green' or 'lime'
# Center PRNs in the marker
offset = s.PRN < 10 and 3 or s.PRN >= 100 and -3 or 0
fh.write(" ctx.beginPath(); ctx.fillStyle = '%s'; " % fill)
if s.PRN > 32: # Draw a square for SBAS satellites
fh.write("ctx.rect(%d, %d, 16, 16); " % (x - 8, y - 8))
else:
fh.write("ctx.arc(%d, %d, 8, 0, circle, 0); " % (x, y))
fh.write("ctx.fill(); ctx.stroke(); "
"ctx.strokeText('%s', %d, %d);\n" %
(s.PRN, x - 6 + offset, y + 4))
fh.write("""
ctx.restore();
}
""")
def make_stale(self):
for t in self.sattrack.values():
if t.stale:
t.stale -= 1
def delete_stale(self):
stales = []
for prn in self.sattrack.keys():
if self.sattrack[prn].stale == 0:
stales.append(prn)
self.needsupdate = 1
for prn in stales:
del self.sattrack[prn]
def insert_sat(self, prn, x, y):
try:
t = self.sattrack[prn]
except KeyError:
self.sattrack[prn] = t = Track(prn)
if t.add(x, y):
self.needsupdate = 1
def update_tracks(self):
self.make_stale()
for s in self.satellites:
x, y = polartocart(s.elevation, s.azimuth)
self.insert_sat(s.PRN, x, y)
self.delete_stale()
def generate_html(self, htmlfile, jsfile):
fh = open(htmlfile, 'w')
self.html(fh, jsfile)
fh.close()
def generate_js(self, jsfile):
fh = open(jsfile, 'w')
self.js(fh)
fh.close()
def run(self, suffix, period):
jsfile = 'gpsd' + suffix + '.js'
htmlfile = 'gpsd' + suffix + '.html'
if period is not None:
end = time.time() + period
self.needsupdate = 1
self.stream(WATCH_ENABLE | WATCH_NEWSTYLE)
for report in self:
if report['class'] not in ('TPV', 'SKY'):
continue
self.update_tracks()
if self.needsupdate:
self.generate_js(jsfile)
self.needsupdate = 0
self.generate_html(htmlfile, jsfile)
if period is not None and (
period <= 0 and self.fix.mode >= MODE_2D or
period > 0 and time.time() > end
):
break
def main():
argv = sys.argv[1:]
factors = {
's': 1, 'm': 60, 'h': 60 * 60, 'd': 24 * 60 * 60
}
arg = argv and argv[0] or '0'
if arg[-1:] in factors.keys():
period = int(arg[:-1]) * factors[arg[-1]]
elif arg == 'c':
period = None
else:
period = int(arg)
prefix = '-' + arg
sat = SatTracks()
# restore the tracks
pfile = 'tracks.p'
if os.path.isfile(pfile):
p = open(pfile, 'rb')
try:
sat.sattrack = pickle.load(p)
except ValueError:
print("Ignoring incompatible tracks file.", file=sys.stderr)
p.close()
try:
sat.run(prefix, period)
except KeyboardInterrupt:
# save the tracks
p = open(pfile, 'wb')
# No protocol is backward-compatible from Python 3 to Python 2,
# so we just use the default and punt at load time if needed.
pickle.dump(sat.sattrack, p)
p.close()
if __name__ == '__main__':
main()
|