def start(self): # Fetch the XBee Manager name from the Settings Manager: xbee_manager_name = SettingsBase.get_setting(self, "xbee_device_manager") dm = self.__core.get_service("device_driver_manager") self.__xbee_manager = dm.instance_get(xbee_manager_name) # Register ourselves with the XBee Device Manager instance: self.__xbee_manager.xbee_device_register(self) # Get the extended address of the device: extended_address = SettingsBase.get_setting(self, "extended_address") # Create a DDO configuration block for this device: xbee_ddo_cfg = XBeeConfigBlockDDO(extended_address) # Call the XBeeSerial function to add the initial set up of our device. # This will set up the destination address of the devidce, and also set # the default baud rate, parity, stop bits and flow control. XBeeSerial.initialize_xbee_serial(self, xbee_ddo_cfg) # Register this configuration block with the XBee Device Manager: self.__xbee_manager.xbee_device_config_block_add(self, xbee_ddo_cfg) # Setup the sleep parameters on this device: will_sleep = SettingsBase.get_setting(self, "sleep") sample_predelay = SettingsBase.get_setting(self, "sample_predelay") awake_time_ms = (SettingsBase.get_setting(self, "awake_time_ms") + sample_predelay) if will_sleep: # Sample time pre-delay, allow the circuitry to power up and # settle before we allow the XBee to send us a sample: xbee_ddo_wh_block = XBeeConfigBlockDDO(extended_address) xbee_ddo_wh_block.apply_only_to_modules((MOD_XB_ZB,)) xbee_ddo_wh_block.add_parameter('WH', sample_predelay) self.__xbee_manager.xbee_device_config_block_add(self, xbee_ddo_wh_block) # The original sample rate is used as the sleep rate: sleep_rate_ms = SettingsBase.get_setting(self, "sample_rate_ms") xbee_sleep_cfg = XBeeConfigBlockSleep(extended_address) if will_sleep: xbee_sleep_cfg.sleep_cycle_set(awake_time_ms, sleep_rate_ms, enable_pin_wake=True) else: xbee_sleep_cfg.sleep_mode_set(SM_DISABLED) self.__xbee_manager.xbee_device_config_block_add(self, xbee_sleep_cfg) # Indicate that we have no more configuration to add: self.__xbee_manager.xbee_device_configure(self) return True
def start(self): # Fetch the XBee Manager name from the Settings Manager: xbee_manager_name = SettingsBase.get_setting(self, "xbee_device_manager") dm = self.__core.get_service("device_driver_manager") self.__xbee_manager = dm.instance_get(xbee_manager_name) # Register ourselves with the XBee Device Manager instance: self.__xbee_manager.xbee_device_register(self) # Get the extended address of the device: extended_address = SettingsBase.get_setting(self, "extended_address") # Retrieve the flag which tells us if we should sleep: # Create a callback specification for our device address, endpoint # Digi XBee profile and sample cluster id: xbdm_rx_event_spec = XBeeDeviceManagerRxEventSpec() xbdm_rx_event_spec.cb_set(self.sample_indication) xbdm_rx_event_spec.match_spec_set( (extended_address, 0xe8, 0xc105, 0x92), (True, True, True, True)) self.__xbee_manager.xbee_device_event_spec_add(self, xbdm_rx_event_spec) # Create a callback specification that calls back this driver when # our device has left the configuring state and has transitioned # to the running state: xbdm_running_event_spec = XBeeDeviceManagerRunningEventSpec() xbdm_running_event_spec.cb_set(self.running_indication) self.__xbee_manager.xbee_device_event_spec_add(self, xbdm_running_event_spec) # Create a DDO configuration block for this device: xbee_ddo_cfg = XBeeConfigBlockDDO(extended_address) # Get the gateway's extended address: gw_xbee_sh, gw_xbee_sl = gw_extended_address_tuple() # Set the destination for I/O samples to be the gateway: xbee_ddo_cfg.add_parameter('DH', gw_xbee_sh) xbee_ddo_cfg.add_parameter('DL', gw_xbee_sl) # Configure pins DI1 .. DI3 for analog input: for io_pin in [ 'D1', 'D2', 'D3' ]: xbee_ddo_cfg.add_parameter(io_pin, 2) # Configure battery-monitor pin DIO11/P1 for digital input: xbee_ddo_cfg.add_parameter('P1', 3) # Enable change detection on DIO11: # # 0x 8 0 0 # 1000 0000 0000 (b) # DDDD DDDD DDDD # IIII IIII IIII # OOOO OOOO OOOO # 1198 7654 3210 # 10 # xbee_ddo_cfg.add_parameter('IC', 0x800) if SettingsBase.get_setting(self, "humidity_present"): # Get gateway module_id, universal to all nodes on the network: gw_dd = self.__xbee_manager.xbee_device_ddo_get_param( None, 'DD', use_cache=True) module_id, product_id = parse_dd(gw_dd) # Re-program DD value to set sensor type to /L/T/H: device_dd = format_dd(module_id, PROD_DIGI_XB_SENSOR_LTH) xbee_ddo_cfg.add_parameter('DD', device_dd) # Configure the IO Sample Rate: # Clip sample_rate_ms to the max value of IR: sample_rate_ms = SettingsBase.get_setting(self, "sample_rate_ms") sample_rate_ms = min(sample_rate_ms, 0xffff) xbee_ddo_cfg.add_parameter('IR', sample_rate_ms) # Register this configuration block with the XBee Device Manager: self.__xbee_manager.xbee_device_config_block_add(self, xbee_ddo_cfg) # Setup the sleep parameters on this device: will_sleep = SettingsBase.get_setting(self, "sleep") sample_predelay = SettingsBase.get_setting(self, "sample_predelay") awake_time_ms = (SettingsBase.get_setting(self, "awake_time_ms") + sample_predelay) if will_sleep: # Sample time pre-delay, allow the circuitry to power up and # settle before we allow the XBee to send us a sample: xbee_ddo_wh_block = XBeeConfigBlockDDO(extended_address) xbee_ddo_wh_block.apply_only_to_modules((MOD_XB_ZB, MOD_XB_S2C_ZB,)) xbee_ddo_wh_block.add_parameter('WH', sample_predelay) self.__xbee_manager.xbee_device_config_block_add(self, xbee_ddo_wh_block) # The original sample rate is used as the sleep rate: sleep_rate_ms = SettingsBase.get_setting(self, "sample_rate_ms") xbee_sleep_cfg = XBeeConfigBlockSleep(extended_address) if will_sleep: xbee_sleep_cfg.sleep_cycle_set(awake_time_ms, sleep_rate_ms) else: xbee_sleep_cfg.sleep_mode_set(SM_DISABLED) self.__xbee_manager.xbee_device_config_block_add(self, xbee_sleep_cfg) # Indicate that we have no more configuration to add: self.__xbee_manager.xbee_device_configure(self) return True
def initialize_xbee_serial(self, xbee_ddo_cfg=None): """\ Creates a DDO command sequence of the user selected serial settings. .. Note:: * This routine does everything EXCEPT calling for final configuration * It will add its own ddo config block * The caller can add another after the return For example:: XBeeSerial.initialize_xbee_serial(self) my_ddo_cfg = XBeeConfigBlockDDO(self._extended_address) my_ddo_cfg.add_parameter('IR', 60000) self._xbee_manager.xbee_device_config_block_add(self, my_ddo_cfg) self._xbee_manager.xbee_device_configure(self) Returns True if successful, False on failure. """ extended_address = SettingsBase.get_setting(self, "extended_address") self._tracer.calls("XBeeSerial.initialize_xbee_serial()") # Create a callback specification for our device address, endpoint # Digi XBee profile and sample cluster id: self._xbee_manager.register_serial_listener(self, self._extended_address, self.__read_callback) # callback for cb_set(self.__running_indication) is in xbee_base() # Create a DDO configuration block for this device: if xbee_ddo_cfg is None: xbee_ddo_cfg = XBeeConfigBlockDDO(self._extended_address) # Set up the baud rate for the device. try: baud = SettingsBase.get_setting(self, "baudrate") except: baud = self.DEF_BAUDRATE baud = self.__derive_baudrate(baud) xbee_ddo_cfg.add_parameter('BD', baud) # Set up the parity for the device. try: parity = SettingsBase.get_setting(self, "parity") except: parity = self.DEF_PARITY parity = self.__derive_parity(parity) xbee_ddo_cfg.add_parameter('NB', parity) if self._xbee_manager.is_zigbee(): # Set up the stop bits for the device. try: stopbits = SettingsBase.get_setting(self, "stopbits") except: stopbits = self.DEF_STOPBITS stopbits = self.__derive_stopbits(stopbits) # The SB command is new. # It may or may not be supported on the XBee Serial Device/Adapter. # If its not supported, then we know the device # is simply at 1 stop bit, and we can ignore the failure. xbee_ddo_cfg.add_parameter('SB', stopbits, failure_callback=self.__ignore_if_fail) # else skip it for 802.15.4 or DigiMesh # Set up the hardware flow control mode for the device. try: hwflow = SettingsBase.get_setting(self, "hardwareflowcontrol") except: hwflow = self.DEF_HWFLOW d6, d7 = self.__derive_hardwareflowcontrol(hwflow) xbee_ddo_cfg.add_parameter('D6', d6) #xbee_ddo_cfg.add_parameter('D7', d7) xbee_ddo_cfg.add_parameter('D7', 7) self._xbee_manager.xbee_device_config_block_add(self, xbee_ddo_cfg) will_sleep = SettingsBase.get_setting(self, "sleep") sample_predelay = SettingsBase.get_setting(self, "sample_predelay") awake_time_ms = (SettingsBase.get_setting(self, "awake_time_ms") + sample_predelay) if will_sleep: # Sample time pre-delay, allow the circuitry to power up and # settle before we allow the XBee to send us a sample: xbee_ddo_wh_block = XBeeConfigBlockDDO(extended_address) xbee_ddo_wh_block.apply_only_to_modules((MOD_XB_ZB1, MOD_XB_S2C_ZB1,)) xbee_ddo_wh_block.add_parameter('WH', sample_predelay) self._xbee_manager.xbee_device_config_block_add(self, xbee_ddo_wh_block) sleep_rate_ms = SettingsBase.get_setting(self, "sample_rate_ms") xbee_sleep_cfg = XBeeConfigBlockSleep(extended_address) if will_sleep: xbee_sleep_cfg.sleep_cycle_set(awake_time_ms, sleep_rate_ms, enable_pin_wake=True) else: xbee_sleep_cfg.sleep_mode_set(SM_DISABLED) self._xbee_manager.xbee_device_config_block_add(self, xbee_sleep_cfg) return True
def start(self): # Fetch the XBee Manager name from the Settings Manager: xbee_manager_name = SettingsBase.get_setting(self, "xbee_device_manager") dm = self.__core.get_service("device_driver_manager") self.__xbee_manager = dm.instance_get(xbee_manager_name) # Register ourselves with the XBee Device Manager instance: self.__xbee_manager.xbee_device_register(self) # Get the extended address of the device: extended_address = SettingsBase.get_setting(self, "extended_address") # Retrieve the flag which tells us if we should sleep: # Create a callback specification for our device address, endpoint # Digi XBee profile and sample cluster id: xbdm_rx_event_spec = XBeeDeviceManagerRxEventSpec() xbdm_rx_event_spec.cb_set(self.sample_indication) xbdm_rx_event_spec.match_spec_set( (extended_address, 0xe8, 0xc105, 0x92), (True, True, True, True)) self.__xbee_manager.xbee_device_event_spec_add(self, xbdm_rx_event_spec) # Create a DDO configuration block for this device: xbee_ddo_cfg = XBeeConfigBlockDDO(extended_address) # Get the gateway's extended address: gw_xbee_sh, gw_xbee_sl = gw_extended_address_tuple() # Set the destination for I/O samples to be the gateway: xbee_ddo_cfg.add_parameter('DH', gw_xbee_sh) xbee_ddo_cfg.add_parameter('DL', gw_xbee_sl) # if adapter is using internal batteries, then configure battery-monitor # pin and add low_battery channel if SettingsBase.get_setting(self, "enable_low_battery"): # configure battery-monitor pin DIO11/P1 for digital input xbee_ddo_cfg.add_parameter('P1', 3) # add low_battery channel self._tracer.info("Adapter is using internal batteries " + "adding low_battery channel") self.add_property( ChannelSourceDeviceProperty(name="low_battery", type=bool, initial=Sample(timestamp=0, value=False), perms_mask=DPROP_PERM_GET, options=DPROP_OPT_AUTOTIMESTAMP)) else: self._tracer.info("Adapter is not using internal batteries") ic = 0 xbee_ddo_cfg.add_parameter('IC', ic) # Configure the IO Sample Rate: # Clip sample_rate_ms to the max value of IR: sample_rate_ms = SettingsBase.get_setting(self, "sample_rate_ms") sample_rate_ms = min(sample_rate_ms, 0xffff) xbee_ddo_cfg.add_parameter('IR', sample_rate_ms) # Register this configuration block with the XBee Device Manager: self.__xbee_manager.xbee_device_config_block_add(self, xbee_ddo_cfg) # Setup the sleep parameters on this device: will_sleep = SettingsBase.get_setting(self, "sleep") awake_time_ms = SettingsBase.get_setting(self, "awake_time_ms") # The original sample rate is used as the sleep rate: sleep_rate_ms = SettingsBase.get_setting(self, "sample_rate_ms") xbee_sleep_cfg = XBeeConfigBlockSleep(extended_address) if will_sleep: xbee_sleep_cfg.sleep_cycle_set(awake_time_ms, sleep_rate_ms) else: xbee_sleep_cfg.sleep_mode_set(SM_DISABLED) self.__xbee_manager.xbee_device_config_block_add(self, xbee_sleep_cfg) # Indicate that we have no more configuration to add: self.__xbee_manager.xbee_device_configure(self) # Scheduling first request and watchdog heart beat poll, # but only if we aren't in sleep mode. if will_sleep != True: self.__schedule_request() self.__reschedule_retry_watchdog() return True
def start(self): """Start the device driver. Returns bool.""" # Fetch the XBee Manager name from the Settings Manager: xbee_manager_name = SettingsBase.get_setting(self, "xbee_device_manager") dm = self.__core.get_service("device_driver_manager") self.__xbee_manager = dm.instance_get(xbee_manager_name) # Register ourselves with the XBee Device Manager instance: self.__xbee_manager.xbee_device_register(self) # Get the extended address of the device: extended_address = SettingsBase.get_setting(self, "extended_address") # Create a callback specification for our device address, endpoint # Digi XBee profile and sample cluster id: xbdm_rx_event_spec = XBeeDeviceManagerRxEventSpec() xbdm_rx_event_spec.cb_set(self.sample_indication) xbdm_rx_event_spec.match_spec_set( (extended_address, 0xe8, 0xc105, 0x92), (True, True, True, True)) self.__xbee_manager.xbee_device_event_spec_add(self, xbdm_rx_event_spec) # Create a DDO configuration block for this device: xbee_ddo_cfg = XBeeConfigBlockDDO(extended_address) # Get the gateway's extended address: gw_xbee_sh, gw_xbee_sl = gw_extended_address_tuple() # Set the destination for I/O samples to be the gateway: xbee_ddo_cfg.add_parameter('DH', gw_xbee_sh) xbee_ddo_cfg.add_parameter('DL', gw_xbee_sl) # Configure pins DI1 & DI2 & DI0 for analog input: xbee_ddo_cfg.add_parameter('D0', 1) for io_pin in ['D3', 'D4', 'D6', 'D7' ]: xbee_ddo_cfg.add_parameter(io_pin, 4) # Configure the IO Sample Rate: sample_rate = SettingsBase.get_setting(self, "sample_rate_ms") xbee_ddo_cfg.add_parameter('IR', sample_rate) # Handle subscribing the devices output to a named channel, # if configured to do so: # power_on_source1 = SettingsBase.get_setting(self, 'power_on_source1') # if power_on_source1 is not None: # cm = self.__core.get_service("channel_manager") # cp = cm.channel_publisher_get() # self.property_set("adder_reg2", Sample(0, float(power_on_source1))) # cp.subscribe(power_on_source1, self.update_power_state1) # power_on_source2 = SettingsBase.get_setting(self, 'power_on_source2') # if power_on_source2 is not None: #W cm = self.__core.get_service("channel_manager") # cp = cm.channel_publisher_get() # cp.subscribe(power_on_source2, self.update_power_state2) # power_on_source3 = SettingsBase.get_setting(self, 'power_on_source3') # if power_on_source3 is not None: # cm = self.__core.get_service("channel_manager") # cp = cm.channel_publisher_get() # cp.subscribe(power_on_source3, self.update_power_state3) # Register this configuration block with the XBee Device Manager: self.__xbee_manager.xbee_device_config_block_add(self, xbee_ddo_cfg) # Setup the sleep parameters on this device: will_sleep = SettingsBase.get_setting(self, "sleep") sample_predelay = SettingsBase.get_setting(self, "sample_predelay") awake_time_ms = (SettingsBase.get_setting(self, "awake_time_ms") + sample_predelay) if will_sleep: # Sample time pre-delay, allow the circuitry to power up and # settle before we allow the XBee to send us a sample: xbee_ddo_wh_block = XBeeConfigBlockDDO(extended_address) xbee_ddo_wh_block.apply_only_to_modules((MOD_XB_ZB,)) xbee_ddo_wh_block.add_parameter('WH', sample_predelay) self.__xbee_manager.xbee_device_config_block_add(self, xbee_ddo_wh_block) # The original sample rate is used as the sleep rate: sleep_rate_ms = SettingsBase.get_setting(self, "sample_rate_ms") xbee_sleep_cfg = XBeeConfigBlockSleep(extended_address) if will_sleep: xbee_sleep_cfg.sleep_cycle_set(awake_time_ms, sleep_rate_ms) else: xbee_sleep_cfg.sleep_mode_set(SM_DISABLED) self.__xbee_manager.xbee_device_config_block_add(self, xbee_sleep_cfg) self.setup(Sample(0, Boolean("off", STYLE_ONOFF))) # Indicate that we have no more configuration to add: self.__xbee_manager.xbee_device_configure(self) return True
def start(self): # Fetch the XBee Manager name from the Settings Manager: xbee_manager_name = SettingsBase.get_setting(self, "xbee_device_manager") dm = self.__core.get_service("device_driver_manager") self.__xbee_manager = dm.instance_get(xbee_manager_name) cm = self.__core.get_service("channel_manager") cp = cm.channel_publisher_get() # Register ourselves with the XBee Device Manager instance: self.__xbee_manager.xbee_device_register(self) # Get the extended address of the device: self.__extended_address = SettingsBase.get_setting(self, "extended_address") address = self.__extended_address self.thermostat = TemplateDevice(("thermostat_" + address), self.__core) time.sleep(3) SettingsBase.set_pending_setting(self, "channel1_source", ("thermostat_" + address + ".heat_on")) SettingsBase.set_pending_setting(self, "channel2_source", ("thermostat_" + address + ".heat_off")) # channel1_source: "thermostat_[00:05:a2:00:40:52:e0:fc]!.heat_on" # channel2_source: "thermostat_[00:05:a2:00:40:52:e0:fc]!.heat_off" print print ("thermostat_" + address + ".heat_on") print ("thermostat_" + address + ".heat_off") # str1 = ("thermostat_" + address + ".heat_on") # str2 = ("thermostat_" + address + ".heat_off") self.apply_settings() # Create a callback specification for our device address, endpoint # Digi XBee profile and sample cluster id: xbdm_rx_event_spec = XBeeDeviceManagerRxEventSpec() xbdm_rx_event_spec.cb_set(self.sample_indication) xbdm_rx_event_spec.match_spec_set( (self.__extended_address, 0xe8, 0xc105, 0x92), (True, True, True, True)) self.__xbee_manager.xbee_device_event_spec_add(self, xbdm_rx_event_spec) # Create a callback specification that calls back this driver when # our device has left the configuring state and has transitioned # to the running state: xbdm_running_event_spec = XBeeDeviceManagerRunningEventSpec() xbdm_running_event_spec.cb_set(self.running_indication) self.__xbee_manager.xbee_device_event_spec_add(self, xbdm_running_event_spec) # Create a DDO configuration block for this device: xbee_ddo_cfg = XBeeConfigBlockDDO(self.__extended_address) xbee_ddo_cfg.add_parameter("NI", "") # Get the gateway's extended address: gw_xbee_sh, gw_xbee_sl = gw_extended_address_tuple() # Set the destination for I/O samples to be the gateway: xbee_ddo_cfg.add_parameter('DH', gw_xbee_sh) xbee_ddo_cfg.add_parameter('DL', gw_xbee_sl) # Configure pins DIO0 .. DIO3 for digital input: pr = 0xe1 # DIO0-3 pullups off, all else on ic = 0 for io_pin in range(4): dir = SettingsBase.get_setting(self, 'channel%d_dir' % (io_pin+1) ) dir = dir.lower() # Enable input on all pins: xbee_ddo_cfg.add_parameter(CONTROL_LINES[io_pin][IN], 3) # Build our change detection mask for all io pins: ic |= 1 << INPUT_CHANNEL_TO_PIN[io_pin] if dir == 'in': # Disable sinking driver output: xbee_ddo_cfg.add_parameter(CONTROL_LINES[io_pin][OUT], 4) elif dir == 'out': # Create the output channel for this IO pin: self.add_property( ChannelSourceDeviceProperty( name='channel%d_output' % (io_pin+1), type=Boolean, initial=Sample(timestamp=0, value=Boolean(False), unit='1'), perms_mask=(DPROP_PERM_GET|DPROP_PERM_SET), options=DPROP_OPT_AUTOTIMESTAMP, set_cb=lambda sample, io=io_pin: \ self.set_output(sample, io) ) ) # If set, subscribe to the channel that drives our output logic: source = SettingsBase.get_setting(self, 'channel%d_source' % (io_pin+1)) print print "here is the place that really matters when it comes to the source" print source if len(source): cp.subscribe(source, lambda chan, io=io_pin: self.update(chan, io) ) # Set initial value of output to low: # TODO: add a default output condition here: xbee_ddo_cfg.add_parameter(CONTROL_LINES[io_pin][OUT], 4) # if adapter is using internal batteries, then configure battery-monitor # pin and add low_battery channel if SettingsBase.get_setting(self, "enable_low_battery"): # configure battery-monitor pin DIO11/P1 for digital input xbee_ddo_cfg.add_parameter('P1', 3) # add low_battery channel print ("XBeeDIO(%s): adapter is using internal batteries adding " \ + "low_battery channel") % self.__name self.add_property( ChannelSourceDeviceProperty(name="low_battery", type=bool, initial=Sample(timestamp=0, value=False), perms_mask=DPROP_PERM_GET, options=DPROP_OPT_AUTOTIMESTAMP)) else: print "XBeeDIO(%s): adapter is not using internal batteries." \ % self.__name # Enable I/O line monitoring on pins DIO0 .. DIO3 & # enable change detection on DIO11: # # 0x 8 0 0 # 1000 0000 0000 (b) # DDDD DDDD DDDD # IIII IIII IIII # OOOO OOOO OOOO # 1198 7654 3210 # 10 # #ic |= 0x800 #xbee_ddo_cfg.add_parameter('IC', ic) # disable the triggered low-battery alarm since a hardware bug # causes it to be unrealiable #ic |= 0x800 xbee_ddo_cfg.add_parameter('IC', ic) # Assert input pull-ups xbee_ddo_cfg.add_parameter('PR', 0x1fff) # this defaults to 0 xbee_ddo_cfg.add_parameter('IR', SettingsBase.get_setting(self, 'sample_rate_ms')) # Enable/disable power output on terminal 6: power = SettingsBase.get_setting(self, "power") if power: xbee_ddo_cfg.add_parameter('p3', 5) else: xbee_ddo_cfg.add_parameter('p3', 4) # Register this configuration block with the XBee Device Manager: self.__xbee_manager.xbee_device_config_block_add(self, xbee_ddo_cfg) # Setup the sleep parameters on this device: will_sleep = SettingsBase.get_setting(self, "sleep") sample_predelay = SettingsBase.get_setting(self, "sample_predelay") awake_time_ms = (SettingsBase.get_setting(self, "awake_time_ms") + sample_predelay) # The original sample rate is used as the sleep rate: sleep_time_ms = SettingsBase.get_setting(self, "sleep_time_ms") xbee_sleep_cfg = XBeeConfigBlockSleep(self.__extended_address) if will_sleep: xbee_sleep_cfg.sleep_cycle_set(awake_time_ms, sleep_time_ms) else: xbee_sleep_cfg.sleep_mode_set(SM_DISABLED) self.__xbee_manager.xbee_device_config_block_add(self, xbee_sleep_cfg) if will_sleep: # Sample time pre-delay, allow the circuitry to power up and # settle before we allow the XBee to send us a sample: xbee_ddo_wh_block = XBeeConfigBlockDDO(self.__extended_address) xbee_ddo_wh_block.apply_only_to_modules((MOD_XB_ZB,)) xbee_ddo_wh_block.add_parameter('WH', sample_predelay) self.__xbee_manager.xbee_device_config_block_add(self, xbee_ddo_wh_block) # Indicate that we have no more configuration to add: self.__xbee_manager.xbee_device_configure(self) return True
def start(self): """Start the device driver. Returns bool.""" # Fetch the XBee Manager name from the Settings Manager: xbee_manager_name = SettingsBase.get_setting(self, "xbee_device_manager") dm = self.__core.get_service("device_driver_manager") self.__xbee_manager = dm.instance_get(xbee_manager_name) # Register ourselves with the XBee Device Manager instance: self.__xbee_manager.xbee_device_register(self) # Get the extended address of the device: extended_address = SettingsBase.get_setting(self, "extended_address") # Retrieve the flag which tells us if we should sleep: # Create a callback specification for our device address, endpoint # Digi XBee profile and sample cluster id: xbdm_rx_event_spec = XBeeDeviceManagerRxEventSpec() xbdm_rx_event_spec.cb_set(self.sample_indication) xbdm_rx_event_spec.match_spec_set( (extended_address, 0xe8, 0xc105, 0x92), (True, True, True, True)) self.__xbee_manager.xbee_device_event_spec_add(self, xbdm_rx_event_spec) # Create a callback specification that calls back this driver when # our device has left the configuring state and has transitioned # to the running state: xbdm_running_event_spec = XBeeDeviceManagerRunningEventSpec() xbdm_running_event_spec.cb_set(self.running_indication) self.__xbee_manager.xbee_device_event_spec_add(self, xbdm_running_event_spec) # Create a DDO configuration block for this device: xbee_ddo_cfg = XBeeConfigBlockDDO(extended_address) # Get the gateway's extended address: gw_xbee_sh, gw_xbee_sl = gw_extended_address_tuple() # Set the destination for I/O samples to be the gateway: xbee_ddo_cfg.add_parameter('DH', gw_xbee_sh) xbee_ddo_cfg.add_parameter('DL', gw_xbee_sl) # Configure pins DI1 .. DI3 for analog input: for io_pin in [ 'D1', 'D2', 'D3' ]: xbee_ddo_cfg.add_parameter(io_pin, 2) # Configure battery-monitor pin DIO11/P1 for digital input: xbee_ddo_cfg.add_parameter('D2', 3) # Enable change detection on DIO11: # # 0x 8 0 0 # 1000 0000 0000 (b) # DDDD DDDD DDDD # IIII IIII IIII # OOOO OOOO OOOO # 1198 7654 3210 # 10 # xbee_ddo_cfg.add_parameter('IC', 0x800) # Configure the IO Sample Rate: # Clip sample_rate_ms to the max value of IR: sample_rate_ms = SettingsBase.get_setting(self, "sample_rate_ms") sample_rate_ms = min(sample_rate_ms, 0xffff) xbee_ddo_cfg.add_parameter('IR', sample_rate_ms) # Register this configuration block with the XBee Device Manager: self.__xbee_manager.xbee_device_config_block_add(self, xbee_ddo_cfg) # Setup the sleep parameters on this device: will_sleep = SettingsBase.get_setting(self, "sleep") sample_predelay = SettingsBase.get_setting(self, "sample_predelay") awake_time_ms = (SettingsBase.get_setting(self, "awake_time_ms") + sample_predelay) if will_sleep: # Sample time pre-delay, allow the circuitry to power up and # settle before we allow the XBee to send us a sample: xbee_ddo_wh_block = XBeeConfigBlockDDO(extended_address) xbee_ddo_wh_block.apply_only_to_modules((MOD_XB_ZB,)) xbee_ddo_wh_block.add_parameter('WH', sample_predelay) self.__xbee_manager.xbee_device_config_block_add(self, xbee_ddo_wh_block) # The original sample rate is used as the sleep rate: sleep_rate_ms = SettingsBase.get_setting(self, "sample_rate_ms") xbee_sleep_cfg = XBeeConfigBlockSleep(extended_address) if will_sleep: xbee_sleep_cfg.sleep_cycle_set(awake_time_ms, sleep_rate_ms, enable_pin_wake=True) else: xbee_sleep_cfg.sleep_mode_set(SM_DISABLED) self.__xbee_manager.xbee_device_config_block_add(self, xbee_sleep_cfg) # Handle subscribing the devices output to a named channel, # if configured to do so: power_on_source = SettingsBase.get_setting(self, 'power_on_source') if power_on_source is not None: cm = self.__core.get_service("channel_manager") cp = cm.channel_publisher_get() cp.subscribe(power_on_source, self.update_power_state) # Indicate that we have no more configuration to add: self.__xbee_manager.xbee_device_configure(self) return True
def start(self): # Fetch the XBee Manager name from the Settings Manager: xbee_manager_name = SettingsBase.get_setting(self, "xbee_device_manager") dm = self.__core.get_service("device_driver_manager") self.__xbee_manager = dm.instance_get(xbee_manager_name) # Register ourselves with the XBee Device Manager instance: self.__xbee_manager.xbee_device_register(self) # Get the extended address of the device: extended_address = SettingsBase.get_setting(self, "extended_address") # Create a callback specification for our device address, endpoint # Digi XBee profile and sample cluster id: xbdm_rx_event_spec = XBeeDeviceManagerRxEventSpec() xbdm_rx_event_spec.cb_set(self.sample_indication) xbdm_rx_event_spec.match_spec_set( (extended_address, 0xe8, 0xc105, 0x92), (True, True, True, True)) self.__xbee_manager.xbee_device_event_spec_add(self, xbdm_rx_event_spec) # Create a callback specification that calls back this driver when # our device has left the configuring state and has transitioned # to the running state: xbdm_running_event_spec = XBeeDeviceManagerRunningEventSpec() xbdm_running_event_spec.cb_set(self.running_indication) self.__xbee_manager.xbee_device_event_spec_add(self, xbdm_running_event_spec) # Create a DDO configuration block for this device: xbee_ddo_cfg = XBeeConfigBlockDDO(extended_address) # Get the gateway's extended address: gw_xbee_sh, gw_xbee_sl = gw_extended_address_tuple() # Set the destination for I/O samples to be the gateway: xbee_ddo_cfg.add_parameter('DH', gw_xbee_sh) xbee_ddo_cfg.add_parameter('DL', gw_xbee_sl) # Configure node sleep behavior: sleep_ms = SettingsBase.get_setting(self, "sleep_ms") awake_time_ms = SettingsBase.get_setting(self, "awake_time_ms") # The original sample rate is used as the sleep rate: xbee_sleep_cfg = XBeeConfigBlockSleep(extended_address) if sleep_ms > 0: # Configure node to sleep for the specified interval: xbee_sleep_cfg.sleep_cycle_set(awake_time_ms, sleep_ms) else: # If sleep_ms is 0, disable sleeping on the node altogether: xbee_sleep_cfg.sleep_mode_set(SM_DISABLED) self.__xbee_manager.xbee_device_config_block_add(self, xbee_sleep_cfg) # Configure pins DIO0 .. DIO3 for digital input: for io_pin in [ 'D0', 'D1', 'D2', 'D3' ]: xbee_ddo_cfg.add_parameter(io_pin, 3) # Turn off LEDs: for led in LED_IO_MAP: xbee_ddo_cfg.add_parameter(LED_IO_MAP[led], 0) # Assert that all pin pull-ups are enabled: xbee_ddo_cfg.add_parameter('PR', 0x1fff) # Enable I/O line monitoring on pins DIO0 .. DIO3: xbee_ddo_cfg.add_parameter('IC', 0xf) # Disable any periodic I/O sampling: xbee_ddo_cfg.add_parameter('IR', 0) # Register this configuration block with the XBee Device Manager: self.__xbee_manager.xbee_device_config_block_add(self, xbee_ddo_cfg) # Indicate that we have no more configuration to add: self.__xbee_manager.xbee_device_configure(self) # Handle channels subscribed to output their data to our led # properties: cm = self.__core.get_service("channel_manager") cp = cm.channel_publisher_get() for i in range(1,4): setting_name = "led%d_source" % i channel_name = SettingsBase.get_setting(self, setting_name) if channel_name is not None: cp.subscribe(channel_name, (lambda prop: lambda cn: self.update_property(prop, cn))( "led%d" % i)) return True
def start(self): # Fetch the XBee Manager name from the Settings Manager: xbee_manager_name = SettingsBase.get_setting(self, "xbee_device_manager") dm = self.__core.get_service("device_driver_manager") self.__xbee_manager = dm.instance_get(xbee_manager_name) # Register ourselves with the XBee Device Manager instance: self.__xbee_manager.xbee_device_register(self) # Get the extended address of the device: extended_address = SettingsBase.get_setting(self, "extended_address") # Create a callback specification for our device address, endpoint # Digi XBee profile and sample cluster id: xbdm_rx_event_spec = XBeeDeviceManagerRxEventSpec() xbdm_rx_event_spec.cb_set(self.sample_indication) xbdm_rx_event_spec.match_spec_set( (extended_address, 0xe8, 0xc105, 0x92), (True, True, True, True)) self.__xbee_manager.xbee_device_event_spec_add(self, xbdm_rx_event_spec) # Create a callback specification that calls back this driver when # our device has left the configuring state and has transitioned # to the running state: xbdm_running_event_spec = XBeeDeviceManagerRunningEventSpec() xbdm_running_event_spec.cb_set(self.running_indication) self.__xbee_manager.xbee_device_event_spec_add(self, xbdm_running_event_spec) # Create a DDO configuration block for this device: xbee_ddo_cfg = XBeeConfigBlockDDO(extended_address) # Get the gateway's extended address: gw_xbee_sh, gw_xbee_sl = gw_extended_address_tuple() # Set the destination for I/O samples to be the gateway: xbee_ddo_cfg.add_parameter('DH', gw_xbee_sh) xbee_ddo_cfg.add_parameter('DL', gw_xbee_sl) for io_pin in range(4): # I/O pin for analog input: xbee_ddo_cfg.add_parameter('D%d' % (io_pin), 2) mode = SettingsBase.get_setting(self, 'channel%d_mode' % (io_pin+1) ) mode = AIO_MODE_MAP[mode.lower()] if io_pin % 2 == 1: last_mode = SettingsBase.get_setting(self, 'channel%d_mode' % (io_pin) ) last_mode = AIO_MODE_MAP[last_mode.lower()] if mode == AIO_MODE_DIFFERENTIAL: if last_mode != AIO_MODE_DIFFERENTIAL: raise ValueError, \ "Differential mode may only be set on odd channels." elif last_mode == AIO_MODE_DIFFERENTIAL: # Nothing to do for this paired channel. continue elif mode != AIO_MODE_DIFFERENTIAL and \ last_mode == AIO_MODE_DIFFERENTIAL: raise ValueError, \ "Unable to change mode, paired channel is" + \ " configured for Differential operation." if mode == AIO_MODE_CURRENTLOOP: xbee_ddo_cfg.add_parameter( AIO_CONTROL_LINES[io_pin][AIO_LINE_A], 4) xbee_ddo_cfg.add_parameter( AIO_CONTROL_LINES[io_pin][AIO_LINE_B], 4) elif mode == AIO_MODE_TENV: xbee_ddo_cfg.add_parameter( AIO_CONTROL_LINES[io_pin][AIO_LINE_A], 5) xbee_ddo_cfg.add_parameter( AIO_CONTROL_LINES[io_pin][AIO_LINE_B], 4) elif mode == AIO_MODE_DIFFERENTIAL: xbee_ddo_cfg.add_parameter( AIO_CONTROL_LINES[io_pin][AIO_LINE_A], 4) xbee_ddo_cfg.add_parameter( AIO_CONTROL_LINES[io_pin][AIO_LINE_B], 5) xbee_ddo_cfg.add_parameter( AIO_CONTROL_LINES[io_pin+1][AIO_LINE_A], 4) # else: for OFF, do nothing # TODO: in the future when we support changing the settings # for this adapter on the fly we would need to do: #if previous_mode == Differential and new_mode != Differential: # xbee_ddo_cfg.add_paramseter( # (AIO_CONTROL_LINES[io_pin+1][AIO_LINE_A], 5)) # # TODO: Set setting for io_pin+1 to TenV Mode # if adapter is using internal batteries, then configure battery-monitor # pin and add low_battery channel if SettingsBase.get_setting(self, "enable_low_battery"): # configure battery-monitor pin DIO11/P1 for digital input xbee_ddo_cfg.add_parameter('P1', 3) # add low_battery channel self.__tracer.info("adapter is using internal batteries... " + "adding low battery channel") self.add_property( ChannelSourceDeviceProperty(name="low_battery", type=bool, initial=Sample(timestamp=0, value=False), perms_mask=DPROP_PERM_GET, options=DPROP_OPT_AUTOTIMESTAMP)) else: self.__tracer.info("adapter is not using internal batteries.") # disable the triggered low-battery alarm since it causes problems # we see the value every time new values are sent anyway ic = 0 xbee_ddo_cfg.add_parameter('IC', ic) # Assert input pull-ups xbee_ddo_cfg.add_parameter('PR', 0x1fff) # Is the unit meant to sleep or not? will_sleep = SettingsBase.get_setting(self, "sleep") # Configure the IO Sample Rate. # # This gets a little trickier in cases where the unit is NOT # in sleep mode, yet the sampling rate is over a minute. # # (The max value for IR is 64K and is in milliseconds. # This ends up being a tad over a minute. # # So if we are NOT in sleep mode, and our sample rate # is over 1 minute, 5 seconds (64K), we will set IR to 0, # and set up Dia (in the make_request call), to call us back # when our poll rate is about to trigger. sample_rate_ms = SettingsBase.get_setting(self, "sample_rate_ms") if will_sleep or (not will_sleep and sample_rate_ms <= 0xffff): # Clip sample_rate_ms to the max value of IR: sample_rate_ms = min(sample_rate_ms, 0xffff) xbee_ddo_cfg.add_parameter('IR', sample_rate_ms) else: xbee_ddo_cfg.add_parameter('IR', 0) # Enable/disable power output on terminal 6: power = SettingsBase.get_setting(self, "power") if power: xbee_ddo_cfg.add_parameter('p3', 5) else: xbee_ddo_cfg.add_parameter('p3', 4) # Register this configuration block with the XBee Device Manager: self.__xbee_manager.xbee_device_config_block_add(self, xbee_ddo_cfg) # Setup the sleep parameters on this device: will_sleep = SettingsBase.get_setting(self, "sleep") sample_predelay = SettingsBase.get_setting(self, "sample_predelay") awake_time_ms = (SettingsBase.get_setting(self, "awake_time_ms") + sample_predelay) # The original sample rate is used as the sleep rate: sleep_rate_ms = SettingsBase.get_setting(self, "sample_rate_ms") xbee_sleep_cfg = XBeeConfigBlockSleep(extended_address) if will_sleep: xbee_sleep_cfg.sleep_cycle_set(awake_time_ms, sleep_rate_ms) else: xbee_sleep_cfg.sleep_mode_set(SM_DISABLED) self.__xbee_manager.xbee_device_config_block_add(self, xbee_sleep_cfg) if will_sleep: # Sample time pre-delay, allow the circuitry to power up and # settle before we allow the XBee to send us a sample: xbee_ddo_wh_block = XBeeConfigBlockDDO(extended_address) xbee_ddo_wh_block.apply_only_to_modules((MOD_XB_ZB, MOD_XB_S2C_ZB,)) xbee_ddo_wh_block.add_parameter('WH', sample_predelay) self.__xbee_manager.xbee_device_config_block_add(self, xbee_ddo_wh_block) # Indicate that we have no more configuration to add: self.__xbee_manager.xbee_device_configure(self) return True