This repository has been archived by the owner on Dec 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
diagnostic-SK-input.py
485 lines (426 loc) · 14 KB
/
diagnostic-SK-input.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
#!/usr/bin/env python
# This file is part of Openplotter.
# Copyright (C) 2015 by sailoog <https://github.com/sailoog/openplotter>
# e-sailing <https://github.com/e-sailing/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 json
import logging
import subprocess
import threading
import os
import websocket
import wx
import time
from classes.conf import Conf
from classes.language import Language
class MyFrame(wx.Frame):
def __init__(self):
self.SK_unit = ''
self.SK_description = ''
self.SK_unit_priv = 0
self.SK_Faktor_priv = 1
self.SK_Offset_priv = 0
self.ws = None
self.thread = threading._DummyThread
self.private_unit_s = 1
logging.basicConfig()
self.buffer = []
self.list_SK = []
self.list_SK_unit = []
self.sortCol = 0
self.conf = Conf()
self.home = self.conf.home
self.currentpath = self.home+self.conf.get('GENERAL', 'op_folder')+'/openplotter'
Language(self.conf)
wx.Frame.__init__(self, None, title='diagnostic SignalK input', size=(670, 435))
self.Bind(wx.EVT_CLOSE, self.OnClose)
panel = wx.Panel(self, wx.ID_ANY)
self.ttimer = 100
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.timer_act, self.timer)
self.SetFont(wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
self.icon = wx.Icon(self.currentpath + '/openplotter.ico', wx.BITMAP_TYPE_ICO)
self.SetIcon(self.icon)
self.list = wx.ListCtrl(panel, -1, style=wx.LC_REPORT | wx.SUNKEN_BORDER)
self.list.InsertColumn(0, _('SRC'), width=245)
self.list.InsertColumn(1, _('SignalK'), width=300)
self.list.InsertColumn(2, _('Value'), wx.LIST_FORMAT_RIGHT, width=100)
self.list.InsertColumn(3, _('Unit'), width=45)
self.list.InsertColumn(4, _('Interval'), wx.LIST_FORMAT_RIGHT, width=55)
self.list.InsertColumn(5, _('Status'), width=50)
self.list.InsertColumn(6, _('Description'), width=500)
sort_SRC = wx.Button(panel, label=_('Sort SRC'))
sort_SRC.Bind(wx.EVT_BUTTON, self.on_sort_SRC)
sort_SK = wx.Button(panel, label=_('Sort SK'))
sort_SK.Bind(wx.EVT_BUTTON, self.on_sort_SK)
self.private_unit = wx.CheckBox(panel, label=_('private Unit'), pos=(360, 32))
self.private_unit.Bind(wx.EVT_CHECKBOX, self.on_private_unit)
self.private_unit.SetValue(self.private_unit_s)
unit_setting = wx.Button(panel, label=_('Unit Setting'))
unit_setting.Bind(wx.EVT_BUTTON, self.on_unit_setting)
vbox = wx.BoxSizer(wx.VERTICAL)
hlistbox = wx.BoxSizer(wx.HORIZONTAL)
hbox = wx.BoxSizer(wx.HORIZONTAL)
hlistbox.Add(self.list, 1, wx.ALL | wx.EXPAND, 5)
hbox.Add(sort_SRC, 0, wx.RIGHT | wx.LEFT, 5)
hbox.Add(sort_SK, 0, wx.RIGHT | wx.LEFT, 5)
hbox.Add((0,0), 1, wx.RIGHT | wx.LEFT, 5)
hbox.Add(self.private_unit, 0, wx.RIGHT | wx.LEFT, 5)
hbox.Add(unit_setting, 0, wx.RIGHT | wx.LEFT, 5)
vbox.Add(hlistbox, 1, wx.ALL | wx.EXPAND, 0)
vbox.Add(hbox, 0, wx.ALL | wx.EXPAND, 0)
panel.SetSizer(vbox)
self.CreateStatusBar()
self.read()
self.start()
self.Show(True)
self.status = ''
self.data = []
self.baudc = 0
self.baud = 0
self.timer.Start(self.ttimer)
self.no_action = 0
self.no_action_limit = 5000 / self.ttimer
def timer_act(self, e):
if len(self.buffer) > 0:
self.no_action = 0
for ii in self.buffer:
if 0 <= ii[0] < self.list.GetItemCount():
self.list.SetStringItem(ii[0], ii[1], ii[2])
else:
self.sorting()
self.buffer = []
else:
self.no_action += 1
if self.no_action > self.no_action_limit:
if self.ws:
self.ws.close()
self.start()
self.no_action = 0
def json_interval(self, time_old, time_new):
sek_n = float(time_new[17:22])
sek_o = float(time_old[17:22])
if sek_n >= sek_o:
dif = sek_n - sek_o
else:
dif = sek_n + 60 - sek_o
return dif
def read(self):
self.list_SK_unit = []
try:
with open(self.home+'/.config/signalk-server-node/node_modules/@signalk/signalk-schema/dist/keyswithmetadata.json') as data_file:
data = json.load(data_file)
except:
#old signalk
with open(self.home+'/.config/signalk-server-node/node_modules/@signalk/signalk-schema/src/keyswithmetadata.json') as data_file:
data = json.load(data_file)
data_sk_unit_private = []
if os.path.isfile(self.home+'/.openplotter/private_unit.json'):
with open(self.home+'/.openplotter/private_unit.json') as data_file:
data_sk_unit_private = json.load(data_file)
for i in data:
if 'units' in data[i].keys():
if 'description' in data[i].keys():
ii = i.replace('/vessels/*/','')
ii = ii.replace('RegExp','*')
ii = ii.replace('/','.')
self.list_SK_unit.append([str(ii), str(data[i]['units']), '', str(data[i]['description'])])
else:
self.list_SK_unit.append([str(ii), str(data[i]['units']), '', ''])
for j in data_sk_unit_private:
for i in self.list_SK_unit:
if j[0] == i[0]:
i[2] = j[2]
break
self.list_SK_unit.sort(key=lambda tup: tup[0])
self.list_SK_unit.sort(key=lambda tup: tup[1])
def lookup_star(self, name):
skip = -1
index = 0
st = ''
for i in name.split('.'):
if index > -1:
if skip == 0:
st += '.*'
else:
if i in ['propulsion', 'sensors']:
skip = 1
elif i in ['electrical', 'registrations','tanks']:
skip = 2
st += '.' + i
index += 1
skip -= 1
st = st[1:]
self.SK_unit = ''
self.SK_unit_priv = ''
self.SK_description = ''
for j in self.list_SK_unit:
exist = False
if j[0] == st:
exist = True
self.SK_unit = j[1]
self.SK_description = j[3]
if j[2] != '':
self.SK_unit_priv = j[2]
else:
self.SK_unit_priv = j[1]
break
if not exist:
print 'no unit for ', st
self.SK_Faktor_priv = 1
self.SK_Offset_priv = 0
if self.SK_unit_priv != self.SK_unit:
if self.SK_unit == 'Hz':
if self.SK_unit_priv == 'RPM':
self.SK_Faktor_priv = 0.0166666666
elif self.SK_unit == 'K':
if self.SK_unit_priv == 'C':
self.SK_Offset_priv = -273.15
elif self.SK_unit_priv == 'F':
self.SK_Faktor_priv = 1.8
self.SK_Offset_priv = -459.67
elif self.SK_unit == 'J':
if self.SK_unit_priv == 'Ah(12V)':
self.SK_Faktor_priv = 43200.
if self.SK_unit_priv == 'Ah(24V)':
self.SK_Faktor_priv = 86400
elif self.SK_unit == 'm':
if self.SK_unit_priv == 'ft':
self.SK_Faktor_priv = 0.3048
elif self.SK_unit_priv == 'nm':
self.SK_Faktor_priv = 1852
elif self.SK_unit_priv == 'km':
self.SK_Faktor_priv = 1000
elif self.SK_unit == 'm/s':
if self.SK_unit_priv == 'kn':
self.SK_Faktor_priv = 0.514444444
elif self.SK_unit_priv == 'kmh':
self.SK_Faktor_priv = 0, 277778
elif self.SK_unit_priv == 'mph':
self.SK_Faktor_priv = 0.44704
elif self.SK_unit == 'm3':
if self.SK_unit_priv == 'l':
self.SK_Faktor_priv = 0.001
elif self.SK_unit_priv == 'gal':
self.SK_Faktor_priv = 0.00378541
elif self.SK_unit == 'm3/s':
if self.SK_unit_priv == 'l/h':
self.SK_Faktor_priv = 2.777778E-7
elif self.SK_unit_priv == 'gal/h':
self.SK_Faktor_priv = 0.0000010515
elif self.SK_unit == 'Pa':
if self.SK_unit_priv == 'hPa':
self.SK_Faktor_priv = 100
elif self.SK_unit_priv == 'Bar':
self.SK_Faktor_priv = 100000
elif self.SK_unit == 'rad' and self.SK_unit_priv == 'deg':
self.SK_Faktor_priv = 0.0174533
elif self.SK_unit == 's':
if self.SK_unit_priv == 'h':
self.SK_Faktor_priv = 3600
elif self.SK_unit_priv == 'd':
self.SK_Faktor_priv = 86400
elif self.SK_unit_priv == 'y':
self.SK_Faktor_priv = 31536000
elif self.SK_unit == 'ratio':
if self.SK_unit_priv == '%':
self.SK_Faktor_priv = 0.01
else:
self.SK_Faktor_priv = 1
self.SK_Offset_priv = 0
def on_sort_SRC(self, e):
self.sortCol = 0
self.sorting()
def on_sort_SK(self, e):
self.sortCol = 1
self.sorting()
def sorting(self):
self.list.DeleteAllItems()
list_new = []
for i in sorted(self.list_SK, key=lambda item: (item[self.sortCol])):
list_new.append(i)
self.list_SK = list_new
self.init2()
def init2(self):
index = 0
for i in self.list_SK:
if type(i[2]) is float:
pass
elif type(i[2]) is str:
pass
else:
i[2] = 0.0
self.list.InsertStringItem(index, str(i[0]))
self.list.SetStringItem(index, 1, str(i[1]))
if not self.private_unit_s:
if type(i[2]) is str:
self.buffer.append([index, 2, i[2]])
else:
self.buffer.append([index, 2, str('%.3f' % i[2])])
self.buffer.append([index, 3, i[3]])
else:
if type(i[2]) is str:
self.buffer.append([index, 2, i[2]])
else:
i[9] = i[2] / i[10] + i[11]
self.buffer.append([index, 2, str('%.3f' % i[9])])
self.buffer.append([index, 3, i[8]])
self.list.SetStringItem(index, 4, str('%.1f' % i[4]))
self.list.SetStringItem(index, 5, str(i[5]))
self.list.SetStringItem(index, 6, str(i[6]))
index += 1
def on_unit_setting(self, e):
subprocess.Popen(['python', self.currentpath + '/unit-private.py'])
def OnClose(self, e):
self.endlive=True
qx=0
while not self.ende and qx<50:
time.sleep(0.1)
qx+=1
if self.ws:
self.ws.close()
self.timer.Stop()
self.Destroy()
def on_message(self, ws, message):
if self.endlive:
self.on_close(ws)
self.ende=True
return
try:
js_up = json.loads(message)['updates'][0]
except:
return
label = ''
src = ''
if '$source' in js_up:
src = js_up['$source']
elif 'source' in js_up:
label = js_up['source']['label']
src = label
if 'type' in js_up['source']:
if js_up['source']['type'] == 'NMEA0183':
if 'talker' in js_up['source']: src +='.'+js_up['source']['talker']
if 'sentence' in js_up['source']: src +='.'+js_up['source']['sentence']
elif js_up['source']['type'] == 'NMEA2000':
if 'src' in js_up['source']: src +='.'+js_up['source']['src']
if 'pgn' in js_up['source']: src +='.'+str(js_up['source']['pgn'])
try:
timestamp = js_up['timestamp']
except:
timestamp = '2000-01-01T00:00:00.000Z'
values_ = js_up['values']
for values in values_:
path = values['path']
value = values['value']
src2 = src
timestamp2 = timestamp
if type(value) is dict:
if 'timestamp' in value: timestamp2 = value['timestamp']
if '$source' in value:
src2 = value['$source']
elif 'source' in value:
src2 = label
if 'type' in value['source']:
if value['source']['type'] == 'NMEA0183':
if 'talker' in value['source']: src2 +='.'+value['source']['talker']
if 'sentence' in value['source']: src2 +='.'+value['source']['sentence']
elif value['source']['type'] == 'NMEA2000':
if 'src' in value['source']: src2 +='.'+value['source']['src']
if 'pgn' in value['source']: src2 +='.'+str(value['source']['pgn'])
for lvalue in value:
result = True
if lvalue in ['source', '$source']:
result = False
elif lvalue == 'timestamp':
if 'position' in path and 'RMC' in src2:
path2 = 'navigation.datetime'
value2 = timestamp2
self.update_add(value2, path2, src2, timestamp2)
result = False
if result:
path2 = path + '.' + lvalue
value2 = value[lvalue]
self.update_add(value2, path2, src2, timestamp2)
else:
self.update_add(value, path, src, timestamp)
def update_add(self, value, path, src, timestamp):
# SRC SignalK Value Unit Interval Status Description timestamp private_Unit private_Value priv_Faktor priv_Offset
# 0 1 2 3 4 5 6 7 8 9 10 11
if type(value) is list: value = value[0]
if type(value) is float: pass
elif type(value) is unicode: value = str(value)
elif type(value) is int: value = float(value)
else: value=0.0
index = 0
exists = False
for i in self.list_SK:
if path == i[1] and i[0] == src:
exists = True
i[0] = src
i[2] = value
if i[4] == 0.0:
i[4] = self.json_interval(i[7], timestamp)
else:
i[4] = i[4] * .8 + 0.2 * self.json_interval(i[7], timestamp)
i[7] = timestamp
self.buffer.append([index, 0, i[0]])
self.buffer.append([index, 4, str('%.2f' % i[4])])
if type(i[2]) is str:
self.buffer.append([index, 2, i[2]])
self.buffer.append([index, 3, i[3]])
break
elif type(i[2]) is float:
pass
else:
i[2] = 0.0
if not self.private_unit_s:
self.buffer.append([index, 2, str('%.3f' % i[2])])
self.buffer.append([index, 3, i[3]])
else:
i[9] = i[2] / i[10] + i[11]
self.buffer.append([index, 2, str('%.3f' % i[9])])
self.buffer.append([index, 3, i[8]])
break
index += 1
if not exists:
self.lookup_star(path)
self.list_SK.append(
[src, path, value, str(self.SK_unit), 0.0, 1, self.SK_description, timestamp, str(self.SK_unit_priv), 0,
self.SK_Faktor_priv, self.SK_Offset_priv])
self.buffer.append([-1, 0, ''])
def on_private_unit(self, e):
self.private_unit_s = self.private_unit.GetValue()
def on_error(self, ws, error):
print error
def on_close(self, ws):
ws.close()
def on_open(self, ws):
pass
def run(self):
self.endlive=False
self.ende=False
self.ws = websocket.WebSocketApp("ws://localhost:3000/signalk/v1/stream?subscribe=self",
on_message=lambda ws, msg: self.on_message(ws, msg),
on_error=lambda ws, err: self.on_error(ws, err),
on_close=lambda ws: self.on_close(ws))
self.ws.on_open = lambda ws: self.on_open(ws)
self.ws.run_forever()
self.ws = None
def start(self):
def run():
self.run()
self.thread = threading.Thread(target=run)
self.thread.start()
app = wx.App()
MyFrame().Show()
app.MainLoop()