1
0
Fork 0

Add a 'startup' python script to set system time from GPS once.

This commit is contained in:
flabbergast 2014-08-30 12:36:28 +02:00
parent 3655a76120
commit e50d632cd9
5 changed files with 75 additions and 6 deletions

View File

@ -8,12 +8,19 @@ priority=1
autostart=true
autorestart=true
[program:time-from-gps-oneshot]
command=/usr/bin/python /opt/gps-timekeep/time-from-gps/time-from-gps.py
numprocs=1
priority=2
autostart=true
autorestart=false
[program:gps-watcher]
command=/usr/bin/python /opt/gps-timekeep/watcher-daemon/gps-watcher.py
directory=/opt/gps-timekeep/watcher-daemon
numprocs=1
priority=2
autostart=true
priority=10
autostart=false
autorestart=true
[program:rpi_gpio_ntp]

View File

@ -5,7 +5,7 @@ driftfile /var/lib/ntp/ntp.drift
# Server from shared memory provided by gpsd
server 127.127.28.0 minpoll 4 maxpoll 4
# also 'prefer'?
fudge 127.127.28.0 time1 +0.1456 refid SHM
fudge 127.127.28.0 time1 +0.1416 refid SHM
# maybe also 'stratum 15'?
# user mode PPS

60
time-from-gps/time-from-gps.py Executable file
View File

@ -0,0 +1,60 @@
#!/usr/bin/python
import gps
import os
import xmlrpclib
import time
#import datetime
#import dateutil.parser
# write a temporary html report
HTML_OUTPUT_DIR = "/run/www"
HTML_OUTPUT_FILE = "/index.html"
if not os.path.exists(HTML_OUTPUT_DIR):
os.makedirs(HTML_OUTPUT_DIR)
fout = open(HTML_OUTPUT_DIR+HTML_OUTPUT_FILE, "w")
fout.write("""
<html>
<title>ntpi info</title>
<meta content="text/html; charset=utf-8;" http-equiv="Content-Type">
<body>
<h1>ntpi info</h1>
<h3>No GPS connection yet</h3>
</body>
</html>
""")
fout.close()
# wait for gpsd to come up
time.sleep(1)
# Listen on port 2947 (gpsd) of localhost
session = gps.gps("localhost", "2947")
session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)
# main loop: wait for a time report from GPS
while True:
try:
report = session.next()
# Wait for a 'TPV' report and display the current time
# To see all report data, uncomment the line below
# print report
if report['class'] == 'TPV':
if hasattr(report, 'time'):
#print report.time
#gpstime = dateutil.parser.parse(report.time)
#print gpstime.isoformat('T')
#print datetime.datetime.now().isoformat('T')
os.system('date --set="'+report.time+'"')
# connect to supervisor
supervisord = xmlrpclib.Server('http://localhost:9001/RPC2')
# start the watcher daemon
supervisord.supervisor.startProcess('gps-watcher')
break
except KeyError:
pass
except KeyboardInterrupt:
quit()
except StopIteration:
session = None
print "GPSD has terminated"

View File

@ -65,7 +65,8 @@ def generate_html_file():
gps_info = str(gpsd)
gps_has_fix = "Yes" if gps_has_signal else "No"
ntp_info = subprocess.check_output(["ntpq", "-pn"], stderr=subprocess.STDOUT)
current_time = datetime.datetime.now()
current_time = datetime.datetime.utcnow().isoformat('T')
# .strftime("%Y-%m-%dT%H:%M:%S.%f %Z")
# write the output file, substituting the variables
fout = open(HTML_OUTPUT_DIR+HTML_OUTPUT_FILE, "w")
fout.write(html_template.format(**locals()))

View File

@ -8,7 +8,7 @@
<blockquote><pre>{gps_info}</pre></blockquote>
<h3>Current server time</h3>
<blockquote>
{current_time} (when this page was generated, will differ from the GPS time!)
{current_time} UTC (when this page was generated, will differ from the GPS time! Also, it is UTC, not local!)
</blockquote>
<h3>NTP daemon info</h3>
<blockquote><pre>{ntp_info}</pre></blockquote>
@ -26,7 +26,8 @@
<a href="#" onclick="javascript:event.target.port=9001">Supervisor (daemon status/management)</a>
<blockquote>Notes:
<ul>
<li><i>gpsd</i>, <i>gps-watcher</i> and <i>lighttpd</i> should be always running</li>
<li><i>gpsd</i> and <i>lighttpd</i> should be always running, <i>gps-watcher</i> also, a couple of second after boot</li>
<li><i>time-from-gps-oneshot</i> is a "startup script", so running at the start and then should be "Exited"</li>
<li><i>gps-watcher</i> needs to be restarted after every restart of <i>gpsd</i></li>
<li><i>gps-watcher</i> will start <i>ntpd</i> and <i>rpi_gpio_ntp</i> if GPS has a fix (in 2-3 seconds)</li>
<li><i>gps-watcher</i> will stop <i>ntpd</i> and <i>rpi_gpio_ntp</i> if GPS does <i>not</i> have a fix</li>