]> www.average.org Git - loctrkd.git/blob - gps303/termconfig.py
make termconfig use unit specific config section
[loctrkd.git] / gps303 / termconfig.py
1 """ For when responding to the terminal is not trivial """
2
3 from configparser import ConfigParser
4 from datetime import datetime, timezone
5 from logging import getLogger
6 from struct import pack
7 import zmq
8
9 from . import common
10 from .gps303proto import *
11 from .zmsg import Bcast, Resp, topic
12
13 log = getLogger("gps303/termconfig")
14
15
16 def runserver(conf: ConfigParser) -> None:
17     # Is this https://github.com/zeromq/pyzmq/issues/1627 still not fixed?!
18     zctx = zmq.Context()  # type: ignore
19     zsub = zctx.socket(zmq.SUB)  # type: ignore
20     zsub.connect(conf.get("collector", "publishurl"))
21     for proto in (
22         STATUS.PROTO,
23         SETUP.PROTO,
24         POSITION_UPLOAD_INTERVAL.PROTO,
25     ):
26         zsub.setsockopt(zmq.SUBSCRIBE, topic(proto))
27     zpush = zctx.socket(zmq.PUSH)  # type: ignore
28     zpush.connect(conf.get("collector", "listenurl"))
29
30     try:
31         while True:
32             zmsg = Bcast(zsub.recv())
33             msg = parse_message(zmsg.packet)
34             log.debug(
35                 "IMEI %s from %s at %s: %s",
36                 zmsg.imei,
37                 zmsg.peeraddr,
38                 datetime.fromtimestamp(zmsg.when).astimezone(tz=timezone.utc),
39                 msg,
40             )
41             if msg.RESPOND is not Respond.EXT:
42                 log.error(
43                     "%s does not expect externally provided response", msg
44                 )
45             if zmsg.imei is not None and conf.has_section(zmsg.imei):
46                 termconfig = common.normconf(conf[zmsg.imei])
47             else:
48                 termconfig = common.normconf(conf["termconfig"])
49             kwargs = {}
50             if isinstance(msg, STATUS):
51                 kwargs = {
52                     "upload_interval": termconfig.get(
53                         "statusintervalminutes", 25
54                     )
55                 }
56             elif isinstance(msg, SETUP):
57                 for key in (
58                     "uploadintervalseconds",
59                     "binaryswitch",
60                     "alarms",
61                     "dndtimeswitch",
62                     "dndtimes",
63                     "gpstimeswitch",
64                     "gpstimestart",
65                     "gpstimestop",
66                     "phonenumbers",
67                 ):
68                     if key in termconfig:
69                         kwargs[key] = termconfig[key]
70             resp = Resp(
71                 imei=zmsg.imei, when=zmsg.when, packet=msg.Out(**kwargs).packed
72             )
73             log.debug("Response: %s", resp)
74             zpush.send(resp.packed)
75
76     except KeyboardInterrupt:
77         pass
78
79
80 if __name__.endswith("__main__"):
81     runserver(common.init(log))