1 """ For when responding to the terminal is not trivial """
3 from configparser import ConfigParser
4 from datetime import datetime, timezone
5 from logging import getLogger
6 from struct import pack
10 from .zx303proto import *
11 from .zmsg import Bcast, Resp, topic
13 log = getLogger("loctrkd/termconfig")
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"))
24 proto_name(POSITION_UPLOAD_INTERVAL),
26 zsub.setsockopt(zmq.SUBSCRIBE, topic(proto))
27 zpush = zctx.socket(zmq.PUSH) # type: ignore
28 zpush.connect(conf.get("collector", "listenurl"))
32 zmsg = Bcast(zsub.recv())
33 msg = parse_message(zmsg.packet)
35 "IMEI %s from %s at %s: %s",
38 datetime.fromtimestamp(zmsg.when).astimezone(tz=timezone.utc),
41 if msg.RESPOND is not Respond.EXT:
43 "%s does not expect externally provided response", msg
45 if zmsg.imei is not None and conf.has_section(zmsg.imei):
46 termconfig = common.normconf(conf[zmsg.imei])
47 elif conf.has_section("termconfig"):
48 termconfig = common.normconf(conf["termconfig"])
52 if isinstance(msg, STATUS):
54 "upload_interval": termconfig.get(
55 "statusintervalminutes", 25
58 elif isinstance(msg, SETUP):
60 "uploadintervalseconds",
71 kwargs[key] = termconfig[key]
73 imei=zmsg.imei, when=zmsg.when, packet=msg.Out(**kwargs).packed
75 log.debug("Response: %s", resp)
76 zpush.send(resp.packed)
78 except KeyboardInterrupt:
81 zctx.destroy() # type: ignore
84 if __name__.endswith("__main__"):
85 runserver(common.init(log))