def running_indication(self): # request initial status here. self.__tracer.info("Running indication") extended_address = SettingsBase.get_setting(self, "extended_address") humidity_present = SettingsBase.get_setting(self, "humidity_present") # this is a flawed design - if the gateway has just rebooted, # and the Xbee sensor sleeps (which it should), then an actual # GET_DDO will be issued, which causes Dia to freeze here and # almost certainly throw exception and put the device off line. try: dd_value = self.__xbee_manager.xbee_device_ddo_get_param( extended_address, 'DD', use_cache=True) except: self.__tracer.warning('Using default DD') dd_value = 0x0003000E module_id, product_id = parse_dd(dd_value) self.__tracer.info('DD info (module_id, product_id) = ' + '(0x%04x, 0x%04x)"', module_id, product_id) if product_id == PROD_DIGI_XB_SENSOR_LTH or humidity_present == True: self.__tracer.info("Sensor is a '%s' adding humidity channel", product_name(product_id)) self.add_property( ChannelSourceDeviceProperty(name="humidity", type=float, initial=Sample(timestamp=0, value=0.0, unit="%"), perms_mask=DPROP_PERM_GET, options=DPROP_OPT_AUTOTIMESTAMP)) else: self.__tracer.info("Sensor is a '%s' no humidity capability.", product_name(product_id))
def running_indication(self): # request initial status here. self._tracer.info("Running indication") humidity_present = SettingsBase.get_setting(self, "humidity_present") # this is a flawed design - if the gateway has just rebooted, # and the Xbee sensor sleeps (which it should), then an actual # GET_DDO will be issued, which causes DIA to freeze here and # almost certainly throw exception and put the device off line. try: dd_value = self._xbee_manager.xbee_device_ddo_get_param( self._extended_address, 'DD', use_cache=True) except: self._tracer.warning('Using default DD') dd_value = 0x0003000E module_id, product_id = parse_dd(dd_value) self._tracer.debug( 'DD info (module_id, product_id) = ' + '(0x%04x, 0x%04x)"', module_id, product_id) if product_id == PROD_DIGI_XB_SENSOR_LTH or humidity_present == True: self._tracer.info("Sensor is a '%s' adding humidity channel", product_name(product_id)) self.add_property( ChannelSourceDeviceProperty(name="humidity", type=float, initial=Sample(timestamp=0, value=0.0, unit="%"), perms_mask=DPROP_PERM_GET, options=DPROP_OPT_AUTOTIMESTAMP)) else: self._tracer.info("Sensor is a '%s' no humidity capability.", product_name(product_id))
def __add_new_device(self, new_extended_address): already_in_system = self.__check_if_device_is_already_in_system( new_extended_address) if already_in_system == True: return # Device is new, and one we didn't have already configured in the # config file. Ask the device what it is... product_type, node_identifier = self.__get_device_data(\ new_extended_address) # If we were unable to get important data about the device, bail early. # The next discovery we will take another crack at it. if product_type == None or node_identifier == None: return self.__tracer.info("XBeeAutoEnum: New Device Found: %-s%s%-s%s%-s", product_name(product_type), " "*4, new_extended_address, " "*4, node_identifier) instance_settings = None for dev in self._auto_device_list: if product_type in dev['supported_products']: instance_settings = deepcopy(dev['userdata']) break else: # If there is no default config for this device, there is no reason # to keep attempting to configure it the next time we see it. # # So we will commit this device to a purgatory bin, in which # the device will remain until a later time, in which we might # have a new Dia config inserted into the running state of the # system that would provide us a default config. self.__tracer.warning("XBeeAutoEnum: No Default Config Found.") self.__banish_device_to_purgatory(product_type, new_extended_address) return if instance_settings == None: # If there is no instance settings for this device, there is no # reason to keep attempting to configure it the next time we see it. # # So we will commit this device to a purgatory bin, in which # the device will remain until a later time, in which we might # have a new Dia config inserted into the running state of the # system that would provide us correct instance settings. entry = dict(product_type = product_type, extended_address = new_extended_address) self.__purgatory.append(entry) return # Go create a custom name derived from the address and node identifier. name = self.__create_name(instance_settings['name'], new_extended_address, node_identifier) instance_settings['name'] = name # Replace the extended address with our custom versions. instance_settings['settings']['extended_address'] = new_extended_address # Get the current instance list for the devices binding: self.__settings_ctx.set_current_binding( ("devices", ) ) self.__settings_ctx.pending_instance_list_append(instance_settings) # Now attempt to start the new device, if it already hasn't been done. try: dm = self.__core.get_service("device_driver_manager") dm.driver_load(instance_settings['driver']) if instance_settings['name'] not in dm.instance_list(): dm.instance_new(instance_settings['driver'], instance_settings['name']) dm.instance_start(instance_settings['name']) except Exception, e: self.__tracer.error("XBeeAutoEnum: Exception during " + "driver load. %s: %s ", e.__class__, str(e)) self.__tracer.debug(traceback.format_exc()) # If starting the instance failed, try to determine if the # failure was because the settings were bad, missing or # simply incorrect. # If they are, then there is no reason to attempt # configure the device again if/when it gets # discovered again. So banish this device instance # to purgatory until the user fixes the settings they # gave us for the device. if e.__class__ == SettingNotFound or e.__class__ == ValueError: self.__banish_device_to_purgatory(product_type, new_extended_address) # Now attempt to remove the driver from Dia. # It is not harmful if it is unable to be removed. try: dm.instance_remove(instance_settings['name']) except Exception, e: self.__tracer.error('%s', str(e)) pass
# to purgatory until the user fixes the settings they # gave us for the device. if e.__class__ == SettingNotFound or e.__class__ == ValueError: self.__banish_device_to_purgatory(product_type, new_extended_address) # Now attempt to remove the driver from Dia. # It is not harmful if it is unable to be removed. try: dm.instance_remove(instance_settings['name']) except Exception, e: self.__tracer.error('%s', str(e)) pass # If here, then notify any Dia modules interested dat = (name, product_type, product_name(product_type), new_extended_address) for cbfnc in self.__callbacks: try: cbfnc(dat) except: self.__tracer.warning('XBeeAutoEnum: clients callback failed') self.__tracer.debug(traceback.print_exc()) # internal functions & classes def main(): pass if __name__ == '__main__':
def __add_new_device(self, new_extended_address): already_in_system = self.__check_if_device_is_already_in_system( new_extended_address) if already_in_system == True: return # Device is new, and one we didn't have already configured in the # config file. Ask the device what it is... product_type, node_identifier = self.__get_device_data(\ new_extended_address) if node_identifier == None: # if NI is truly NULL, make empty-string instead node_identifier = '' # If we were unable to get important data about the device, bail early. # The next discovery we will take another crack at it. if product_type == None: return self.__tracer.info("XBeeAutoEnum: New Device Found: %-s%s%-s%s%-s", product_name(product_type), " " * 4, new_extended_address, " " * 4, node_identifier) instance_settings = None for dev in self._auto_device_list: if product_type in dev['supported_products']: instance_settings = deepcopy(dev['userdata']) break else: # If there is no default config for this device, there is # no reason to keep attempting to configure it the next # time we see it. # # So we will commit this device to a purgatory bin, in which # the device will remain until a later time, in which we might # have a new DIA config inserted into the running state of the # system that would provide us a default config. self.__tracer.warning("XBeeAutoEnum: No Default Config Found.") self.__banish_device_to_purgatory(product_type, new_extended_address) return if instance_settings == None: # If there is no instance settings for this device, there is no # reason to keep attempting to configure it the next time # we see it. # # So we will commit this device to a purgatory bin, in which # the device will remain until a later time, in which we might # have a new DIA config inserted into the running state of the # system that would provide us correct instance settings. entry = dict(product_type=product_type, extended_address=new_extended_address) self.__purgatory.append(entry) return # Go create a custom name derived from the address and node identifier. name = self.__create_name(instance_settings['name'], new_extended_address, node_identifier) instance_settings['name'] = name # Replace the extended address with our custom versions. instance_settings['settings']['extended_address'] = \ new_extended_address # Get the current instance list for the devices binding: self.__settings_ctx.set_current_binding(("devices", )) self.__settings_ctx.pending_instance_list_append(instance_settings) # Now attempt to start the new device, if it already hasn't been done. try: dm = self.__core.get_service("device_driver_manager") dm.driver_load(instance_settings['driver']) if instance_settings['name'] not in dm.instance_list(): dm.instance_new(instance_settings['driver'], instance_settings['name']) dm.instance_start(instance_settings['name']) except Exception, e: self.__tracer.error( "XBeeAutoEnum: Exception during " + "driver load. %s: %s ", e.__class__, str(e)) self.__tracer.debug(traceback.format_exc()) # If starting the instance failed, try to determine if the # failure was because the settings were bad, missing or # simply incorrect. # If they are, then there is no reason to attempt # configure the device again if/when it gets # discovered again. So banish this device instance # to purgatory until the user fixes the settings they # gave us for the device. if e.__class__ == SettingNotFound or e.__class__ == ValueError: self.__banish_device_to_purgatory(product_type, new_extended_address) # Now attempt to remove the driver from DIA. # It is not harmful if it is unable to be removed. try: dm.instance_remove(instance_settings['name']) except Exception, e: self.__tracer.error('%s', str(e))
self.__tracer.debug(traceback.format_exc()) # If starting the instance failed, try to determine if the # failure was because the settings were bad, missing or # simply incorrect. # If they are, then there is no reason to attempt # configure the device again if/when it gets # discovered again. So banish this device instance # to purgatory until the user fixes the settings they # gave us for the device. if e.__class__ == SettingNotFound or e.__class__ == ValueError: self.__banish_device_to_purgatory(product_type, new_extended_address) # Now attempt to remove the driver from DIA. # It is not harmful if it is unable to be removed. try: dm.instance_remove(instance_settings['name']) except Exception, e: self.__tracer.error('%s', str(e)) # If here, then notify any DIA modules interested dat = (name, product_type, product_name(product_type), new_extended_address) for cbfnc in self.__callbacks: try: cbfnc(dat) except: self.__tracer.warning('XBeeAutoEnum: clients callback failed') self.__tracer.debug(traceback.print_exc())