Initial commit
This commit is contained in:
		
							
								
								
									
										0
									
								
								gpx_tracker/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								gpx_tracker/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										113
									
								
								gpx_tracker/__main__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										113
									
								
								gpx_tracker/__main__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,113 @@
 | 
			
		||||
#!/usr/bin/env python
 | 
			
		||||
import argparse
 | 
			
		||||
import datetime
 | 
			
		||||
import gps
 | 
			
		||||
import gpxpy
 | 
			
		||||
import gpxpy.gpx
 | 
			
		||||
import logging
 | 
			
		||||
import os
 | 
			
		||||
import sys
 | 
			
		||||
import time
 | 
			
		||||
import threading
 | 
			
		||||
import RPi.GPIO as GPIO
 | 
			
		||||
 | 
			
		||||
log_level = logging.INFO
 | 
			
		||||
LOG_FILE = "/var/log/gpxtracker.log"
 | 
			
		||||
LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s"
 | 
			
		||||
 | 
			
		||||
HOST_FOLDER = "/home/tkl/Nextcoud/karre"
 | 
			
		||||
REMOTE_FOLDER = "/home/tkl/karre"
 | 
			
		||||
REMOTE_USER = "tkl"
 | 
			
		||||
REMOTE_HOST = "nuc"
 | 
			
		||||
 | 
			
		||||
logging.basicConfig(format=LOG_FORMAT, level=log_level, filename=LOG_FILE)
 | 
			
		||||
# logging.basicConfig(format=LOG_FORMAT, level=log_level)
 | 
			
		||||
log = logging.getLogger('gpxtracker')
 | 
			
		||||
 | 
			
		||||
class IgnitionMonitor(threading.Thread):
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
        super(IgnitionMonitor, self).__init__()
 | 
			
		||||
        self.run_condition = True
 | 
			
		||||
        GPIO.setmode(GPIO.BCM)
 | 
			
		||||
        GPIO.setup(9, GPIO.IN)
 | 
			
		||||
 | 
			
		||||
    def run(self):
 | 
			
		||||
        log.info("Start ignition polling.")
 | 
			
		||||
        while self.run_condition is True:
 | 
			
		||||
            if GPIO.input(9) == 1:
 | 
			
		||||
                self.run_condition = False
 | 
			
		||||
            else:
 | 
			
		||||
                time.sleep(0.2)
 | 
			
		||||
        log.info("Ignition off detected.")
 | 
			
		||||
        GPIO.cleanup()
 | 
			
		||||
 | 
			
		||||
    def stop(self):
 | 
			
		||||
        self.run_condition = False
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class GpsTracker(threading.Thread):
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
        super(GpsTracker, self).__init__()
 | 
			
		||||
        self.run_condition = True
 | 
			
		||||
 | 
			
		||||
    def run(self):
 | 
			
		||||
        gpsd = gps.gps(mode=gps.WATCH_ENABLE)
 | 
			
		||||
        gpx = gpxpy.gpx.GPX()
 | 
			
		||||
 | 
			
		||||
        gpx_track = gpxpy.gpx.GPXTrack()
 | 
			
		||||
        gpx.tracks.append(gpx_track)
 | 
			
		||||
 | 
			
		||||
        gpx_segment = gpxpy.gpx.GPXTrackSegment()
 | 
			
		||||
        gpx_track.segments.append(gpx_segment)
 | 
			
		||||
 | 
			
		||||
        track_name = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + ".gpx"
 | 
			
		||||
 | 
			
		||||
        last_altitude = 0
 | 
			
		||||
 | 
			
		||||
        while self.run_condition is True:
 | 
			
		||||
            gpsd.next()
 | 
			
		||||
            if gpsd.fix.mode > 1:
 | 
			
		||||
                now = datetime.datetime.strptime(gpsd.utc, "%Y-%m-%dT%H:%M:%S.%fZ")
 | 
			
		||||
                if gpsd.fix.mode > 2:
 | 
			
		||||
                    gpx_segment.points.append(gpxpy.gpx.GPXTrackPoint(gpsd.fix.latitude,
 | 
			
		||||
                                                                      gpsd.fix.longitude,
 | 
			
		||||
                                                                      gpsd.fix.altitude,
 | 
			
		||||
                                                                      now))
 | 
			
		||||
                else:
 | 
			
		||||
                    gpx_segment.points.append(gpxpy.gpx.GPXTrackPoint(gpsd.fix.latitude,
 | 
			
		||||
                                                                      gpsd.fix.longitude,
 | 
			
		||||
                                                                      time=now))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        with open(os.path.join(HOST_FOLDER, track_name), "w") as f:
 | 
			
		||||
            f.write(gpx.to_xml())
 | 
			
		||||
 | 
			
		||||
    def stop(self):
 | 
			
		||||
        self.run_condition = False
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def main():
 | 
			
		||||
    ignition_monitor = IgnitionMonitor()
 | 
			
		||||
    gps_tracker = GpsTracker()
 | 
			
		||||
 | 
			
		||||
    ignition_monitor.start()
 | 
			
		||||
    gps_tracker.start()
 | 
			
		||||
 | 
			
		||||
    try:
 | 
			
		||||
        ignition_monitor.join()
 | 
			
		||||
    except KeyboardInterrupt:
 | 
			
		||||
        ignition_monitor.stop()
 | 
			
		||||
        ignition_monitor.join()
 | 
			
		||||
 | 
			
		||||
    gps_tracker.stop()
 | 
			
		||||
    gps_tracker.join()
 | 
			
		||||
 | 
			
		||||
    remote = REMOTE_USER + "@" + REMOTE_HOST + ":" + REMOTE_FOLDER
 | 
			
		||||
    subprocess.call(["rsync", "-avz", HOST_FOLDER, remote])
 | 
			
		||||
 | 
			
		||||
    os.system("shutdown /s /t 1")
 | 
			
		||||
 | 
			
		||||
    return 0
 | 
			
		||||
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    sys.exit(main())
 | 
			
		||||
							
								
								
									
										3
									
								
								gpxtracker
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								gpxtracker
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,3 @@
 | 
			
		||||
