NAzT
12/20/2017 - 7:10 AM

gps.py

gps.py

#!/usr/bin/env python3.5
# coding=utf-8

import time
from time import sleep
from datetime import datetime
import json
import paho.mqtt.publish as publish

from gps3.agps3threaded import AGPS3mechanism

agps_thread = AGPS3mechanism()  # Instantiate AGPS3 Mechanisms
agps_thread.stream_data()  # From localhost (), or other hosts, by example, (host='gps.ddns.net')
agps_thread.run_thread(usnap=.2)  # Throttle time to sleep after an empty lookup, default 0.2 second, default daemon=True

seconds_nap = int(5)

while True:  # All data is available via instantiated thread data stream attribute.
    data_stream = agps_thread.data_stream
    for nod in range(0, seconds_nap):
        print('{:.0%} wait period of {} seconds'.format(nod / float(seconds_nap), seconds_nap))
        sleep(1)
    print('---------------------')
    print('---------------------')
    print(                   data_stream.time)
    print('Lat:{}   '.format(data_stream.lat))
    print('Lon:{}   '.format(data_stream.lon))
    print('Speed:{} '.format(data_stream.speed))
    print('Track:{}'.format(data_stream.track))
    print('---------------------')
    if (data_stream.lat != 'n/a'):
        unix = time.mktime(datetime.strptime(data_stream.time, "%Y-%m-%dT%H:%M:%S.%fZ").timetuple())
        payload = json.dumps({'unix': int(unix), 'lat': data_stream.lat, 'lon': data_stream.lon, 'speed': data_stream.speed, 'utc': data_stream.time})
        m = publish.single("gps/test", retain=True, payload=payload, hostname="localhost")
        print (m)
    else:
        print "invalid gps infomation"