Changeset 68a7bfd
- Timestamp:
- May 11, 2020, 9:03:03 PM (7 years ago)
- Branches:
- debian/bullseye, master
- Children:
- 55a5895
- Parents:
- b5d1ed0
- File:
-
- 1 edited
-
src/gnuboat/device/daemon/gpsdpoller.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/gnuboat/device/daemon/gpsdpoller.py
rb5d1ed0 r68a7bfd 1 1 # -*- coding: utf-8 -*- 2 2 ''' 3 Copyright (C) 2016 - 20 17EngSaS - Langenbach. All rights reserved.3 Copyright (C) 2016 - 2020 EngSaS - Langenbach. All rights reserved. 4 4 5 5 This program is free software: you can redistribute it and/or modify … … 18 18 19 19 import threading 20 import time21 20 import dateutil.parser 22 21 import datetime 23 import gps 24 import traceback 25 26 # Copied from https://gist.github.com/wolfg1969/4653340 22 import socket 23 import time 24 import json 27 25 28 26 29 27 class GpsdPoller(threading.Thread): 30 def __init__(self, logperiod): 31 threading.Thread.__init__(self) 32 self._session = None 33 self._session = None 34 self._currentValue = None 28 def __init__(self, host='127.0.0.1', port=2947, ready_event=None): 29 super().__init__() 30 self._host = host 31 self._port = port 32 self._ready_event = ready_event 33 34 self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 35 self._socket.settimeout(5) 36 self._stream = None 37 self._current_value = None 35 38 self._running = True 36 self._logperiod = logperiod37 39 38 40 def latitude(self): 39 if self._current Value is None or self.gpsFix(40 ) < 2 or 'lat' not in self._current Value:41 if self._current_value is None or self.gps_fix( 42 ) < 2 or 'lat' not in self._current_value: 41 43 return 0 42 return self._current Value.lat44 return self._current_value['lat'] 43 45 44 def latitude Direction(self):46 def latitude_direction(self): 45 47 return 'N' 46 48 47 49 def longitude(self): 48 if self._current Value is None or self.gpsFix(49 ) < 2 or 'lon' not in self._current Value:50 if self._current_value is None or self.gps_fix( 51 ) < 2 or 'lon' not in self._current_value: 50 52 return 0 51 return self._current Value.lon53 return self._current_value['lon'] 52 54 53 def longitude Direction(self):55 def longitude_direction(self): 54 56 return 'E' 55 57 56 def speed OverGround(self):57 if self._current Value is None or self.gpsFix(58 ) < 2 or 'speed' not in self._current Value:58 def speed_over_ground(self): 59 if self._current_value is None or self.gps_fix( 60 ) < 2 or 'speed' not in self._current_value: 59 61 return 0 60 return self._current Value.speed62 return self._current_value['speed'] 61 63 62 def course OverGround(self):63 if self._current Value is None or self.gpsFix(64 ) < 2 or 'track' not in self._current Value:64 def course_over_ground(self): 65 if self._current_value is None or self.gps_fix( 66 ) < 2 or 'track' not in self._current_value: 65 67 return 0 66 return self._current Value.track68 return self._current_value['track'] 67 69 68 def gpsFix(self): 69 if self._currentValue is None or 'mode' not in self._currentValue: 70 def error_horizontal_position(self): 71 """ 72 Estimated horizontal Position (2D) Error in meters. 73 Also known as Estimated Position Error (epe) 74 """ 75 if self.gps_fix() < 2 or 'eph' not in self._current_value: 76 return None 77 return self._current_value['eph'] 78 79 def error_vertical_position(self): 80 """ 81 Estimated vertical error in meters. 82 """ 83 if self.gps_fix() < 2 or 'epv' not in self._current_value: 84 return None 85 return self._current_value['epv'] 86 87 def error_vertical_position(self): 88 """ 89 Estimated Spherical (3D) Position Error in meters 90 """ 91 if self.gps_fix() < 2 or 'sep' not in self._current_value: 92 return None 93 return self._current_value['sep'] 94 95 def gps_fix(self): 96 if self._current_value is None or 'mode' not in self._current_value: 70 97 return 0 71 return self._current Value.mode98 return self._current_value['mode'] 72 99 73 100 def status(self): 74 if self._current Value is None or self.gpsFix() < 2:101 if self._current_value is None or self.gps_fix() < 2: 75 102 return 'V' 76 103 return 'A' 77 104 78 def time Utc(self):79 if self._current Value is None or self.gpsFix(80 ) < 2 or 'time' not in self._current Value:105 def time_utc(self): 106 if self._current_value is None or self.gps_fix( 107 ) < 2 or 'time' not in self._current_value: 81 108 return None 82 currentTime = self._current Value.time109 currentTime = self._current_value['time'] 83 110 if isinstance(currentTime, float): 84 return datetime.datetime.fromtimestamp(currentTime )111 return datetime.datetime.fromtimestamp(currentTime, timezone.utc) 85 112 return dateutil.parser.parse(currentTime) 113 114 def close_stream(self): 115 if self._stream is not None: 116 self._stream.close() 117 self._stream = None 118 self._socket.close() 86 119 87 120 def stop(self): 88 121 self._running = False 122 self.close_stream() 89 123 90 124 def run(self): 91 try: 92 while self._running: 93 try: 94 if self._session is None: 95 self._session = gps.gps(mode=gps.WATCH_ENABLE) 96 current_value = self._session.next() 97 # only update current value, if we got an epX entry 98 # dop entries are ignored right now 99 if 'mode' in current_value: 100 self._currentValue = current_value 101 except StopIteration: 102 break 103 except: 104 print('Failed to read gpsd data') 105 print(traceback.format_exc()) 106 self._currentValue = None 107 self._session = None 108 except StopIteration: 109 pass 110 111 112 if __name__ == "__main__": 113 import sys 114 gpsd = GpsdPoller(int(sys.argv[1])) 125 while self._running: 126 try: 127 time.sleep(0.25) 128 # connection to the gpsd daemon 129 if self._stream is None: 130 self._socket.connect((self._host, self._port)) 131 self._stream = self._socket.makefile(mode='rw') 132 self._stream.write( 133 '?WATCH={"enable": true, "json": true}\n') 134 self._stream.flush() 135 print('Connected to {}:{}'.format( 136 self._host, self._port 137 )) 138 connectionrefused_error_printed = False 139 if self._ready_event: 140 self._ready_event.set() 141 # read values 142 self._stream.write('?POLL;\n') 143 self._stream.flush() 144 data = json.loads(self._stream.readline()) 145 if 'active' not in data or not data['active'] or \ 146 'tpv' not in data or len(data['tpv']) < 1: 147 self._current_value = None 148 else: 149 self._current_value = data['tpv'][-1] 150 except json.JSONDecodeError as e: 151 pass 152 except OSError as e: 153 self._stream = None 154 pass 155 except BrokenPipeError as e: 156 self._stream = None 157 pass 158 except KeyboardInterrupt as e: 159 self.close_stream() 160 break
Note:
See TracChangeset
for help on using the changeset viewer.
