forked from sailoog/openplotter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_gps.py
65 lines (59 loc) · 1.9 KB
/
time_gps.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
# This file is part of Openplotter.
# Copyright (C) 2015 by sailoog <https://github.com/sailoog/openplotter>
#
# Openplotter is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# any later version.
# Openplotter is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Openplotter. If not, see <http://www.gnu.org/licenses/>.
import socket, pynmea2, subprocess
fecha=''
hora=''
try:
sock_in = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock_in.settimeout(10)
sock_in.connect(('127.0.0.1', 10110))
except socket.error, error_msg:
print 'Failed to connect with localhost:10110.'
print 'Error: '+ str(error_msg[0])
else:
cont = 0
while True:
frase_nmea =''
try:
frase_nmea = sock_in.recv(1024)
except socket.error, error_msg:
try:
print 'Failed to connect with localhost:10110.'
print 'Error: '+ str(error_msg[0])
sys.stdout.flush()
except: pass
break
else:
if frase_nmea:
try:
nmea_list=frase_nmea.split()
for i in nmea_list:
msg = pynmea2.parse(i)
nmea_type=msg.sentence_type
if nmea_type == 'RMC':
fecha = msg.datestamp
hora = msg.timestamp
break
else:
cont=cont+1
except: pass
if cont >= 30: break
if fecha and hora:
subprocess.call([ 'date', '--set', fecha.strftime('%Y-%m-%d'), '--utc'])
subprocess.call([ 'date', '--set', hora.strftime('%H:%M:%S'), '--utc'])
print 'Date and time retrieved from NMEA data successfully.'
else:
print 'Unable to retrieve date or time from NMEA data.'