99 sys .path .insert (0 , os .path .join (here , '..' , '..' ,'libs' ))
1010 sys .path .insert (0 , os .path .join (here , '..' , '..' ,'external_libs' ))
1111
12+ #============================ verify installation =============================
13+
14+ from SmartMeshSDK .utils import SmsdkInstallVerifier
15+ (goodToGo ,reason ) = SmsdkInstallVerifier .verifyComponents (
16+ [
17+ SmsdkInstallVerifier .PYTHON ,
18+ SmsdkInstallVerifier .PYSERIAL ,
19+ ]
20+ )
21+ if not goodToGo :
22+ print "Your installation does not allow this application to run:\n "
23+ print reason
24+ raw_input ("Press any button to exit" )
25+ sys .exit (1 )
26+
1227#============================ imports =========================================
1328
1429# built-in
2439 ConnectionError , \
2540 CommandTimeoutError
2641from SmartMeshSDK .protocols .NetworkHealthAnalyzer import NetworkHealthAnalyzer
42+ from SmartMeshSDK .protocols .Hr import HrParser
2743
2844# DustCli
2945from dustCli import DustCli
3349#============================ globals =========================================
3450
3551connector = None
36- notifThread = None
3752snapshotThread = None
3853
3954#============================ helpers =========================================
40-
55+
4156def printExcAndQuit (err ):
4257
4358 output = []
@@ -56,63 +71,37 @@ def printExcAndQuit(err):
5671
5772#============================ threads =========================================
5873
59- class NotifThread (object ):
74+ class SnapshotThread (threading .Thread ):
75+
76+ SNAPSHOTDELAY_INITIAL_S = 5
77+ DFLT_SNAPSHOTPERIOD = 3600
6078
6179 def __init__ (self ,connector ):
6280
6381 # store params
64- self .connector = connector
82+ self .connector = connector
83+
84+ # local variables
85+ self .goOn = True
86+ self .dataLock = threading .RLock ()
87+ self .delayCounter = self .SNAPSHOTDELAY_INITIAL_S
88+ self .snapshotPeriod = self .DFLT_SNAPSHOTPERIOD
89+ self .networkHealthAnalyzer = NetworkHealthAnalyzer .NetworkHealthAnalyzer ()
90+ self .hrParser = HrParser .HrParser ()
91+ self .lastResults = ""
92+ self .dataForAnalyzer = {}
93+ self .dataForAnalyzer ['devicehr' ] = {}
6594
66- # subscriber
95+ # subscribe
6796 self .subscriber = IpMgrSubscribe .IpMgrSubscribe (self .connector )
6897 self .subscriber .start ()
6998 self .subscriber .subscribe (
7099 notifTypes = [
71100 IpMgrSubscribe .IpMgrSubscribe .NOTIFHEALTHREPORT ,
72101 ],
73102 fun = self ._notifHealthReportCb ,
74- isRlbl = True ,
103+ isRlbl = False ,
75104 )
76- '''
77- self.subscriber.subscribe(
78- notifTypes = [
79- IpMgrSubscribe.IpMgrSubscribe.ERROR,
80- IpMgrSubscribe.IpMgrSubscribe.FINISH,
81- ],
82- fun = self.disconnectedCallback,
83- isRlbl = True,
84- )
85- '''
86-
87- #======================== public ==========================================
88-
89- def disconnect (self ):
90- self .connector .disconnect ()
91-
92- #======================== private =========================================
93-
94- def _notifHealthReportCb (self , notifName , notifParams ):
95- print notifName
96- print notifParams
97- print "TODO _notifHealthReportCb"
98-
99- class SnapshotThread (threading .Thread ):
100-
101- SNAPSHOTDELAY_INITIAL_S = 5
102- DFLT_SNAPSHOTPERIOD = 3600
103-
104- def __init__ (self ,connector ):
105-
106- # store params
107- self .connector = connector
108-
109- # local variables
110- self .goOn = True
111- self .dataLock = threading .RLock ()
112- self .delayCounter = self .SNAPSHOTDELAY_INITIAL_S
113- self .snapshotPeriod = self .DFLT_SNAPSHOTPERIOD
114- self .networkHealthAnalyzer = NetworkHealthAnalyzer .NetworkHealthAnalyzer ()
115- self .lastResults = ""
116105
117106 # initialize parent
118107 threading .Thread .__init__ (self )
@@ -121,8 +110,6 @@ def __init__(self,connector):
121110 # start itself
122111 self .start ()
123112
124- #======================== thread ==========================================
125-
126113 def run (self ):
127114
128115 try :
@@ -174,19 +161,39 @@ def close(self):
174161
175162 with self .dataLock :
176163 self .goOn = False
177-
164+
178165 #======================== private =========================================
179166
167+ def _notifHealthReportCb (self , notifName , notifParams ):
168+
169+ try :
170+ with self .dataLock :
171+
172+ assert notifName == IpMgrSubscribe .IpMgrSubscribe .NOTIFHEALTHREPORT
173+
174+ mac = notifParams .macAddress
175+ hr = self .hrParser .parseHr (notifParams .payload )
176+
177+ if ('Device' in hr ):
178+
179+ self .dataForAnalyzer ['devicehr' ][tuple (mac )] = hr ['Device' ]
180+
181+ except Exception as err :
182+ print type (err )
183+ print err
184+ raise
185+
180186 def _doSnapshot (self ):
181187
182188 try :
183189 with self .dataLock :
184190
185- dataForAnalyzer = {}
186- motes = []
187-
191+ self .dataForAnalyzer ['moteinfo' ] = {}
192+ self .dataForAnalyzer ['networkpaths' ] = {}
193+ self .dataForAnalyzer ['networkinfo' ] = {}
194+ motes = []
195+
188196 # getMoteConfig on all motes
189- dataForAnalyzer ['moteinfo' ] = {}
190197 currentMac = (0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ) # start getMoteConfig() iteration with the 0 MAC address
191198 continueAsking = True
192199 while continueAsking :
@@ -197,16 +204,15 @@ def _doSnapshot(self):
197204 else :
198205 currentMac = res .macAddress
199206 motes += [currentMac ]
200- dataForAnalyzer ['moteinfo' ][tuple (currentMac )] = self ._namedTupleToDict (res )
201-
207+ self . dataForAnalyzer ['moteinfo' ][tuple (currentMac )] = self ._namedTupleToDict (res )
208+
202209 # getMoteInfo on all motes
203210 for mac in motes :
204211 res = self .connector .dn_getMoteInfo (mac )
205212 for (k ,v ) in self ._namedTupleToDict (res ).items ():
206- dataForAnalyzer ['moteinfo' ][tuple (mac )][k ] = v
213+ self . dataForAnalyzer ['moteinfo' ][tuple (mac )][k ] = v
207214
208- # get path info on all paths of all motes
209- dataForAnalyzer ['networkpaths' ] = {}
215+ # getNextPathInfo on all paths of all motes
210216 for mac in motes :
211217 currentPathId = 0
212218 continueAsking = True
@@ -215,16 +221,20 @@ def _doSnapshot(self):
215221 res = self .connector .dn_getNextPathInfo (mac ,0 ,currentPathId )
216222 fromMAC = tuple (res .source )
217223 toMAC = tuple (res .dest )
218- dataForAnalyzer ['networkpaths' ][(fromMAC ,toMAC )] = self ._namedTupleToDict (res )
224+ self . dataForAnalyzer ['networkpaths' ][(fromMAC ,toMAC )] = self ._namedTupleToDict (res )
219225 except APIError :
220226 continueAsking = False
221227 else :
222228 currentPathId = res .pathId
229+
230+ # getNetworkInfo
231+ res = self .connector .dn_getNetworkInfo ()
232+ self .dataForAnalyzer ['networkinfo' ] = self ._namedTupleToDict (res )
223233
224234 print "running test at {0}" .format (self ._now ())
225235
226236 # run NetworkHealthAnalyzer
227- results = self .networkHealthAnalyzer .analyze (dataForAnalyzer )
237+ results = self .networkHealthAnalyzer .analyze (self . dataForAnalyzer )
228238
229239 # format the results
230240 self .lastResults = self ._formatResults (results )
@@ -234,7 +244,7 @@ def _doSnapshot(self):
234244
235245 except Exception as err :
236246 printExcAndQuit (err )
237-
247+
238248 def _formatResults (self ,results ):
239249 output = []
240250 output += ['' ]
@@ -288,17 +298,17 @@ def _namedTupleToDict(self,nt):
288298 def _now (self ):
289299 return time .strftime ("%Y-%m-%d %H:%M:%S" ,time .localtime ())
290300
301+
291302#============================ CLI handlers ====================================
292303
293304def connect_clicb (params ):
294305 global connector
295- global notifThread
296306 global snapshotThread
297307
298308 # filter params
299309 port = params [0 ]
300310
301- # create a coonnector
311+ # create a connector
302312 connector = IpMgrConnectorSerial .IpMgrConnectorSerial ()
303313
304314 # connect to the manager
@@ -310,7 +320,6 @@ def connect_clicb(params):
310320 printExcAndQuit (err )
311321
312322 # start threads
313- notifThread = NotifThread (connector )
314323 snapshotThread = SnapshotThread (connector )
315324
316325def now_clicb (params ):
@@ -339,13 +348,10 @@ def period_clicb(params):
339348
340349def quit_clicb ():
341350 global connector
342- global notifThread
343351 global snapshotThread
344352
345353 if connector :
346354 connector .disconnect ()
347- if notifThread :
348- notifThread .disconnect ()
349355 if snapshotThread :
350356 snapshotThread .close ()
351357
0 commit comments