#!/bin/bash
 | 
			
		||||
 | 
			
		||||
python3 -m gpx_tracker $@
 | 
			
		||||
							
								
								
									
										10
									
								
								gpxtracker.service
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								gpxtracker.service
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
			
		||||
[Unit]
 | 
			
		||||
Description=Gpx tracker
 | 
			
		||||
After=multi-user.target
 | 
			
		||||
 | 
			
		||||
[Service]
 | 
			
		||||
Type=idle
 | 
			
		||||
ExecStart=/usr/local/bin/gpxtracker
 | 
			
		||||
 | 
			
		||||
[Install]
 | 
			
		||||
WantedBy=multi-user.target
 | 
			
		||||
							
								
								
									
										27
									
								
								setup.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								setup.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,27 @@
 | 
			
		||||
#!/usr/bin/env python
 | 
			
		||||
 | 
			
		||||
import sys
 | 
			
		||||
import os
 | 
			
		||||
import shutil
 | 
			
		||||
import stat
 | 
			
		||||
from setuptools import setup
 | 
			
		||||
 | 
			
		||||
NAME = 'gpxtracker'
 | 
			
		||||
VERSION = '0.1.0'
 | 
			
		||||
AUTHOR = 'Thomas Klaehn'
 | 
			
		||||
EMAIL = 'tkl@blackfinn.de'
 | 
			
		||||
PACKAGES = ['gpx_tracker']
 | 
			
		||||
SCRIPTS = ['gpxtracker']
 | 
			
		||||
REQUIRES = ['gps', 'gpxpy', 'rpi.gpio']
 | 
			
		||||
 | 
			
		||||
SERVICEDIR = "/lib/systemd/system"
 | 
			
		||||
 | 
			
		||||
DAEMON_START_SCRIPT = os.path.join(SERVICEDIR, "gpxtracker.service")
 | 
			
		||||
 | 
			
		||||
if sys.argv[1] == 'install':
 | 
			
		||||
    os.makedirs(SERVICEDIR, exist_ok=True)
 | 
			
		||||
    shutil.copyfile("gpxtracker.service", DAEMON_START_SCRIPT)
 | 
			
		||||
    os.chmod(DAEMON_START_SCRIPT, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
 | 
			
		||||
 | 
			
		||||
setup(name=NAME, version=VERSION, author=AUTHOR, author_email=EMAIL,
 | 
			
		||||
      packages=PACKAGES, scripts=SCRIPTS, install_requires=REQUIRES)
 | 
			
		||||
		Reference in New Issue
	
	Block a user