Exemplo n.º 1
0
    async def publish_values(self):
        with PubSub.Listener(self.hub) as queue:
            while True:
                key, value = await queue.get()

                try:
                    mapping = self.mappings[key]
                except KeyError:
                    continue

                if mapping.mode == Mapping.MODE_WRITEONLY:
                    continue

                if mapping.throttle:
                    try:
                        last_send = self.last_send[mapping.key]
                    except KeyError:
                        last_send = 0

                    if last_send + mapping.throttle > time.time():
                        continue

                await self.client.publish(self.write_topic(mapping.key),
                                          mapping.formatter.to_mqtt(value))
                self.last_send[mapping.key] = time.time()
Exemplo n.º 2
0
async def printer(hub, ignored_topics=frozenset()):
    with PubSub.Listener(hub) as queue:
        while True:
            key, msg = await queue.get()
            if key not in ignored_topics:
                print(f'Reader for key {key} got message: {msg}')