def stringifyMacIpAddresses(indict): ''' in: { 'field1': 123, 'macAddress': [0,1,2,3,4,5,6,7], } out: { 'field1': 123, 'macAddress': '00-01-02-03-04-05-06-07', } ''' outdict = indict for name in ['macAddress','source','dest']: try: assert len(outdict[name])==8 outdict[name] = u.formatMacString(outdict[name]) except KeyError: pass for name in ['ipv6Address']: try: assert len(outdict[name])==16 outdict[name] = u.formatIpString(outdict[name]) except KeyError: pass return outdict
def stringifyMacIpAddresses(indict): ''' in: { 'field1': 123, 'macAddress': [0,1,2,3,4,5,6,7], } out: { 'field1': 123, 'macAddress': '00-01-02-03-04-05-06-07', } ''' outdict = indict for name in ['macAddress', 'source', 'dest']: try: assert len(outdict[name]) == 8 outdict[name] = u.formatMacString(outdict[name]) except KeyError: pass for name in ['ipv6Address']: try: assert len(outdict[name]) == 16 outdict[name] = u.formatIpString(outdict[name]) except KeyError: pass return outdict
def handle_event(self, notifName, notifParams): topic = '{0}/{1}/json'.format( DEVICE_TYPE, ':'.join(["%.2x" % i for i in notifParams.macAddress[4:]])) if notifName == 'eventMoteJoin': print "Mote joined: {0}".format( FormatUtils.formatMacString(notifParams.macAddress)) payload = '{\"s|state\": \"joined\"}' elif notifName == 'eventMoteOperational': print "Mote operational: {0}".format( FormatUtils.formatMacString(notifParams.macAddress)) payload = '{\"s|state\": \"operational\"}' elif notifName == 'eventPathDelete': print "Path deleted: from {0} to {1} ({2})".format( formatMacString(notifParams.source), formatMacString(notifParams.dest), notifParams.direction) elif notifName == 'eventPathCreate': print "Path create: from {0} to {1} ({2})".format( formatMacString(notifParams.source), formatMacString(notifParams.dest), notifParams.direction) elif notifName == 'eventMoteLost': print "Mote disconnected: {0}".format( FormatUtils.formatMacString(notifParams.macAddress)) payload = '{\"s|state\": \"lost\"}' else: print "Event: {0}\r\nData:[{1}]".format(notifName, notifParams) client.publish(topic, payload) getOperationalMotes() printOperationalMotes()
def publish(self, mac, datastream, value): # log if log.isEnabledFor(logging.DEBUG): output = [] output += ['publish() called with:'] output += [ '- mac {0}'.format(FormatUtils.formatMacString(mac)) ] output += ['- datastream {0}'.format(datastream)] output += ['- value {0}'.format(value)] output = '\n'.join(output) log.debug(output) # verify subscriber alive, if exists if self.subscriber: if not self.subscriber.isAlive(): self.subscriber = None raise SystemError("subscriber is not alive during publish()") # create the datastream (or retrieve feed_id if exists) feed_id = self.createDatastream(mac, datastream) # publish to datastream self.client.put( url='feeds/{0}.json'.format(feed_id, ), body={ 'version': '1.0.0', 'datastreams': [ { 'id': datastream, 'current_value': value, }, ] }, ) # log if log.isEnabledFor(logging.DEBUG): output = [] output += ['published to Xively:'] output += [ '- mac {0}'.format(FormatUtils.formatMacString(mac)) ] output += ['- datastream {0}'.format(datastream)] output += ['- value {0}'.format(value)] output = '\n'.join(output) log.debug(output)
def _print_allMoteInfo(self): columnNames = [] for (mac, v) in self.allMoteInfo.items(): for n in v: if n in ['macAddress', 'RC', 'reserved']: continue columnNames += [n] columnNames = sorted(list(set(columnNames))) data_matrix = [] data_matrix += [['mac'] + columnNames] for (mac, v) in self.allMoteInfo.items(): thisline = [] thisline += [FormatUtils.formatMacString(mac)] for n in columnNames: if n in v: thisline += [v[n]] else: thisline += [''] data_matrix += [thisline] table = plotly.tools.FigureFactory.create_table(data_matrix) plotly.offline.plot( table, filename='allMoteInfo.html', )
def oapinfo_response(mac, oap_resp): output = [] output += ["GET /info response from {0}:".format(FormatUtils.formatMacString(mac))] output = '\n'.join(output) print output print (mac, oap_resp)
def log(self, event_type, mac=None, temperature='', generationTime=None): assert event_type in self.EVENT_ALL if mac: macString = FormatUtils.formatMacString(mac) else: macString = '' if generationTime != None and self.timeOffset != None: timestamp = self.timeOffset + generationTime else: timestamp = time.time() timestampString = '{0}.{1:03d}'.format( time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp)), int(1000 * (timestamp - int(timestamp))), ) logline = '{TIMESTAMP},{EVENT_TYPE},{MAC},{TEMPERATURE}\n'.format( TIMESTAMP=timestampString, EVENT_TYPE=event_type, MAC=macString, TEMPERATURE=temperature, ) print logline, self.datalogfile.write(logline) self.datalogfile.flush()
def _notifDataCallback(self, notifName, notifParams): assert notifName == NOTIFDATA try: # log log.debug( "notifClient._notifDataCallback {0}:\n{1}".format(NOTIFDATA, FormatUtils.formatNamedTuple(notifParams)) ) # read MAC address from notification mac = tuple(notifParams.macAddress) # update role self._changeDeviceRole(mac, ROLE_DATA) # update counters self._incrementCounter(mac, COL_NOTIF_DATA) # parse packet if notifParams.srcPort == 0xF0B9: self.oap_dispatch.dispatch_pkt(NOTIFDATA, notifParams) else: raise SystemError("expected srcPort {0}".format(notifParams.srcPort)) except Exception as err: output = [] output += [type(err)] output += [err] output += [traceback.format_exc()] output = "\n".join([str(o) for o in output]) log.error(output) print output
def _moteListFrameCb_toggleLed(self, mac, button): if isinstance(self.apiDef, IpMgrDefinition.IpMgrDefinition): # find out whether to switch LED on of off if button.cget("text") == "ON": val = 1 button.configure(text="OFF") else: val = 0 button.configure(text="ON") # send the OAP message try: self.oap_clients[mac].send( OAPMessage.CmdType.PUT, # command [3, 2], # address data_tags=[OAPMessage.TLVByte(t=0, v=val)], # parameters cb=None, # callback ) except APIError as err: self.statusFrame.write("[WARNING] {0}".format(err)) else: # update status self.statusFrame.write( "Toggle LED command sent successfully to mote {0}.".format(FormatUtils.formatMacString(mac)) ) else: button.configure(text="N.A.") # update status self.statusFrame.write("This feature is only present in SmartMesh IP")
def log(self,event_type,mac=None,temperature='',generationTime=None): assert event_type in self.EVENT_ALL if mac: macString = FormatUtils.formatMacString(mac) else: macString = '' if generationTime!=None and self.timeOffset!=None: timestamp = self.timeOffset+generationTime else: timestamp = time.time() timestampString = '{0}.{1:03d}'.format( time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(timestamp)), int(1000*(timestamp-int(timestamp))), ) logline = '{TIMESTAMP},{EVENT_TYPE},{MAC},{TEMPERATURE}\n'.format( TIMESTAMP = timestampString, EVENT_TYPE = event_type, MAC = macString, TEMPERATURE = temperature, ) print logline, self.datalogfile.write(logline) self.datalogfile.flush()
def handle_oap_data(mac, notif): if isinstance(notif, OAPNotif.OAPTempSample): print 't={TEMP:.2f}C at {MAC}'.format( TEMP=float(notif.samples[0]) / 100, MAC=FormatUtils.formatMacString(mac), ) simple_data_Logging(mac, notif.samples)
def _moteListFrameCb_toggleLed(self, mac, button): if isinstance(self.apiDef, IpMgrDefinition.IpMgrDefinition): # find out whether to switch LED on of off if button.cget("text") == 'ON': val = 1 button.configure(text="OFF") else: val = 0 button.configure(text="ON") # send the OAP message try: self.oap_clients[mac].send( OAPMessage.CmdType.PUT, # command [3, 2], # address data_tags=[OAPMessage.TLVByte(t=0, v=val)], # parameters cb=None, # callback ) except APIError as err: self.statusFrame.write("[WARNING] {0}".format(err)) else: # update status self.statusFrame.write( "Toggle LED command sent successfully to mote {0}.".format( FormatUtils.formatMacString(mac), )) else: button.configure(text="N.A.") # update status self.statusFrame.write( "This feature is only present in SmartMesh IP")
def _print_allMoteInfo(self): columnNames = [] for (mac,v) in self.allMoteInfo.items(): for n in v: if n in ['macAddress','RC','reserved']: continue columnNames += [n] columnNames = sorted(list(set(columnNames))) data_matrix = [] data_matrix += [['mac']+columnNames] for (mac,v) in self.allMoteInfo.items(): thisline = [] thisline += [FormatUtils.formatMacString(mac)] for n in columnNames: if n in v: thisline += [v[n]] else: thisline += [''] data_matrix += [thisline] table = plotly.tools.FigureFactory.create_table(data_matrix) plotly.offline.plot(table,filename='allMoteInfo.html',)
def _list_motes_per_manager(self, manager): returnVal = [] try: currentMac = ( 0, 0, 0, 0, 0, 0, 0, 0 ) # start getMoteConfig() iteration with the 0 MAC address continueAsking = True while continueAsking: try: with self.dataLock: res = self.managerHandlers[ manager].connector.dn_getMoteConfig( currentMac, True) except APIError: continueAsking = False else: if ((not res.isAP) and (res.state in [ 4, ])): returnVal.append(u.formatMacString(res.macAddress)) currentMac = res.macAddress except ConnectionError as err: pass # happens when manager is disconnected return returnVal
def _manager_raw_notif_handler(self, manager, notifName, notif): # parse further if notifName == IpMgrSubscribe.IpMgrSubscribe.NOTIFDATA: # try to parse data notifications as OAP (fails if not OAP payload, no problem) self.oapDispatch.dispatch_pkt(notifName, notif) elif notifName == IpMgrSubscribe.IpMgrSubscribe.NOTIFHEALTHREPORT: hr = self.hrParser.parseHr(notif.payload) # POST HR to some URL self.notifCb( notifName='hr', notifJson={ 'name': 'hr', 'mac': u.formatMacString(notif.macAddress), 'hr': hr, }, ) # POST raw notification to some URL if notifName.startswith('event'): nm = 'event' else: nm = notifName fields = stringifyMacIpAddresses(notif._asdict()) self.notifCb( notifName=nm, notifJson={ 'manager': manager, 'name': notifName, 'fields': fields, }, )
def _manager_raw_notif_handler(self,manager,notifName,notif): # parse further if notifName==IpMgrSubscribe.IpMgrSubscribe.NOTIFDATA: # try to parse data notifications as OAP (fails if not OAP payload, no problem) self.oapDispatch.dispatch_pkt(notifName,notif) elif notifName==IpMgrSubscribe.IpMgrSubscribe.NOTIFHEALTHREPORT: hr = self.hrParser.parseHr(notif.payload) # POST HR to some URL self.notifCb( notifName = 'hr', notifJson = { 'name': 'hr', 'mac': u.formatMacString(notif.macAddress), 'hr': hr, }, ) # POST raw notification to some URL if notifName.startswith('event'): nm = 'event' else: nm = notifName fields = stringifyMacIpAddresses(notif._asdict()) self.notifCb( notifName = nm, notifJson = { 'manager': manager, 'name': notifName, 'fields': fields, }, )
def _printExpectedOAPResp(self): with self.dataLock: print ' expectedOAPResp ({0} items):'.format(len(self.expectedOAPResp)) if self.expectedOAPResp: for mac in self.expectedOAPResp: print ' - {0}'.format(u.formatMacString(mac)) else: print ' (empty)'
def handle_oap_data(mac,notif): if isinstance(notif,OAPNotif.OAPTempSample): print 't={TEMP:.2f}C at {MAC}'.format( TEMP = float(notif.samples[0])/100, MAC = FormatUtils.formatMacString(mac), )
def _recursive_dict_update(self,d,u): for k, v in u.items(): if isinstance(v, collections.Mapping): r = self._recursive_dict_update(d.get(k, {}), v) d[k] = r else: d[k] = u[k] return d
def _recursive_dict_update(self, d, u): for k, v in u.items(): if isinstance(v, collections.Mapping): r = self._recursive_dict_update(d.get(k, {}), v) d[k] = r else: d[k] = u[k] return d
def _moteListFrameCb_clearTemp(self, mac, button): # clear the temperature data self.notifClientHandler.clearTemp(mac) # update status self.statusFrame.write( "Temperature data for mote {0} cleared successfully.".format( FormatUtils.formatMacString(mac), ))
def publish(self,mac,datastream,value): # log if log.isEnabledFor(logging.DEBUG): output = [] output += ['publish() called with:'] output += ['- mac {0}'.format(FormatUtils.formatMacString(mac))] output += ['- datastream {0}'.format(datastream)] output += ['- value {0}'.format(value)] output = '\n'.join(output) log.debug(output) # verify subscriber alive, if exists if self.subscriber: if not self.subscriber.isAlive(): self.subscriber = None raise SystemError("subscriber is not alive during publish()") # create the datastream (or retrieve feed_id if exists) feed_id = self.createDatastream(mac,datastream) # publish to datastream self.client.put( url = 'feeds/{0}.json'.format( feed_id, ), body = { 'version': '1.0.0', 'datastreams': [ { 'id' : datastream, 'current_value': value, }, ] }, ) # log if log.isEnabledFor(logging.DEBUG): output = [] output += ['published to Xively:'] output += ['- mac {0}'.format(FormatUtils.formatMacString(mac))] output += ['- datastream {0}'.format(datastream)] output += ['- value {0}'.format(value)] output = '\n'.join(output) log.debug(output)
def _moteListFrameCb_clearCtrs(self, mac, button): # clear the counters self.notifClientHandler.clearNotifCounters(mac) # update status self.statusFrame.write( "Counters for mote {0} cleared successfully.".format( FormatUtils.formatMacString(mac), ))
def _moteListFrameCb_getd3(self,mac,button): self.oap_clients[mac].send( OAPMessage.CmdType.GET, [2,3], data_tags=None, cb=self._oap_getd3, ) self.statusFrame.write("Get digital input 3 of mote {0}".format(FormatUtils.formatMacString(mac)))
def _moteListFrameCb_Ledoff(self,mac,button): self.oap_clients[mac].send( OAPMessage.CmdType.PUT, [3,2], data_tags=[ OAPMessage.TLVByte(t=0,v=0)], cb=None, ) self.statusFrame.write("Set led off of mote {0}".format(FormatUtils.formatMacString(mac)))
def _printExpectedOAPResp(self): with self.dataLock: print ' expectedOAPResp ({0} items):'.format( len(self.expectedOAPResp)) if self.expectedOAPResp: for mac in self.expectedOAPResp: print ' - {0}'.format(u.formatMacString(mac)) else: print ' (empty)'
def printOperationalMotes(): output = [] output += ["{0} operational motes:".format(len(AppData().get('operationalmotes')))] for (i,m) in enumerate(AppData().get('operationalmotes')): output += ['{0}: {1}'.format(i,FormatUtils.formatMacString(m))] output = '\n'.join(output) print output
def _moteListFrameCb_clearCtrs(self,mac,button): # clear the counters self.notifClientHandler.clearNotifCounters(mac) # update status self.statusFrame.write( "Counters for mote {0} cleared successfully.".format( FormatUtils.formatMacString(mac), ) )
def _moteListFrameCb_clearTemp(self,mac,button): # clear the temperature data self.notifClientHandler.clearTemp(mac) # update status self.statusFrame.write( "Temperature data for mote {0} cleared successfully.".format( FormatUtils.formatMacString(mac), ) )
def _handle_oap_notif(self, mac, notif): receive_time = float(time.time()) - self.mapmgrtime.pctomgr_time_offset output = "OAP notification from {0} (receive time {1}):\n{2}".format( FormatUtils.formatMacString(mac), receive_time, notif) if AppData().get('printNotifs'): print output if AppData().get('logNotifs'): self.log_file.write('{0}\n'.format(output))
def _moteListFrameCb_clearPkGen(self, mac, button): # log log.debug("_moteListFrameCb_clearPkGen") # clear the PkGen counters self.notifClientHandler.clearPkGenCounters(mac) # update status self.statusFrame.write( "pkGen counters for mote {0} cleared successfully.".format( FormatUtils.formatMacString(mac), ))
def _handle_oap_notif(self,mac,notif): receive_time = float(time.time()) - self.mapmgrtime.pctomgr_time_offset output = "OAP notification from {0} (receive time {1}):\n{2}".format( FormatUtils.formatMacString(mac), receive_time, notif ) if AppData().get('printNotifs'): print output if AppData().get('logNotifs'): self.log_file.write('{0}\n'.format(output))
def mac2feedId(self,mac): if type(mac) in [tuple,list]: mac = FormatUtils.formatMacString(mac) returnVal = None with self.dataLock: for d in self.xivelyDevices: if d['serial']==mac: returnVal = d['feed_id'] break return returnVal
def notifCallback(notifName, notifParams): global responseEvent if notifName != IpMgrSubscribe.IpMgrSubscribe.EVENTPINGRESPONSE: return print 'response from {0} delay={1}ms voltage={2}mV temp={3}C'.format( FormatUtils.formatMacString(notifParams.macAddress), notifParams.delay, notifParams.voltage, notifParams.temperature, ) responseEvent.set()
def mac2feedId(self, mac): if type(mac) in [tuple, list]: mac = FormatUtils.formatMacString(mac) returnVal = None with self.dataLock: for d in self.xivelyDevices: if d['serial'] == mac: returnVal = d['feed_id'] break return returnVal
def _moteListFrameCb_clearPkGen(self,mac,button): # log log.debug("_moteListFrameCb_clearPkGen") # clear the PkGen counters self.notifClientHandler.clearPkGenCounters(mac) # update status self.statusFrame.write( "pkGen counters for mote {0} cleared successfully.".format( FormatUtils.formatMacString(mac), ) )
def _notifCallback(self, notifName, notifParams): try: assert notifName==IpMgrSubscribe.IpMgrSubscribe.NOTIFHEALTHREPORT mac = FormatUtils.formatMacString(notifParams.macAddress) hr = self.hrParser.parseHr(notifParams.payload) # log hrlog.info('from {0}:\n{1}'.format( mac, self.hrParser.formatHr(hr), ), ) with self.dataLock: # format of data: # [ # ['' , 'Device', 'Neighbors', 'Discovered', 'RSSI'], # ['11-11-11-11-11-11-11-11' , 0, 3, 2, 4], # ['22-22-22-22-22-22-22-22' , 0, 3, 2, 5], # ] # find notifName row found=False for row in self.data: if row[0]==mac: found=True break # create row if needed if not found: self.data.append([mac,0,0,0,0]) row = self.data[-1] # increment counters if 'Device' in hr: row[1] += 1 if 'Neighbors' in hr: row[2] += 1 if 'Discovered' in hr: row[3] += 1 if 'Extended' in hr: row[4] += 1 except Exception as err: print type(err) print err raise
def _oap_handle_response(self, mac, oap_resp): macString = u.formatMacString(mac) with self.dataLock: if macString in self.responses: raise SystemError('response unread') if macString in self.outstandingEvents: self.responses[macString] = oap_resp self.outstandingEvents[macString].set() del self.outstandingEvents[macString] else: # received a response I'm not waiting for anymore (increase timeout?)' pass
def _notifCallback(self, notifName, notifParams): try: assert notifName == IpMgrSubscribe.IpMgrSubscribe.NOTIFHEALTHREPORT mac = FormatUtils.formatMacString(notifParams.macAddress) hr = self.hrParser.parseHr(notifParams.payload) # log hrlog.info( 'from {0}:\n{1}'.format( mac, self.hrParser.formatHr(hr), ), ) with self.dataLock: # format of data: # [ # ['' , 'Device', 'Neighbors', 'Discovered', 'RSSI'], # ['11-11-11-11-11-11-11-11' , 0, 3, 2, 4], # ['22-22-22-22-22-22-22-22' , 0, 3, 2, 5], # ] # find notifName row found = False for row in self.data: if row[0] == mac: found = True break # create row if needed if not found: self.data.append([mac, 0, 0, 0, 0]) row = self.data[-1] # increment counters if 'Device' in hr: row[1] += 1 if 'Neighbors' in hr: row[2] += 1 if 'Discovered' in hr: row[3] += 1 if 'Extended' in hr: row[4] += 1 except Exception as err: print type(err) print err raise
def _parseData(self,byteArray): returnVal = {} # log log.debug("_parseData with byteArray {0}".format(FormatUtils.formatBuffer(byteArray))) # command ID try: (returnVal['cmdId'],) = struct.unpack('>H', self._toString(byteArray[:2])) except struct.error as err: raise ValueError(err) if returnVal['cmdId']==CMDID_CONFIGURATION: try: ( returnVal['reportPeriod'], returnVal['bridgeSettlingTime'], returnVal['ldoOnTime'], ) = struct.unpack('>III', self._toString(byteArray[2:])) except struct.error as err: raise ValueError(err) elif returnVal['cmdId']==CMDID_REPORT: try: ( returnVal['temperature'], returnVal['adcValue'], ) = struct.unpack('>IH', self._toString(byteArray[2:])) except struct.error as err: raise ValueError(err) elif returnVal['cmdId']==ERR_NO_SERVICE: pass elif returnVal['cmdId'] in [ERR_NOT_ENOUGH_BW,ERR_INVALID]: try: ( returnVal['val'], ) = struct.unpack('>I', self._toString(byteArray[2:])) except struct.error as err: raise ValueError(err) else: raise ValueError("unexpected command ID {0}".format(returnVal['cmdId'])) return returnVal
def loadNetworkMotes(self,networkMotes): # abort if not motes reported if not networkMotes: return # convert to tuple of strings newMotes = [FormatUtils.formatMacString(m) for m in networkMotes] # sort newMotes = sorted(newMotes) # turn into tuple newMotes = tuple(newMotes) if newMotes!=self.networkMotes: # remember last selection lastSelection = self.presenterMote.get() # record new options self.networkMotes = newMotes # delete old options self.moteDropDown['menu'].delete(0, 'end') # load new options for mote in self.networkMotes : self.moteDropDown['menu'].add_command( label=mote, command=Tkinter._setit( self.presenterMote, mote, ) ) # load option to select none self.moteDropDown['menu'].add_command( label=self.PRESENTERMOTE_DFLT, command=Tkinter._setit( self.presenterMote, self.PRESENTERMOTE_DFLT, ) ) # change selection, if needed if not self.networkMotes or (lastSelection not in self.networkMotes): if lastSelection!=self.PRESENTERMOTE_DFLT: self.presenterMote.set(self.PRESENTERMOTE_DFLT)
def selectOperationalMote(moteNum): if moteNum>len(AppData().get('operationalmotes')): print 'Cannot select mote {0}, there are only {1} motes'.format( moteNum, len(AppData().get('operationalmotes')), ) return AppData().set('currentmote',moteNum) print '\nCurrently using mote {0} ({1}).'.format( AppData().get('currentmote'), FormatUtils.formatMacString(AppData().get('operationalmotes')[AppData().get('currentmote')]) )
def printOperationalMotes(): strMac = {} strTime = {} output = [] output += [ "{0} operational motes:".format(len(AppData().get('operationalmotes'))) ] strTime['currentTime'] = '{}'.format(datetime.datetime.now()) print(strTime) with open('operMote.json', 'a') as operF: json.dump(strTime, operF) operF.write('\n') for (i, m) in enumerate(AppData().get('operationalmotes')): output += ['{0}: {1}'.format(i, FormatUtils.formatMacString(m))] strMac['MACDec'] = m strMac['MACHex'] = FormatUtils.formatMacString(m) strMac['MoteID'] = i + 2 #strMac['Time'] = with open('operMote.json', 'a') as operF: json.dump(strMac, operF) operF.write('\n') output = '\n'.join(output) print output
def simple_data_Logging(mac, samples): # generating random humidity, light, wind speed, accelerometer data #spoof_samples = [samples[0], randint(0,10000),randint(0,1000), randint(0,600)] #acceldata = [] #for index in range(10): #acceldata.append((randint(0,10), randint(0,10), randint(0,10))) #spoof_samples.append(acceldata) # logging currentDTandTM = datetime.datetime.now() logFile = open("sampleLog.log", "a") logFile.writelines('\n{TIME} - mote: ({MAC}), sampled: {SAMPLES}'.format( TIME=currentDTandTM.strftime('%H:%M:%S'), MAC=FormatUtils.formatMacString(mac), SAMPLES=samples, ))
def refresh(self, macs): with self.guiLock: self.motes["menu"].delete(0, "end") # format the MAC addresses into strings formattedMacs = [FormatUtils.formatMacString(mac) for mac in macs] # update the optionmenu for mac in formattedMacs: self.motes["menu"].add_command(label=mac, command=Tkinter._setit(self.selectedMote, mac)) # update the selected mote, if pre previousSelectedMote = self.selectedMote.get() if (formattedMacs) and (previousSelectedMote not in formattedMacs): self.selectedMote.set(formattedMacs[0])
def _manager_oap_notif_handler(self, mac, notif): macString = u.formatMacString(mac) # add an oapClient, if needed self._oap_add_client_if_needed(mac) # POST OAP notification to some URLs fields = stringifyMacIpAddresses(notif._asdict()) self.notifCb( notifName='oap', notifJson={ 'name': 'oap', 'mac': macString, 'fields': fields, }, )
def _manager_oap_notif_handler(self,mac,notif): macString = u.formatMacString(mac) # add an oapClient, if needed self._oap_add_client_if_needed(mac) # POST OAP notification to some URLs fields = stringifyMacIpAddresses(notif._asdict()) self.notifCb( notifName = 'oap', notifJson = { 'name': 'oap', 'mac': macString, 'fields': fields, }, )
def _sendUnicast(self, macToSendTo): with self.dataLock: print ' --> sending unicast to {0} (attempt {1}/{2})'.format( u.formatMacString(macToSendTo), self.numUnicastAttempts, self.MAX_NUM_UNICASTS, ) # send OAP packet self._sendOap(macToSendTo) # arm timeout self.toutTimer = threading.Timer(self.TOUT_OAP_RESPONSE_UNICAST, self._timeout) self.toutTimer.start()
def _moteListFrameCb_rateGet(self,mac): # send the OAP message try: self.oap_clients[mac].send( OAPMessage.CmdType.GET, # command [5], # address data_tags=None, # parameters cb=self._oap_rateGet_resp, # callback ) except APIError as err: self.statusFrame.write("[WARNING] {0}".format(err)) else: # update status self.statusFrame.write( "Publish rate get request sent successfully to mote {0}.".format( FormatUtils.formatMacString(mac), ) )
def _sendUnicast(self,macToSendTo): with self.dataLock: print ' --> sending unicast to {0} (attempt {1}/{2})'.format( u.formatMacString(macToSendTo), self.numUnicastAttempts, self.MAX_NUM_UNICASTS, ) # send OAP packet self._sendOap(macToSendTo) # arm timeout self.toutTimer = threading.Timer( self.TOUT_OAP_RESPONSE_UNICAST, self._timeout ) self.toutTimer.start()
def _oap_add_client_if_needed(self,mac): if type(mac)==str: macString = mac mac = [int(b,16) for b in mac.split('-')] else: macString = u.formatMacString(mac) with self.dataLock: if macString not in self.oapClients: # get MACs per manager for (manager,motes) in self.motes_GET().items(): if macString in manager: break # create OAPClient self.oapClients[macString] = OAPClient.OAPClient( mac, self.managerHandlers[manager].connector.dn_sendData, self.oapDispatch, )
def _moteListFrameCb_rateSet(self,mac,val): # send the OAP message try: self.oap_clients[mac].send( OAPMessage.CmdType.PUT, # command [5], # address data_tags=[OAPMessage.TLVByte(t=0,v=1), OAPMessage.TLVLong(t=1,v=val),],# parameters cb=None, # callback ) except APIError as err: self.statusFrame.write("[WARNING] {0}".format(err)) else: # update status self.statusFrame.write( "Publish rate set({0}) request sent successfully to mote {1}.".format( val, FormatUtils.formatMacString(mac), ) )
def _list_motes_per_manager(self,manager): returnVal = [] try: currentMac = (0,0,0,0,0,0,0,0) # start getMoteConfig() iteration with the 0 MAC address continueAsking = True while continueAsking: try: with self.dataLock: res = self.managerHandlers[manager].connector.dn_getMoteConfig(currentMac,True) except APIError: continueAsking = False else: if ((not res.isAP) and (res.state in [4,])): returnVal.append(u.formatMacString(res.macAddress)) currentMac = res.macAddress except ConnectionError as err: pass # happens when manager is disconnected return returnVal
def calculate(self,data): if log.isEnabledFor(logging.DEBUG): log.debug('calculating for data={0}'.format(FormatUtils.formatBuffer(data))) ptr = 0 tempfcs = 0xffff while ptr<len(data): tempfcs = (tempfcs >> 8) ^ self._fcstab[(tempfcs ^ data[ptr]) & 0xff]; ptr += 1 tempfcs ^= 0xffff fcs = [] fcs.append( (tempfcs>>0) & 0xff ) fcs.append( (tempfcs>>8) & 0xff ) if log.isEnabledFor(logging.DEBUG): log.debug('fcs=0x%2x%2x',fcs[0],fcs[1]) return fcs #======================== private =========================================
def loadNetworkMotes(self, networkMotes): # abort if not motes reported if not networkMotes: return # convert to tuple of strings newMotes = [FormatUtils.formatMacString(m) for m in networkMotes] # sort newMotes = sorted(newMotes) # turn into tuple newMotes = tuple(newMotes) if newMotes != self.networkMotes: # remember last selection lastSelection = self.presenterMote.get() # record new options self.networkMotes = newMotes # delete old options self.moteDropDown["menu"].delete(0, "end") # load new options for mote in self.networkMotes: self.moteDropDown["menu"].add_command(label=mote, command=Tkinter._setit(self.presenterMote, mote)) # load option to select none self.moteDropDown["menu"].add_command( label=self.PRESENTERMOTE_DFLT, command=Tkinter._setit(self.presenterMote, self.PRESENTERMOTE_DFLT) ) # change selection, if needed if not self.networkMotes or (lastSelection not in self.networkMotes): if lastSelection != self.PRESENTERMOTE_DFLT: self.presenterMote.set(self.PRESENTERMOTE_DFLT)