def __init__(self): # OAP dispatcher AppData().set('oap_dispatch',OAPDispatcher.OAPDispatcher()) AppData().get('oap_dispatch').register_notif_handler(self._handle_oap_notif) # subscriber self.subscriber = IpMgrSubscribe.IpMgrSubscribe(AppData().get('connector')) self.subscriber.start() self.subscriber.subscribe( notifTypes = [ IpMgrSubscribe.IpMgrSubscribe.NOTIFDATA, ], fun = self._cb_NOTIFDATA, isRlbl = False, ) # list operational motes AppData().set('oap_clients',{}) numMotes = getOperationalMotes() if numMotes: printOperationalMotes() selectOperationalMote(0) AppData().set('printNotifs',False) togglePrintNotifs() self.log_file = open(LOGFILE, 'w') AppData().set('logNotifs',True) toggleLogNotifs() # Added to calculate Mgr vs system time offset in the log prints self.mapmgrtime = MgrTime(0, 20) self.mapmgrtime.start()
def __init__(self): self.fileLock = threading.RLock() self.hdlc = OpenHdlc.OpenHdlc() self.hrParser = HrParser.HrParser() self.api = IpMgrDefinition.IpMgrDefinition() self.connSerial = IpMgrConnectorSerial.IpMgrConnectorSerial() self.oapLock = threading.RLock() self.oap = OAPDispatcher.OAPDispatcher() self.oap.register_notif_handler(self._handle_oap_notif)
def __init__(self, apiDef, connector, disconnectedCallback): # store params self.apiDef = apiDef self.connector = connector self.disconnectedCallback = disconnectedCallback # log log.debug("Initialize notifClient") # variables self.dataLock = threading.RLock() self.isMoteActive = {} self.tabledata = {} self.tableupdates = {} # subscriber self.subscriber = IpMgrSubscribe.IpMgrSubscribe(self.connector) self.subscriber.start() self.subscriber.subscribe( notifTypes = [ NOTIFDATA, ], fun = self._notifDataCallbackTunneled, isRlbl = False, ) self.subscriber.subscribe( notifTypes = [ NOTIFEVENT, ], fun = self._notifEventCallback, isRlbl = True, ) self.subscriber.subscribe( notifTypes = [ NOTIFHEALTHREPORT, ], fun = self._notifHrCallback, isRlbl = True, ) self.subscriber.subscribe( notifTypes = [ IpMgrSubscribe.IpMgrSubscribe.ERROR, IpMgrSubscribe.IpMgrSubscribe.FINISH, ], fun = self.disconnectedCallback, isRlbl = True, ) # OAP dispatcher self.oap_dispatch = OAPDispatcher.OAPDispatcher() self.oap_dispatch.register_notif_handler(self._handle_oap_notif)
def __init__(self, autoaddmgr, autodeletemgr, serialport, notifCb, configfilename=None): # store params self.autoaddmgr = autoaddmgr self.autodeletemgr = autodeletemgr self.serialport = serialport self.notifCb = notifCb self.configfilename = configfilename # local variables self.startTime = time.time() self.dataLock = threading.RLock() self._loadConfig() # populates self.config dictionnary self.managerHandlers = {} self.oapDispatch = OAPDispatcher.OAPDispatcher() self.oapDispatch.register_notif_handler( self._manager_oap_notif_handler) self.oapClients = {} self.snapshotThread = SnapshotThread( self.raw_POST, self.notifCb, ) self.outstandingEvents = {} self.responses = {} self.hrParser = HrParser.HrParser() # connect to managers (if any) self._syncManagers() # if autoaddmgr, have SerialScanner looks for manager if self.autoaddmgr: self.serialScanner = SerialScanner.SerialScanner() self.serialScanner.availableManagerNotifier( cb=self._availablemanagers_cb, period=60, ) # if autodeletemgr, start DeleMgrThread if self.autodeletemgr: DeleMgrThread(self)
def __init__(self, apiDef, connector, disconnectedCallback, latencyCalculator): # store params self.apiDef = apiDef self.connector = connector self.disconnectedCallback = disconnectedCallback self.latencyCalculator = latencyCalculator # variables self.dataLock = threading.Lock() self.isMoteActive = {} self.data = {} self.updates = {} # subscriber self.subscriber = IpMgrSubscribe.IpMgrSubscribe(self.connector) self.subscriber.start() self.subscriber.subscribe( notifTypes = [ IpMgrSubscribe.IpMgrSubscribe.NOTIFDATA, ], fun = self._dataCallback, isRlbl = False, ) self.subscriber.subscribe( notifTypes = [ IpMgrSubscribe.IpMgrSubscribe.NOTIFEVENT, ], fun = self._eventCallback, isRlbl = True, ) self.subscriber.subscribe( notifTypes = [ IpMgrSubscribe.IpMgrSubscribe.ERROR, IpMgrSubscribe.IpMgrSubscribe.FINISH, ], fun = self.disconnectedCallback, isRlbl = True, ) # OAP dispatcher self.oap_dispatch = OAPDispatcher.OAPDispatcher() self.oap_dispatch.register_notif_handler(self._handle_oap_notif)
def __init__(self): # log log.info('creating instance') # initialize parent class EventBusClient.EventBusClient.__init__( self, 'notifData', self._identifyApp, queuesize=self.QUEUESIZE, ) self.name = 'DataConnector_AppIdentifier' # add stats # local variables self.oap_dispatch = OAPDispatcher.OAPDispatcher() self.oap_dispatch.register_notif_handler(self._handle_oap_notif) self.appTransports = {}
def run(self): while self.goOn: try: print 'Connecting to {0}...'.format(self.serialPort), self.connector = IpMgrConnectorSerial.IpMgrConnectorSerial() self.connector.connect({'port': self.serialPort}) subscriber = IpMgrSubscribe.IpMgrSubscribe(self.connector) subscriber.start() subscriber.subscribe( notifTypes=[ IpMgrSubscribe.IpMgrSubscribe.ERROR, IpMgrSubscribe.IpMgrSubscribe.FINISH, ], fun=self._handle_ErrorFinish, isRlbl=True, ) subscriber.subscribe( notifTypes=[ IpMgrSubscribe.IpMgrSubscribe.NOTIFDATA, ], fun=self._handle_data, isRlbl=False, ) self.oap_dispatch = OAPDispatcher.OAPDispatcher() self.oap_dispatch.register_notif_handler(self._handle_oap) AppData().setConnector(self.connector) except Exception as err: print 'FAIL:' print err time.sleep(1) else: print 'PASS.' self.reconnectEvent.clear() self.reconnectEvent.wait() finally: try: self.connector.disconnect() except: pass
def __init__(self, connector, disconnectedCallback): # store params self.connector = connector self.disconnectedCallback = disconnectedCallback self.oap_clients = {} # variables # subscriber self.subscriber = IpMgrSubscribe.IpMgrSubscribe(self.connector) self.subscriber.start() self.subscriber.subscribe( notifTypes=[ IpMgrSubscribe.IpMgrSubscribe.NOTIFEVENT, ], fun=self._eventHandler, isRlbl=True, ) self.subscriber.subscribe( notifTypes=[ IpMgrSubscribe.IpMgrSubscribe.ERROR, IpMgrSubscribe.IpMgrSubscribe.FINISH, ], fun=self._errorHandler, isRlbl=True, ) self.subscriber.subscribe( notifTypes=[ IpMgrSubscribe.IpMgrSubscribe.NOTIFDATA, ], fun=self._notifDataHandler, isRlbl=False, ) # OAP dispatcher self.oap_dispatch = OAPDispatcher.OAPDispatcher() self.oap_dispatch.register_notif_handler(self._handle_oap_notif)
def _connectionFrameCb_connected(self, connector): ''' \brief Called when the connectionFrame has connected. ''' # store the connector self.connector = connector # schedule the GUI to update self.LEDPingFrame.after(LEDUPDATEPERIOD, self._gui_refresh_led) self.LEDPingFrame.after(RATEUPDATEPERIOD, self._gui_refresh_rate) # have LEDPingFrame enable button self.LEDPingFrame.enableButton() # OAP dispatcher self.oap_dispatch = OAPDispatcher.OAPDispatcher() # create a subscriber self.subscriber = IpMgrSubscribe.IpMgrSubscribe(self.connector) self.subscriber.start() self.subscriber.subscribe( notifTypes=[ IpMgrSubscribe.IpMgrSubscribe.NOTIFDATA, ], fun=self.oap_dispatch.dispatch_pkt, isRlbl=False, ) self.subscriber.subscribe( notifTypes=[ IpMgrSubscribe.IpMgrSubscribe.ERROR, IpMgrSubscribe.IpMgrSubscribe.FINISH, ], fun=self._notifClientCb_disconnected, isRlbl=True, )
def __init__(self, apiDef, connector, disconnectedCallback): # store params self.apiDef = apiDef self.connector = connector self.disconnectedCallback = disconnectedCallback # log log.debug("Initialize notifClient") # variables self.dataLock = threading.Lock() self.isMoteActive = {} self.data = {} self.updates = {} self.rateCalc = {} # OAP dispatcher self.oap_dispatch = OAPDispatcher.OAPDispatcher() self.oap_dispatch.register_notif_handler(self._handle_oap_notif) # subscriber if isinstance(self.apiDef, IpMgrDefinition.IpMgrDefinition): # we are connected to an IP manager self.subscriber = IpMgrSubscribe.IpMgrSubscribe(self.connector) self.subscriber.start() self.subscriber.subscribe( notifTypes=[ IpMgrSubscribe.IpMgrSubscribe.NOTIFDATA, ], fun=self._dataCallback, isRlbl=False, ) self.subscriber.subscribe( notifTypes=[ IpMgrSubscribe.IpMgrSubscribe.NOTIFEVENT, ], fun=self._eventCallback, isRlbl=True, ) self.subscriber.subscribe( notifTypes=[ IpMgrSubscribe.IpMgrSubscribe.ERROR, IpMgrSubscribe.IpMgrSubscribe.FINISH, ], fun=self.disconnectedCallback, isRlbl=True, ) elif isinstance(self.apiDef, HartMgrDefinition.HartMgrDefinition): # we are connected to a HART manager self.subscriber = HartMgrSubscriber(self.connector, self._dataCallback, self._eventCallback, self.disconnectedCallback) self.subscriber.start() else: output = "apiDef of type {0} unexpected".format(type(self.apiDef)) log.critical(output) print output raise SystemError(output)
print 't={TEMP:.2f}C at {MAC}'.format( TEMP=float(notif.samples[0]) / 100, MAC=FormatUtils.formatMacString(mac), ) simple_data_Logging(mac, notif.samples) # ============================ main ============================================ # print banner print 'TempLogger - (c) Dust Networks' print 'SmartMesh SDK {0}'.format('.'.join( [str(b) for b in sdk_version.VERSION])) # set up the OAP dispatcher (which parses OAP packets) oapdispatcher = OAPDispatcher.OAPDispatcher() oapdispatcher.register_notif_handler(handle_oap_data) # start logging file logFile = open("sampleLog.log", "w") logFile.write('Date: ' + datetime.datetime.now().strftime('%m/%d/%Y' + '\n')) logFile.close() # ask user for serial port number serialport = raw_input( '\nSmartMesh IP manager\'s API serial port (leave blank for ' + DEFAULT_SERIALPORT + '): ') if not serialport.strip(): serialport = DEFAULT_SERIALPORT
def _notifCallback(self, notifName, notifParams): try: if (notifName == IpMgrSubscribe.IpMgrSubscribe.NOTIFHEALTHREPORT): print notifName mac = FormatUtils.formatMacString(notifParams.macAddress) hr = self.hrParser.parseHr(notifParams.payload) print mac, hr db = Database() print hr if 'Device' in hr: Db_array = [] voltage = hr['Device']['batteryVoltage'] # Db_array.append(voltage) print 'voltage', voltage / 1000 packetloss = hr['Device']['numRxLost'] #send to Db_array and reset Db_array Db_array.append(mac) Db_array.append(timeStamp()) Db_array.append(epochTimeStamp()) Db_array.append(voltage / 1000.0) Db_array.append(packetloss) insert_query = """ INSERT INTO health_report_table (`hr_macid`,`hr_timeStamp`,`hr_epochStamp`,`hr_batt_voltage`,`hr_packetloss`) VALUES (%s,%s,%s,%s,%s)""" print insert_query, Db_array db.insert(insert_query, Db_array) elif 'Neighbors' in hr: average = [] Db_array = [] for neighbor in hr['Neighbors']['neighbors']: average.append(neighbor['rssi']) print average avg_rssi_signal = numpy.mean(average) Db_array.append(mac) Db_array.append(timeStamp()) Db_array.append(epochTimeStamp()) Db_array.append(avg_rssi_signal) print "signal strength: ", avg_rssi_signal insert_query = """ INSERT INTO health_report_table (`hr_macid`,`hr_timeStamp`,`hr_epochStamp`,`hr_avg_rssi`) VALUES (%s,%s,%s,%s)""" print insert_query, Db_array db.insert(insert_query, Db_array) elif (notifName == IpMgrSubscribe.IpMgrSubscribe.NOTIFDATA): #print notifName #print notifParams oapdispatcher = OAPDispatcher.OAPDispatcher() oapdispatcher.register_notif_handler(handle_oap_data) oapdispatcher.dispatch_pkt(notifName, notifParams) except Exception as err: print type(err) print err raise
def __init__(self, tcpport, serialport, notifprefix, configfilename): # store params self.tcpport = tcpport self.serialport = serialport self.notifprefix = notifprefix self.configfilename = configfilename # local variables self.startTime = time.time() self.dataLock = threading.RLock() self._loadConfig() # populates self.config dictionnary self.managerHandlers = {} self.oapDispatch = OAPDispatcher.OAPDispatcher() self.oapDispatch.register_notif_handler( self._manager_oap_notif_handler) self.oapClients = {} self.outstandingEvents = {} self.responses = {} self.hrParser = HrParser.HrParser() #=== CLI interface self.cli = DustCli.DustCli("JsonServer", self._clihandle_quit) self.cli.registerCommand( name='status', alias='s', description='get the current status of the application', params=[], callback=self._clihandle_status, ) self.cli.registerCommand( name='seriaports', alias='sp', description='list the available serialports', params=[], callback=self._clihandle_serialports, ) self.cli.registerCommand( name='connectmanager', alias='cm', description='connect to a manager\'s API serial port', params=['serialport'], callback=self._clihandle_connectmanager, ) self.cli.registerCommand( name='disconnectmanager', alias='dm', description='disconnect from a manager\'s API serial port', params=['serialport'], callback=self._clihandle_disconnectmanager, ) #=== web server self.websrv = bottle.Bottle() #=== root self.websrv.route('/', 'GET', self._webhandle_root_GET) #=== static self.websrv.route('/static/<filename>', 'GET', self._webhandle_static) #=== status self.websrv.route('/api/v1/status', 'GET', self._webhandle_status_GET) #=== raw self.websrv.route('/api/v1/raw', 'POST', self._webhandle_raw_POST) #=== oap # /info self.websrv.route('/api/v1/oap/<mac>/info', 'GET', self._webhandle_oap_info_GET) self.websrv.route('/api/v1/oap/<mac>/0', 'GET', self._webhandle_oap_info_GET) # /main self.websrv.route('/api/v1/oap/<mac>/main', 'GET', self._webhandle_oap_main_GET) self.websrv.route('/api/v1/oap/<mac>/1', 'GET', self._webhandle_oap_main_GET) self.websrv.route('/api/v1/oap/<mac>/main', 'PUT', self._webhandle_oap_main_PUT) self.websrv.route('/api/v1/oap/<mac>/1', 'PUT', self._webhandle_oap_main_PUT) # /digital_in self.websrv.route('/api/v1/oap/<mac>/digital_in/D0', 'GET', self._webhandle_oap_digital_in_D0_GET) self.websrv.route('/api/v1/oap/<mac>/2/0', 'GET', self._webhandle_oap_digital_in_D0_GET) self.websrv.route('/api/v1/oap/<mac>/digital_in/D0', 'PUT', self._webhandle_oap_digital_in_D0_PUT) self.websrv.route('/api/v1/oap/<mac>/2/0', 'PUT', self._webhandle_oap_digital_in_D0_PUT) self.websrv.route('/api/v1/oap/<mac>/digital_in/D1', 'GET', self._webhandle_oap_digital_in_D1_GET) self.websrv.route('/api/v1/oap/<mac>/2/1', 'GET', self._webhandle_oap_digital_in_D1_GET) self.websrv.route('/api/v1/oap/<mac>/digital_in/D1', 'PUT', self._webhandle_oap_digital_in_D1_PUT) self.websrv.route('/api/v1/oap/<mac>/2/1', 'PUT', self._webhandle_oap_digital_in_D1_PUT) self.websrv.route('/api/v1/oap/<mac>/digital_in/D2', 'GET', self._webhandle_oap_digital_in_D2_GET) self.websrv.route('/api/v1/oap/<mac>/2/2', 'GET', self._webhandle_oap_digital_in_D2_GET) self.websrv.route('/api/v1/oap/<mac>/digital_in/D2', 'PUT', self._webhandle_oap_digital_in_D2_PUT) self.websrv.route('/api/v1/oap/<mac>/2/2', 'PUT', self._webhandle_oap_digital_in_D2_PUT) self.websrv.route('/api/v1/oap/<mac>/digital_in/D3', 'GET', self._webhandle_oap_digital_in_D3_GET) self.websrv.route('/api/v1/oap/<mac>/2/3', 'GET', self._webhandle_oap_digital_in_D3_GET) self.websrv.route('/api/v1/oap/<mac>/digital_in/D3', 'PUT', self._webhandle_oap_digital_in_D3_PUT) self.websrv.route('/api/v1/oap/<mac>/2/3', 'PUT', self._webhandle_oap_digital_in_D3_PUT) # /digital_out self.websrv.route('/api/v1/oap/<mac>/digital_out/D4', 'PUT', self._webhandle_oap_digital_out_D4_PUT) self.websrv.route('/api/v1/oap/<mac>/3/0', 'PUT', self._webhandle_oap_digital_out_D4_PUT) self.websrv.route('/api/v1/oap/<mac>/digital_out/D5', 'PUT', self._webhandle_oap_digital_out_D5_PUT) self.websrv.route('/api/v1/oap/<mac>/3/1', 'PUT', self._webhandle_oap_digital_out_D5_PUT) self.websrv.route('/api/v1/oap/<mac>/digital_out/INDICATOR_0', 'PUT', self._webhandle_oap_digital_out_INDICATOR_0_PUT) self.websrv.route('/api/v1/oap/<mac>/3/2', 'PUT', self._webhandle_oap_digital_out_INDICATOR_0_PUT) # /analog self.websrv.route('/api/v1/oap/<mac>/analog/A0', 'GET', self._webhandle_oap_analog_A0_GET) self.websrv.route('/api/v1/oap/<mac>/4/0', 'GET', self._webhandle_oap_analog_A0_GET) self.websrv.route('/api/v1/oap/<mac>/analog/A0', 'PUT', self._webhandle_oap_analog_A0_PUT) self.websrv.route('/api/v1/oap/<mac>/4/0', 'PUT', self._webhandle_oap_analog_A0_PUT) self.websrv.route('/api/v1/oap/<mac>/analog/A1', 'GET', self._webhandle_oap_analog_A1_GET) self.websrv.route('/api/v1/oap/<mac>/4/1', 'GET', self._webhandle_oap_analog_A1_GET) self.websrv.route('/api/v1/oap/<mac>/analog/A1', 'PUT', self._webhandle_oap_analog_A1_PUT) self.websrv.route('/api/v1/oap/<mac>/4/1', 'PUT', self._webhandle_oap_analog_A1_PUT) self.websrv.route('/api/v1/oap/<mac>/analog/A2', 'GET', self._webhandle_oap_analog_A2_GET) self.websrv.route('/api/v1/oap/<mac>/4/2', 'GET', self._webhandle_oap_analog_A2_GET) self.websrv.route('/api/v1/oap/<mac>/analog/A2', 'PUT', self._webhandle_oap_analog_A2_PUT) self.websrv.route('/api/v1/oap/<mac>/4/2', 'PUT', self._webhandle_oap_analog_A2_PUT) self.websrv.route('/api/v1/oap/<mac>/analog/A3', 'GET', self._webhandle_oap_analog_A3_GET) self.websrv.route('/api/v1/oap/<mac>/4/3', 'GET', self._webhandle_oap_analog_A3_GET) self.websrv.route('/api/v1/oap/<mac>/analog/A3', 'PUT', self._webhandle_oap_analog_A3_PUT) self.websrv.route('/api/v1/oap/<mac>/4/3', 'PUT', self._webhandle_oap_analog_A3_PUT) # /temperature self.websrv.route('/api/v1/oap/<mac>/temperature', 'GET', self._webhandle_oap_temperature_GET) self.websrv.route('/api/v1/oap/<mac>/5', 'GET', self._webhandle_oap_temperature_GET) self.websrv.route('/api/v1/oap/<mac>/temperature', 'PUT', self._webhandle_oap_temperature_PUT) self.websrv.route('/api/v1/oap/<mac>/5', 'PUT', self._webhandle_oap_temperature_PUT) # /pkgen self.websrv.route('/api/v1/oap/<mac>/pkgen/echo', 'GET', self._webhandle_oap_pkgen_echo_GET) self.websrv.route('/api/v1/oap/<mac>/254/0', 'GET', self._webhandle_oap_pkgen_echo_GET) self.websrv.route('/api/v1/oap/<mac>/pkgen', 'PUT', self._webhandle_oap_pkgen_PUT) self.websrv.route('/api/v1/oap/<mac>/254', 'PUT', self._webhandle_oap_pkgen_PUT) #=== helpers self.websrv.route('/api/v1/helpers/serialports', 'GET', self._webhandle_helpers_serialports_GET) self.websrv.route('/api/v1/helpers/motes', 'GET', self._webhandle_helpers_motes_GET) self.websrv.route('/api/v1/helpers/oapmotes', 'GET', self._webhandle_helpers_oapmotes_GET) #=== config self.websrv.route('/api/v1/config', 'GET', self._webhandle_config_GET) self.websrv.route('/api/v1/config', 'POST', self._webhandle_config_POST) #=== managers self.websrv.route('/api/v1/config/managers', 'PUT', self._webhandle_managers_PUT) self.websrv.route('/api/v1/config/managers', 'DELETE', self._webhandle_managers_DELETE) self.websrv.error(code=404)(self._errorhandle_404) self.websrv.error(code=500)(self._errorhandle_500) self.websrv.hook('after_request')( self._add_JsonServer_token_if_requested) webthread = threading.Thread(target=self.websrv.run, kwargs={ 'host': '127.0.0.1', 'port': self.tcpport, 'quiet': True, 'debug': False, }) webthread.name = 'WebServer' webthread.daemon = True webthread.start() # connect to managers (if any) self._syncManagers() # start CLI print 'SmartMesh SDK {0}'.format('.'.join( [str(i) for i in sdk_version.VERSION])) self.cli.start()