-
Notifications
You must be signed in to change notification settings - Fork 3
/
plugin.py
594 lines (551 loc) · 18.2 KB
/
plugin.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
import time
import threading
import time
import os
import datetime
import sys
import re
import glob
REC_HDR='H'
REC_DATA='D'
NAME="history"
class HistoryFileWriter:
def __init__(self,filename,fields,ts=None):
self.filename=filename
self.fields=fields
self.file=open(filename,"a")
if ts is None:
ts=time.time()
self.file.write("\n") #if the previous line was not completed...
self.writeRecord(REC_HDR,[str(ts)]+self.fields,False)
def isOpen(self):
return self.file is not None
def writeRecord(self,type,fields,toStrings=True):
self.file.write("%s,"%type)
if toStrings:
self.file.write(",".join(map(lambda a: str(a) if a is not None else '',fields)))
else:
self.file.write(",".join(fields))
self.file.write("\n")
self.file.flush()
def close(self):
if self.file is None:
return
self.file.close()
class HistoryFileReader:
def __init__(self,filename,fields,api=None):
self.filename=filename
self.fields=fields
self.file=None
self.requestedFields=fields
self.api=api
if os.path.isfile(filename):
self.file=open(filename,"r")
def isOpen(self):
return self.file is not None
def computeMappings(self,newHdrLine):
if len(newHdrLine) < 3:
return None
mappings={}
for f in range(0,len(self.fields)):
for i in range(2,len(newHdrLine)):
if newHdrLine[i]==self.fields[f]:
mappings[i]=f
return mappings
def getRecords(self,minTime=None,maxTime=None):
self.file.seek(0)
rt=[]
#mapping maps the filed in the file to the position
#in the putput array
mapping=None
try:
for line in self.file:
fields = line.rstrip().split(",")
if line.startswith(REC_HDR):
mapping=self.computeMappings(fields)
continue
if mapping is None:
continue
if not line.startswith(REC_DATA):
continue
if len(fields) < 3:
continue
try:
ts=float(fields[1])
if minTime is not None and ts < minTime:
continue
if maxTime is not None and ts > maxTime:
return rt
opline=[None] * (len(self.fields)+1)
opline[0]=ts #timestamp
hasFields=False
for i in range(2,len(fields)):
idx=mapping.get(i)
if idx is None:
continue
if fields[i] is None or fields[i] == "":
continue
opline[idx+1]=float(fields[i])
hasFields=True
if hasFields:
rt.append(opline)
except:
if self.api is not None:
self.api.debug("unable to read record %s",line)
continue
except Exception as e:
if self.api is not None:
self.api.error("Error reading from file %s: %s",self.filename,str(e))
return rt
def close(self):
if self.file is None:
return
self.file.close()
class Average:
def __init__(self):
self.count=0
self.sum=None
def add(self,value):
if value is None:
return
try:
if self.sum is None:
self.sum=float(value)
self.count+=1
else:
self.sum+=float(value)
self.count += 1
return True
except:
return False
def cur(self):
if self.count == 0 or self.sum is None:
return self.sum
return float(self.sum)/float(self.count)
def reset(self):
self.count=0
self.sum=None
def hasData(self):
return self.count > 0 and self.sum is not None
class Plugin:
CONFIG=[
{
'name': 'period',
'description': 'period for writing in s',
'default': 30,
'type': 'NUMBER'
}
,
{
'name': 'pollingInterval',
'description': 'polling interval for internal store (seconds), defaults to period/10',
'default': 30,
'type':'NUMBER'
}
,
{
'name': 'storeTime',
'description': 'how long to store the history (h)',
'default': 48,
'type':'NUMBER'
}
]
OLD_CONFIG=[{
'name': 'sensorNames',
'description': 'the sensor names we look for in XDR records separated by ,',
'default': None
},
{
'name': 'storeKeys',
'description': 'data to be fetched from the AvNav internal store ,',
'default': None
}
]
@classmethod
def pluginInfo(cls):
"""
the description for the module
@return: a dict with the content described below
parts:
* description (mandatory)
* data: list of keys to be stored (optional)
* path - the key - see AVNApi.addData, all pathes starting with "gps." will be sent to the GUI
* description
"""
return {
'description': 'seatalk remote control',
'config': cls.CONFIG+cls.OLD_CONFIG ,
'data': [
]
}
def __init__(self,api):
"""
initialize a plugins
do any checks here and throw an exception on error
do not yet start any threads!
@param api: the api to communicate with avnav
@type api: AVNApi
"""
self.api = api # type: AVNApi
#we register an handler for API requests
self.xdrNames=None
self.dataKeys=None
self.storeKeys=None
self.storePollingPeriod=None
self.values=[]
self.baseDir=None
self.storeTime=None
self.period=None
self.sequence=1 #changed if new data is added or data removed
self.changeSequence=0
self.startSequence=0
self.canEdit=False
if hasattr(self.api,'registerEditableParameters'):
self.api.registerEditableParameters(self.CONFIG,self._changeConfig)
self.canEdit=True
if hasattr(self.api,'registerRestart'):
self.api.registerRestart(self._apiRestart)
def _apiRestart(self):
self.startSequence+=1
self.changeSequence+=1
def _changeConfig(self,newValues):
self.api.saveConfigValues(newValues)
self.changeSequence+=1
def updateSequence(self):
self.sequence+=1
def computeFileName(self,daysDiff=0):
now=datetime.datetime.utcnow()
if daysDiff != 0:
now+=datetime.timedelta(days=daysDiff)
return os.path.join(self.baseDir,str(now.strftime("%Y-%m-%d")+".avh"))
def getAllFileNames(self):
'''
get all needed filenames depending on the storeTime
:return:
'''
days = int(self.storeTime / 24) + 1
files = []
for d in range(days + 1,-1,-1):
files.append(self.computeFileName(-d))
return files
def getConfigValue(self,name):
defaults=self.pluginInfo()['config']
for cf in defaults:
if cf['name'] == name:
return self.api.getConfigValue(name,cf.get('default'))
return self.api.getConfigValue(name)
def convertValue(self, value, unit):
'''
:param value:
:param type:
:return:
'''
#see e.g. https://gpsd.gitlab.io/gpsd/NMEA.html#_xdr_transducer_measurement
if unit == "C":
value+=273.15
if unit == "B":
value=value*100*1000
return value
STOP_RETURN=0
STOP_AGAIN=1
STOP_WAIT=2
def run(self):
self.api.registerRequestHandler(self.handleApiRequest)
startSequence=self.startSequence
self.api.registerUserApp(self.api.getBaseUrl()+'/index.html',os.path.join('icons','show_chart.svg'),'History')
while startSequence == self.startSequence:
rt=self.runInternal()
if rt is None or rt == self.STOP_RETURN:
return
if rt == self.STOP_WAIT:
changeSequence=self.changeSequence
while changeSequence == self.changeSequence:
time.sleep(1)
def runInternal(self):
"""
the run method
this will be called after successfully instantiating an instance
this method will be called in a separate Thread
The example simply counts the number of NMEA records that are flowing through avnav
and writes them to the store every 10 records
@return:
"""
changeSequence=self.changeSequence
seq=0
self.values=[]
self.api.log("started")
enabled=self.getConfigValue('enabled')
if enabled is not None and enabled.lower()!='true':
self.api.setStatus("INACTIVE", "disabled by config")
return self.STOP_RETURN
try:
self.period=float(self.getConfigValue('period'))
except Exception as e:
self.api.setStatus("ERROR", "error getting period: %s" % str(e))
return self.STOP_WAIT
try:
self.storeTime=float(self.getConfigValue('storeTime'))
except Exception as e:
self.api.setStatus("ERROR", "error getting storeTime: %s" % str(e))
return self.STOP_WAIT
sensors=self.getConfigValue('sensorNames')
self.dataKeys=[]
if sensors is not None:
self.xdrNames=list(filter(lambda k: k != "",sensors.split(",")))
self.dataKeys.extend(self.xdrNames)
storeKeys=self.getConfigValue('storeKeys')
if storeKeys is not None:
self.storeKeys=list(filter(lambda k: k != "",storeKeys.split(",")))
self.dataKeys.extend(self.storeKeys)
if len(self.dataKeys) < 1:
self.api.setStatus("ERROR", "no parameter sensorNames or storeKeys configured")
return self.STOP_WAIT
if self.dataKeys[0] == "":
self.api.setStatus("ERROR", "empty parameter sensorNames / storeKeys configured")
return self.STOP_WAIT
self.baseDir=os.path.join(self.api.getDataDir(),"plugins",NAME)
if not os.path.exists(self.baseDir):
os.makedirs(self.baseDir)
if not os.path.isdir(self.baseDir):
self.api.setStatus("ERROR", "unable to create basedir %s"%self.baseDir)
raise Exception("unable to create basedir %s"%self.baseDir)
self.storePollingPeriod=self.getConfigValue('pollingInterval')
if self.storePollingPeriod is None:
self.storePollingPeriod=self.period/10
if self.storePollingPeriod < 1:
self.storePollingPeriod=1
else:
try:
self.storePollingPeriod=float(self.storePollingPeriod)
if self.storePollingPeriod < 1:
self.storePollingPeriod=1
if self.storePollingPeriod > self.period/2:
self.storePollingPeriod=self.period/2
except Exception as e:
self.api.setStatus("ERROR","unable to get store period: %s"%str(e))
return self.STOP_WAIT
minTime=time.time()-self.storeTime*3600
currentFile = self.computeFileName()
allFiles=self.getAllFileNames()
for historyFile in allFiles:
try:
if os.path.exists(historyFile):
self.api.log("reading file %s", historyFile)
reader=HistoryFileReader(historyFile,self.dataKeys,self.api)
self.values.extend(reader.getRecords(minTime))
reader.close()
except Exception as e:
self.api.error("error reading history: %s",str(e))
self.api.log("%d entries in history", len(self.values))
cleanupThread=threading.Thread(target=self.cleanup,name="barograph-cleanup")
cleanupThread.setDaemon(True)
cleanupThread.start()
currentValues={}
self.updateSequence()
for f in self.dataKeys:
currentValues[f]=Average()
lastWrite=0
hasValues=False
hwriter=HistoryFileWriter(currentFile,self.dataKeys)
self.api.setStatus("INACTIVE","writing to %s"%currentFile)
dataReceived=False
waitTime=0.3
lastStorePoll=0
while changeSequence == self.changeSequence:
try:
seq,data=self.api.fetchFromQueue(seq,10,filter="$XDR",waitTime=waitTime)
if len(data) > 0:
for line in data:
#$--XDR,a,x.x,a,c--c, ..... *hh<CR><LF>
line=re.sub("\*.*","",line.rstrip())
fields=line.split(",")
lf=len(fields)
i=1
while i < lf:
if i < (lf-3):
try:
#we need 4 fields
if fields[i+1] is not None and fields[i] != "":
ttype=fields[i]
tdata=float(fields[i+1])
tunit=fields[i+2]
tname=fields[i+3]
if tname in self.dataKeys:
self.api.debug("received %f for %s",tdata,tname)
currentValues[tname].add(self.convertValue(tdata,tunit))
hasValues=True
except Exception as e:
self.api.error("NMEA error in %s: %s",line,str(e))
i+=4
now=time.time()
if self.storeKeys is not None and len(self.storeKeys) > 0 and (now >= (lastStorePoll + self.storePollingPeriod) or now < lastStorePoll):
for sk in self.storeKeys:
try:
v=self.api.getSingleValue(sk)
if currentValues[sk].add(v):
hasValues=True
except Exception as e:
self.api.debug("unable to fetch %s: %s",sk,str(e))
lastStorePoll=now
if hasValues and (now >= (lastWrite+self.period) or now < lastWrite):
if not dataReceived:
self.api.setStatus("NMEA","writing to %s"%hwriter.filename)
dataReceived=True
record=[now]
for f in self.dataKeys:
v=currentValues[f].cur()
currentValues[f].reset()
record.append(v)
nextFile=self.computeFileName()
if nextFile != hwriter.filename:
self.api.log("opening new file %s",nextFile)
dataReceived=False
hwriter.close()
hwriter=HistoryFileWriter(nextFile,self.dataKeys)
self.values.append(record)
hwriter.writeRecord(REC_DATA,record)
self.updateSequence()
lastWrite=now
hasValues=False
except Exception as e:
self.api.error("error in plugin loop: %s",str(e))
return self.STOP_AGAIN
def cleanup(self):
changeSequence=self.changeSequence
lastCleanup=0
while changeSequence == self.changeSequence:
now=time.time()
if now < (lastCleanup +60) and now >= lastCleanup:
time.sleep(1)
continue
lastCleanup=now
self.api.debug("cleanup loop")
cleanupTime=time.time()-self.storeTime*3600
numRemoved=0
while len(self.values) >0 and self.values[0][0] < cleanupTime:
self.values.pop(0)
numRemoved+=1
if numRemoved > 0:
self.updateSequence()
self.api.debug("removed %d entries from history",numRemoved)
keepFiles=[self.computeFileName(+1)]
keepFiles.extend(self.getAllFileNames())
currentFiles = glob.glob(os.path.join(self.baseDir, u"*.avh"))
for file in currentFiles:
if file in keepFiles:
continue
self.api.log("removing old file %s",file)
os.remove(file)
def getFilteredValues(self,indices,row):
rt=[row[0]]
for i in indices:
if i>=0 and i < len(row):
rt.append(row[i])
else:
rt.append(None)
return rt
def _getRequestParam(self,param,name,raiseMissing=True):
data = param.get(name)
if data is None or len(data) != 1:
if not raiseMissing:
return None
raise Exception("missing parameter %s"%name)
return data[0]
def _appendKeys(self,keylist,storeData,prefix=None):
BLACKLIST=['tag','source','mode','updatealarm','updateleg','updateroute']
if prefix is None:
prefix='gps'
else:
BLACKLIST=[]
for k,v in storeData.items():
if type(v) is not dict:
if k not in BLACKLIST:
keylist.append(prefix+'.'+k)
else:
self._appendKeys(keylist,v,prefix+'.'+k)
def handleApiRequest(self,url,handler,args):
"""
handler for API requests send from the JS
@param url: the url after the plugin base
@param handler: the HTTP request handler
https://docs.python.org/2/library/basehttpserver.html#BaseHTTPServer.BaseHTTPRequestHandler
@param args: dictionary of query arguments
@return:
"""
if url == 'status':
return {'status': 'OK',
'numRecords':len(self.values),
'oldest': self.values[0][0] if len(self.values) else None,
'fields': self.dataKeys,
'period': self.period,
'storeTime': self.storeTime,
'sequence': self.sequence,
'canEdit': self.canEdit
}
if url == 'history':
fromTime=args.get('fromTime')
if fromTime is not None:
fromTime=float(fromTime[0])
toTime=args.get('toTime')
if toTime is not None:
toTime=float(toTime[0])
indices=None
keys=args.get('fields')
if keys is not None:
keys=keys[0].split(",")
indices=[]
for k in keys:
for i in range(0,len(self.dataKeys)):
if k == self.dataKeys[i]:
indices.append(i+1) #first element is always the time
else:
keys=self.dataKeys
if indices is not None and len(indices) < 1:
values=[]
else:
values=list(map(lambda r: r if indices is None else self.getFilteredValues(indices,r),
list(filter(lambda r:
(fromTime is None or r[0] >= fromTime) and (toTime is None or r[0] <= toTime),
self.values))))
return {
'status':'OK',
'fields': keys,
'period': self.period,
'sequence': self.sequence,
'data':values
}
if url == 'listKeys':
storeData=self.api.getDataByPrefix('gps')
keylist=[]
self._appendKeys(keylist,storeData)
return {
'status':'OK',
'data':keylist
}
if url == 'getKeys':
return {
'status':'OK',
'data': self.getConfigValue('storeKeys')
}
if url == 'saveSettings':
if not self.canEdit:
raise Exception("unable to store settings in this version")
newKeys=self._getRequestParam(args,'storeKeys')
period=self._getRequestParam(args,'period',raiseMissing=False)
storeTime=self._getRequestParam(args,'storeTime',raiseMissing=False)
sensorNames=self._getRequestParam(args,'sensorNames',raiseMissing=False)
newConfig={'storeKeys':newKeys}
if period is not None:
period=float(period)
if (period < 1 ):
raise Exception("period must be > 1")
newConfig['period']=period
if storeTime is not None:
storeTime=float(storeTime)
if (storeTime < 1):
raise Exception("store time must be > 1")
if sensorNames is not None:
newConfig['sensorNames']=sensorNames
self._changeConfig(newConfig)
return {'status':'OK'}
return {'status','unknown request'}