class ZigBeeDirect(PollingProcessor): def __init__(self, usbPort, baudRate=9600): super(ZigBeeDirect, self).__init__() import serial from Lib.xbee import ZigBee try: self._port = serial.Serial(usbPort, baudRate) except Exception as e: print >> sys.stderr, "Unable to connect to zigbee port, check that the port name (%s) and permissions are correct (should be a+rw)" % ( usbPort) raise e self._zigbee = ZigBee(self._port) # Place for the callback methods to store "static" values. # Currently only needed for the Moving Average Filter for the MCP9700 # temperature sensor temperature calculations. self._handler_memory = {} self._channels = {} self._sr = StateResolver() self._sensorDao = Sensors() self._sensors = self._sensorDao.findSensors() self._warned = [] def __del__(self): self._port.close() super(ZigBeeDirect, self).__del__() @property def channels(self): if self._channels == None: self._channels = {} return self._channels def start(self): print "Started polling directly connected zigBee sensors" self._addPollingProcessor('zigBeeDirect', self.pollZigbeeSensors, None, 0.1) def stop(self): print "Stopped polling directly connected zigBee sensors" self._removePollingProcessor('zigBeeDirect') # ZigBee thread main loop def pollZigbeeSensors(self): """ Read the data from the Zigbee sensors directly connected to this machine """ try: #data, _ = self._xbee.wait_read_frame() data = self._zigbee.wait_read_frame() except Exception as e: if str(type(e)) not in self._warned: print >> sys.stderr, "Error while receiving data from ZigBeeDirect: %s" % e self._warned.append(str(type(e))) return if data["id"] == "rx_explicit": mac = repr(data['source_addr_long']).replace( '\\x', ':').strip(":'").lower() # If NI (Network Id)recognised include NI string in returned values try: channels = self._zigbee._parse_samples( data['rf_data'])[0] # Parse IO data except Exception as e: print >> sys.stderr, "Error reading zigbee data: %s" % e return for channel, _value in channels.items(): channel = "!" + channel.lower() try: sensor = next(s for s in self._sensors if s['ChannelDescriptor'] == str(mac) + str(channel)) except StopIteration: # Only warn once, or we'll flood the console if str(mac) + str(channel) not in self._warned: print "Warning: Unable to locate sensor record for ZigBee sensor ID: %s" % ( str(mac) + str(channel)) self._warned.append(str(mac) + str(channel)) continue _device = sensor['locationName'] _pin = sensor['name'] _id = sensor['sensorId'] _rule = sensor['sensorRule'] _value = _valueHelper.filterValue(_value, sensor['sensorTypeName']) if _value != None: _type = sensor['sensorTypeName'] _uuid = '%s_%s' % (mac, channel) _status = self._sr.getDisplayState({ 'sensorTypeName': _type, 'value': _value, 'sensorId': _id, 'sensorRule': _rule }) self._channels[_uuid] = { 'id': _id, 'room': _device, 'channel': _pin, 'value': _value, 'status': _status }
class ZigBeeDirect(PollingProcessor): def __init__(self, usbPort, baudRate=9600): super(ZigBeeDirect, self).__init__() import serial from Lib.xbee import ZigBee self._port = serial.Serial(usbPort, baudRate) self._zigbee = ZigBee(self._port) # Place for the callback methods to store "static" values. # Currently only needed for the Moving Average Filter for the MCP9700 # temperature sensor temperature calculations. self._handler_memory = {} self._channels = {} self._sr = StateResolver() self._sensorDao = Sensors() self._sensors = self._sensorDao.findSensors() self._warned = [] def __del__(self): self._port.close() super(ZigBeeDirect, self).__del__() @property def channels(self): if self._channels == None: self._channels = {} return self._channels def start(self): print "Started polling directly connected zigBee sensors" self._addPollingProcessor('zigBeeDirect', self.pollZigbeeSensors, None, 0.1) def stop(self): print "Stopped polling directly connected zigBee sensors" self._removePollingProcessor('zigBeeDirect') # ZigBee thread main loop def pollZigbeeSensors(self): """ Read the data from the Zigbee sensors directly connected to this machine """ try: #data, _ = self._xbee.wait_read_frame() data = self._zigbee.wait_read_frame() except Exception as e: if id(self) + type(e) not in self._warned: print >> sys.stderr, "Error while receiving data from ZigBeeDirect: %s" % e self._warned.append(id(self) + type(e)) return if data["id"] == "rx_explicit": mac = repr(data['source_addr_long']).replace('\\x', ':').strip(":'").lower() # If NI (Network Id)recognised include NI string in returned values try: channels = self._zigbee._parse_samples(data['rf_data'])[0] # Parse IO data except Exception as e: print >> sys.stderr, "Error reading zigbee data: %s" % e return for channel, _value in channels.items(): channel = "!" + channel.lower() try: sensor = next(s for s in self._sensors if s['ChannelDescriptor'] == str(mac) + str(channel)) except StopIteration: # Only warn once, or we'll flood the console if str(mac) + str(channel) not in self._warned: print "Warning: Unable to locate sensor record for ZigBee sensor ID: %s" % (str(mac) + str(channel)) self._warned.append(str(mac) + str(channel)) continue _device = sensor['locationName'] _pin = sensor['name'] _id = sensor['sensorId'] _rule = sensor['sensorRule'] _value = _valueHelper.filterValue(_value, sensor['sensorTypeName']) if _value != None: _type = sensor['sensorTypeName'] _uuid = '%s_%s' % (mac , channel) _status = self._sr.getDisplayState({'sensorTypeName': _type, 'value': _value, 'sensorId': _id, 'sensorRule': _rule }) self._channels[_uuid] = { 'id': _id, 'room': _device, 'channel': _pin, 'value': _value, 'status': _status }