def __init__(self, bridge, host_eui64_hex, thread_exit_event, command_queue, event_queue, log_queue, watchdog_delegate): self.host_eui64_hex = host_eui64_hex self.commandProcessingEnabled = True self.eventProcessingEnabled = True self.logProcessingEnabled = True self.thread_exit_event = thread_exit_event self.command_queue = command_queue self.event_queue = event_queue self.log_queue = log_queue self.watchdog_delegate = watchdog_delegate self.current_json_message = None self.bridge = bridge self.networkUp = bridge.networkUp self.handshake_sent = False self.logger = Logger('cloud') self.command_logger = Logger('cloud.command') self.event_logger = Logger('cloud.event') self.logging_logger = Logger('cloud.logging') self.socket_logger = Logger('cloud.socket') self.resolver = simple_caching_resolver.SimpleCachingResolver() self.sm = statistics_monitor.StatisticsMonitor() self.event_sources = {} self.connection_failure_count = 0 self.ip_pool = [] self.last_connected_at = None self.ws = None socket.setdefaulttimeout(HTTP_SOCKET_TIMEOUT)
def executeCommand(self, command): sm = statistics_monitor.StatisticsMonitor() if isinstance(command, bridge_command.BridgeCommand): self.logger.debug("Executing bridge command '%s'" % command.command_name) try: command.return_code = self.executeBridgeCommand(command) except: self.logger.exception('Unexpected command thread error:', sys.exc_info()) command.return_code = 255 elif isinstance(command, device_command.DeviceCommand): self.logger.debug('Delivering command payload to device %s' % command.device_address_hex) try: command.return_code = self.executeDeviceCommand(command) command.transfer_time = sm.commandTransferTime() command.rssi_stats = sm.rssiStatsForEui64( command.device_address_hex) sm.commandComplete(command.return_code) except: self.logger.exception('Unexpected log queue thread error:', sys.exc_info()) command.return_code = 255 self.logger.debug('Enqueueing completed command into event queue') self.event_queue.put(command, True)
def set_bridge(self, bridge): self._bridge = bridge self._wdm = watchdog_monitor.WatchdogMonitor() self._sm = statistics_monitor.StatisticsMonitor() self._ver = version.Version() self._htmlLogLines = []
def run(self): global app linux_hub.setup_gpio() linux_hub.reset_ncp() linux_hub.set_up_ipv4_timeouts() bridge_config = { 'extended_pan_id_prefix': 1111839303L, 'radio_power': 8, 'network_create': True, 'network_permit_joining': True, 'print_progress_dots': False, 'purge_link_keys': True } random.seed() bridge_config['extended_pan_id'] = ( bridge_config['extended_pan_id_prefix'] << 32) + random.getrandbits(32) try: self.device = api.WeminucheApi(self.event_queue, bridge_config) except: self.logger.critical('Failed to initialise the NCP') self.shutdown(14) return self.device.energyScanRequest(0) while not app.device.channelScanResultsReady(): time.sleep(0.1) ( total, failed, energies, ) = app.device.collectAndClearChannelScanResults() lowest_energy = 0 selected_channel = 0 for energy in energies: ( channel, dbm, ) = energy if dbm < lowest_energy: selected_channel = channel lowest_energy = dbm self.logger.info('Switching to channel %d with lowest energy %d' % (selected_channel, lowest_energy)) app.device.channelChangeRequest(selected_channel) self.system_environment = self.collectEnvironment() self.host_eui64 = self.device.getHostEui64() self.host_eui64_hex = byte_tuple.eui64ToHexString( self.host_eui64, False) self.watchdog_thread_exit_event = threading.Event() self.watchdog_delegate = watchdog_monitor.WatchdogMonitor( self.options.watchdog, self.watchdog_thread_exit_event) self.watchdog_thread = threading.Thread( target=self.watchdog_delegate.patWatchdog) self.watchdog_thread.start() self.device.setTelemetryDelegate( statistics_monitor.StatisticsMonitor(self)) self.berg_cloud_thread_exit_event = threading.Event() self.berg_cloud_api = berg_cloud_socket_api.BergCloudSocketApi( self, self.host_eui64_hex, self.berg_cloud_thread_exit_event, self.command_queue, self.event_queue, self.log_queue, self.watchdog_delegate) self.berg_cloud_thread = threading.Thread( target=self.berg_cloud_api.start) self.berg_cloud_thread.start() self.ver_info = version.Version() self.ver_info.ncp_stack_version = self.device.getStackVersionString() self.logger.info('Starting webserver') self.httpd = status_server.BottleStats() self.httpd.set_bridge(self) self.webserver_thread = threading.Thread(target=self.httpd.run) self.webserver_thread.start() self.device.setWatchdogDelegate(self.watchdog_delegate) self.command_thread_exit_event = threading.Event() self.command_thread = threading.Thread(target=self.runQueuedCommands) self.command_thread.start() self.zigbee_led_thread_exit_event = threading.Event() self.zigbee_led_thread = threading.Thread( target=self.checkConnectedDevices) self.zigbee_led_thread.start() if self.options.daemon: while self.shutdown_triggered == False: try: time.sleep(1) except KeyboardInterrupt: self.logger.critical( 'Shutdown triggered in app.run() with KeyboardInterrupt' ) self.shutdown()