-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGPSd.py
193 lines (179 loc) · 7.51 KB
/
GPSd.py
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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
###############################################################################
# GPSd.py
#
# A gpsd server simulator
#
# -----------------------------------------------------------------------------
# gpsmap - A GPSD simulator based on map positions
# (C) 2014 Gerardo García Peña <[email protected]>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
###############################################################################
import json
import logging
import threading
import time
import select
import socket
class GPSdThread(threading.Thread):
device = '/dev/ojetegps'
host = ''
port = 2947
quit = False
time_started = None
def __init__(self, mc):
super(GPSdThread, self).__init__()
self.quit = False
self.mc = mc
self.time_started = time.gmtime()
def get_time(self, t = None):
if t is None:
t = time.gmtime()
return time.strftime("%Y-%m-%dT%H:%M:%S.000Z", t)
def get_cmd_VERSION(self, params):
return "%s\r\n" % \
json.dumps({
"class" : "VERSION",
"version" : "69dev",
"rev" : "ojete",
"proto_major" : 3,
"proto_minor" : 1,
})
def get_cmd_GST(self):
return "%s\r\n" % json.dumps({
"class" : "GST",
"tag" : "GST",
"device" : self.device,
"time" : self.get_time(),
"rms" : 4.100,
"major" : 13.000,
"minor" : 8.300,
"orient" : 133.000,
"lat" : 11.000,
"lon" : 11.000,
"alt" : 46.000,
})
def get_cmd_TPV(self):
return "%s\r\n" % json.dumps({
"class" : "TPV",
"tag" : "RMC",
"device" : self.device,
"mode" : 3,
"time" : self.get_time(),
"ept" : 0.005,
"lat" : self.mc.curr_pos[0],
"lon" : self.mc.curr_pos[1],
"alt" : 0,
"epx" : 10.000, # longitude error estimated in meters
"epy" : 10.000, # latitude error estimate in meters
"epv" : 0, # estimated vertical error in meters
"track" : 0.0000,
"speed" : 0.000,
"climb" : 0.000,
"eps" : 0.00, # speed error estimated in meters/second
})
def get_cmd_DEVICES(self):
return "%s\r\n" % json.dumps({
"class" : "DEVICES",
"devices" :
[
{ "class" : "DEVICE",
"path" : self.device,
"activated" : self.get_time(self.time_started),
"flags" : 1,
"driver" : "Generic NMEA",
"native" : 0,
"bps" : 57600,
"parity" : "N",
"stopbits" : 1,
"cycle" : 1.00 }
],
})
def get_cmd_WATCH(self, params):
return self.get_cmd_DEVICES() \
+ "%s\r\n" % json.dumps({
"class" : "WATCH",
"enable" : True,
"json" : True,
"nmea" : False,
"raw" : 0,
"scaled" : False,
"timing" : False,
})
def run(self):
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((self.host, self.port))
server_socket.listen(5)
logging.info("Listening on port %d" % self.port)
read_list = [server_socket]
write_list = []
while not self.quit:
readable, writable, errored = select.select(read_list, [], [], 1)
if len(readable) > 0:
for s in readable:
if s is server_socket:
client_socket, address = server_socket.accept()
read_list.append(client_socket)
logging.info("Connection from %s" % str(address))
client_socket.send(self.get_cmd_VERSION({}))
else:
data = s.recv(4096)
data
if data:
# remove line terminators
if data[-1] == '\n': data = data[0:-1]
if data[-1] == '\r': data = data[0:-1]
# parse line
command, i, params = data.partition("=")
if command[0] != "?":
logging.error("received trash [%s]" % data)
continue
command = command[1:]
if params is not None and params != "": # no params
if params[-1] == ';':
params = params[0:-1]
logging.info("%s = %s" % (command, json.dumps(params, indent = 4)))
params = json.loads(params)
else:
params = { }
logging.info("wo params")
if len(command) > 0 and command[-1] == ";":
command = command[0:-1]
# process command
if command == "VERSION":
s.send(self.get_cmd_VERSION(params))
elif command == "WATCH":
s.send(self.get_cmd_WATCH(params))
write_list.append(client_socket)
else:
logging.error("UNKNOWN COMMAND [%s]" % data)
s.send(
json.dumps({
"class": "ERROR",
"message": "Unrecognized request '%s'" % command}) + "\r\n")
else:
logging.info("Closed gpsd connection.")
s.close()
read_list.remove(s)
if s in write_list:
write_list.remove(s)
else:
if self.mc.curr_pos is not None:
for s in write_list:
s.send(self.get_cmd_GST() + self.get_cmd_TPV())