File: publish.py

package info (click to toggle)
python-aranet4 2.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 232 kB
  • sloc: python: 1,845; makefile: 5
file content (66 lines) | stat: -rw-r--r-- 1,683 bytes parent folder | download
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
from dataclasses import asdict
import sys

from paho.mqtt import publish

import aranet4

def buildMsgs(readings, topic):
    return [
        (topic + "temperature", readings["temperature"]),
        (topic + "pressure", readings["pressure"]),
        (topic + "humidity", readings["humidity"]),
        (topic + "co2", readings["co2"]),
        (topic + "battery", readings["battery"])
    ]

def readArg(argv, key, default, error="Invalid value"):
    if key in argv:
        idx = argv.index(key) + 1
        if idx >= len(argv):
            print(error)
            raise Exception(error)
        return argv[idx]
    return default

def main(argv):
    if len(argv) < 3:
        print("Missing device address, topic base and/or hostname.")
        argv[0] = "?"

    if "help" in argv or "?" in argv:
        print("Usage: python publish.py DEVICE_ADDRESS HOSTNAME TOPIC_BASE [OPTIONS]")
        print("  -P  <port>      Broker port")
        print("  -u  <user>      Auth user name")
        print("  -p  <password>  Auth user password")

        print("")
        return

    device_mac = argv[0]
    host = argv[1]
    topic = argv[2]

    port = readArg(argv, "-P", "1883")
    user = readArg(argv, "-u", "")
    pwd =  readArg(argv, "-p", "")

    auth = None

    if len(user) > 0:
        auth = {"username":user}

        if len(pwd) > 0:
            auth["password"] = pwd

    if topic[-1] != "/":
        topic += "/"

    current = aranet4.client.get_current_readings(device_mac)

    print("Publishing results...")
    publish.multiple(buildMsgs(asdict(current), topic), hostname=host, port=int(port), auth=auth)


if __name__== "__main__":
    main(sys.argv[1:])