Пример #1
0
class RFXCOMCollector(object):

    def __init__(self, device):
        self.transport = PySerialTransport(device)
        self.transport.reset()
        self.unparsable = 0

    def __iter__(self):
        return self

    def __next__(self):
        try:
            event = self.transport.receive_blocking()
            self.unparsable = 0
        except Exception:
            logger.exception("Got an unparsable byte")
            self.unparsable += 1
            if self.unparsable > 10:
                raise
            return None
        logger.debug(event)
        # general case, temp, rain, wind
        packet = SensorPacket()
        packet.from_packet(event.device.pkt)
        return packet
Пример #2
0
class Dispatcher(object):
    def __init__(self, device, names, server, config):
        self.transport = PySerialTransport(device)
        self.transport.reset()
        self.names = names
        self.mqtt = MQTT(server, config)
        self.config = config

    def loopforever(self):
        unparsable = 0

        while True:
            try:
                event = self.transport.receive_blocking()
                unparsable = 0
            except Exception:
                logger.exception("Got an unparsable byte")
                unparsable += 1
                if unparsable > 10:
                    raise
                continue

            logger.debug(event)

            if event is None:
                continue
            now = int(time.time())

            # special case. Temp / Humid / Barometer sensors are
            # turned into 2 sensors, barometer and a temp one.
            if isinstance(event.device.pkt, ll.TempHumidBaro):
                b_packet = SensorPacket(stype=IS_BARO)
                b_packet.from_packet(event.device.pkt)
                self.mqtt.send("barometer", b_packet.as_json(timestamp=now))

            # general case, temp, rain, wind
            packet = SensorPacket()
            packet.from_packet(event.device.pkt)

            if packet.is_temp:
                name = self.names.get(packet.sensor_id)
                if name:
                    topic = "temperature/%s" % name
                else:
                    topic = "unknown/%s" % packet.sensor_id
                self.mqtt.send(topic, packet.as_json(timestamp=now))

            if packet.is_wind:
                self.mqtt.send("wind", packet.as_json(timestamp=now))

            if packet.is_rain:
                self.mqtt.send("rain", packet.as_json(timestamp=now))
Пример #3
0
 def __init__(self, device, names, server, config):
     self.transport = PySerialTransport(device)
     self.transport.reset()
     self.names = names
     self.mqtt = MQTT(server, config)
     self.config = config
Пример #4
0
 def __init__(self, device):
     self.transport = PySerialTransport(device)
     self.transport.reset()
     self.unparsable = 0