forked from miurahr/gpslogger
/
main.py
executable file
·298 lines (247 loc) · 9.07 KB
/
main.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/python
################################################################################
# Copyright (c) by George Ruinelli (2012)
# License: GPL3
# Modified by Hiroshi Miura, 2013
# Modified by Mario Frasca, 2018
################################################################################
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtDeclarative import *
from PySide.QtGui import QDesktopServices as QDS
# import math
import platform
import time
import datetime
import os
import string
import ConfigParser
# import gconf
from datetime import tzinfo, timedelta, datetime
import logging
logger = logging.getLogger(__name__)
################################################################################
class GPSLoggerApp(QApplication):
root = "/opt/gps-logger/"
filehandle = None
recording = False
path = "/home/user/MyDocs/GPS-Logger"
version = "??"
build = "?"
def __init__(self, argv):
super(GPSLoggerApp, self).__init__(argv)
if(platform.machine().startswith('arm')):
pass
else:
self.root = "./"
self.path = "./data/"
try:
os.makedirs(self.path)
except:
pass
try:
with open(os.path.join(self.root, "version"), 'r') as file:
self.version = file.readline().strip()
self.build = file.readline().strip()
logger.info("Version: %s-%s" % (self.version, self.build))
except:
logger.error("Version file not found, please check your installation!")
self.config = Configuration()
self.gpx = GPX()
def finished(self):
self.config.write()
if(self.gpx.recording == True):
logger.info("we are still recording, close file properly")
self.gpx.stop_recording()
logger.debug("Closed")
def does_opt_in(self):
logger.info("config.opt_in:", self.config.opt_in)
return self.config.opt_in
@Slot(str, int, result=str)
def start(self, filename, interval):
return self.gpx.start_recording(self.path, filename, interval)
@Slot()
def stop(self):
logger.debug("stop")
self.gpx.stop_recording()
@Slot()
def pause(self):
self.gpx.paused = True
@Slot()
def resume(self):
self.gpx.start_new_segment()
self.gpx.paused = False
@Slot(float, float, float, float)
def add_point(self, lon, lat, alt, speed):
logger.debug("recording point - lon:%s lat:%s alt:%s speed:%s time:%s" % (lon, lat, alt, speed, time))
self.gpx.add_entry(lon, lat, alt, speed)
@Slot(float, float, float, float, str)
def add_waypoint(self, lon, lat, alt, speed, waypoint):
logger.debug("recording waypoint - lon:%s lat:%s alt:%s speed:%s time:%s" % (lon, lat, alt, speed, time))
self.gpx.add_waypoint(lon, lat, alt, speed, waypoint)
@Slot(result=str)
def get_version(self):
return str(self.version) + "-" + str(self.build)
@Slot(bool)
def Opt_In(self, v):
self.opt_in = v
if(v is False):
config.write()
logger.warn("We have to quit now, so sorry")
quit()
################################################################################
class Configuration():
configpath = ""
configfile = "config.conf"
opt_in = False
def __init__(self):
self.configpath = os.path.join(QDS.storageLocation(QDS.DataLocation), "GPS-Logger")
self.configfile = self.configpath + "/" + self.configfile
logger.debug("Loading configuration from: %s" % self.configfile)
self.ConfigParser = ConfigParser.SafeConfigParser()
try:
self.ConfigParser.read(self.configfile)
except: # use default config
logger.warn("Configuration file %s not existing or not compatible" % self.configfile)
try:
self.ConfigParser.add_section('main')
except:
pass
try:
self.opt_in = self.ConfigParser.getboolean("main", "opt_in")
logger.debug("Configuration loaded")
except:
logger.error("Error loading configuration, using default value")
def write(self):
logger.debug("Write configuration to: %s" % self.configfile)
self.ConfigParser.set('main', 'opt_in', str(self.opt_in))
try:
os.makedirs(self.configpath)
except:
pass
try:
with open(self.configfile, 'w') as handle:
self.ConfigParser.write(handle)
logger.debug("Configuration saved")
except:
logger.error("Failed to write configuration file!")
################################################################################
class GPX():
filehandle = None
recording = False
waypoints = []
paused = False
def start_recording(self, datapath, filename, interval):
if(filename == ""):
filename = "track"
suffix = 1
filename2 = filename + ".gpx" # try first without a numeric suffix
full_filename = datapath + "/" + filename2
if(os.path.exists(full_filename)):
while(os.path.exists(full_filename)):
filename2 = "%s_%04d.gpx" % (filename, suffix)
full_filename = datapath + "/" + filename2
suffix = suffix + 1
logger.info("Start recording %s %s" % (full_filename, interval))
try:
self.filehandle = open(full_filename, 'w')
self.recording = True
self.filehandle.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n")
txt = '''\
<gpx xmlns="http://www.topografix.com/GPX/1/1"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
creator="N9 GPS Logger"
version="1.1"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">'''
self.filehandle.write(txt)
txt = '''
<metadata>
<link href="http://github.com/mfrasca/gpslogger">
<text>N9 GPS Logger</text>
</link>
</metadata>'''
self.filehandle.write(txt)
txt = '''
<trk>
<name>%s</name>
<trkseg>''' % filename
self.filehandle.write(txt)
return filename2
except:
logger.error("failed to open file: %s" % full_filename)
return ""
def start_new_segment(self):
if(self.recording is True):
txt = '''
</trkseg>
<trkseg>'''
self.filehandle.write(txt)
else:
logger.error("file closed, can not write to it")
def add_entry(self, lon, lat, alt, speed):
if(self.recording is True):
if self.paused:
return
logger.debug("adding entry")
tt = datetime.utcnow().timetuple() # time in UTC
try:
alt = str(int(alt))
except:
alt = "0"
t = "%d-%02d-%02dT%02d:%02d:%02dZ" % tt[:6]
s = speed * 3.6
txt = '''
<trkpt lat="%(lat)s" lon="%(lon)s">
<ele>%(ele)s</ele>
<time>%(time)s</time>
<desc>Lat.=%(lat)s, Long.=%(lon)s, Alt.=%(ele)sm, Speed=%(speed)sKm/h</desc>
</trkpt>''' % {'lat': lat, 'lon': lon, 'ele': alt, 'time': t, 'speed': s}
self.filehandle.write(txt)
else:
logger.warn("file closed, can not add entry")
def add_waypoint(self, lon, lat, alt, speed, waypoint):
if(self.recording is True):
t = "%d-%02d-%02dT%02d:%02d:%02dZ" % datetime.utcnow().timetuple()[:6]
self.waypoints.append({'lat': lat, 'lon': lon, 'ele': alt, 'time': t, 'name': waypoint})
else:
logger.warn("file closed, can not add entry")
def stop_recording(self):
logger.debug("Stop recording")
if(self.recording is True):
txt = '''
</trkseg>
</trk>%s
</gpx>
'''
waypoint_format = '''
<wpt lat="%(lat)s" lon="%(lon)s">
<ele>%(ele)s</ele>
<time>%(time)s</time>
<name>%(name)s</name>
</wpt>'''
waypoints_xml = ''.join(waypoint_format % item for item in self.waypoints)
self.filehandle.write(txt % waypoints_xml)
self.waypoints = []
try:
self.filehandle.close()
self.recording = False
except:
pass
################################################################################
if __name__ == '__main__':
gpslogger = GPSLoggerApp(sys.argv)
logging.basicConfig(stream=sys.stderr, level=logging.WARNING)
view = QDeclarativeView()
context = view.rootContext()
context.setContextProperty("app", gpslogger)
view.setSource(QUrl.fromLocalFile(gpslogger.root + 'qml/main.qml'))
if(platform.machine().startswith('arm')):
view.showFullScreen()
view.show()
if(gpslogger.does_opt_in() is False):
root = view.rootObject()
root.show_Opt_In()
gpslogger.exec_() # endless loop
gpslogger.finished()