def wait_until_xbee_ready(self): ''' wait until we can read something again ''' while True: try: # HACK: remove once firmware issue resolved ddo_get_param(None, 'SH') break except Exception: sleep(1) continue
def retry_ddo_get_param(retries, ext_addr, param, timeout = GLOBAL_DDO_TIMEOUT): """\ Attempt to get a DDO param from an XBee device. This function will attempt to get a DDO command/parameter from a local or remote XBee device. It will attempt to get the command/parameter up to 'retries' times. Returns the result, or None on failure. """ result = None while 1: try: result = xbee.ddo_get_param(ext_addr, param, timeout) break except: retries -= 1 if retries > 0: continue raise DDOTimeoutException, "could not get '%s' from '%s'" % (param, ext_addr) return result
def run(self): global quit_flag while not quit_flag: # make sure rci and xbee are connected try: if self.enable_xig and rci.connected() and xbee.ddo_get_param(None, "VR"): try: self.xig = xig.Xig() try: # set port for xig_console_handler self.xig_console_handler.port = self.xig.getConfig().xbee_udp_port except: pass # start XIG running forever unless a user quit was issued (status == 0) status = self.xig.go() if status == 0: logger.info("Shutting down XIG GUI...") self.enable_xig = False time.sleep(5) quit_flag = True except Exception, e: logger.error("Exception when running XIG: %s" % e) except: pass # expected exception when ddo_get_param fails self.xig_console_handler.port = None self.xig = None time.sleep(1)
def run(self): global quit_flag while not quit_flag: # make sure rci and xbee are connected try: if self.enable_xig and rci.connected() and xbee.ddo_get_param( None, "VR"): try: self.xig = xig.Xig() try: # set port for xig_console_handler self.xig_console_handler.port = self.xig.getConfig( ).xbee_udp_port except: pass # start XIG running forever unless a user quit was issued (status == 0) status = self.xig.go() if status == 0: logger.info("Shutting down XIG GUI...") self.enable_xig = False time.sleep(5) quit_flag = True except Exception, e: logger.error("Exception when running XIG: %s" % e) except: pass # expected exception when ddo_get_param fails self.xig_console_handler.port = None self.xig = None time.sleep(1)
def main(): # make sure the XBee is connected and available. This is only really an issue on a PC. while 1: try: xbee.ddo_get_param(None, 'VR') break except: time.sleep(0.250) # take off every Xig! xig = Xig() ret = -1 try: ret = xig.go() except Exception, e: logger.error("Problem in go(), exiting program: %s" % e) traceback.print_exc(file=sys.stdout) time.sleep(10)
def __rci_at(self, xig_tree): """\ Process a an "at" node, xig_tree is an ElementTree. Returns a string response. """ destination = str(xig_tree.get("hw_address")) command = str(xig_tree.get("command")).upper() if destination is None: return self.__xml_err_msg('invalid hw_address "%s" (missing \'!\'?)' % destination) if command is None: return self.__xml_err_msg('invalid command "%s"' % command) value = xig_tree.get("value") if value is not None: value = str(value) apply = False try: apply = bool(xig_tree.get("apply").lower() == "true") except: pass # interpret value: if command in ("NI","DN"): pass # interpret value as string elif command in ("AC", "WR"): value = "" elif value is None or len(value) == 0 or value.isspace(): value = None # will cause us to read instead of write param elif value.lower().startswith("0x"): try: value = int(value, 16) except: return self.__xml_err_msg( "unable to parse hex int for cmd %s" % repr(command)) else: try: value = int(value) except: return self.__xml_err_msg( "unable to parse int for cmd %s" % repr(command)) # run command: try: result = "" operation = "set" if value is not None: logger.info("AT set param to %s %s=%s" % (repr(destination), command, repr(value))) value = xbee.ddo_set_param(destination, command, value, apply=apply) result = "ok" else: operation = "get" logger.info("AT get param %s from %s" % (command, repr(destination))) value = xbee.ddo_get_param(destination, command) result = "ok" except Exception, e: result = "error" value = str(e)
def _coordinator_ddo_config(self): # handle rare case where coordinator child-table is 'full' after # restart. Since we have no idea how long we've been offline, it is # possible all 10 children seek new association, and with NC=0 the # coordinator will NOT offer association for 3 * SN * SP time # so if sensor wakes once per 4 hours, then would take 12 hours for # coordinator to begin allowing association and data resumption nc = ord(ddo_get_param(None, 'NC')) self._tracer.debug("Coordinator NC=%d at startup", nc) if nc == 0: # then issue network reset self._tracer.debug("Child Table is full - Reset coordinator") ddo_set_param(None, 'NR', 0) # note: reading NC immediately will still have NC=0 sn_sp_val = SettingsBase.get_setting(self, 'sn_sp_override') if sn_sp_val != ZigBeeDeviceManager.SN_SP_DEFAULT: sn_sp_override = _time_formatter(sn_sp_val, test_only=False) if sn_sp_override != None: try: self._sn_sp_override = generate_sn_sp(sn_sp_override) self._tracer.debug('Setting SN/SP override.') except ValueError: self._tracer.warning( 'SN/SP override could not be set. ' 'The given time does not factor cleanly.') ar_new = SettingsBase.get_setting(self, 'source_routing') self._tracer.debug("Zigbee Source Routing setting is:%s", ar_new) ar_new = self.__parse_source_routing(ar_new) if ar_new is not None and (ar_new != ord(ddo_get_param(None, 'AR'))): # only update if changing self._tracer.debug("Zigbee Source Routing: " \ "set AR=%d (0x%02x)", ar_new, ar_new) ddo_set_param(None, 'AR', ar_new, timeout=15) ddo_set_param(None, 'AC', timeout=15) ddo_set_param(None, 'WR', timeout=15) return
def _coordinator_ddo_config(self): # handle rare case where coordinator child-table is 'full' after # restart. Since we have no idea how long we've been offline, it is # possible all 10 children seek new association, and with NC=0 the # coordinator will NOT offer association for 3 * SN * SP time # so if sensor wakes once per 4 hours, then would take 12 hours for # coordinator to begin allowing association and data resumption nc = ord(ddo_get_param(None, 'NC')) self._tracer.debug("Coordinator NC=%d at startup", nc) if nc == 0: # then issue network reset self._tracer.debug("Child Table is full - Reset coordinator") ddo_set_param(None, 'NR', 0) # note: reading NC immediately will still have NC=0 sn_sp_val = SettingsBase.get_setting(self, 'sn_sp_override') if sn_sp_val != ZigBeeDeviceManager.SN_SP_DEFAULT: sn_sp_override = _time_formatter(sn_sp_val, test_only=False) if sn_sp_override != None: try: self._sn_sp_override = generate_sn_sp(sn_sp_override) self._tracer.debug('Setting SN/SP override.') except ValueError: self._tracer.warning('SN/SP override could not be set. ' 'The given time does not factor cleanly.') ar_new = SettingsBase.get_setting(self, 'source_routing') self._tracer.debug("Zigbee Source Routing setting is:%s", ar_new) ar_new = self.__parse_source_routing(ar_new) if ar_new is not None and (ar_new != ord(ddo_get_param(None, 'AR'))): # only update if changing self._tracer.debug("Zigbee Source Routing: " \ "set AR=%d (0x%02x)", ar_new, ar_new) ddo_set_param(None, 'AR', ar_new, timeout=15) ddo_set_param(None, 'AC', timeout=15) ddo_set_param(None, 'WR', timeout=15) return
def retry_ddo_get_param(retries, ext_addr, param, timeout=GLOBAL_DDO_TIMEOUT): """\ Attempt to get a DDO param from an XBee device. This function will attempt to get a DDO command/parameter from a local or remote XBee device. It will attempt to get the command/parameter up to 'retries' times. Returns the result, or None on failure. """ result = None while 1: try: result = xbee.ddo_get_param(ext_addr, param, timeout) break except: retries -= 1 if retries > 0: continue raise DDOTimeoutException('could not get "%s" from "%s"' % ((param, ext_addr))) return result
except Exception, e: print e ################################################################################################### # Detect radio type on the gateway ################################################################################################### # This function detects the hardware version and uses that as a basis to determine what # Addressing information we should use. # In this case, anything over 6400 we determine as a Znet 2.5, anything before that # as 802.15.4. try: hw_version = xbee.ddo_get_param(None, "HV") hw_version = struct.unpack("=H", hw_version)[0] except Exception, e: hw_version = None print e print "Failed to retrieve hardware version from local radio" print "Assuming it's a series 1 device" if hw_version != None: if hw_version > 6400: #If the hardware version is greater then 6400, it must be a series 2 radio print "Detected Series 2 radio in gateway, configuring zigbee socket appropriately" end_point = 0xE8 #232 profile_id = 0xC105 cluster_id = 0x11 #Out the UART MAX_ZIG_PACKET_SIZE = 72 #Packet sizes are at maximum 72 bytes
def get_dd_value(): dd_raw = xbee.ddo_get_param(None, 'DD') dd_raw = struct.unpack('=I', dd_raw)[0] dd = dd_raw & 0xF0000 return dd
desc="Name of host operating system", accessor=lambda: sys.platform)) ).attach( BranchNode('boot_stats', 'Primary interface').attach( SimpleLeafNode( 'ip', dtype=DTYPE.IPV4, desc='IP Address', accessor=create_accessor('ip_address', '0.0.0.0')))).attach( BranchNode('zigbee_state', 'Gateway XBee').attach( SimpleLeafNode('gateway_addr', dtype=DTYPE.XBEE_EXT_ADDR, desc='XBee extended address', accessor=lambda: ':'.join( "%02x" % ord(x) for x in xbee.ddo_get_param(None, 'SH') + xbee.ddo_get_param(None, 'SL')))))) #-- RCI Settings --# rci_tree.attach(RciSettings().attach( BranchNode("system", 'System Settings').attach( SimpleLeafNode("contact", accessor=create_accessor('contact'))).attach( SimpleLeafNode( "location", accessor=create_accessor('location'))).attach( SimpleLeafNode("description", accessor=create_accessor('description'))) ).attach( BranchNode("mgmtglobal", 'Global Mangement').attach( SimpleLeafNode("deviceid", accessor=create_accessor('device_id'))))) #-- RCI Reboot --# #rci_tree.attach(RciReboot())
def __rci_at(self, xig_tree): """\ Process a an "at" node, xig_tree is an ElementTree. Returns a string response. """ destination = str(xig_tree.get("hw_address")) command = str(xig_tree.get("command")).upper() if destination is None: return self.__xml_err_msg( 'invalid hw_address "%s" (missing \'!\'?)' % destination) if command is None: return self.__xml_err_msg('invalid command "%s"' % command) value = xig_tree.get("value") if value is not None: value = str(value) apply = False try: apply = bool(xig_tree.get("apply").lower() == "true") except: pass # interpret value: if command in ("NI", "DN"): pass # interpret value as string elif command in ("AC", "WR"): value = "" elif value is None or len(value) == 0 or value.isspace(): value = None # will cause us to read instead of write param elif value.lower().startswith("0x"): try: value = int(value, 16) except: return self.__xml_err_msg( "unable to parse hex int for cmd %s" % repr(command)) else: try: value = int(value) except: return self.__xml_err_msg("unable to parse int for cmd %s" % repr(command)) # run command: try: result = "" operation = "set" if value is not None: logger.info("AT set param to %s %s=%s" % (repr(destination), command, repr(value))) value = xbee.ddo_set_param(destination, command, value, apply=apply) result = "ok" else: operation = "get" logger.info("AT get param %s from %s" % (command, repr(destination))) value = xbee.ddo_get_param(destination, command) result = "ok" except Exception, e: result = "error" value = str(e)
def get_ee_value(): ee_raw = xbee.ddo_get_param(None, 'EE') ee_val = struct.unpack('=B', ee_raw)[0] return ee_val
accessor=lambda: ":".join("%02X" % ((settings.get('mac', 0) >> (i*8)) & 0xFF) for i in xrange(6)))) .attach(SimpleLeafNode('product', dtype=DTYPE.STRING, desc="product", accessor=create_accessor('device_type'))) .attach(SimpleLeafNode('company', dtype=DTYPE.STRING, desc="company", accessor=create_accessor('company'))) .attach(SimpleLeafNode('os_name', dtype=DTYPE.STRING, desc="Name of host operating system", accessor=lambda: sys.platform)) ) .attach(BranchNode('boot_stats', 'Primary interface') .attach(SimpleLeafNode('ip', dtype=DTYPE.IPV4, desc='IP Address', accessor=create_accessor('ip_address', '0.0.0.0'))) ) .attach(BranchNode('zigbee_state', 'Gateway XBee') .attach(SimpleLeafNode('gateway_addr', dtype=DTYPE.XBEE_EXT_ADDR, desc='XBee extended address', accessor=lambda: ':'.join("%02x" % ord(x) for x in xbee.ddo_get_param(None, 'SH')+xbee.ddo_get_param(None, 'SL')))) ) ) #-- RCI Settings --# rci_tree.attach(RciSettings() .attach(BranchNode("system", 'System Settings') .attach(SimpleLeafNode("contact", accessor=create_accessor('contact'))) .attach(SimpleLeafNode("location", accessor=create_accessor('location'))) .attach(SimpleLeafNode("description", accessor=create_accessor('description'))) ) .attach(BranchNode("mgmtglobal", 'Global Mangement') .attach(SimpleLeafNode("deviceid", accessor=create_accessor('device_id'))) ) ) #-- RCI Reboot --#