Skip to content

Commit

Permalink
Merge pull request #22 from hokus15/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
hokus15 committed Mar 24, 2024
2 parents 404efe7 + 4737829 commit f4b7b39
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 36 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

iotconnect/iotconnect.config.json
iotconnect/iotconnect.config.devel.json
iotconnect/iotconnect.config.live.json
iotconnect/logging.live.conf

*.log*

Expand Down
2 changes: 1 addition & 1 deletion iotconnect/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion iotconnect/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
76 changes: 43 additions & 33 deletions iotconnect/monitors/gps/gps_mon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
gps==3.19
gps3==0.33.3
obd==0.7.2
paho-mqtt==1.6.1
wrapt==1.16.0
Expand Down
2 changes: 2 additions & 0 deletions scripts/devel-mode-off.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
sudo cp /opt/IOTConnect/iotconnect/iotconnect.config.live.json /opt/IOTConnect/iotconnect/iotconnect.config.json
3 changes: 3 additions & 0 deletions scripts/devel-mode-on.sh
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions scripts/update-iotconnect.sh
Original file line number Diff line number Diff line change
@@ -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!'

0 comments on commit f4b7b39

Please sign in to comment.