30
30
from SmartMeshSDK import sdk_version
31
31
32
32
#============================ defines =========================================
33
- BCAST_CNT = 2
34
- LOGFILE = "OapClient_log.txt"
33
+
34
+ NUM_BCAST_TO_SEND = 2
35
+ LOGFILE = "OapClient_log.txt"
35
36
36
37
#============================ globals =========================================
37
38
38
39
#============================ helpers =========================================
39
40
40
41
def printExcAndQuit (err ):
41
-
42
42
output = []
43
43
output += ["=" * 30 ]
44
44
output += ["error" ]
@@ -80,19 +80,25 @@ def getOperationalMotes():
80
80
AppData ().get ('oap_dispatch' ),
81
81
)
82
82
83
+ return len (operationalmotes )
84
+
83
85
def printOperationalMotes ():
86
+
84
87
output = []
85
- output += ["\n Operational motes:" ]
88
+ output += ["{0} operational motes:" . format ( len ( AppData (). get ( 'operationalmotes' ))) ]
86
89
for (i ,m ) in enumerate (AppData ().get ('operationalmotes' )):
87
90
output += ['{0}: {1}' .format (i ,FormatUtils .formatMacString (m ))]
88
91
output = '\n ' .join (output )
89
92
90
93
print output
91
94
92
- def selectOperationalMotes (moteNum ):
95
+ def selectOperationalMote (moteNum ):
93
96
94
97
if moteNum > len (AppData ().get ('operationalmotes' )):
95
- print 'Cannot select mote {0}, out of range' .format (moteNum )
98
+ print 'Cannot select mote {0}, there are only {1} motes' .format (
99
+ moteNum ,
100
+ len (AppData ().get ('operationalmotes' )),
101
+ )
96
102
return
97
103
98
104
AppData ().set ('currentmote' ,moteNum )
@@ -103,6 +109,7 @@ def selectOperationalMotes(moteNum):
103
109
)
104
110
105
111
def togglePrintNotifs ():
112
+
106
113
if AppData ().get ('printNotifs' )== False :
107
114
AppData ().set ('printNotifs' ,True )
108
115
print "notifications are ON."
@@ -111,6 +118,7 @@ def togglePrintNotifs():
111
118
print "notifications are OFF."
112
119
113
120
def toggleLogNotifs ():
121
+
114
122
if AppData ().get ('logNotifs' )== False :
115
123
AppData ().set ('logNotifs' ,True )
116
124
print "logging to logfile is ON."
@@ -143,6 +151,9 @@ def set(self,k,v):
143
151
def get (self ,k ):
144
152
with self .dataLock :
145
153
return self .data [k ]
154
+ def delete (self ,k ):
155
+ with self .dataLock :
156
+ del self .data [k ]
146
157
147
158
class Manager (object ):
148
159
@@ -165,9 +176,10 @@ def __init__(self):
165
176
166
177
# list operational motes
167
178
AppData ().set ('oap_clients' ,{})
168
- getOperationalMotes ()
169
- printOperationalMotes ()
170
- selectOperationalMotes (0 )
179
+ numMotes = getOperationalMotes ()
180
+ if numMotes :
181
+ printOperationalMotes ()
182
+ selectOperationalMote (0 )
171
183
AppData ().set ('printNotifs' ,False )
172
184
togglePrintNotifs ()
173
185
@@ -188,6 +200,7 @@ def disconnect(self):
188
200
#======================== private =========================================
189
201
190
202
def _cb_NOTIFDATA (self ,notifName ,notifParams ):
203
+
191
204
AppData ().get ('oap_dispatch' ).dispatch_pkt (notifName , notifParams )
192
205
if AppData ().get ('logNotifs' ):
193
206
if notifParams .data [0 ] == 0 :
@@ -196,30 +209,33 @@ def _cb_NOTIFDATA(self,notifName,notifParams):
196
209
def _handle_oap_notif (self ,mac ,notif ):
197
210
198
211
receive_time = float (time .time ()) - self .mapmgrtime .pctomgr_time_offset
199
- output = " Received-Time = {0} OAP notification from {1}: {2}" .format (
200
- receive_time ,
212
+ output = "OAP notification from {0} (receive time {1}):\n {2}" .format (
201
213
FormatUtils .formatMacString (mac ),
214
+ receive_time ,
202
215
notif
203
216
)
204
-
217
+
205
218
if AppData ().get ('printNotifs' ):
206
219
print output
207
220
if AppData ().get ('logNotifs' ):
208
221
self .log_file .write ('{0}\n ' .format (output ))
209
222
210
223
class MgrTime (threading .Thread ):
211
- # This class sends getTime API command to map network time to UTC time. The offset is then
212
- # used to calculate the pkt arrival time for the same time base as the mote
224
+ '''
225
+ This class periodically sends a getTime() API command to the manager to map
226
+ network time to UTC time. The offset is used to calculate the pkt arrival
227
+ time for the same time base as the mote.
228
+ '''
213
229
214
230
def __init__ (self , pctomgr_time_offset , sleepperiod ):
215
231
# init the parent
216
232
threading .Thread .__init__ (self )
217
233
self .event = threading .Event ()
218
- self .sleepperiod = sleepperiod
234
+ self .sleepperiod = sleepperiod
219
235
self .daemon = True
220
236
self .pctomgr_time_offset = pctomgr_time_offset
221
237
# give this thread a name
222
- self .name = 'MgrTimePin '
238
+ self .name = 'MgrTime '
223
239
224
240
def run (self ):
225
241
while True :
@@ -229,7 +245,7 @@ def run(self):
229
245
mgr_time = mgr_timepinres .utcSecs + mgr_timepinres .utcUsecs / 1000000.0
230
246
mgr_asn = int ('' .join (["%02x" % i for i in mgr_timepinres .asn ]),16 )
231
247
self .pctomgr_time_offset = pc_time - mgr_time
232
-
248
+
233
249
self .event .wait (self .sleepperiod )
234
250
235
251
#============================ CLI handlers ====================================
@@ -239,6 +255,14 @@ def connect_clicb(params):
239
255
# filter params
240
256
port = params [0 ]
241
257
258
+ try :
259
+ AppData ().get ('connector' )
260
+ except KeyError :
261
+ pass
262
+ else :
263
+ print 'already connected.'
264
+ return
265
+
242
266
# create a connector
243
267
AppData ().set ('connector' ,IpMgrConnectorSerial .IpMgrConnectorSerial ())
244
268
@@ -248,7 +272,12 @@ def connect_clicb(params):
248
272
'port' : port ,
249
273
})
250
274
except ConnectionError as err :
251
- printExcAndQuit (err )
275
+ print 'Could not connect to {0}: {1}' .format (
276
+ port ,
277
+ err ,
278
+ )
279
+ AppData ().delete ('connector' )
280
+ return
252
281
253
282
# start threads
254
283
AppData ().set ('manager' ,Manager ())
@@ -258,14 +287,17 @@ def list_clicb(params):
258
287
printOperationalMotes ()
259
288
260
289
def select_clicb (params ):
261
- selectOperationalMotes (int (params [0 ]))
290
+ selectOperationalMote (int (params [0 ]))
262
291
263
292
def notifs_clicb (params ):
264
293
togglePrintNotifs ()
265
294
266
295
def writelogfile_clicb (params ):
267
296
toggleLogNotifs ()
268
297
298
+ def _resppoipoi (mac , resp , trans ):
299
+ print (mac , resp , trans )
300
+
269
301
def led_clicb (params ):
270
302
271
303
# filter params
@@ -276,6 +308,13 @@ def led_clicb(params):
276
308
isBcast = True
277
309
ledState = params [1 ]
278
310
311
+ if moteId > len (AppData ().get ('operationalmotes' )):
312
+ print 'moteId {0} impossible, there are only {1} motes' .format (
313
+ moteId ,
314
+ len (AppData ().get ('operationalmotes' )),
315
+ )
316
+ return
317
+
279
318
if ledState == "0" :
280
319
ledVal = 0
281
320
else :
@@ -287,6 +326,7 @@ def led_clicb(params):
287
326
cmd_type = OAPMessage .CmdType .PUT ,
288
327
addr = [3 ,2 ],
289
328
data_tags = [OAPMessage .TLVByte (t = 0 ,v = ledVal )],
329
+ cb = _respPoipoi ,
290
330
)
291
331
else :
292
332
# build OAP message
@@ -300,8 +340,8 @@ def led_clicb(params):
300
340
)
301
341
oap_msg = [ord (b ) for b in oap_msg ]
302
342
303
- # send OAP message broadcast BCAST_CNT times
304
- for i in range (BCAST_CNT ):
343
+ # send OAP message broadcast NUM_BCAST_TO_SEND times
344
+ for i in range (NUM_BCAST_TO_SEND ):
305
345
AppData ().get ('connector' ).dn_sendData (
306
346
macAddress = [0xff ]* 8 ,
307
347
priority = 0 ,
@@ -322,6 +362,13 @@ def temp_clicb(params):
322
362
tempOn = int (params [1 ])
323
363
pktPeriod = int (params [2 ])
324
364
365
+ if moteId > len (AppData ().get ('operationalmotes' )):
366
+ print 'moteId {0} impossible, there are only {1} motes' .format (
367
+ moteId ,
368
+ len (AppData ().get ('operationalmotes' )),
369
+ )
370
+ return
371
+
325
372
# send OAP command ... single or all broadcast
326
373
if not isBcast :
327
374
AppData ().get ('oap_clients' )[AppData ().get ('operationalmotes' )[moteId ]].send (
@@ -347,8 +394,8 @@ def temp_clicb(params):
347
394
)
348
395
oap_msg = [ord (b ) for b in oap_msg ]
349
396
350
- # send OAP message broadcast BCAST_CNT times
351
- for i in range (BCAST_CNT ):
397
+ # send OAP message broadcast NUM_BCAST_TO_SEND times
398
+ for i in range (NUM_BCAST_TO_SEND ):
352
399
AppData ().get ('connector' ).dn_sendData (
353
400
macAddress = [0xff ]* 8 ,
354
401
priority = 0 ,
@@ -371,6 +418,13 @@ def pkgen_clicb(params):
371
418
pktSize = int (params [3 ])
372
419
pktstartPID = 0
373
420
421
+ if moteId > len (AppData ().get ('operationalmotes' )):
422
+ print 'moteId {0} impossible, there are only {1} motes' .format (
423
+ moteId ,
424
+ len (AppData ().get ('operationalmotes' )),
425
+ )
426
+ return
427
+
374
428
# send OAP command ... single mote, or all unicast, or all broadcast
375
429
if isBcast == False :
376
430
AppData ().get ('oap_clients' )[AppData ().get ('operationalmotes' )[moteId ]].send (
@@ -418,8 +472,8 @@ def pkgen_clicb(params):
418
472
)
419
473
oap_msg = [ord (b ) for b in oap_msg ]
420
474
421
- # send OAP message broadcast BCAST_CNT times
422
- for i in range (BCAST_CNT ):
475
+ # send OAP message broadcast NUM_BCAST_TO_SEND times
476
+ for i in range (NUM_BCAST_TO_SEND ):
423
477
AppData ().get ('connector' ).dn_sendData (
424
478
macAddress = [0xff ]* 8 ,
425
479
priority = 0 ,
@@ -431,6 +485,30 @@ def pkgen_clicb(params):
431
485
else :
432
486
print (' unknown paramater ... {0}' .format (params [0 ]))
433
487
488
+ def analog_clicb (params ):
489
+
490
+ # filter params
491
+ moteId = int (params [0 ])
492
+ channel = int (params [1 ])
493
+ enable = int (params [2 ])
494
+ rate = int (params [3 ])
495
+
496
+ if moteId > len (AppData ().get ('operationalmotes' )):
497
+ print 'moteId {0} impossible, there are only {1} motes' .format (
498
+ moteId ,
499
+ len (AppData ().get ('operationalmotes' )),
500
+ )
501
+ return
502
+
503
+ AppData ().get ('oap_clients' )[AppData ().get ('operationalmotes' )[moteId ]].send (
504
+ cmd_type = OAPMessage .CmdType .PUT ,
505
+ addr = [4 ,channel ],
506
+ data_tags = [
507
+ OAPMessage .TLVByte (t = 0 ,v = enable ), # enable
508
+ OAPMessage .TLVLong (t = 1 ,v = rate ), # rate
509
+ ],
510
+ )
511
+
434
512
def quit_clicb ():
435
513
436
514
if AppData ().get ('connector' ):
@@ -511,6 +589,14 @@ def main():
511
589
callback = pkgen_clicb ,
512
590
dontCheckParamsLength = False ,
513
591
)
592
+ cli .registerCommand (
593
+ name = 'analog' ,
594
+ alias = 'a' ,
595
+ description = 'set the analog application on the mote' ,
596
+ params = ['moteId' ,'channel' ,'enable' ,'rate' ],
597
+ callback = analog_clicb ,
598
+ dontCheckParamsLength = False ,
599
+ )
514
600
515
601
# print SmartMesh SDK version
516
602
print 'SmartMesh SDK {0}' .format ('.' .join ([str (i ) for i in sdk_version .VERSION ]))
0 commit comments