-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathModem_GPS_Client.py
More file actions
270 lines (226 loc) · 8.16 KB
/
Modem_GPS_Client.py
File metadata and controls
270 lines (226 loc) · 8.16 KB
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
#-------------------------------------------------------------------------------
# Name: GPS_Clent
# Purpose: Encpasulate rRPC interface to GPS service
#
# Author: Laurent Carré
#
# Created: 15/03/2020
# Copyright: (c) Laurent Carré Sterwen Technologies 2020
# Licence: <your licence>
#-------------------------------------------------------------------------------
import os
import sys
import inspect
cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0], "../modem_gps")))
sys.path.insert(0, cmd_subfolder)
import threading
import logging
import time
import json
import grpc
from GPS_Service_pb2 import *
import GPS_Service_pb2_grpc
class Modem_GPS_GRPC_Client() :
s_attr=('fix','gps_time','nbsat','date','hdop')
v_attr=('fix','gps_time','latitude','longitude','altitude','SOG','COG')
mf_attr=('model','IMEI','IMSI','ICCID','network_reg','PLMNID','network_name','rat','band','lac','ci','rssi')
ms_attr=('network_reg','PLMNID','network_name','rat','band','lac','ci','rssi')
mo_attr=('model','IMEI','IMSI','ICCID','network_reg','PLMNID','network_name','rat','band','lac','ci','rssi','operators')
def __init__(self,addr,logger) :
self._logger=logger
self._logger.debug("Modem GPS creating gRPC channel on:"+addr)
self._channel= grpc.insecure_channel(addr)
self._stub= GPS_Service_pb2_grpc.GPS_ServiceStub(self._channel)
self._gps_running=False
self._modem_running=False
self._streamer=None
def modem_status(self):
req=ModemCmd(command='status')
try:
resp=self._stub.modemCommand(req)
except grpc.RpcError as err:
self._logger.error(str(err))
return None
self._logger.debug("Read modem status result "+resp.response)
if resp.response == 'OK':
out=Modem_GPS_GRPC_Client.GpsMsg_ToDict(resp.status,Modem_GPS_GRPC_Client.mf_attr)
return out
else:
return None
def modem_operators(self):
req=ModemCmd(command='operator')
try:
resp=self._stub.modemCommand(req)
except grpc.RpcError as err:
self._logger.error(str(err))
return None
self._logger.debug("Read modem status result "+resp.response)
if resp.response == 'OK':
out=Modem_GPS_GRPC_Client.GpsMsg_ToDict(resp.status,Modem_GPS_GRPC_Client.mo_attr)
return out
else:
return None
def gps_status(self):
req=PositionSpec(spec=PositionSpec.P2D)
try:
resp=self._stub.getPrecision(req)
except grpc.RpcError as err:
self._logger.error(str(err))
return None
out=Modem_GPS_GRPC_Client.GpsMsg_ToDict(resp,Modem_GPS_GRPC_Client.s_attr)
if resp.nbsat > 0:
sat_num=[]
for n in resp.sat_num :
sat_num.append(n)
out['sat_num']=sat_num
return out
def gpsVector(self):
req=PositionSpec(spec=PositionSpec.P2D)
try:
resp=self._stub.getVector(req)
except grpc.RpcError as err:
self._logger.error(str(err))
return None
if resp.fix:
out= Modem_GPS_GRPC_Client.GpsMsg_ToDict(resp,Modem_GPS_GRPC_Client.v_attr)
else:
out={"fix":False}
return out
def startGPSPeriodicRead(self,period,callback):
self._gps_period=period
if not self._gps_running :
self._gps_running=True
self._gps_callback=callback
self.readGpsAndCallback()
def startModemPeriodicRead(self,period,callback):
self._modem_period=period
if not self._modem_running :
self._modem_running = True
self._modem_callback=callback
self.armModemTimer()
def stopGPSPeriodicRead(self):
self._gps_running=False
def stopGPS(self):
if self._streamer != None :
self.stopGPSStreaming()
else:
self.stopGPSPeriodicRead()
def stopModemPeriodicRead(self):
self._modem_running = False
def armGPSTimer(self):
self._gps_timer=threading.Timer(self._gps_period,self.readGpsAndCallback)
self._gps_timer.start()
def armModemTimer(self):
self._modem_timer=threading.Timer(self._modem_period,self.readModemAndCallback)
self._modem_timer.start()
def readModemAndCallback(self):
req=ModemCmd(command='status')
try:
resp=self._stub.modemCommand(req)
except grpc.RpcError as err:
self._logger.error(str(err))
# error here we shall not stop the periodic reading
# self._modem_running=False
# return
else:
if resp.response == 'OK' :
out=Modem_GPS_GRPC_Client.GpsMsg_ToDict(resp.status,Modem_GPS_GRPC_Client.ms_attr)
self._modem_callback(out)
if self._modem_running :
self.armModemTimer()
def readGpsAndCallback(self):
resp=self.gpsVector()
if resp==None :
self._gps_running = False
return
self._logger.debug("GPS Periodic read result "+str(resp) )
self._gps_callback(resp)
if self._gps_running :
self.armGPSTimer()
def startGPSStreaming(self,callback,end_callback,rules):
rules_j=json.dumps(rules)
self._streamer=ContinuousGPSReader(self,callback,end_callback,self._logger,rules_j)
self._streamer.start()
def stopGPSStreaming(self):
req=ModemCmd()
req.command="STOP"
resp=self._stub.stopStream(req)
self._logger.debug("Stop streaming acknowledgement:"+resp.response)
@staticmethod
def GpsMsg_ToDict(msg,attribs):
out={}
for attr in attribs:
try:
val=getattr(msg,attr)
out[attr]=val
except AttributeError :
print("Modem GPS service missing field:"+attr)
out[attr]=None
return out
class ContinuousGPSReader(threading.Thread):
'''
this class is used to read a continuous stream of gps event
and throwing a call for each of them
'''
def __init__(self,client,callback,end_callback,logger,rules):
threading.Thread.__init__(self)
self._client=client
self._callback=callback
self._end_callback=end_callback
self._logger=logger
self._rules=rules
def run(self):
req=ModemCmd()
req.command=self._rules
error=None
try:
for pos in self._client._stub.streamGPS(req) :
if pos.fix:
resp=Modem_GPS_GRPC_Client.GpsMsg_ToDict(pos,Modem_GPS_GRPC_Client.v_attr)
else:
resp={}
resp['fix']=False
self._callback(resp)
except Exception as err:
self._logger.error("Error in GPS streaming:"+str(err))
error=err
pass
self._client._streamer=None
self._logger.info("End GPS streaming")
self._end_callback(error)
# self._logger.info("After end callback")
class T():
def printGPS(self,resp) :
if resp.fix :
print("LAT:",resp.latitude,"LONG:",resp.longitude,"SOG:",resp.SOG,"COG:",resp.COG)
else:
print("No fix")
def pos_callback(pos):
print("Callback:",pos)
def main():
logger= logging.getLogger('Test-GPS')
logging.basicConfig()
logger.setLevel(logging.DEBUG)
gps=Modem_GPS_GRPC_Client("127.0.0.1:20231",logger)
resp=gps.modem_status()
if resp == None : return
v= gps.gps_status()
print (v )
'''
if v.fix == True:
print("LAT:",v.latitude,"LONG:",v.longitude,"SOG:",v.SOG,"COG:",v.COG)
else:
print("GPS not fixed")
# test periodic reading
t=T()
gps.startPeriodicRead(10.,t.printGPS)
time.sleep(30.)
gps.stopPeriodicRead()
time.sleep(15.)
'''
rules={"fixtime":10,"no_fix_time":60,"distance":100}
gps.startGPSStreaming(pos_callback,rules)
time.sleep(60.)
gps.stopGPSStreaming()
if __name__ == '__main__':
main()