diff options
| author | jwansek <eddie.atten.ea29@gmail.com> | 2023-11-19 18:29:32 +0000 | 
|---|---|---|
| committer | jwansek <eddie.atten.ea29@gmail.com> | 2023-11-19 18:29:32 +0000 | 
| commit | 450b5ce0582773f4684dfc197f04765b81f429c9 (patch) | |
| tree | e19c4ba8deabfd4132230792b17eeb56e6e93802 /mqtt-client | |
| parent | d31d273073eec5220b4eb6cbeb576b3661ec6d7b (diff) | |
| download | power.eda.gay-450b5ce0582773f4684dfc197f04765b81f429c9.tar.gz power.eda.gay-450b5ce0582773f4684dfc197f04765b81f429c9.zip | |
Revert "Added python script to subscribe to MQTT topics and push them to InfluxDB"
This reverts commit d31d273073eec5220b4eb6cbeb576b3661ec6d7b.
Diffstat (limited to 'mqtt-client')
| -rw-r--r-- | mqtt-client/Dockerfile | 10 | ||||
| -rw-r--r-- | mqtt-client/mqtt-client.py | 63 | ||||
| -rw-r--r-- | mqtt-client/requirements.txt | 3 | 
3 files changed, 0 insertions, 76 deletions
| diff --git a/mqtt-client/Dockerfile b/mqtt-client/Dockerfile deleted file mode 100644 index ad59f9b..0000000 --- a/mqtt-client/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -FROM debian:11-slim -ENV TZ=Europe/London -RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone -RUN apt-get update -y -RUN apt-get install -y python3-pip iputils-ping -COPY . /app -WORKDIR /app -RUN pip3 install -r requirements.txt -ENTRYPOINT ["python3"] -CMD ["mqtt-client.py"] diff --git a/mqtt-client/mqtt-client.py b/mqtt-client/mqtt-client.py deleted file mode 100644 index 248e5af..0000000 --- a/mqtt-client/mqtt-client.py +++ /dev/null @@ -1,63 +0,0 @@ -import paho.mqtt.client as paho -from influxdb_client import InfluxDBClient, Point, WritePrecision -from influxdb_client.client.write_api import SYNCHRONOUS -import json -import os - -class MQTTClient: -    def __init__(self): -        self.influxc = InfluxDBClient( -            url = "http://%s:8086" % INFLUXDB_HOST, -            token = os.environ["DOCKER_INFLUXDB_INIT_ADMIN_TOKEN"], -            org = os.environ["DOCKER_INFLUXDB_INIT_ORG"]  -        ) -        self.influxc.ping() - -        self.mqttc = paho.Client('power-listener', clean_session = True) -        self.mqttc.on_connect = self._on_connect_cb -        self.mqttc.on_message = self._on_message_cb - -        self.mqttc.username_pw_set(os.environ["MQTT_USER"], password = os.environ["MQTT_PASSWD"]) -        self.mqttc.connect(MQTT_HOST, 1883, 60) -        self.mqttc.loop_forever() - -    def _on_connect_cb(self, mqtt, userdata, flags, rc): -        print("Connected to broker") -        self.mqttc.subscribe("tele/+/SENSOR") - -    def _on_message_cb(self, mqtt, userdata, msg): -        print('Topic: {0} | Message: {1}'.format(msg.topic, msg.payload)) - -        if "Tasmota" in msg.topic: -            self.handle_tasmota(msg) - -    def handle_tasmota(self, msg): -        from_ = msg.topic.split("/")[1] -        msg_j = json.loads(msg.payload.decode()) -        #print(from_) -        fields = {k: v for k, v in msg_j["ENERGY"].items() if k not in {"TotalStartTime"}} -        points = [{"measurement": "tasmota_power", "tags": {"plug": from_}, "fields": fields}] -        write_api = self.influxc.write_api(write_options = SYNCHRONOUS) -        write_api.write( -            os.environ["DOCKER_INFLUXDB_INIT_BUCKET"], -            os.environ["DOCKER_INFLUXDB_INIT_ORG"], -            points, -            write_precision = WritePrecision.S -        ) - -if __name__ == "__main__": -    env_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "config.env") -    if os.path.exists(env_path): -        import dotenv -        dotenv.load_dotenv(dotenv_path = env_path) -        INFLUXDB_HOST = "localhost" -        MQTT_HOST = "localhost" -    else: -        INFLUXDB_HOST = "influxdb" -        MQTT_HOST = "mqtt" - -    mqtt_client = MQTTClient() - - - - diff --git a/mqtt-client/requirements.txt b/mqtt-client/requirements.txt deleted file mode 100644 index 2532957..0000000 --- a/mqtt-client/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -paho-mqtt -python-dotenv -influxdb-client | 
