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 .gps303proto import *
11 from .zmsg import Bcast, Resp, topic
13 log = getLogger("gps303/termconfig")
16 def runserver(conf: ConfigParser) -> None:
17 termconfig = common.normconf(conf["termconfig"])
18 # Is this https://github.com/zeromq/pyzmq/issues/1627 still not fixed?!
19 zctx = zmq.Context() # type: ignore
20 zsub = zctx.socket(zmq.SUB) # type: ignore
21 zsub.connect(conf.get("collector", "publishurl"))
25 POSITION_UPLOAD_INTERVAL.PROTO,
27 zsub.setsockopt(zmq.SUBSCRIBE, topic(proto))
28 zpush = zctx.socket(zmq.PUSH) # type: ignore
29 zpush.connect(conf.get("collector", "listenurl"))
33 zmsg = Bcast(zsub.recv())
34 msg = parse_message(zmsg.packet)
36 "IMEI %s from %s at %s: %s",
39 datetime.fromtimestamp(zmsg.when).astimezone(tz=timezone.utc),
42 if msg.RESPOND is not Respond.EXT:
44 "%s does not expect externally provided response", msg
47 if isinstance(msg, STATUS):
49 "upload_interval": termconfig.get(
50 "statusintervalminutes", 25
53 elif isinstance(msg, SETUP):
55 "uploadintervalseconds",
66 kwargs[key] = termconfig[key]
68 imei=zmsg.imei, when=zmsg.when, packet=msg.Out(**kwargs).packed
70 log.debug("Response: %s", resp)
71 zpush.send(resp.packed)
73 except KeyboardInterrupt:
77 if __name__.endswith("__main__"):
78 runserver(common.init(log))