Index: src/gnuboat/device/daemon/gpsdpoller.py
===================================================================
--- src/gnuboat/device/daemon/gpsdpoller.py	(revision b5d1ed097327e5df5e9093ab8199b68e6c5de898)
+++ src/gnuboat/device/daemon/gpsdpoller.py	(revision 68a7bfd735692c223cfe9f31ee9eee40364acbb1)
@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-
 '''
-    Copyright (C) 2016 - 2017 EngSaS - Langenbach. All rights reserved.
+    Copyright (C) 2016 - 2020 EngSaS - Langenbach. All rights reserved.
 
     This program is free software: you can redistribute it and/or modify
@@ -18,97 +18,143 @@
 
 import threading
-import time
 import dateutil.parser
 import datetime
-import gps
-import traceback
-
-# Copied from https://gist.github.com/wolfg1969/4653340
+import socket
+import time
+import json
 
 
 class GpsdPoller(threading.Thread):
-    def __init__(self, logperiod):
-        threading.Thread.__init__(self)
-        self._session = None
-        self._session = None
-        self._currentValue = None
+    def __init__(self, host='127.0.0.1', port=2947, ready_event=None):
+        super().__init__()
+        self._host = host
+        self._port = port
+        self._ready_event = ready_event
+
+        self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        self._socket.settimeout(5)
+        self._stream = None
+        self._current_value = None
         self._running = True
-        self._logperiod = logperiod
 
     def latitude(self):
-        if self._currentValue is None or self.gpsFix(
-        ) < 2 or 'lat' not in self._currentValue:
+        if self._current_value is None or self.gps_fix(
+        ) < 2 or 'lat' not in self._current_value:
             return 0
-        return self._currentValue.lat
+        return self._current_value['lat']
 
-    def latitudeDirection(self):
+    def latitude_direction(self):
         return 'N'
 
     def longitude(self):
-        if self._currentValue is None or self.gpsFix(
-        ) < 2 or 'lon' not in self._currentValue:
+        if self._current_value is None or self.gps_fix(
+        ) < 2 or 'lon' not in self._current_value:
             return 0
-        return self._currentValue.lon
+        return self._current_value['lon']
 
-    def longitudeDirection(self):
+    def longitude_direction(self):
         return 'E'
 
-    def speedOverGround(self):
-        if self._currentValue is None or self.gpsFix(
-        ) < 2 or 'speed' not in self._currentValue:
+    def speed_over_ground(self):
+        if self._current_value is None or self.gps_fix(
+        ) < 2 or 'speed' not in self._current_value:
             return 0
-        return self._currentValue.speed
+        return self._current_value['speed']
 
-    def courseOverGround(self):
-        if self._currentValue is None or self.gpsFix(
-        ) < 2 or 'track' not in self._currentValue:
+    def course_over_ground(self):
+        if self._current_value is None or self.gps_fix(
+        ) < 2 or 'track' not in self._current_value:
             return 0
-        return self._currentValue.track
+        return self._current_value['track']
 
-    def gpsFix(self):
-        if self._currentValue is None or 'mode' not in self._currentValue:
+    def error_horizontal_position(self):
+        """
+            Estimated horizontal Position (2D) Error in meters.
+            Also known as Estimated Position Error (epe)
+        """
+        if self.gps_fix() < 2 or 'eph' not in self._current_value:
+            return None
+        return self._current_value['eph']
+
+    def error_vertical_position(self):
+        """
+            Estimated vertical error in meters.
+        """
+        if self.gps_fix() < 2 or 'epv' not in self._current_value:
+            return None
+        return self._current_value['epv']
+
+    def error_vertical_position(self):
+        """
+            Estimated Spherical (3D) Position Error in meters
+        """
+        if self.gps_fix() < 2 or 'sep' not in self._current_value:
+            return None
+        return self._current_value['sep']
+
+    def gps_fix(self):
+        if self._current_value is None or 'mode' not in self._current_value:
             return 0
-        return self._currentValue.mode
+        return self._current_value['mode']
 
     def status(self):
-        if self._currentValue is None or self.gpsFix() < 2:
+        if self._current_value is None or self.gps_fix() < 2:
             return 'V'
         return 'A'
 
-    def timeUtc(self):
-        if self._currentValue is None or self.gpsFix(
-        ) < 2 or 'time' not in self._currentValue:
+    def time_utc(self):
+        if self._current_value is None or self.gps_fix(
+        ) < 2 or 'time' not in self._current_value:
             return None
-        currentTime = self._currentValue.time
+        currentTime = self._current_value['time']
         if isinstance(currentTime, float):
-            return datetime.datetime.fromtimestamp(currentTime)
+            return datetime.datetime.fromtimestamp(currentTime, timezone.utc)
         return dateutil.parser.parse(currentTime)
+
+    def close_stream(self):
+        if self._stream is not None:
+            self._stream.close()
+            self._stream = None
+        self._socket.close()
 
     def stop(self):
         self._running = False
+        self.close_stream()
 
     def run(self):
-        try:
-            while self._running:
-                try:
-                    if self._session is None:
-                        self._session = gps.gps(mode=gps.WATCH_ENABLE)
-                    current_value = self._session.next()
-                    # only update current value, if we got an epX entry
-                    # dop entries are ignored right now
-                    if 'mode' in current_value:
-                        self._currentValue = current_value
-                except StopIteration:
-                    break
-                except:
-                    print('Failed to read gpsd data')
-                    print(traceback.format_exc())
-                    self._currentValue = None
-                    self._session = None
-        except StopIteration:
-            pass
-
-
-if __name__ == "__main__":
-    import sys
-    gpsd = GpsdPoller(int(sys.argv[1]))
+        while self._running:
+            try:
+                time.sleep(0.25)
+                # connection to the gpsd daemon
+                if self._stream is None:
+                    self._socket.connect((self._host, self._port))
+                    self._stream = self._socket.makefile(mode='rw')
+                    self._stream.write(
+                        '?WATCH={"enable": true, "json": true}\n')
+                    self._stream.flush()
+                    print('Connected to {}:{}'.format(
+                        self._host, self._port
+                    ))
+                    connectionrefused_error_printed = False
+                    if self._ready_event:
+                        self._ready_event.set()
+                # read values
+                self._stream.write('?POLL;\n')
+                self._stream.flush()
+                data = json.loads(self._stream.readline())
+                if 'active' not in data or not data['active'] or \
+                        'tpv' not in data or len(data['tpv']) < 1:
+                    self._current_value = None
+                else:
+                    self._current_value = data['tpv'][-1]
+            except json.JSONDecodeError as e:
+                pass
+            except OSError as e:
+                self._stream = None
+                pass
+            except BrokenPipeError as e:
+                self._stream = None
+                pass
+            except KeyboardInterrupt as e:
+                self.close_stream()
+                break
