summaryrefslogtreecommitdiff
path: root/include/py/homekit/mqtt/_module.py
blob: 80f27bb9f3a71962ec1886060cee343d265c19a2 (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
60
61
62
63
64
65
66
67
68
69
70
from __future__ import annotations

import abc
import logging
import threading

from time import sleep
from ..util import next_tick_gen

from typing import TYPE_CHECKING, Optional
if TYPE_CHECKING:
    from ._node import MqttNode
    from ._payload import MqttPayload


class MqttModule(abc.ABC):
    _tick_interval: int
    _initialized: bool
    _connected: bool
    _ticker: Optional[threading.Thread]
    _mqtt_node_ref: Optional[MqttNode]

    def __init__(self, tick_interval=0):
        self._tick_interval = tick_interval
        self._initialized = False
        self._ticker = None
        self._logger = logging.getLogger(self.__class__.__name__)
        self._connected = False
        self._mqtt_node_ref = None

    def on_connect(self, mqtt: MqttNode):
        self._connected = True
        self._mqtt_node_ref = mqtt
        if self._tick_interval:
            self._start_ticker()

    def on_disconnect(self, mqtt: MqttNode):
        self._connected = False
        self._mqtt_node_ref = None

    def is_initialized(self):
        return self._initialized

    def set_initialized(self):
        self._initialized = True

    def unset_initialized(self):
        self._initialized = False

    def tick(self):
        pass

    def _tick(self):
        g = next_tick_gen(self._tick_interval)
        while self._connected:
            sleep(next(g))
            if not self._connected:
                break
            self.tick()

    def _start_ticker(self):
        if not self._ticker or not self._ticker.is_alive():
            name_part = f'{self._mqtt_node_ref.id}/' if self._mqtt_node_ref else ''
            self._ticker = None
            self._ticker = threading.Thread(target=self._tick,
                                            name=f'mqtt:{self.__class__.__name__}/{name_part}ticker')
            self._ticker.start()

    def handle_payload(self, mqtt: MqttNode, topic: str, payload: bytes) -> Optional[MqttPayload]:
        pass