summaryrefslogtreecommitdiff
path: root/src/sensors_mqtt_sender.py
blob: f4f8ec9ad1d7c2fe609dd337aa822f5feeff360f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
import paho.mqtt.client as mqtt
import logging
import time
import json

from home.util import parse_addr, MySimpleSocketClient
from home.mqtt import MQTTBase, poll_tick
from home.mqtt.message import Temperature
from home.config import config

logger = logging.getLogger(__name__)


class MQTTClient(MQTTBase):
    def on_connect(self, client: mqtt.Client, userdata, flags, rc):
        super().on_connect(client, userdata, flags, rc)

    def poll(self):
        freq = int(config['mqtt']['sensors']['poll_freq'])
        logger.debug(f'freq={freq}')

        g = poll_tick(freq)
        while True:
            time.sleep(next(g))
            for k, v in config['mqtt']['sensors']['si7021'].items():
                host, port = parse_addr(v['addr'])
                self.publish_si7021(host, port, k)

    def publish_si7021(self, host: str, port: int, name: str):
        logging.debug(f"publish_si7021/{name}: {host}:{port}")

        try:
            now = time.time()
            socket = MySimpleSocketClient(host, port)

            socket.write('read')
            response = json.loads(socket.read().strip())

            temp = response['temp']
            humidity = response['humidity']

            logging.debug(f'publish_si7021/{name}: temp={temp} humidity={humidity}')

            packer = Temperature()
            self.client.publish(f'home/{self.home_id}/si7021/{name}',
                                payload=packer.pack(round(now), temp, humidity),
                                qos=1)
        except Exception as e:
            logger.exception(e)


if __name__ == '__main__':
    config.load('sensors_mqtt_sender')

    client = MQTTClient()
    client.configure_tls()
    client.connect_and_loop(loop_forever=False)
    client.poll()