diff --git a/.gitignore b/.gitignore index ccb9163..d77a158 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,8 @@ iotconnect/iotconnect.config.json +iotconnect/iotconnect.config.devel.json +iotconnect/iotconnect.config.live.json +iotconnect/logging.live.conf *.log* diff --git a/iotconnect/__main__.py b/iotconnect/__main__.py index c96a542..ca8ee9e 100644 --- a/iotconnect/__main__.py +++ b/iotconnect/__main__.py @@ -106,7 +106,7 @@ def main(): # Wait some time before cheking again the monitors time.sleep(15) except (KeyboardInterrupt, SystemExit): # when you press ctrl+c - logger.warn('IOTConnect execution cancelled by user.') + logger.warning('IOTConnect execution cancelled by user.') main_running = False except Exception as ex: main_running = False diff --git a/iotconnect/monitor.py b/iotconnect/monitor.py index 4fad957..04c2745 100644 --- a/iotconnect/monitor.py +++ b/iotconnect/monitor.py @@ -25,8 +25,8 @@ def monitor(self): def run(self): while self._running: + now = time.time() try: - now = time.time() monitor_result = self.monitor() for publisher in self._publishers: if not publisher.is_initialized(): diff --git a/iotconnect/monitors/gps/gps_mon.py b/iotconnect/monitors/gps/gps_mon.py index 71caa5c..c149e42 100644 --- a/iotconnect/monitors/gps/gps_mon.py +++ b/iotconnect/monitors/gps/gps_mon.py @@ -3,10 +3,10 @@ import threading import logging import json -import gps +from gps3 import agps3 from iotconnect.monitor import Monitor -gpsd = None # setting the global variable +fix = None # setting the global variable class GpsdThread(threading.Thread): @@ -15,15 +15,17 @@ class GpsdThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.setDaemon(True) - global gpsd # bring it in scope - gpsd = gps.gps(mode=gps.WATCH_ENABLE) # starting the stream of info def run(self): - global gpsd - while True: - # this will continue to loop and grab EACH set of - # gpsd info to clear the buffer - next(gpsd) + global fix # bring it in scope + gps_socket = agps3.GPSDSocket() + data_stream = agps3.DataStream() + gps_socket.connect() + gps_socket.watch() + for new_data in gps_socket: + if new_data: + data_stream.unpack(new_data) + fix = data_stream class GpsMonitor(Monitor): @@ -42,55 +44,63 @@ def __init__(self, config, callback): def monitor(self): """Monitor the gps.""" - fix = {} - + monitor_result = {} # It may take some poll calls to get good data - if (gpsd.fix.mode == 1): + if (fix.mode == 1 or fix.mode == 'na'): self._handle_no_fix('Position not fixed') - fix_accuracy = max(gpsd.fix.epy, - gpsd.fix.epx) + latitude_error = 100000 + if fix.epx != 'na': + latitude_error = fix.epx + + longitude_error = 1000 + if fix.epy != 'na': + longitude_error = fix.epy + + fix_accuracy = max(latitude_error, + longitude_error) + self._log.info('Position fixed. Accuracy: +/- %s m', fix_accuracy) self._log.debug('Latitude error (EPY): +/- %s m', - gpsd.fix.epy) + fix.epy) self._log.debug('Longitude error (EPX): +/- %s m', - gpsd.fix.epx) + fix.epx) if fix_accuracy < self._min_accuracy: self._retries = 0 - fix.update({ + monitor_result.update({ 'last_update': int(round(time.time())), - 'latitude': gpsd.fix.latitude, - 'longitude': gpsd.fix.longitude, + 'latitude': fix.lat, + 'longitude': fix.lon, 'gps_accuracy': fix_accuracy, # Estimated Speed error - 'eps': gpsd.fix.eps, + 'eps': fix.eps, # Estimated longitude error - 'epx': gpsd.fix.epx, + 'epx': fix.epx, # Estimated latitude error - 'epy': gpsd.fix.epy, + 'epy': fix.epy, # Estimated altitude error - 'epv': gpsd.fix.epv, + 'epv': fix.epv, # Estimated time error - 'ept': gpsd.fix.ept, - 'speed': gpsd.fix.speed, # m/s - 'climb': gpsd.fix.climb, - 'track': gpsd.fix.track, - 'mode': gpsd.fix.mode + 'ept': fix.ept, + 'speed': fix.speed, # m/s + 'climb': fix.climb, + 'track': fix.track, + 'mode': fix.mode }) if self._previous_latitude != 0 and self._previous_longitude != 0: # Previous latitude and longitude data is useful to # measure distance travelled between updates. - fix.update({ + monitor_result.update({ # Latitude got from previous read 'platitude': self._previous_latitude, # Longitude got from previous read 'plongitude': self._previous_longitude }) - self._previous_latitude = gpsd.fix.latitude - self._previous_longitude = gpsd.fix.longitude - self._log.info("type: gps fix, data: %s", json.dumps(fix)) - return {'location': fix} + self._previous_latitude = fix.lat + self._previous_longitude = fix.lon + self._log.info("type: gps fix, data: %s", json.dumps(monitor_result)) + return {'location': monitor_result} else: self._handle_no_fix('Low accuracy: it\'s +/- {} m but +/- {} m required' .format(fix_accuracy, self._min_accuracy)) diff --git a/requirements.txt b/requirements.txt index 5f13881..969ff40 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -gps==3.19 +gps3==0.33.3 obd==0.7.2 paho-mqtt==1.6.1 wrapt==1.16.0 diff --git a/scripts/devel-mode-off.sh b/scripts/devel-mode-off.sh new file mode 100644 index 0000000..092df85 --- /dev/null +++ b/scripts/devel-mode-off.sh @@ -0,0 +1,2 @@ +#!/bin/bash +sudo cp /opt/IOTConnect/iotconnect/iotconnect.config.live.json /opt/IOTConnect/iotconnect/iotconnect.config.json diff --git a/scripts/devel-mode-on.sh b/scripts/devel-mode-on.sh new file mode 100644 index 0000000..892e96d --- /dev/null +++ b/scripts/devel-mode-on.sh @@ -0,0 +1,3 @@ +#!/bin/bash +sudo service iotconnect stop +sudo cp /opt/IOTConnect/iotconnect/iotconnect.config.devel.json /opt/IOTConnect/iotconnect/iotconnect.config.json diff --git a/scripts/update-iotconnect.sh b/scripts/update-iotconnect.sh new file mode 100644 index 0000000..a02f67e --- /dev/null +++ b/scripts/update-iotconnect.sh @@ -0,0 +1,17 @@ +#!/bin/bash +echo 'Stopping IOTConnect service...' +sudo service iotconnect stop +echo 'done!' +cd /opt/IOTConnect +tag=$(git describe --tags `git rev-list --tags --max-count=1`) +echo "Updating IOTConnect to ${tag}..." +git reset --hard +git checkout master +git branch --delete latest +git checkout "$tag" -b latest +echo 'done!' +cp iotconnect/logging.live.conf iotconnect/logging.conf +sudo systemctl daemon-reload +echo 'Starting IOTConnect service...' +sudo service iotconnect start +echo 'done!' \ No newline at end of file