I am working on a python class that represents an air quality station. Each air quality station has a GPS associated with it so that we can identify the latitudinal and longitudinal coordinates of the station. A GpsPoller is invoked on a separate thread and runs continuously until the thread is terminated. The GpsPoller should be used to get the most recent GPS data at any given time.
Here's what I have done so far:
class AirStation:
def __init__(self):
if not self._init_station():
raise InitException("Could not initialize the AirStation!")
def _init_station(self):
self._id = utils.get_mac('wlan0')
self._gpsp = utils.GpsPoller()
self._gpsp.start() # start polling the GPS sensor
self._lat = self._gpsp.get_gps_data()
self._lon = self._gpsp.get_gps_data()
return True
class GpsPoller(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.session = gps(mode=WATCH_ENABLE)
self.current_value = None
self.running = True
def get_gps_data(self):
return self.current_value
def run(self):
while self.running:
self.current_value = self.session.next()
I have two problems/questions about this code:
What is the best way of ensuring a latitudinal and longitudinal reading in the initialization function of the AirStation class? At initilization I am getting a latitudinal and longitudinal value of None (I suspect due to the lack of time that the GPS has had to get a satellite fix).
What is the standard way of ensuring that the GpsPoller gets terminated along with the AirStation instance? Whenever I test this at the command line, the
exit()
command hangs because the additional thread hangs.
I have taken a look at quite a few examples and documentation including the following:
- https://docs.python.org/2/library/threading.html#thread-objects
- http://www.danmandle.com/blog/getting-gpsd-to-work-with-python/
- https://stackoverflow.com/a/6146351/3342427
Any additional resources or direct answers would be much appreciated!