forked from heldersepu/GMapCatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapGPS.py
170 lines (151 loc) · 5.43 KB
/
mapGPS.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
## @package gmapcatcher.mapGPS
# GPS Support
available = True
try:
import gps
import serialGPS
from gmapcatcher.gps import misc
except:
available = False
import mapConst
import widgets.mapPixbuf as mapPixbuf
from threading import Event, Thread
import time
from datetime import datetime, timedelta
if time.localtime().tm_isdst:
offset = timedelta(seconds=time.altzone)
else:
offset = timedelta(seconds=time.timezone)
class GPS:
def __init__(self, gps_callback, conf):
global available
# GPS Disabled at start
self.conf = conf
self.mode = conf.gps_mode
self.type = conf.gps_type
self.location = None
self.gps_callback = gps_callback
self.pixbuf = self.get_marker_pixbuf()
self.update_rate = float(conf.gps_update_rate)
self.serial_port = conf.gps_serial_port
self.baudrate = conf.gps_serial_baudrate
self.gps_updater = None
self.gpsfix = None
if self.mode != mapConst.GPS_DISABLED:
self.startGPS()
def startGPS(self):
global available
available = True
if self.type == mapConst.TYPE_GPSD:
try:
# Open binding to GPS daemon
self.gps_session = gps.gps()
self.gps_session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)
self.gps_updater = GPSUpdater(self.type, self.update_rate, self.update, gps_session=self.gps_session)
if self.mode != mapConst.GPS_DISABLED:
self.gps_updater.start()
except:
# No GPS connected
available = False
elif self.type == mapConst.TYPE_SERIAL:
print 'serial started'
try:
self.gps_updater = GPSUpdater(self.type, self.update_rate, self.update, serial_port=self.serial_port, baudrate=self.baudrate)
if self.mode != mapConst.GPS_DISABLED:
self.gps_updater.start()
except:
available = False
else:
available = False
def stop_all(self):
try:
self.gps_updater.cancel()
except:
pass
## Sets the behaviour of the GPS functionality
def set_mode(self, mode):
self.mode = mode
if self.gps_updater:
if mode == mapConst.GPS_DISABLED:
self.gps_updater.cancel()
elif not self.gps_updater.is_alive():
self.startGPS()
elif mode != mapConst.GPS_DISABLED:
self.startGPS()
## Get GPS position
def get_location(self):
if self.mode != mapConst.GPS_DISABLED and self.gpsfix.latitude and self.gpsfix.longitude:
return (self.gpsfix.latitude, self.gpsfix.longitude)
return None
## Callback from the GPSUpdater
def update(self, fix):
self.gpsfix = fix
self.gps_callback()
## Load GPS marker image
def get_marker_pixbuf(self):
return mapPixbuf.getImage('marker_gps.png', 48, 48)
## Continuously updates GPS coordinates.
class GPSUpdater(Thread):
def __init__(self, gps_type, interval, function, gps_session=None, serial_port=None, baudrate=None):
global available
Thread.__init__(self)
self.gps_type = gps_type
self.interval = interval
self.function = function
self.gps_session = gps_session
self.serial_port = serial_port
self.baudrate = baudrate
self.finished = Event()
self.event = Event()
self.lastUpdate = time.time()
def run(self):
global available
if self.gps_type == mapConst.TYPE_GPSD and self.gps_session:
try:
while not self.finished.isSet():
self.gps_session.next()
if time.time() - self.lastUpdate > self.interval:
self.function(self.gps_session.fix)
self.lastUpdate = time.time()
except StopIteration:
self.gps_session.fix.mode = mapConst.MODE_NO_FIX
self.function(self.gps_session.fix)
available = False
print "GPSD has terminated"
elif self.gps_type == mapConst.TYPE_SERIAL and self.serial_port and self.baudrate:
sergps = serialGPS.SerialGPS(self.serial_port, self.baudrate)
sergps.start()
while not self.finished.isSet():
time.sleep(self.interval)
available = sergps.available
if not available:
break
self.function(sergps.fix)
sergps.stop()
else:
available = False
def cancel(self):
self.finished.set()
self.join(3)
def makeGPSTime(gps_time, gps_type, use_offset=False):
d = None
if gps_type == mapConst.TYPE_GPSD:
if not isinstance(gps_time, int) and not isinstance(gps_time, float):
gps_time = misc.isotime(gps_time)
d = datetime.utcfromtimestamp(gps_time)
else:
dNow = datetime.utcnow()
if '.' in gps_time:
try:
d = datetime.strptime(gps_time, '%H%M%S.%f')
except TypeError:
return
else:
try:
d = datetime.strptime(gps_time, '%H%M%S')
except TypeError:
return
d = d.replace(year=dNow.year, month=dNow.month, day=dNow.day)
if use_offset:
d = d - offset
return d