-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolmanager.py
817 lines (684 loc) · 27.9 KB
/
solmanager.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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
#!/usr/bin/python
__version__ = (2, 3, 0, 0)
# =========================== adjust path =====================================
import sys
import os
if __name__ == "__main__":
here = sys.path[0]
sys.path.insert(0, os.path.join(here, 'libs', 'sol-REL-1.7.5.0'))
sys.path.insert(0, os.path.join(here, 'libs', 'smartmeshsdk-REL-1.3.0.1', 'libs'))
sys.path.insert(0, os.path.join(here, 'libs', 'duplex-REL-1.1.0.0'))
# =========================== imports =========================================
# from default Python
import time
import json
import threading
import logging.config
import base64
import traceback
import argparse
import subprocess
import platform
# project-specific
from SmartMeshSDK import sdk_version, \
ApiException
from SmartMeshSDK.utils import JsonManager, \
FormatUtils
from dustCli import DustCli
from sensorobjectlibrary import Sol as sol, \
SolDefines, \
SolUtils
from DuplexClient import DuplexClient
# =========================== logging =========================================
logging.config.fileConfig('logging.conf', disable_existing_loggers=False)
log = logging.getLogger("solmanager")
# =========================== defines =========================================
DFLT_CONFIGFILE = 'solmanager.config'
STATSFILE = 'solmanager.stats'
BACKUPFILE = 'solmanager.backup'
ALLSTATS = [
#== admin
'ADM_NUM_CRASHES',
#== notifications from manager
# note: we count the number of notifications form the manager, for each time, e.g. NUMRX_NOTIFDATA
# all stats start with "NUMRX_"
#== publication
'PUB_TOTAL_SENTTOPUBLISH',
# to file
'PUBFILE_PUBBINARY',
'PUBFILE_BACKLOG',
'PUBFILE_WRITES',
# to server
'PUBSERVER_PUBBINARY',
'PUBSERVER_PUBJSON',
'PUBSERVER_FROMSERVER',
]
# =========================== helpers =========================================
def get_stats():
versions = get_versions()
stats = {
'solmanager_version': versions["SolManager"],
'sol_version': versions["Sol"],
'sdk_version': versions["SmartMesh SDK"],
'ram_usage': get_ram_usage(),
'disk_usage': get_disk_usage(),
}
return stats
def get_ram_usage():
"""Returns the percentage of used memory"""
out = subprocess.Popen(['free', '-m'],
stdout=subprocess.PIPE
).communicate()[0].split(b'\n')
total_index = out[0].split().index(b'total') + 1
avail_index = out[0].split().index(b'available') + 1
usage = 100 * float(out[1].split()[avail_index]) / float(out[1].split()[total_index])
return int(round(usage))
def get_disk_usage():
"""Returns the percentage of used disk space"""
out = subprocess.Popen(['df', '-h', '/'],
stdout=subprocess.PIPE
).communicate()[0].split(b'\n')
use_index = out[0].split().index(b'Use%')
usage = int(out[1].split()[use_index].replace('%', ''))
return usage
def get_versions():
return {
'SolManager' : list(__version__),
'Sol' : list(sol.version()),
'SmartMesh SDK' : list(sdk_version.VERSION),
}
# =========================== classes =========================================
class Tracer(object):
"""
Singleton that writes trace to CLI
"""
_instance = None
_init = False
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(Tracer, cls).__new__(cls, *args, **kwargs)
return cls._instance
def __init__(self):
if self._init:
return
self._init = True
self.dataLock = threading.RLock()
self.traceOn = False
#======================== public ==========================================
def setTraceOn(self,newTraceOn):
assert newTraceOn in [True,False]
with self.dataLock:
self.traceOn = newTraceOn
def trace(self,msg):
with self.dataLock:
go = self.traceOn
if go:
print msg
# ======= generic abstract classes
class DoSomethingPeriodic(threading.Thread):
"""
Abstract DoSomethingPeriodic thread
"""
def __init__(self, periodvariable):
self.goOn = True
# start the thread
threading.Thread.__init__(self)
self.name = 'DoSomethingPeriodic'
self.daemon = True
self.periodvariable = periodvariable*60
self.currentDelay = 0
def run(self):
try:
self.currentDelay = 5
while self.goOn:
self.currentDelay -= 1
if self.currentDelay == 0:
self._doSomething()
self.currentDelay = self.periodvariable
time.sleep(1)
except Exception as err:
SolUtils.logCrash(err, SolUtils.AppStats(), threadName=self.name)
def close(self):
self.goOn = False
def _doSomething(self):
raise SystemError() # abstract method
# ======= connecting to the SmartMesh IP manager
class MgrThread(object):
"""
Thread to start the connection with the Dust Manager using the JsonManager
"""
def __init__(self):
# local variables
self.macManager = None
self.dataLock = threading.RLock()
# initialize JsonManager
self.jsonManager = JsonManager.JsonManager(
autoaddmgr = False,
autodeletemgr = False,
serialport = SolUtils.AppConfig().get("serialport"),
notifCb = self._notif_cb,
)
# todo replace this by JsonManager method to know when a manager is ready
while self.jsonManager.managerHandlers == {}:
time.sleep(1)
while self.jsonManager.managerHandlers[self.jsonManager.managerHandlers.keys()[0]].connector is None:
time.sleep(1)
# record the manager's MAC address
while self.macManager is None:
try:
self.macManager = self.get_mac_manager()
except ApiException.ConnectionError as err:
log.warn(err)
time.sleep(1)
log.debug("Connected to manager {0}".format(self.macManager))
# ======================= public ==========================================
def get_mac_manager(self):
if self.macManager is None:
resp = self.jsonManager.raw_POST(
manager = 0,
commandArray = ["getMoteConfig"],
fields = {
"macAddress": [0, 0, 0, 0, 0, 0, 0, 0],
"next": True
},
)
assert resp['isAP'] is True
self.macManager = FormatUtils.formatBuffer(resp['macAddress'])
return self.macManager
def from_server_cb_MgrThread(self,o):
try:
if o['command']=='JsonManager':
'''
o = {
'type': 'manager',
'id': '00-17-0d-00-00-30-3c-03',
'format': 'json',
'command': 'JsonManager',
'timestamp': '2018-01-30 15:55:12.056165+00:00',
'data': {
'function': 'status_GET',
'args': {},
'token': 'myToken',
}
}
'''
assert o['type']=='manager'
assert o['format']=='json'
try:
assert o['data']['function'].split('_')[-1] in ['GET','PUT','POST','DELETE']
# find the function to call
func = getattr(self.jsonManager,o['data']['function'])
# call the function
res = func(**o['data']['args'])
except Exception as err:
value = {
'success': False,
'return': str(err),
}
else:
value = {
'success': True,
'return': res,
}
finally:
if 'token' in o['data']:
value['token'] = o['data']['token']
json_res = {
'type': 'JsonManagerResponse',
'mac': o['id'],
'manager': self.macManager,
'value': value,
}
PubServer().publishJson(json_res)
elif o['command']=='oap':
'''
o = {
'type': 'mote',
'id': '00-17-0d-00-00-38-03-69',
'format': 'json',
'command': 'oap',
'timestamp': '2018-01-30 15:55:12.056165+00:00',
'data': {
'function': 'digital_out_PUT',
'args': {
"pin" : 2,
"body": {
"value": 1
}
},
'token': 'myToken',
}
}
'''
assert o['type']=='mote'
assert o['format']=='json'
try:
assert o['data']['function'].split('_')[-1] in ['GET','PUT','POST','DELETE']
# find the function to call
func = getattr(self.jsonManager,'oap_{0}'.format(o['data']['function']))
# format the args
args = o['data']['args']
args['mac'] = o['id']
# call the function
res = func(**args)
except NameError:
value = {
'success': False,
'error': 'timeout',
}
except Exception as err:
value = {
'success': False,
'error': str(err),
}
else:
value = {
'success': True,
'return': res,
}
finally:
if 'token' in o['data']:
value['token'] = o['data']['token']
json_res = {
'type': 'oapResponse',
'mac': o['id'],
'manager': self.macManager,
'value': value,
}
PubServer().publishJson(json_res)
except Exception as err:
log.error("could not execute {0}: {1}".format(o,traceback.format_exc()))
def close(self):
pass
# ======================= private =========================================
def _notif_cb(self, notifName, notifJson):
self._handler_dust_notifs(
notifJson,
notifName
)
def _handler_dust_notifs(self, dust_notif, notif_name=""):
if (notif_name!="") and ('name' not in dust_notif):
dust_notif['name'] = notif_name
elif (notif_name=="") and ('name' not in dust_notif):
logging.warning("Cannot find notification name")
return
try:
# trace
Tracer().trace('from manager: {0}'.format(dust_notif['name']))
# filter raw HealthReport notifications
if dust_notif['name'] == "notifHealthReport":
return
# change "manager" field of snaphots (for stars to display correctly)
if dust_notif['name'] == "snapshot":
dust_notif['manager'] = self.get_mac_manager()
# update stats
SolUtils.AppStats().increment('NUMRX_{0}'.format(dust_notif['name']))
# get time
epoch = None
if hasattr(dust_notif, "utcSecs") and hasattr(dust_notif, "utcUsecs"):
netTs = self._calcNetTs(dust_notif)
epoch = self._netTsToEpoch(netTs)
# convert dust notification to JSON SOL Object
sol_jsonl = sol.dust_to_json(
dust_notif = dust_notif,
mac_manager = self.get_mac_manager(),
timestamp = epoch,
)
for sol_json in sol_jsonl:
# publish
PubFile().publishBinary(sol_json) # to the backup file
PubServer().publishBinary(sol_json) # to the solserver over the Internet
except Exception as err:
SolUtils.logCrash(err, SolUtils.AppStats())
# === misc
def _calcNetTs(self, notif):
return int(float(notif.utcSecs) + float(notif.utcUsecs / 1000000.0))
def _syncNetTsToUtc(self, netTs):
with self.dataLock:
self.tsDiff = time.time() - netTs
def _netTsToEpoch(self, netTs):
with self.dataLock:
return int(netTs + self.tsDiff)
# ======= publishers
class Pub(object):
"""
Abstract publish thread.
"""
def __init__(self):
self.dataLock = threading.RLock()
def publishBinary(self, o):
raise SystemError("abstract method")
def publishJson(self, o):
raise SystemError("abstract method")
class PubFile(Pub,DoSomethingPeriodic):
"""
Singleton that writes Sol JSON objects to a file every period_pubfile_min.
"""
_instance = None
_init = False
# we buffer objects for BUFFER_PERIOD second to ensure they are written to
# file chronologically
BUFFER_PERIOD = 30
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(PubFile, cls).__new__(cls, *args, **kwargs)
return cls._instance
def __init__(self):
if self._init:
return
self._init = True
self.toPublishBinary = []
# initialize parent classes
Pub.__init__(self)
DoSomethingPeriodic.__init__(self, SolUtils.AppConfig().get("period_pubfile_min"))
self.name = 'PubFile'
self.start()
#======================== public ==========================================
def publishBinary(self, o):
# update stats
SolUtils.AppStats().increment('PUBFILE_PUBBINARY')
with self.dataLock:
self.toPublishBinary += [o]
# update stats
SolUtils.AppStats().update("PUBFILE_BACKLOG", len(self.toPublishBinary))
def publishJson(self, o):
raise SystemError('publishJson not supported in PubFile')
def getBacklogLength(self):
with self.dataLock:
return len(self.toPublishBinary)
#======================== private =========================================
def _doSomething(self):
self._publishNow()
def _publishNow(self):
# update stats
SolUtils.AppStats().increment('PUBFILE_WRITES')
# trace
Tracer().trace('write to backup file')
with self.dataLock:
# order toPublishBinary chronologically
self.toPublishBinary.sort(key=lambda i: i['timestamp'])
# extract the JSON SOL objects heard more than BUFFER_PERIOD ago
now = time.time()
solJsonObjectsToWrite = []
while True:
if not self.toPublishBinary:
break
if now-self.toPublishBinary[0]['timestamp'] < self.BUFFER_PERIOD:
break
solJsonObjectsToWrite += [self.toPublishBinary.pop(0)]
# update stats
SolUtils.AppStats().update("PUBFILE_BACKLOG", len(self.toPublishBinary))
# write those to file
if solJsonObjectsToWrite:
sol.dumpToFile(
solJsonObjectsToWrite,
BACKUPFILE,
)
class PubServer(Pub):
"""
Singleton that sends objects to the solserver.
"""
_instance = None
_init = False
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(PubServer, cls).__new__(cls, *args, **kwargs)
return cls._instance
def __init__(self):
if self._init:
return
self._init = True
Pub.__init__(self)
self.name = 'PubServer'
self.duplex_client = None
#======================== public ==========================================
def setDuplexClient(self, duplex_client):
with self.dataLock:
self.duplex_client = duplex_client
def publishBinary(self, o):
# stop if duplex_client not configured yet
with self.dataLock:
if not self.duplex_client:
return
# update stats
SolUtils.AppStats().increment('PUBSERVER_PUBBINARY')
# convert objects and push to duplex_client
o = sol.json_to_bin(o)
o = base64.b64encode(''.join(chr(b) for b in o))
o = json.dumps(['b',o])
log.debug("sending binary object, size: {0} B".format(len(o)))
self.duplex_client.to_server(o)
def publishJson(self, o):
# stop if duplex_client not configured yet
with self.dataLock:
if not self.duplex_client:
return
# update stats
SolUtils.AppStats().increment('PUBSERVER_PUBJSON')
# convert objects and push to duplex_client
o = json.dumps(['j',o])
log.debug("sending json object, size: {0} B".format(len(o)))
self.duplex_client.to_server(o)
# ======= periodically do something
class SolSnapshotThread(DoSomethingPeriodic):
def __init__(self, mgrThread=None):
assert mgrThread
# store params
self.mgrThread = mgrThread
# initialize parent class
super(SolSnapshotThread, self).__init__(SolUtils.AppConfig().get("period_snapshot_min"))
self.name = 'SolSnapshotThread'
self.start()
# initialize local attributes
self.last_snapshot = None
def _doSomething(self):
self._doSnapshot()
def _doSnapshot(self):
# trace
Tracer().trace('trigger snapshot')
ret = self.mgrThread.jsonManager.snapshot_POST(manager=0)
class StatsThread(DoSomethingPeriodic):
"""
Publish application statistics every period_stats_min.
"""
def __init__(self, mgrThread):
# store params
self.mgrThread = mgrThread
# initialize parent class
super(StatsThread, self).__init__(SolUtils.AppConfig().get("period_stats_min"))
self.name = 'StatsThread'
self.start()
def _doSomething(self):
if platform.system() == "Linux": # TODO get_stats does not work on windows
# trace
Tracer().trace('collect statistics')
# create sensor object
sobject = {
'mac': self.mgrThread.get_mac_manager(),
'timestamp': int(time.time()),
'type': SolDefines.SOL_TYPE_SOLMANAGER_STATS_2,
'value': get_stats(),
}
# publish
PubFile().publishBinary(sobject)
PubServer().publishBinary(sobject)
# ======= main application thread
class SolManager(threading.Thread):
def __init__(self, configfile):
# store params
self.configfile = configfile
# local variables
self.goOn = True
self.threads = {
"mgrThread" : None,
"pubFile" : None,
"pubServer" : None,
"solSnapshotThread" : None,
"statsThread" : None,
"pollForCommandsThread" : None,
}
self.duplex_client = None
# init Singletons
SolUtils.AppConfig(config_file=self.configfile)
SolUtils.AppStats(stats_file=STATSFILE, stats_list=ALLSTATS)
# CLI interface
self.cli = DustCli.DustCli(
appName = "SolManager",
quit_cb = self._clihandle_quit,
versions = get_versions(),
)
self.cli.registerCommand(
name = 'trace',
alias = 't',
description = 'switch trace on/off',
params = ["state",],
callback = self._clihandle_trace,
)
self.cli.registerCommand(
name = 'stats',
alias = 's',
description = 'print the stats',
params = [],
callback = self._clihandle_stats,
)
self.cli.registerCommand(
name = 'versions',
alias = 'v',
description = 'print the versions of the different components',
params = [],
callback = self._clihandle_versions,
)
# start myself
threading.Thread.__init__(self)
self.name = 'SolManager'
self.daemon = True
self.start()
def run(self):
try:
# start manager thread
self.threads["mgrThread"] = MgrThread()
# start the duplexClient
self.duplex_client = DuplexClient.from_url(
server_url = 'http://{0}/api/v1/o.json'.format(SolUtils.AppConfig().get("solserver_host")),
id = self.threads["mgrThread"].get_mac_manager(),
token = SolUtils.AppConfig().get("solserver_token"),
polling_period = SolUtils.AppConfig().get("period_pollserver_min")*60,
from_server_cb = self.from_server_cb_JsonManager,
buffer_tx = False,
)
while self.duplex_client is None:
log.warning("Waiting for duplex client to be started")
time.sleep(1)
log.debug("duplex client started")
# start the all other threads
self.threads["pubFile"] = PubFile()
self.threads["pubServer"] = PubServer()
self.threads["pubServer"].setDuplexClient(self.duplex_client)
self.threads["solSnapshotThread"] = SolSnapshotThread(
mgrThread=self.threads["mgrThread"],
)
self.threads["statsThread"] = StatsThread(
mgrThread=self.threads["mgrThread"],
)
# wait for all threads to have started
all_started = False
while not all_started and self.goOn:
all_started = True
for t in self.threads.itervalues():
try:
if not t.isAlive():
all_started = False
log.info("Waiting for %s to start", t.name)
except AttributeError:
pass # happens when not a real thread
time.sleep(5)
log.info("All threads started")
# return as soon as one thread not alive
while self.goOn:
# verify that all threads are running
all_running = True
for t in self.threads.itervalues():
try:
if not t.isAlive():
all_running = False
log.debug("Thread {0} is not running. Quitting.".format(t.name))
except AttributeError:
pass # happens when not a real thread
if not all_running:
self.goOn = False
time.sleep(5)
except Exception as err:
SolUtils.logCrash(err, SolUtils.AppStats(), threadName=self.name)
self.close()
def close(self):
os._exit(0) # bypass CLI thread
def _clihandle_quit(self):
time.sleep(.3)
print "bye bye."
# all threads as daemonic, will close automatically
def _clihandle_trace(self, params):
if params[0]=='on':
Tracer().setTraceOn(True)
print 'trace on'
else:
Tracer().setTraceOn(False)
print 'trace off'
def _clihandle_stats(self, params):
stats = SolUtils.AppStats().get()
output = []
output += ['#== admin']
output += self._returnStatsGroup(stats, 'ADM_')
output += ['#== notifications from manager']
output += self._returnStatsGroup(stats, 'NUMRX_')
output += ['#== publication']
output += self._returnStatsGroup(stats, 'PUB_')
output += ['# to file']
output += self._returnStatsGroup(stats, 'PUBFILE_')
output += ['# to server']
output += self._returnStatsGroup(stats, 'PUBSERVER_')
output = '\n'.join(output)
print output
def _clihandle_versions(self, params):
output = []
for (k,v) in get_versions().items():
output += ["{0:>15} {1}".format(k, '.'.join([str(b) for b in v]))]
output = '\n'.join(output)
print output
def _clihandle_tx(self, params):
msg = params[0]
self.duplex_client.to_server([{'msg': msg}])
def _returnStatsGroup(self, stats, prefix):
keys = []
for (k, v) in stats.items():
if k.startswith(prefix):
keys += [k]
returnVal = []
for k in sorted(keys):
returnVal += [' {0:<30}: {1}'.format(k, stats[k])]
return returnVal
def from_server_cb_JsonManager(self, os):
# update stats
SolUtils.AppStats().increment('PUBSERVER_FROMSERVER')
log.debug("from_server_cb_JsonManager: {0}".format(os))
for o in os:
try:
name = '{0}.{1}.{2}'.format(
o['id'],
o['command'],
o['data']['function'],
)
except:
name = 'from_server'
threading.Thread(
target = self.threads["mgrThread"].from_server_cb_MgrThread,
args = (o,),
name = name,
).start()
# =========================== main ============================================
def main(args):
SolManager(**args)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--configfile', default=DFLT_CONFIGFILE)
args = vars(parser.parse_args())
main(args)