From 9962c27d217ef37cf1896652d4f5d7fbe2af269d Mon Sep 17 00:00:00 2001 From: Jordi Date: Sun, 24 Mar 2024 18:59:41 +0100 Subject: [PATCH 1/8] ci(gitignore): add specific files to gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) 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* From ce11229412cc61934232bd40abb1bff4521c2f72 Mon Sep 17 00:00:00 2001 From: Jordi Date: Sun, 24 Mar 2024 19:00:32 +0100 Subject: [PATCH 2/8] fix(main): use of warning instead of deprecated warn --- iotconnect/__main__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 0faa1701b7b5567e03ad52fbcc3f616f5ffa1c80 Mon Sep 17 00:00:00 2001 From: Jordi Date: Sun, 24 Mar 2024 19:01:58 +0100 Subject: [PATCH 3/8] fix(gps_monitor): replace gps by gps3 library to mantain python 3.9 or later compatibility --- iotconnect/monitors/gps/gps_mon.py | 77 +++++++++++++++++------------- requirements.txt | 2 +- 2 files changed, 45 insertions(+), 34 deletions(-) diff --git a/iotconnect/monitors/gps/gps_mon.py b/iotconnect/monitors/gps/gps_mon.py index 71caa5c..6b22279 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,64 @@ def __init__(self, config, callback): def monitor(self): """Monitor the gps.""" - fix = {} - + global fix # bring it in scope + 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 0e55ade..04026e2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -gps==3.19 +gps3==0.33.3 obd==0.7.1 paho-mqtt==1.6.1 wrapt==1.12.1 From e436a7af468a576eea2e22d5e9388322105fe33c Mon Sep 17 00:00:00 2001 From: Jordi Date: Sun, 24 Mar 2024 19:03:01 +0100 Subject: [PATCH 4/8] feat(scripts): add useful scripts to manage and update the service --- scripts/devel-mode-off.sh | 2 ++ scripts/devel-mode-on.sh | 3 +++ scripts/update-iotconnect.sh | 16 ++++++++++++++++ 3 files changed, 21 insertions(+) create mode 100644 scripts/devel-mode-off.sh create mode 100644 scripts/devel-mode-on.sh create mode 100644 scripts/update-iotconnect.sh diff --git a/scripts/devel-mode-off.sh b/scripts/devel-mode-off.sh new file mode 100644 index 0000000..5ded989 --- /dev/null +++ b/scripts/devel-mode-off.sh @@ -0,0 +1,2 @@ +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..9fd9c5b --- /dev/null +++ b/scripts/devel-mode-on.sh @@ -0,0 +1,3 @@ +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..a65d6fe --- /dev/null +++ b/scripts/update-iotconnect.sh @@ -0,0 +1,16 @@ +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 From 61783b26b2f687cf61608eb4910c4f4612a9e35b Mon Sep 17 00:00:00 2001 From: Jordi Date: Sun, 24 Mar 2024 19:17:15 +0100 Subject: [PATCH 5/8] fix(gps_mon): remove non assigned global variable --- iotconnect/monitors/gps/gps_mon.py | 1 - 1 file changed, 1 deletion(-) diff --git a/iotconnect/monitors/gps/gps_mon.py b/iotconnect/monitors/gps/gps_mon.py index 6b22279..c149e42 100644 --- a/iotconnect/monitors/gps/gps_mon.py +++ b/iotconnect/monitors/gps/gps_mon.py @@ -44,7 +44,6 @@ def __init__(self, config, callback): def monitor(self): """Monitor the gps.""" - global fix # bring it in scope monitor_result = {} # It may take some poll calls to get good data if (fix.mode == 1 or fix.mode == 'na'): From 6707b8e787371549117e8d792a79a3047b12760a Mon Sep 17 00:00:00 2001 From: Jordi Date: Sun, 24 Mar 2024 19:17:52 +0100 Subject: [PATCH 6/8] fix(scripts): add shebang to scripts --- scripts/devel-mode-off.sh | 2 +- scripts/devel-mode-on.sh | 2 +- scripts/update-iotconnect.sh | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/devel-mode-off.sh b/scripts/devel-mode-off.sh index 5ded989..092df85 100644 --- a/scripts/devel-mode-off.sh +++ b/scripts/devel-mode-off.sh @@ -1,2 +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 index 9fd9c5b..892e96d 100644 --- a/scripts/devel-mode-on.sh +++ b/scripts/devel-mode-on.sh @@ -1,3 +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 index a65d6fe..2af7d7a 100644 --- a/scripts/update-iotconnect.sh +++ b/scripts/update-iotconnect.sh @@ -1,3 +1,4 @@ +#!/bin/bash echo 'Stopping IOTConnect service...' sudo service iotconnect stop echo 'done!' From 0f28a9e103e370439863373880a83fb2aedd6770 Mon Sep 17 00:00:00 2001 From: Jordi Date: Sun, 24 Mar 2024 19:22:10 +0100 Subject: [PATCH 7/8] fix(scripts): double quote to prevent globbing and word splitting --- scripts/update-iotconnect.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/update-iotconnect.sh b/scripts/update-iotconnect.sh index 2af7d7a..a02f67e 100644 --- a/scripts/update-iotconnect.sh +++ b/scripts/update-iotconnect.sh @@ -8,7 +8,7 @@ echo "Updating IOTConnect to ${tag}..." git reset --hard git checkout master git branch --delete latest -git checkout $tag -b latest +git checkout "$tag" -b latest echo 'done!' cp iotconnect/logging.live.conf iotconnect/logging.conf sudo systemctl daemon-reload From 4737829fd7b9559c2ebc1a550d3297d9a5cb6f3e Mon Sep 17 00:00:00 2001 From: Jordi Date: Sun, 24 Mar 2024 19:25:53 +0100 Subject: [PATCH 8/8] fix(monitor): fix using variable 'now' before assignment --- iotconnect/monitor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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():