@@ -68,14 +68,17 @@ def __init__(self,configfile=None,simulate=False,noupload=False):
68
68
#default of 20 microseconds
69
69
self .serialsleep = 20e-3
70
70
71
+ #possibly create the data directory if it doesn't exist yet
72
+ if not os .path .exists (self .cfg ['data_dir' ]):
73
+ os .mkdir (self .cfg ['data_dir' ])
71
74
#set the filename of the open logfile
72
75
self .openLogFile = os .path .join (self .cfg ['data_dir' ],self .cfg ['file_base' ]+ ".tmp" )
73
76
#default (will be updated from GNSS info)
74
77
self .logdate = date .today ()
75
78
76
79
self .openSerial ()
77
80
self .setupWebdav ()
78
-
81
+
79
82
def setupWebdav (self ):
80
83
if "webdav" in self .cfg :
81
84
self .webdav = self .cfg ['webdav' ]['url' ]
@@ -102,11 +105,15 @@ async def rotateNMEAlog(self):
102
105
rmcregex = re .compile (b'^\$G[NPL]RMC' )
103
106
#open logstream
104
107
self .openLog ()
105
- while True :
108
+ while self .isLogging :
109
+
106
110
#Asynchronously wait for new serial data
107
111
nmeamsg = await self .getnmea ()
108
112
if not nmeamsg .endswith (b"\n " ):
109
- #no info -> try again later
113
+ #no info -> try again later (or in the case of simulate data rewind the buffer
114
+ if self .simulate :
115
+ self .serial .seek (0 )
116
+ continue
110
117
print ("no data found on the serial port, retrying in one second" )
111
118
await asyncio .sleep (1 )
112
119
continue
@@ -142,6 +149,10 @@ def closeLog(self):
142
149
if self .logfid :
143
150
self .logfid .close ()
144
151
self .logfid = None
152
+ else :
153
+ #nothing to do
154
+ return
155
+
145
156
#also move the file to a more suitable name
146
157
logfilebase = os .path .join (self .cfg ['data_dir' ],f"{ self .cfg ['file_base' ]} _{ self .logdate .isoformat ()} " )
147
158
#make sure not to overwrite existing files
@@ -217,7 +228,8 @@ async def uploadLogs(self):
217
228
218
229
219
230
async def startLoggingDaemon (self ):
220
- while True :
231
+ self .isLogging = True
232
+ while self .isLogging :
221
233
synctask = asyncio .create_task (self .uploadLogs ())
222
234
await self .rotateNMEAlog ()
223
235
#wait for synctask to finish with timeout as a backup
@@ -227,3 +239,7 @@ async def startLoggingDaemon(self):
227
239
except asyncio .TimeoutError :
228
240
pass
229
241
242
+ def stopLoggingDaemon (self ,* args ):
243
+ """Gracefully stop logging (closes logging file)"""
244
+ self .isLogging = False
245
+
0 commit comments