I wrote this little script that runs as part of my observatory start. It just checks that my position is reasonable, and runs that systemctl command if not. It would be easy to extend the check to restart gpsd if the reported date is too old while the gps thinks it has a fix.
#!/usr/bin/env python3
import subprocess
from json import loads
from time import sleep
def get_gps_data():
cmd = ['gpspipe', '-w', '-n', '8']
cmd_out = subprocess.run(cmd, text=True, capture_output=True).stdout
rv = {}
for x in cmd_out.splitlines():
d = loads(x)
k = d.pop('class', None)
rv[k] = d
return rv
def check_gps_confusion():
gps = get_gps_data()
#print(gps['TPV'])
#print(gps['SKY'])
have_fix = (gps['TPV']['mode'] in [2, 3] and gps['TPV']['status'] in [1,2,3,4])
is_zero = (gps['TPV']['lat'] == 0.0 and gps['TPV']['lon'] == 0.0 and gps['TPV']['altHAE'] == 0.0)
have_sats = gps['SKY']['uSat'] > 5
return have_sats and have_fix and is_zero
def main():
if check_gps_confusion():
print("Restarting GPSD")
subprocess.run(['sudo', 'systemctl', 'restart','gpsd'])
sleep(5)
if check_gps_confusion():
print("ERROR: GPSD did not reset")
else:
print("GPS OK")
else:
print("GPS OK")
if __name__ == '__main__':
main()