def run(self):

        while 1:
            if self.__stopevent.isSet():
                self.__stopevent.clear()
                break

            try:
                device_stats = query_state("device_stats")

                for stat in [
                        'uptime', 'cpu', 'freemem', 'usedmem', 'totalmem'
                ]:
                    for item in device_stats:
                        data = item.find(stat)
                        if data != None:
                            data = data.text
                            break
                    else:
                        continue

                    if stat == 'uptime':
                        self.property_set(
                            "uptime", Sample(0, int(float(data)), unit="sec"))
                    elif stat == 'cpu':
                        self.property_set("cpu_utilization",
                                          Sample(0, int(data), unit="%"))
                    elif stat == 'freemem':
                        self.property_set("free_memory",
                                          Sample(0, int(data), unit="bytes"))
                    elif stat == 'usedmem':
                        self.property_set("used_memory",
                                          Sample(0, int(data), unit="bytes"))
                    elif stat == 'totalmem':
                        self.property_set("total_memory",
                                          Sample(0, int(data), unit="bytes"))

            except Exception, e:
                self.__tracer.error("Unable to update stat: %s", str(e))

            digitime.sleep(SettingsBase.get_setting(self, "update_rate"))
    def run(self):

        while 1:
            if self.__stopevent.isSet():
                self.__stopevent.clear()
                break

            try:
                device_stats = query_state("device_stats")

                for stat in ['uptime', 'cpu', 'freemem', 'usedmem', 'totalmem']:
                    for item in device_stats:
                        data = item.find(stat)
                        if data != None:
                            data = data.text
                            break
                    else:
                        continue

                    if stat == 'uptime':
                        self.property_set("uptime",
                            Sample(0, int(data), unit="sec"))
                    elif stat == 'cpu':
                        self.property_set("cpu_utilization",
                            Sample(0, int(data), unit="%"))
                    elif stat == 'freemem':
                        self.property_set("free_memory",
                            Sample(0, int(data), unit="bytes"))
                    elif stat == 'usedmem':
                        self.property_set("used_memory",
                            Sample(0, int(data), unit="bytes"))
                    elif stat == 'totalmem':
                        self.property_set("total_memory",
                            Sample(0, int(data), unit="bytes"))

            except Exception, e:
                self.__tracer.error("Unable to update stat: %s", str(e))

            time.sleep(SettingsBase.get_setting(self,"update_rate"))
    def start(self):
        """Start the device driver.  Returns bool."""

        # Attempt to detect if we are on older units that require a different
        # algorithm for determine calibration.
        try:
            device_info = query_state("device_info")
            for item in device_info:
                hardware_strapping = item.find('hardwarestrapping')
                if hardware_strapping != None:
                    hardware_strapping = hardware_strapping.text
                    break
            else:
                hardware_strapping = ''

            if hardware_strapping == self.OLD_HARDWARE_STRAPPING_A or hardware_strapping == self.OLD_HARDWARE_STRAPPING_B:
                self.__tracer.info("Old hardware detected. " +
                                   "Turning on old support flag.")
                self.__OLD_HARDWARE = True
        except:
            pass

        # Grab the calibration rate setting, and store it
        self.__calibration_interval = SettingsBase.get_setting(self.__parent,
                                                   "calibration_rate_ms")

        self.__calibration_interval /= 1000.0

        # Force a calibration the first time getting samples.
        self.__calibration_time = -self.__calibration_interval

        # Fetch the XBee Manager name from the Settings Manager:
        xbee_manager_name = SettingsBase.get_setting(self.__parent, "xbee_device_manager")
        dm = self.__core.get_service("device_driver_manager")
        self.__xbee_manager = dm.instance_get(xbee_manager_name)

        # Walk through the channels, allocating required channel types
        # and creating each channels respective channel properties.
        for channel in self.__aio_channels:
            channel_name = "channel%d" % (channel + 1)
            mode = SettingsBase.get_setting(self.__parent, 'channel%d_mode' % (channel + 1))
            aio_chan = XBeeLocalAIO(channel_name, self, channel, mode.lower())
            self.__aio_channel_structures.append(aio_chan)

        for channel in self.__dio_channels:
            channel_name = "channel%d" % (channel + 1)
            direction = SettingsBase.get_setting(self.__parent, 'channel%d_dir' % (channel + 1))
            direction = direction.lower()
            source = SettingsBase.get_setting(self.__parent, 'channel%d_source' % (channel + 1))
            source = source.lower()
            dio_chan = XBeeLocalDIO(channel_name, self, channel, direction.lower(), source)
            self.__dio_channel_structures.append(dio_chan)

        # Register ourselves with the XBee Device Manager instance:
        self.__xbee_manager.xbee_device_register(self)

        # 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 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((None,
        xbdm_rx_event_spec.match_spec_set((gw_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.
        # None indicates that we will be using the local radio.
        xbee_ddo_cfg = XBeeConfigBlockDDO(None)

        # 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 the X3, aux power can be turned on/off.
        # For all other platforms, it is always on, and cannot be turned off.
        if get_platform_name() == 'digix3':
            power = SettingsBase.get_setting(self.__parent, "power")
            if power == Boolean(True, style = STYLE_ONOFF):
                xbee_ddo_cfg.add_parameter('d4', 5)
            elif power == Boolean(False, style=STYLE_ONOFF):
                xbee_ddo_cfg.add_parameter('d4', 4)

        # Call each channels start function to configure itself and start.
        # It will append any needed DDO commands to the 'xbee_ddo_cfg' block
        # we pass in.
        for channel in self.__aio_channel_structures:
            channel.start(xbee_ddo_cfg)

        for channel in self.__dio_channel_structures:
            channel.start(xbee_ddo_cfg)

        # Walk the digital channels again, if any are set to input,
        # we need to have the pin monitored.
        ic = 0
        for channel in self.__dio_channel_structures:
            if channel.mode() == "in":
                ic |= 1 << channel.channel()

        # 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
        #
        xbee_ddo_cfg.add_parameter('IC', ic)

        # 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)

        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)

        # Attempt to detect if we are on older units that require a different
        # algorithm for determine calibration.
        try:
            device_info = query_state("device_info")
            for item in device_info:
                hardware_strapping = item.find('hardwarestrapping')
                if hardware_strapping != None:
                    hardware_strapping = hardware_strapping.text
                    break
            else:
                hardware_strapping = ''

            if hardware_strapping == self.LOCAL_AIO_R1_STRAPPING_A or hardware_strapping == self.LOCAL_AIO_R1_STRAPPING_B:
                self.__tracer.info("Old hardware detected. Turning on old support flag.")
                self.__OLD_HARDWARE = True
        except:
            pass

        # Register ourselves with the XBee Device Manager instance:
        self.__xbee_manager.xbee_device_register(self)

        # 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)

        extended_address = None

        # Create a DDO configuration block for this device:
        xbee_ddo_cfg = XBeeConfigBlockDDO(extended_address)

        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 = self.LOCAL_AIO_MODE_MAP[mode.lower()]

            if mode == self.LOCAL_AIO_MODE_CURRENTLOOP:
                digihw.configure_channel(io_pin, 1)
                xbee_ddo_cfg.add_parameter(self.LOCAL_AIO_CONTROL_LINES[io_pin], 2)
            elif mode == self.LOCAL_AIO_MODE_TENV:
                digihw.configure_channel(io_pin, 2)
                xbee_ddo_cfg.add_parameter(self.LOCAL_AIO_CONTROL_LINES[io_pin], 2)
            
        # 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)

        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)

        # Attempt to detect if we are on older units that require a different
        # algorithm for determine calibration.
        try:
            device_info = query_state("device_info")
            for item in device_info:
                hardware_strapping = item.find('hardwarestrapping')
                if hardware_strapping != None:
                    hardware_strapping = hardware_strapping.text
                    break
            else:
                hardware_strapping = ''

            if hardware_strapping == self.LOCAL_AIO_R1_STRAPPING_A or hardware_strapping == self.LOCAL_AIO_R1_STRAPPING_B:
                self.__tracer.info(
                    "Old hardware detected. Turning on old support flag.")
                self.__OLD_HARDWARE = True
        except:
            pass

        # Register ourselves with the XBee Device Manager instance:
        self.__xbee_manager.xbee_device_register(self)

        # 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)

        extended_address = None

        # Create a DDO configuration block for this device:
        xbee_ddo_cfg = XBeeConfigBlockDDO(extended_address)

        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 = self.LOCAL_AIO_MODE_MAP[mode.lower()]

            if mode == self.LOCAL_AIO_MODE_CURRENTLOOP:
                digihw.configure_channel(io_pin, 1)
                xbee_ddo_cfg.add_parameter(
                    self.LOCAL_AIO_CONTROL_LINES[io_pin], 2)
            elif mode == self.LOCAL_AIO_MODE_TENV:
                digihw.configure_channel(io_pin, 2)
                xbee_ddo_cfg.add_parameter(
                    self.LOCAL_AIO_CONTROL_LINES[io_pin], 2)

        # 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)

        return True
    def start(self):
        """Start the device driver.  Returns bool."""

        # Attempt to detect if we are on older units that require a different
        # algorithm for determine calibration.
        try:
            device_info = query_state("device_info")
            for item in device_info:
                hardware_strapping = item.find('hardwarestrapping')
                if hardware_strapping != None:
                    hardware_strapping = hardware_strapping.text
                    break
            else:
                hardware_strapping = ''

            if hardware_strapping == self.OLD_HARDWARE_STRAPPING_A or hardware_strapping == self.OLD_HARDWARE_STRAPPING_B:
                self.__tracer.info("Old hardware detected. " +
                                   "Turning on old support flag.")
                self.__OLD_HARDWARE = True
        except:
            pass

        # Grab the calibration rate setting, and store it
        self.__calibration_interval = SettingsBase.get_setting(self.__parent,
                                                   "calibration_rate_ms")

        self.__calibration_interval /= 1000.0

        # Force a calibration the first time getting samples.
        self.__calibration_time = -self.__calibration_interval

        # Fetch the XBee Manager name from the Settings Manager:
        xbee_manager_name = SettingsBase.get_setting(self.__parent, "xbee_device_manager")
        dm = self.__core.get_service("device_driver_manager")
        self.__xbee_manager = dm.instance_get(xbee_manager_name)

        # Walk through the channels, allocating required channel types
        # and creating each channels respective channel properties.
        for channel in self.__aio_channels:
            channel_name = "channel%d" % (channel + 1)
            mode = SettingsBase.get_setting(self.__parent, 'channel%d_mode' % (channel + 1))
            aio_chan = XBeeLocalAIO(channel_name, self, channel, mode.lower())
            self.__aio_channel_structures.append(aio_chan)

        for channel in self.__dio_channels:
            channel_name = "channel%d" % (channel + 1)
            direction = SettingsBase.get_setting(self.__parent, 'channel%d_dir' % (channel + 1))
            direction = direction.lower()
            source = SettingsBase.get_setting(self.__parent, 'channel%d_source' % (channel + 1))
            source = source.lower()
            dio_chan = XBeeLocalDIO(channel_name, self, channel, direction.lower(), source)
            self.__dio_channel_structures.append(dio_chan)

        # Register ourselves with the XBee Device Manager instance:
        self.__xbee_manager.xbee_device_register(self)

        # 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 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((None,
        xbdm_rx_event_spec.match_spec_set((gw_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.
        # None indicates that we will be using the local radio.
        xbee_ddo_cfg = XBeeConfigBlockDDO(None)

        # 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 the X3, aux power can be turned on/off.
        # For all other platforms, it is always on, and cannot be turned off.
        if get_platform_name() == 'digix3':
            power = SettingsBase.get_setting(self.__parent, "power")
            if power == Boolean(True, style = STYLE_ONOFF):
                xbee_ddo_cfg.add_parameter('d4', 5)
            elif power == Boolean(False, style=STYLE_ONOFF):
                xbee_ddo_cfg.add_parameter('d4', 4)

        # Call each channels start function to configure itself and start.
        # It will append any needed DDO commands to the 'xbee_ddo_cfg' block
        # we pass in.
        for channel in self.__aio_channel_structures:
            channel.start(xbee_ddo_cfg)

        for channel in self.__dio_channel_structures:
            channel.start(xbee_ddo_cfg)

        # Walk the digital channels again, if any are set to input,
        # we need to have the pin monitored.
        ic = 0
        for channel in self.__dio_channel_structures:
            if channel.mode() == "in":
                ic |= 1 << channel.channel()

        # 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
        #
        xbee_ddo_cfg.add_parameter('IC', ic)

        # 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)

        return True