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 __init__(self,connector,connectParams): assert isinstance(connector,ApiConnector.ApiConnector) assert isinstance(connectParams,(str,tuple)) # store params self.connector = connector self.connectParams = connectParams # local variables self.netname = FormatUtils.formatConnectionParams(self.connectParams) self.goOn = True self.delayCounter = self.SNAPSHOT_PERIOD_FIRST self.dataLock = threading.Lock() self.busySnapshotting = threading.Lock() # initialize parent threading.Thread.__init__(self) self.name = '{0}_SnapShot'.format(self.netname) # connect to EventBus dispatcher.connect( self._timeToNextSnapShot, signal = "timeToNextSnapShot_{0}".format(self.netname), weak = False, ) dispatcher.connect( self._snapShotNow, signal = "snapShotNow_{0}".format(self.netname), weak = False, ) # start itself self.start()
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 _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(self,mac,notif): try: if not isinstance(notif,OAPNotif.OAPTempSample): return mac = FormatUtils.formatMacString(mac) temp = float(notif.samples[0])/100.0 AppData().setTemperature(mac,temp) except Exception as err: criticalError(err)
def _handle_oap(self, mac, notif): try: if not isinstance(notif, OAPNotif.OAPTempSample): return mac = FormatUtils.formatMacString(mac) temp = float(notif.samples[0]) / 100.0 AppData().setTemperature(mac, temp) except Exception as err: criticalError(err)
def _handle_oap_notif(self,mac,notif): # convert MAC to tuple mac = tuple(mac) if isinstance(notif,OAPNotif.OAPTempSample): # this is a temperature notification value = float(notif.samples[0])/100.0 # /100 since unit in 100th of C xivelyConnectorThread().publish( mac = mac, datastream = 'temperature', value = value, ) if mac not in self.oap_clients: publisher = xivelyConnectorThread().publisher if publisher: try: # create datastream publisher.publish( mac = mac, datastream = 'led', value = 0, ) # subscribe publisher.subscribe( mac = mac, datastream = 'led', callback = self._led_cb, ) # create OAP client self.oap_clients[mac] = OAPClient.OAPClient( mac, self._sendDataToConnector, self.oap_dispatch, ) except Exception as err: output = [] output += ['==============='] output += ['{0}: Exception when creating and subscribing to datastream'] output += ['- mac: {0}'.format(FormatUtils.formatMacString(mac))] output += ['{0}'.format(type(err))] output += ['{0}'.format(err)] output += [''] output = '\n'.join(output) log.error(output) print output
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 _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 subPageLister(self): username = str(web.ctx.session.username) dld = DustLinkData.DustLinkData() try: return [{ 'url': FormatUtils.quote(n), 'title': n, } for n in dld.getNetnames(username=username)] except DataVaultException.Unauthorized: return []
def postDataDyn(self,receivedData,dynPath,subResource,username): netname = FormatUtils.unquote(dynPath) dld = DustLinkData.DustLinkData() if subResource==['testschedule']: assert isinstance(receivedData,dict) assert receivedData.keys()==['fieldName','fieldValue'] if receivedData['fieldName']=='period (min)': assert type(receivedData['fieldValue'])==int period_seconds = 60*int(receivedData['fieldValue']) dld.setTestPeriod(netname,period_seconds,username=username) elif receivedData['fieldName']==' ': # dummy read to make sure user has GET privileges on testResults dld.getTestPeriod(netname,username=username) # trigger a snapshot secondsToNextRaw = dispatcher.send( signal = 'snapShotNow_{0}'.format(netname), data = None, ) else: raise web.notfound() elif subResource==['testreset']: assert isinstance(receivedData,dict) assert receivedData.keys()==['command'] assert isinstance(receivedData['command'],str) if receivedData['command']=='reset': # log log.info("deleting testResults for network {0}".format(netname)) # erase all test results dld.delete(['testResults',netname,'results'],username=username) # as user (to verify privileges) dld.put( ['testResults',netname,'results'],None) # as ADMIN # reset 'numOperationalEvents' counter of all motes in the network motes = dld.getNetworkMotes(netname) for mote in motes: try: dld.delete(['motes',mote,'info','numOperationalEvents']) except DataVaultException.NotFound: # happens when mote has not 'numOperationalEvents' counter pass else: raise web.notfound()
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 subPageLister(self): username = str(web.ctx.session.username) dld = DustLinkData.DustLinkData() try: return [ { 'url': FormatUtils.quote(n), 'title': n, } for n in dld.getNetnames(username=username) ] except DataVaultException.Unauthorized: return []
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 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 _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'] == ERR_NOT_ENOUGH_BW: try: (returnVal['bw'], ) = 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 _doSnapshot(self): try: idToMac = {} macs = [] connector = AppData().getConnector() currentMac = (0,0,0,0,0,0,0,0) continueAsking = True while continueAsking: try: res = connector.dn_getMoteConfig(currentMac,True) except APIError: continueAsking = False else: idToMac[res.moteId] = FormatUtils.formatMacString(res.macAddress) currentMac = res.macAddress macs += [currentMac] AppData().setIdToMac(idToMac) paths = [] for mac in macs: currentPathId = 0 continueAsking = True while continueAsking: try: res = connector.dn_getNextPathInfo(mac,0,currentPathId) except APIError: continueAsking = False else: currentPathId = res.pathId paths += [ ( FormatUtils.formatMacString(res.source), FormatUtils.formatMacString(res.dest), ) ] AppData().setPaths(paths) except Exception as err: print "snapshot FAILED:" print err
def _doSnapshot(self): try: idToMac = {} macs = [] connector = AppData().getConnector() currentMac = (0, 0, 0, 0, 0, 0, 0, 0) continueAsking = True while continueAsking: try: res = connector.dn_getMoteConfig(currentMac, True) except APIError: continueAsking = False else: idToMac[res.moteId] = FormatUtils.formatMacString( res.macAddress) currentMac = res.macAddress macs += [currentMac] AppData().setIdToMac(idToMac) paths = [] for mac in macs: currentPathId = 0 continueAsking = True while continueAsking: try: res = connector.dn_getNextPathInfo( mac, 0, currentPathId) except APIError: continueAsking = False else: currentPathId = res.pathId paths += [( FormatUtils.formatMacString(res.source), FormatUtils.formatMacString(res.dest), )] AppData().setPaths(paths) except Exception as err: print "snapshot FAILED:" print err
def _parseData(self, byteArray): returnVal = {} #log log.debug("_parseData with byteArray {0}".format( FormatUtils.formatBuffer(byteArray))) try: ( returnVal['current'], returnVal['charge'], ) = struct.unpack('>HB', self._toString(byteArray[:])) except struct.error as err: raise ValueError(err) return returnVal
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']==ERR_NOT_ENOUGH_BW: try: ( returnVal['bw'], ) = 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 _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'], # ['11-11-11-11-11-11-11-11' , 0, 3, 2], # ['22-22-22-22-22-22-22-22' , 0, 3, 2], # ] # 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]) 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 except Exception as err: print type(err) print err raise
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'], # ['11-11-11-11-11-11-11-11' , 0, 3, 2], # ['22-22-22-22-22-22-22-22' , 0, 3, 2], # ] # 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]) 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 except Exception as err: print type(err) print err raise
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 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 _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 __init__(self, connectParams): # log log.info("creating instance") # store params self.connectParams = connectParams # local variables self.netname = FormatUtils.formatConnectionParams(self.connectParams) self.hrParser = HrParser.HrParser() self.dataLock = threading.Lock() self.snapPaths = [] self.snapshotOngoing = False # initialize parent class EventBusClient.EventBusClient.__init__( self, signal='notifEvent_{0}'.format(self.netname), cb=self._ebHandler_notifEvent, teardown_cb=self._cleanup, queuesize=self.QUEUESIZE, ) self.name = '{0}_NetworkState'.format(self.netname) # connect extra applications dispatcher.connect( self._ebHandler_snapShotStart, signal='snapShotStart_{0}'.format(self.netname), weak=False, ) dispatcher.connect( self._ebHandler_managerCmd, signal='managerCmd_{0}'.format(self.netname), weak=False, ) dispatcher.connect( self._ebHandler_snapShotEnd, signal='snapShotEnd_{0}'.format(self.netname), weak=False, ) dispatcher.connect( self._ebHandler_notifHealthReport, signal='notifHealthReport_{0}'.format(self.netname), weak=False, )
def __init__(self,connectParams): # log log.info("creating instance") # store params self.connectParams = connectParams # local variables self.netname = FormatUtils.formatConnectionParams(self.connectParams) self.hrParser = HrParser.HrParser() self.dataLock = threading.Lock() self.snapPaths = [] self.snapshotOngoing = False # initialize parent class EventBusClient.EventBusClient.__init__(self, signal = 'notifEvent_{0}'.format(self.netname), cb = self._ebHandler_notifEvent, teardown_cb = self._cleanup, queuesize = self.QUEUESIZE, ) self.name = '{0}_NetworkState'.format(self.netname) # connect extra applications dispatcher.connect( self._ebHandler_snapShotStart, signal = 'snapShotStart_{0}'.format(self.netname), weak = False, ) dispatcher.connect( self._ebHandler_managerCmd, signal = 'managerCmd_{0}'.format(self.netname), weak = False, ) dispatcher.connect( self._ebHandler_snapShotEnd, signal = 'snapShotEnd_{0}'.format(self.netname), weak = False, ) dispatcher.connect( self._ebHandler_notifHealthReport, signal = 'notifHealthReport_{0}'.format(self.netname), weak = False, )
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 _ebHandler_snapShotEnd(self,sender,signal,data): dld = DustLinkData.DustLinkData() testsFailed = False # discover and run all the network tests with self.busyTesting: for f in dir(self): if f.startswith("_nettest_"): # execute the test (outcome,description) = getattr(self,f)() assert outcome in dld.TEST_OUTCOME_ALL assert type(description)==str # remember if test failed if outcome==dld.TEST_OUTCOME_FAIL: testsFailed = True # log if log.isEnabledFor(logging.DEBUG): log.debug('testResult outcome={0} description={1}'.format(outcome,description)) # dispatch self._dispatch( signal = 'testResult_{0}'.format(self.netname), data = { 'testName': f, 'testDesc': getattr(self,f).__doc__, 'outcome': outcome, 'description': description, }, ) # write to banner if tests failed if testsFailed: dld._setBanner( 'some tests failed for network <a href="/health/_{0}">{1}</a>'.format( FormatUtils.quote(self.netname), self.netname ) )
def __init__(self,connectParams): # log log.info("creating instance") # store params self.connectParams = connectParams # local variables self.netname = FormatUtils.formatConnectionParams(self.connectParams) # initialize parent class EventBusClient.EventBusClient.__init__(self, signal = 'testResult_{0}'.format(self.netname), cb = self._ebHandler_testResult, teardown_cb = self._cleanup, queuesize = self.QUEUESIZE, ) self.name = '{0}_NetworkStatePublisher'.format(self.netname)
def _ebHandler_snapShotEnd(self, sender, signal, data): dld = DustLinkData.DustLinkData() testsFailed = False # discover and run all the network tests with self.busyTesting: for f in dir(self): if f.startswith("_nettest_"): # execute the test (outcome, description) = getattr(self, f)() assert outcome in dld.TEST_OUTCOME_ALL assert type(description) == str # remember if test failed if outcome == dld.TEST_OUTCOME_FAIL: testsFailed = True # log if log.isEnabledFor(logging.DEBUG): log.debug( 'testResult outcome={0} description={1}'.format( outcome, description)) # dispatch self._dispatch( signal='testResult_{0}'.format(self.netname), data={ 'testName': f, 'testDesc': getattr(self, f).__doc__, 'outcome': outcome, 'description': description, }, ) # write to banner if tests failed if testsFailed: dld._setBanner( 'some tests failed for network <a href="/health/_{0}">{1}</a>'. format(FormatUtils.quote(self.netname), self.netname))
def _moteListFrameCb_action1(self, mac, button): # send the command to the mote try: self.connector.dn_sendData( macAddress=mac, priority=2, srcPort=0xf0b9, dstPort=0xf0b9, options=0, data=[0xB3, 0xCE], ) except Exception as err: print "ERROR!!!" print err else: # update status self.statusFrame.write( "action1 command sent successfully to mote {0}.".format( FormatUtils.formatMacString(mac), ))
def __init__(self, connectParams): # log log.info("creating instance") # store params self.connectParams = connectParams # local variables self.netname = FormatUtils.formatConnectionParams(self.connectParams) # initialize parent class EventBusClient.EventBusClient.__init__( self, signal='testResult_{0}'.format(self.netname), cb=self._ebHandler_testResult, teardown_cb=self._cleanup, queuesize=self.QUEUESIZE, ) self.name = '{0}_NetworkStatePublisher'.format(self.netname)
def _moteListFrame_Url(self,mac,button): # get FeedId productId = xivelyConnectorThread().getProductId() if not productId: self.toolTipFrame.write('Cannot open live data. Not connected to Xively.') return # format URL url = "https://xively.com/manage/{0}/devices/{1}".format( productId, FormatUtils.formatMacString(mac), ) # open browser webbrowser.open( url = url, new = 2, # 2==Open new tab if possible )
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 __init__(self,connectParams): # log log.info("creating instance") # store params self.connectParams = connectParams # local variables self.netname = FormatUtils.formatConnectionParams(self.connectParams) self.busyTesting = threading.Lock() # initialize parent class EventBusClient.EventBusClient.__init__(self, signal = 'snapShotEnd_{0}'.format(self.netname), cb = self._ebHandler_snapShotEnd, teardown_cb = self._cleanup, queuesize = self.QUEUESIZE, ) self.name = '{0}_NetworkStateAnalyzer'.format(self.netname)
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 __init__(self, connectParams): # log log.info("creating instance") # store params self.connectParams = connectParams # local variables self.netname = FormatUtils.formatConnectionParams(self.connectParams) self.busyTesting = threading.Lock() # initialize parent class EventBusClient.EventBusClient.__init__( self, signal='snapShotEnd_{0}'.format(self.netname), cb=self._ebHandler_snapShotEnd, teardown_cb=self._cleanup, queuesize=self.QUEUESIZE, ) self.name = '{0}_NetworkStateAnalyzer'.format(self.netname)
def _deleteManagerConnection(self, connectParam): dld = DustLinkData.DustLinkData() with dld.dataLock: #===== NetworkStateAnalyzer if connectParam in self.publishers: self.publishers[connectParam].tearDown() del self.publishers[connectParam] log.info('deleted publisher {0}'.format(connectParam)) #===== NetworkStateAnalyzer if connectParam in self.analyzers: self.analyzers[connectParam].tearDown() del self.analyzers[connectParam] log.info('deleted analyzer {0}'.format(connectParam)) #===== NetworkState if connectParam in self.netstate: self.netstate[connectParam].tearDown() del self.netstate[connectParam] log.info('deleted netstate {0}'.format(connectParam)) #===== GatewayListener if connectParam in self.listeners: self.listeners[connectParam].tearDown() del self.listeners[connectParam] log.info('deleted listener {0}'.format(connectParam)) #===== apiconnectors if connectParam in self.apiconnectors: self.apiconnectors[connectParam].disconnect() del self.apiconnectors[connectParam] log.info('deleted apiconnector {0}'.format(connectParam)) #===== delete network try: dld.deleteNetwork(FormatUtils.formatConnectionParams(connectParam)) except ValueError: pass # happens if network was already deleted, e.g. by an earlier call to this function
def _moteListFrameCb_action1(self,mac,button): # send the command to the mote try: self.connector.dn_sendData( macAddress = mac, priority = 2, srcPort = 0xf0b9, dstPort = 0xf0b9, options = 0, data = [0xB3,0xCE], ) except Exception as err: print "ERROR!!!" print err else: # update status self.statusFrame.write( "action1 command sent successfully to mote {0}.".format( FormatUtils.formatMacString(mac), ) )
def _nettest_stabilityVsRssi(self): ''' <p> This test verifies that stability of a path is plausible given its RSSI. </p> <p> In the absence of heavy interference, the is a straightforward relationship between the RSSI and stability of a path: <ul> <li>if the RSSI is above THRES_HIGH_RSSI, the stability is expected to be above THRES_HIGH_STAB.</li> <li>if the RSSI is below THRES_LOW_RSSI, the stability is expected to be below THRES_LOW_STAB.</li> </ul> </p> <p> The stability is calculated as the ratio between the number of packets transmitted successfully and transmission attempts; it is also known as Packet Delivery Ratio (PDR). </p> <p> This test is run once for each path in the network over which at least THRES_NUM_PACKETS packet have been transmitted. </p> ''' dld = DustLinkData.DustLinkData() descPASS = [] descFAIL = [] descNOTRUN = [] currentPaths = dld.getNetworkPaths(self.netname) for (fromMote, toMote) in currentPaths: pathInfo = dld.getPathInfo(self.netname, fromMote, toMote) # make sure path information contains all the counters if ('rssi' not in pathInfo) or ('numTxPackets' not in pathInfo) or ('numTxFailures' not in pathInfo): continue # make sure source has sent enough packets to destination if pathInfo['numTxPackets'] < self.THRES_NUM_PACKETS: continue # calculate link stability and RSSI linkStability = 1 - float(pathInfo['numTxFailures']) / float( pathInfo['numTxPackets']) linkRssi = pathInfo['rssi'] # test for high RSSI if linkRssi > self.THRES_HIGH_RSSI: if linkStability > self.THRES_HIGH_STAB: descPASS += [ 'link {0}->{1} has RSSI {2} (>{3}) and stability {4} (>{5})' .format( FormatUtils.formatMacString(fromMote), FormatUtils.formatMacString(toMote), linkRssi, self.THRES_HIGH_RSSI, linkStability, self.THRES_HIGH_STAB, ) ] else: descFAIL += [ 'link {0}->{1} has RSSI {2} (>{3}) and stability {4} (<{5})' .format( FormatUtils.formatMacString(fromMote), FormatUtils.formatMacString(toMote), linkRssi, self.THRES_HIGH_RSSI, linkStability, self.THRES_HIGH_STAB, ) ] # test for low RSSI if linkRssi < self.THRES_LOW_RSSI: if linkStability < self.THRES_LOW_STAB: descPASS += [ 'link {0}->{1} has RSSI {2} (<{3}) and stability {4} (<{5})' .format( FormatUtils.formatMacString(fromMote), FormatUtils.formatMacString(toMote), linkRssi, self.THRES_LOW_RSSI, linkStability, self.THRES_LOW_STAB, ) ] else: descFAIL += [ 'link {0}->{1} has RSSI {2} (<{3}) and stability {4} (>{5})' .format( FormatUtils.formatMacString(fromMote), FormatUtils.formatMacString(toMote), linkRssi, self.THRES_LOW_RSSI, linkStability, self.THRES_LOW_STAB, ) ] # decide outcome if descFAIL: outcome = dld.TEST_OUTCOME_FAIL elif descPASS: outcome = dld.TEST_OUTCOME_PASS else: outcome = dld.TEST_OUTCOME_NOTRUN # write report description = [] if descPASS: description += ["PASS:"******"FAIL:"] description += descFAIL if descNOTRUN: description += ["NOTRUN:"] description += descNOTRUN description = '<br/>'.join(description) # return test result return (outcome, description)
def _nettest_oneSingleParentMote(self): ''' <p> This test verifies that there is exactly mote with only one parent. </p> <p> Graph theory indicates that, when building a bi-DAG, exactly one node ends up with one parent (it will be a one-hop neighbor of the root). This test verifies that this is the case in this network. </p> <p> This test is run once for the whole network. </p> ''' dld = DustLinkData.DustLinkData() descPASS = [] descFAIL = [] descNOTRUN = [] numParents = {} singleParentMotes = [] # get all the paths in the network currentPaths = dld.getNetworkPaths(self.netname) # count number of parents for each mote for mac in dld.getNetworkMotes(self.netname): numParents[mac] = 0 for (fromMote, toMote) in currentPaths: pathInfo = dld.getPathInfo(self.netname, fromMote, toMote) if fromMote == mac and pathInfo[ 'direction'] == 2 and pathInfo['numLinks'] > 0: numParents[mac] += 1 # count number of single-parents motes for (mac, n) in numParents.items(): if n == 1: singleParentMotes = [mac] # run test if len(singleParentMotes) == 1: descPASS += [ 'only mote {0} has a single parent'.format( FormatUtils.formatMacString(singleParentMotes[0]), ) ] else: description = [] description += [ 'The following {0} motes have one parent only: '.format( len(singleParentMotes)) ] description += [ ' '.join( FormatUtils.formatMacString(m) for m in singleParentMotes) ] description = ''.join(description) descPASS += [description] # decide outcome if descFAIL: outcome = dld.TEST_OUTCOME_FAIL elif descPASS: outcome = dld.TEST_OUTCOME_PASS else: outcome = dld.TEST_OUTCOME_NOTRUN # write report description = [] if descPASS: description += ["PASS:"******"FAIL:"] description += descFAIL if descNOTRUN: description += ["NOTRUN:"] description += descNOTRUN description = '<br/>'.join(description) # return test result return (outcome, description)
def _nettest_perMoteAvailability(self): ''' <p> This test verifies that the availability for each mote is above MIN_MOTEAVAILABILITY. </p> <p> The mote availability is the portion of the packets generated by the mote's application which were actually sent into the network. If the mote's protocol stack is busy, it will reject the application's data, resulting in a lower availability. </p> <p> This test is run once for each mote in the network. </p> ''' dld = DustLinkData.DustLinkData() descPASS = [] descFAIL = [] descNOTRUN = [] for mac in dld.getNetworkMotes(self.netname): moteinfo = dld.getMoteInfo(mac) #==== filter edge cases where the test can not be run if ('isAP' in moteinfo) and moteinfo['isAP'] == True: # don't run test on AP continue if 'numTxOk' not in moteinfo: descNOTRUN += [ 'This test could not run because mote {0} did not report any numTxOk counter (the counters it did report are {1}).' .format(FormatUtils.formatMacString(mac), moteinfo.keys()) ] continue if 'numTxFail' not in moteinfo: descNOTRUN += [ 'This test could not run because mote {0} did not report any numTxFail counter (the counters it did report are {1}).' .format(FormatUtils.formatMacString(mac), moteinfo.keys()) ] continue if not moteinfo['numTxOk']: descNOTRUN += [ 'This test could not run because mote {0} did not send any packets succesfully (yet?) (numTxOk=={1}) and so its\'s impossible to calculate a ratio.' .format(FormatUtils.formatMacString(mac), moteinfo['numTxOk']) ] continue #==== run the test availability = ( 1 - float(moteinfo['numTxFail']) / float(moteinfo['numTxOk'])) if availability < self.MIN_MOTEAVAILABILITY: descFAIL += [ 'availability for mote {0} is {1}, expected at least {2}.'. format(FormatUtils.formatMacString(mac), availability, self.MIN_MOTEAVAILABILITY) ] else: descPASS += [ 'availability for mote {0} is {1}, which is better than {2}.' .format(FormatUtils.formatMacString(mac), availability, self.MIN_MOTEAVAILABILITY) ] # decide outcome if descFAIL: outcome = dld.TEST_OUTCOME_FAIL elif descPASS: outcome = dld.TEST_OUTCOME_PASS else: outcome = dld.TEST_OUTCOME_NOTRUN # write report description = [] if descPASS: description += ["PASS:"******"FAIL:"] description += descFAIL if descNOTRUN: description += ["NOTRUN:"] description += descNOTRUN description = '<br/>'.join(description) # return test result return (outcome, description)
def getDataDyn(self, dynPath, subResource, username): netname = FormatUtils.unquote(dynPath) dld = DustLinkData.DustLinkData() if subResource == ['info']: netInfo = dld.getNetworkInfo(netname, username=username) if netInfo: return [{ 'name': k, 'value': v, 'type': 'text', 'editable': False, } for (k, v) in netInfo.items()] else: return [] elif subResource == ['topology']: return ( NetworksPages.topologyToDot(netname, username), WebHandler.WebHandler.ALREADY_JSONIFIED, ) elif subResource == ['motes']: # data data = [] for mac in dld.getNetworkMotes(netname, username=username): data.append([DustLinkData.DustLinkData.macToString(mac)]) # columnNames columnNames = ['mac'] return VizTable.VizTable.formatReturnVal(columnNames, data) elif subResource == ['paths']: with dld.dataLock: pathsToReturn = dld.getNetworkPaths(netname, username=username) # columnNames columnNames = [ 'from', 'to', 'direction', 'numLinks', 'quality' ] # data if pathsToReturn: data = [] for p in pathsToReturn: pathInfo = dld.getPathInfo(netname, p[0], p[1], username=username) if (pathInfo and ('direction' in pathInfo) and (pathInfo['direction'] in [2, 3]) and ('numLinks' in pathInfo) and ('quality' in pathInfo)): data += [[ DustLinkData.DustLinkData.macToString( p[0]), DustLinkData.DustLinkData.macToString( p[1]), IpMgrDefinition.IpMgrDefinition. fieldOptionToShortDesc( 'pathDirection', pathInfo['direction']), pathInfo['numLinks'], pathInfo['quality'], ]] else: data = [] return VizTable.VizTable.formatReturnVal(columnNames, data) else: raise web.notfound()
def deserialize(self,type,id,byteArray): notRcOk = False returnFields = {} nameArray = [self.ApiDef.idToName(type,id)] index = 0 # log if log.isEnabledFor(logging.DEBUG): output = [] output += ["deserialize ..."] output += ["- type: {0}".format(type)] output += ["- id: {0}".format(id)] output += ["- byteArray: {0}".format(FormatUtils.formatBuffer(byteArray))] output = '\n'.join(output) log.debug(output) continueParsing = True while continueParsing: fieldDefs = self.ApiDef.getResponseFields(type,nameArray) for fieldDef in fieldDefs: fieldMissing = False # isolate the piece of the byteArray corresponding to this field if fieldDef.length: # this field has an expected length thisFieldArray = byteArray[index:index+fieldDef.length] index += fieldDef.length if len(thisFieldArray)==0: # field missing: allowed fieldMissing = True elif len(thisFieldArray)<fieldDef.length: # incomplete field: not allowed raise CommandError( CommandError.TOO_FEW_BYTES, "incomplete field {0}".format(fieldDef.name), ) else: thisFieldArray = byteArray[index:] index = len(byteArray) if len(thisFieldArray)<1: # too few bytes fieldMissing = True # find thisFieldValue if fieldMissing: thisFieldValue = None else: if fieldDef.format==ApiDefinition.FieldFormats.STRING: thisFieldValue = '' for byte in thisFieldArray: thisFieldValue += chr(byte) elif fieldDef.format==ApiDefinition.FieldFormats.BOOL: if len(thisFieldArray)==1 and thisFieldArray[0]==0x00: thisFieldValue = False elif len(thisFieldArray)==1 and thisFieldArray[0]==0x01: thisFieldValue = True else: raise CommandError(CommandError.VALUE_NOT_IN_OPTIONS, "field="+fieldDef.name+" value="+str(thisFieldValue)) elif fieldDef.format==ApiDefinition.FieldFormats.INT: thisFieldValue = 0 for i in range(len(thisFieldArray)): thisFieldValue += thisFieldArray[i]*pow(2,8*(len(thisFieldArray)-i-1)) elif fieldDef.format==ApiDefinition.FieldFormats.INTS: tempList = [chr(i) for i in thisFieldArray] tempString = ''.join(tempList) if len(thisFieldArray)==1: (thisFieldValue,) = struct.unpack_from('>b',tempString) elif len(thisFieldArray)==2: (thisFieldValue,) = struct.unpack_from('>h',tempString) elif len(thisFieldArray)==4: (thisFieldValue,) = struct.unpack_from('>i',tempString) else: raise SystemError('field with format='+fieldDef.format+' and length='+str(fieldDef.length)+' unsupported.') elif fieldDef.format==ApiDefinition.FieldFormats.HEXDATA: thisFieldValue = thisFieldArray else: raise SystemError('unknown field format='+fieldDef.format) # make sure thisFieldValue in fieldDef.options if fieldDef.options.validOptions: if thisFieldValue not in fieldDef.options.validOptions: raise CommandError(CommandError.VALUE_NOT_IN_OPTIONS, "field="+fieldDef.name+" value="+str(thisFieldValue)) if fieldDef.name in ApiDefinition.ApiDefinition.RESERVED: # the subcommand specifier cannot be missing if thisFieldValue==None: raise CommandError( CommandError.TOO_FEW_BYTES, "reserved field missing {0}".format(fieldDef.name), ) idNextCommand = thisFieldValue else: returnFields[fieldDef.name] = thisFieldValue # stop if not RC_OK if ( (ApiDefinition.ApiDefinition.RC in returnFields) and ( returnFields[ApiDefinition.ApiDefinition.RC]!= \ ApiDefinition.ApiDefinition.RC_OK ) ): notRcOk = True break # continue if subCommand if ( (not notRcOk) and continueParsing and self.ApiDef.hasSubcommands(type,nameArray) ): # find name of subCommand nameArray.append(self.ApiDef.subcommandIdToName(type, nameArray, idNextCommand)) continueParsing = True else: continueParsing = False # stop if not RC_OK if ( continueParsing and notRcOk ): continueParsing = False # stop if end of packet reached if ( continueParsing and index>=len(byteArray) ): continueParsing = False if log.isEnabledFor(logging.DEBUG): output = [] output += ["... deserialized into"] output += ["- nameArray: {0}".format(nameArray)] output += ["- returnFields: {0}".format(returnFields)] output = '\n'.join(output) log.debug(output) return nameArray,returnFields
def _nettest_multipleJoins(self): ''' <p> This test verifies that each mote has joined exactly once. </p> <p> In a normal deployment, all motes should join exactly once. Joining more than once may indicate a mote reset. </p> <p> This test is run once for each node in the network (both AP and mote). </p> ''' dld = DustLinkData.DustLinkData() descPASS = [] descFAIL = [] descNOTRUN = [] # run test motesExactlyOnce = [] motesNotExactlyOnce = [] motesNotRun = [] for mac in dld.getNetworkMotes(self.netname): moteinfo = dld.getMoteInfo(mac) if ('numOperationalEvents' in moteinfo): if moteinfo['numOperationalEvents'] == 1: descPASS += [ '- {0} has numOperationalEvents=={1}'.format( FormatUtils.formatMacString(mac), moteinfo['numOperationalEvents'], ) ] else: descFAIL += [ '- {0} has numOperationalEvents=={1}'.format( FormatUtils.formatMacString(mac), moteinfo['numOperationalEvents'], ) ] else: if (('state' in moteinfo) and (moteinfo['state'] == 4)): descPASS += [ '- {0} has no numOperationalEvents parameters, but its state is {1}' .format( FormatUtils.formatMacString(mac), moteinfo['state'], ) ] else: descNOTRUN += [ '- {0} has neither numOperationalEvents, nor state attribute' .format(FormatUtils.formatMacString(mac), ) ] # decide outcome if descFAIL: outcome = dld.TEST_OUTCOME_FAIL elif descPASS: outcome = dld.TEST_OUTCOME_PASS else: outcome = dld.TEST_OUTCOME_NOTRUN # write report description = [] if descPASS: description += ["PASS</b>:"] description += descPASS if descFAIL: description += ["FAIL:"] description += descFAIL if descNOTRUN: description += ["NOTRUN:"] description += descNOTRUN description = '<br/>'.join(description) # return test result return (outcome, description)
def run(self): while True: elem = self.queue.get() if elem==self.CLOSE_MESSAGE: if self.publisher: self.publisher.close() return (mac,datastream,value) = elem AppData().incrementMoteCounter(mac,COL_NUMDATAPUB) if self.publisher==None: apiKey = AppData().getApiKey() if apiKey: self.publisher = xivelyConnector.xivelyConnector( apiKey = apiKey, productName = 'SmartMesh IP Starter Kit', productDesc = 'Manager {0}'.format( FormatUtils.formatMacString(AppData().getManager()), ), ) if self.publisher==None: continue try: # publish self.publisher.publish( mac = mac, datastream = datastream, value = value, ) # log output = [] output += ['pushed following data 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) except Exception as err: output = [] output += ['==============='] output += ['{0}: Exception when publishing'.format(self.name)] output += ['- mac: {0}'.format(FormatUtils.formatMacString(mac))] output += ['- datastream: {0}'.format(datastream)] output += ['{0}'.format(type(err))] output += ['{0}'.format(err)] output += [''] output = '\n'.join(output) log.error(output) print output else: AppData().incrementMoteCounter(mac,COL_NUMDATAPUBOK)
def _publish(self,sender,signal,data): now = time.time() dld = DustLinkData.DustLinkData() mac = data['mac'] #========== connect/disconnect if (self.lastCheck==None) or (now-self.lastCheck>self.CHECKDELAY): # remember I just checked self.lastCheck = now # we need to use "raw" access because dld.getPublisherSettings() # does not return all settings settings = dld.get(['system','publishers','xively']) # record the xivelyApiKey xivelyApiKey = None if ('xivelyApiKey' in settings) and settings['xivelyApiKey']: xivelyApiKey = settings['xivelyApiKey'] # update status if xivelyApiKey==None: with self.statusLock: self.status['apiKeySet'] = 'NO' else: with self.statusLock: self.status['apiKeySet'] = 'YES' # decide whether to connect/disconnect if (not self.connector) and xivelyApiKey: # connect # log log.info("Connecting to Xively") # remember API key self.xivelyApiKey = xivelyApiKey # connect try: self.connector = xivelyConnector.xivelyConnector( apiKey = self.xivelyApiKey, productName = 'SmartMesh IP Starter Kit', productDesc = 'Connecting using DustLink', ) except Exception as err: # log log.error("Error while connecting to Xively: {0}".format(err)) # update status with self.statusLock: self.status['status'] = 'CONNECTION FAILED' self.status['numConnectionsFailed']+= 1 # disconnect self._disconnect() else: # update status with self.statusLock: self.status['status'] = 'CONNECTED' self.status['numConnectionsOK'] += 1 self.status['lastConnected'] = dld.timestampToStringShort(now) elif ((self.connector) and (not xivelyApiKey)) or (self.xivelyApiKey!=xivelyApiKey): # disconnect self._disconnect() #========== publish data if self.connector: try: self.connector.publish( mac = data['mac'], datastream = data['type'], value = data['lastvalue'], ) except Exception as err: # log log.error( "Error while publishing to {0}/{1}: {2}".format( FormatUtils.formatMacString(mac), data['type'], err, ) ) # update status with self.statusLock: self.status['numPublishedFail'] += 1 # disconnect self._disconnect() else: # update status with self.statusLock: self.status['numPublishedOK'] += 1 #========== subscribe if self.connector: if mac not in self.subscribedMotes: try: if ('subscribeToLed' in data) and (data['subscribeToLed']): # create datastream self.connector.publish( mac = mac, datastream = 'led', value = 0, ) # subscribe self.connector.subscribe( mac = mac, datastream = 'led', callback = self._led_cb, ) except Exception as err: # log log.error( "Error while subscribing to {0}/{1}: {2}".format( FormatUtils.formatMacString(mac), 'led', err, ) ) # update status with self.statusLock: self.status['status'] = 'SUBSCRIPTION FAILED' self.status['numSubscriptionsFailed'] += 1 # disconnect self._disconnect() else: self.subscribedMotes += [mac]
def handle_data(notifName, notifParams, mymanager, networkID, timestamp): print notifName, "recieved from network: " + str(networkID) if notifName == "eventMoteJoin": print "mote's macAddress: ", notifParams.macAddress ############# NotifHealthReport ################### if notifName == "notifHealthReport": global firstNotifHandled global queueReady mac = FormatUtils.formatMacString(notifParams.macAddress) mac = mac.upper() hrParser = HrParser.HrParser() hr = hrParser.parseHr(notifParams.payload) try: res = mymanager.dn_getMoteConfig(notifParams.macAddress, False) print "MoteID: ", res.moteId, ", MAC: ", mac, ", AP:", res.isAP, ", State:", res.state, ", Routing:", res.isRouting moteId = res.moteId isAP = res.isAP isRouting = res.isRouting state = res.state except: print "error connecting to mote" moteId = -1 isAP = "unknown" isRouting = "unknown" state = "unknown" try: settingsDict = moteDict[str(mac)] except KeyError: print "macAddress: " + str(mac) + " not found in settings" ''' if 'settingsDict' not in locals(): try: settingsDict = moteDict[str(moteId)] except KeyError: print "moteId: " + str(moteId) + " not found in settings" if 'settingsDict' in locals(): if settingsDict['moteId'] != str(moteId): print "warning: moteId (" + settingsDict['moteId'] + ") in settings does not match actual moteId: " + str(moteId) x = settingsDict['x'] y = settingsDict['y'] z = settingsDict['z'] substrate = settingsDict['substrate'] antenna = settingsDict['antenna'] else: ''' x = '0' y = '0' z = '0' substrate = '0' antenna = '0' print "x ", x, "y ", x, "z ", z, "substrate: ", substrate, "antenna: ", antenna dataBaseJsonString = "" dataBaseJsonString += "{'Time': " + "'" + str(timestamp) + "' ," dataBaseJsonString += "'networkID' : " + str(networkID) + "," dataBaseJsonString += "'MAC' : " + mac + "," dataBaseJsonString += "'moteID' : " + str(moteId) + "," dataBaseJsonString += "'isAP' : " + str(isAP) + "," dataBaseJsonString += "'isRouting' : " + str(isRouting) + "," dataBaseJsonString += "'state' : " + str(state) + "," dataBaseJsonString += "'x' : " + str(x) + "," dataBaseJsonString += "'y' : " + str(y) + "," dataBaseJsonString += "'z' : " + str(z) + "," dataBaseJsonString += "'substrate' : " + str(substrate) + "," dataBaseJsonString += "'antenna' : " + str(antenna) + "," dataBaseJsonString += "'hr' : " + str(hr) + "," dataBaseJsonString += "'session_name': " + str( database_session['session_name']) + "," dataBaseJsonString += "'start_time': " + "'" + str( database_session['start_time']) + "'" dataBaseJsonString += '}' dataBaseYaml = yaml.load(dataBaseJsonString) dataBaseJson = json.dumps(dataBaseYaml) with open('datafile', 'ab+') as datafile: print timestamp print "mac:" + mac print "moteid: " + str(moteId) print "payload: " print hrParser.formatHr(hr) #if a health notification is already in the datafile, remove the ']}' at the end of the file #and write a ',' so the json in datafile is formatted properly if firstNotifHandled: datafile.seek(0, os.SEEK_END) pos = datafile.tell() - 1 while pos > 0 and datafile.read(1) != "\n": pos -= 1 datafile.seek(pos, os.SEEK_SET) if pos > 0: datafile.seek(pos, os.SEEK_SET) datafile.truncate() datafile.write(',\n') #write the health report to the datafile ''' datafile.write("\n{'Time':" + str(timestamp) + ",") datafile.write('\n') datafile.write("'networkID' : " + str(networkID) + ",") datafile.write('\n') datafile.write("'MAC' : " + mac + ",") datafile.write('\n') datafile.write("'moteID' : " + str(moteId) + ",") datafile.write('\n') datafile.write("'isAP' : " + str(isAP) + ",") datafile.write('\n') datafile.write("'isRouting' : " + str(isRouting) + ",") datafile.write('\n') datafile.write("'state' : " + str(state) + ",") datafile.write('\n') datafile.write(str(hr)) datafile.write('}') datafile.write('\n') datafile.write(']}') datafile.write('\n') ''' datafile.write('\n' + str(dataBaseJson)) datafile.write('\n') datafile.write(']}') datafile.write('\n') print "health report handled successfully and added to datafile\n" sendJSONtoServer(dataBaseJson) firstNotifHandled = True
def __init__(self,connector,connectParams): assert isinstance(connector,ApiConnector.ApiConnector) assert isinstance(connectParams,(str,tuple)) # record parameters self.connector = connector self.connectParams = connectParams # log log.info("creating instance") # variables self.netname = FormatUtils.formatConnectionParams(self.connectParams) self.statsLock = threading.Lock() self._clearStats() # start snapshot thread this manager self.snapShotThread = SnapShot.SnapShot(connector,connectParams) # subscribe to flows try: self.subscriber = IpMgrSubscribe.IpMgrSubscribe(self.connector) self.subscriber.start() self.subscriber.subscribe( notifTypes = [ IpMgrSubscribe.IpMgrSubscribe.NOTIFDATA, ], fun = self._subs_notifData, isRlbl = False, ) self.subscriber.subscribe( notifTypes = [ IpMgrSubscribe.IpMgrSubscribe.NOTIFEVENT, ], fun = self._subs_notifEvent, isRlbl = True, ) self.subscriber.subscribe( notifTypes = [ IpMgrSubscribe.IpMgrSubscribe.NOTIFHEALTHREPORT, ], fun = self._subs_notifHealthReport, isRlbl = True, ) self.subscriber.subscribe( notifTypes = [ IpMgrSubscribe.IpMgrSubscribe.ERROR, IpMgrSubscribe.IpMgrSubscribe.FINISH, ], fun = self._subs_errorORfinish, isRlbl = True, ) except TypeError as err: log.error(str(err)) raise ApiException.ConnectionError(str(err)) self.subscriber._thread.name = '{0}_IpMgrSubscribe'.format(self.netname) # initialize parent class EventBusClient.EventBusClient.__init__(self, 'dataToMesh_{0}'.format(self.netname), self._ebHandler_dataToMesh, ) # give this thread a name self.name = '{0}_GatewayListener'.format(self.netname)
def postDataDyn(self, receivedData, dynPath, subResource, username): netname = FormatUtils.unquote(dynPath) raise web.notfound()
def _nettest_numLinks(self): ''' <p> This test verifies that the number of links assigned to each mote does not exceed the maximum limit. </p> <p> The manager is never supposed to allocate more than MAX_AP_RXLINKS receive links to the AP, nor more than MAX_MOTE_LINKS links (both transmit and receive) for a non-AP mote. </p> <p> This test is run once for each node in the network (both AP and mote). </p> ''' dld = DustLinkData.DustLinkData() descPASS = [] descFAIL = [] descNOTRUN = [] # get all the paths in the network currentPaths = dld.getNetworkPaths(self.netname) for mac in dld.getNetworkMotes(self.netname): (numTx, numRx) = self._countTxRxLinks(currentPaths, mac) moteinfo = dld.getMoteInfo(mac) if moteinfo['isAP']: if numRx < self.MAX_AP_RXLINKS: descPASS += [ 'AP {0} has {1} RX links, less than maximum {2}'. format(FormatUtils.formatMacString(mac), numRx, self.MAX_AP_RXLINKS) ] else: descFAIL += [ 'AP {0} has {1} RX links, more than maximum {2}'. format(FormatUtils.formatMacString(mac), numRx, self.MAX_AP_RXLINKS) ] else: numLinks = numTx + numRx if numLinks < self.MAX_MOTE_LINKS: descPASS += [ 'mote {0} has {1} links, less than maximum {2}'.format( FormatUtils.formatMacString(mac), numLinks, self.MAX_MOTE_LINKS) ] else: descFAIL += [ 'mote {0} has {1} links, more than maximum {2}'.format( FormatUtils.formatMacString(mac), numLinks, self.MAX_MOTE_LINKS) ] # decide outcome if descFAIL: outcome = dld.TEST_OUTCOME_FAIL elif descPASS: outcome = dld.TEST_OUTCOME_PASS else: outcome = dld.TEST_OUTCOME_NOTRUN # write report description = [] if descPASS: description += ["PASS:"******"FAIL:"] description += descFAIL if descNOTRUN: description += ["NOTRUN:"] description += descNOTRUN description = '<br/>'.join(description) # return test result return (outcome, description)
def getDataDyn(self,dynPath,subResource,username): netname = FormatUtils.unquote(dynPath) dld = DustLinkData.DustLinkData() if subResource==['testschedule']: period_min = float(dld.getTestPeriod(netname,username=username))/60.0 secondsToNextRaw = dispatcher.send( signal = 'timeToNextSnapShot_{0}'.format(netname), data = None, ) assert len(secondsToNextRaw)==1 secondsToNext = secondsToNextRaw[0][1] return [ { 'name': 'period (min)', 'value': period_min, 'type': 'text', 'editable': True, }, { 'name': 'next test in', 'value': '{0}s'.format(secondsToNext), 'type': 'text', 'editable': False, }, { 'name': ' ', 'value': 'Run tests now', 'type': 'button', }, ] elif subResource==['testresults']: testResults = dld.getResults(netname,username=username) # fill in data data = [] if testResults: for (testName,res) in testResults.items(): # outcomeIcon if res['last']['outcome']==dld.TEST_OUTCOME_PASS: outcomeIcon = 'pass.png' elif res['last']['outcome']==dld.TEST_OUTCOME_FAIL: outcomeIcon = 'fail.png' elif res['last']['outcome']==dld.TEST_OUTCOME_NOTRUN: outcomeIcon = 'notrun.png' else: SystemError("outcome {0} not expected".format(res['outcome'])) # outcomeDesc outcomeDesc = res['last']['outcome'] # weatherIcon numPass = res['history'].count(dld.TEST_OUTCOME_PASS) numFail = res['history'].count(dld.TEST_OUTCOME_FAIL) if numFail==0 and numFail==0: weatherScore = 1.0 else: weatherScore = float(numPass)/float(numPass+numFail) if weatherScore>0.8: weatherIcon = 'weather-80plus.png' elif weatherScore>0.6: weatherIcon = 'weather-60to79.png' elif weatherScore>0.4: weatherIcon = 'weather-40to59.png' elif weatherScore>0.2: weatherIcon = 'weather-20to39.png' else: weatherIcon = 'weather-00to19.png' # weatherDesc weatherDesc = [] weatherDesc += ['<ul>'] weatherDesc += ['<li>{0} tests PASS</li>'.format(numPass)] weatherDesc += ['<li>{0} tests FAIL</li>'.format(numFail)] weatherDesc += ['</ul>'] weatherDesc = '\n'.join(weatherDesc) # testName testName = testName.replace('_nettest_','',1) # testDesc testDesc = res['description'] # lastRun lastRun = DustLinkData.DustLinkData.timestampToStringShort( res['last']['timestamp'] ) # lastRunDesc lastRunDesc = res['last']['description'] # lastSuccess if res['lastSuccess']['timestamp']: lastSuccess = DustLinkData.DustLinkData.timestampToStringShort( res['lastSuccess']['timestamp'] ) else: lastSuccess = 'N/A' # lastSuccessDesc lastSuccessDesc = res['lastSuccess']['description'] # lastFailure if res['lastFailure']['timestamp']: lastFailure = DustLinkData.DustLinkData.timestampToStringShort( res['lastFailure']['timestamp'] ) else: lastFailure = 'N/A' # lastFailureDesc lastFailureDesc = res['lastFailure']['description'] data += [ { 'outcomeIcon': outcomeIcon, 'outcomeDesc': outcomeDesc, 'weatherIcon': weatherIcon, 'weatherDesc': weatherDesc, 'testName': testName, 'testDesc': testDesc, 'lastRun': lastRun, 'lastRunDesc': lastRunDesc, 'lastSuccess': lastSuccess, 'lastSuccessDesc': lastSuccessDesc, 'lastFailure': lastFailure, 'lastFailureDesc': lastFailureDesc, }, ] return data elif subResource==['testreset']: return [ { 'name': 'command', 'value': '', 'type': 'text', } ] else: raise web.notfound()
def _nettest_numGoodNeighbors(self): ''' <p> This test verifies that each mote has enough good neighbors. </p> <p> The manager can build a robust network if each mote in the network has at least MIN_NUMGOODNEIGHBORS neighbors. </p> <p> This test is run once for each mote in the network. </p> ''' dld = DustLinkData.DustLinkData() descPASS = [] descFAIL = [] descNOTRUN = [] for mac in dld.getNetworkMotes(self.netname): moteinfo = dld.getMoteInfo(mac) if 'numGoodNbrs' not in moteinfo: descNOTRUN += [ 'This test could not run because mote {0} did not report any numGoodNbrs counter (the counters it did report are {1}).' .format(FormatUtils.formatMacString(mac), moteinfo.keys()) ] elif moteinfo['numGoodNbrs'] < self.MIN_NUMGOODNEIGHBORS: descFAIL += [ 'mote {0} has {1} good neighbors, expected at least {2}.'. format(FormatUtils.formatMacString(mac), moteinfo['numGoodNbrs'], self.MIN_NUMGOODNEIGHBORS) ] else: descPASS += [ 'mote {0} has {1} good neighbors, which is more than {2}.'. format(FormatUtils.formatMacString(mac), moteinfo['numGoodNbrs'], self.MIN_NUMGOODNEIGHBORS) ] # decide outcome if descFAIL: outcome = dld.TEST_OUTCOME_FAIL elif descPASS: outcome = dld.TEST_OUTCOME_PASS else: outcome = dld.TEST_OUTCOME_NOTRUN # write report description = [] if descPASS: description += ["PASS:"******"FAIL:"] description += descFAIL if descNOTRUN: description += ["NOTRUN:"] description += descNOTRUN description = '<br/>'.join(description) # return test result return (outcome, description)
def serialize(self,commandArray,fieldsToFill): # log if log.isEnabledFor(logging.DEBUG): output = [] output += ["serialize ..."] output += ["- commandArray: {0}".format(commandArray)] output += ["- fieldsToFill: {0}".format(fieldsToFill)] output = '\n'.join(output) log.debug(output) # validate input if type(commandArray)!=types.ListType and type(commandArray)!=types.TupleType: raise TypeError("First parameter should be a list or tuple, not "+str(type(commandArray))) # initialize the output byteArray = [] for cmdCounter in range(len(commandArray)): # packet payload definition = self.ApiDef.getDefinition( ApiDefinition.ApiDefinition.COMMAND, commandArray[:cmdCounter+1] ) fields = [ApiDefinition.Field(fieldRaw,self.ApiDef.fieldOptions) for fieldRaw in definition['request']] for field in fields: thisFieldByteArray = [] if field.name in ApiDefinition.ApiDefinition.RESERVED: thisFieldByteArray.append( self.ApiDef.subcommandNameToId( ApiDefinition.ApiDefinition.COMMAND, commandArray[:cmdCounter+1], commandArray[cmdCounter+1] ) ) else: val = fieldsToFill[field.name] if field.format==ApiDefinition.FieldFormats.STRING: thisFieldByteArray += [ord(car) for car in val] elif field.format==ApiDefinition.FieldFormats.BOOL: thisFieldByteArray.append(val) elif field.format==ApiDefinition.FieldFormats.INT: thisFieldByteArray += [operator.mod(int(val>>(8*i)), 0x100) for i in xrange(field.length-1, -1, -1)] elif field.format==ApiDefinition.FieldFormats.INTS: if field.length==1: temp = struct.pack('>b',int(val)) elif field.length==2: temp = struct.pack('>h',int(val)) elif field.length==4: temp = struct.pack('>i',int(val)) else: raise SystemError('field with format='+field.format+' and length='+str(field.length)+' unsupported.') for i in range(len(temp)): thisFieldByteArray.append(ord(temp[i])) elif field.format==ApiDefinition.FieldFormats.HEXDATA: thisFieldByteArray += val else: raise SystemError('unknown field format='+field.format) # padding while len(thisFieldByteArray)<field.length: thisFieldByteArray = [0x00]+thisFieldByteArray byteArray = byteArray+thisFieldByteArray cmdId = self.ApiDef.nameToId(ApiDefinition.ApiDefinition.COMMAND,commandArray) if log.isEnabledFor(logging.DEBUG): output = [] output += ["... serialize into"] output += ["- cmdId: {0}".format(cmdId)] output += ["- byteArray: {0}".format(FormatUtils.formatBuffer(byteArray))] output = '\n'.join(output) log.debug(output) return cmdId,byteArray
def handle_data(notifName, notifParams, mymanager, networkID, timestamp): print notifName, "recieved from network: " + str(networkID) if notifName == "eventMoteJoin": print "mote's macAddress: ", notifParams.macAddress ############# NotifHealthReport ################### if notifName == "notifHealthReport": global firstNotifHandled global queueReady mac = FormatUtils.formatMacString(notifParams.macAddress) mac = mac.upper() hrParser = HrParser.HrParser() hr = hrParser.parseHr(notifParams.payload) try: res = mymanager.dn_getMoteConfig(notifParams.macAddress,False) print "MoteID: ", res.moteId,", MAC: ",mac,", AP:", res.isAP,", State:", res.state, ", Routing:", res.isRouting moteId = res.moteId isAP = res.isAP isRouting = res.isRouting state = res.state except: print "error connecting to mote" moteId = -1 isAP = "unknown" isRouting = "unknown" state = "unknown" try: settingsDict = moteDict[str(mac)] except KeyError: print "macAddress: " + str(mac) + " not found in settings" ''' if 'settingsDict' not in locals(): try: settingsDict = moteDict[str(moteId)] except KeyError: print "moteId: " + str(moteId) + " not found in settings" if 'settingsDict' in locals(): if settingsDict['moteId'] != str(moteId): print "warning: moteId (" + settingsDict['moteId'] + ") in settings does not match actual moteId: " + str(moteId) x = settingsDict['x'] y = settingsDict['y'] z = settingsDict['z'] substrate = settingsDict['substrate'] antenna = settingsDict['antenna'] else: ''' x = '0' y = '0' z = '0' substrate = '0' antenna = '0' print "x ", x, "y ", x, "z ", z, "substrate: ", substrate, "antenna: ", antenna dataBaseJsonString = "" dataBaseJsonString += "{'Time': " + "'" + str(timestamp) + "' ," dataBaseJsonString += "'networkID' : " + str(networkID) + "," dataBaseJsonString += "'MAC' : " + mac + "," dataBaseJsonString += "'moteID' : " + str(moteId) + "," dataBaseJsonString += "'isAP' : " + str(isAP) + "," dataBaseJsonString += "'isRouting' : " + str(isRouting) + "," dataBaseJsonString += "'state' : " + str(state) + "," dataBaseJsonString += "'x' : " + str(x) + "," dataBaseJsonString += "'y' : " + str(y) + "," dataBaseJsonString += "'z' : " + str(z) + "," dataBaseJsonString += "'substrate' : " + str(substrate) + "," dataBaseJsonString += "'antenna' : " + str(antenna) + "," dataBaseJsonString += "'hr' : " + str(hr) + "," dataBaseJsonString += "'session_name': " + str(database_session['session_name']) + "," dataBaseJsonString += "'start_time': " + "'" + str(database_session['start_time']) + "'" dataBaseJsonString += '}' dataBaseYaml = yaml.load(dataBaseJsonString) dataBaseJson = json.dumps(dataBaseYaml) with open('datafile', 'ab+') as datafile: print timestamp print "mac:" + mac print "moteid: " + str(moteId) print "payload: " print hrParser.formatHr(hr) #if a health notification is already in the datafile, remove the ']}' at the end of the file #and write a ',' so the json in datafile is formatted properly if firstNotifHandled: datafile.seek(0, os.SEEK_END) pos = datafile.tell() - 1 while pos > 0 and datafile.read(1) != "\n": pos -= 1 datafile.seek(pos, os.SEEK_SET) if pos > 0: datafile.seek(pos, os.SEEK_SET) datafile.truncate() datafile.write(',\n') #write the health report to the datafile ''' datafile.write("\n{'Time':" + str(timestamp) + ",") datafile.write('\n') datafile.write("'networkID' : " + str(networkID) + ",") datafile.write('\n') datafile.write("'MAC' : " + mac + ",") datafile.write('\n') datafile.write("'moteID' : " + str(moteId) + ",") datafile.write('\n') datafile.write("'isAP' : " + str(isAP) + ",") datafile.write('\n') datafile.write("'isRouting' : " + str(isRouting) + ",") datafile.write('\n') datafile.write("'state' : " + str(state) + ",") datafile.write('\n') datafile.write(str(hr)) datafile.write('}') datafile.write('\n') datafile.write(']}') datafile.write('\n') ''' datafile.write('\n' + str(dataBaseJson)) datafile.write('\n') datafile.write(']}') datafile.write('\n') print "health report handled successfully and added to datafile\n" sendJSONtoServer(dataBaseJson) firstNotifHandled = True