1 """ For when responding to the terminal is not trivial """
3 from datetime import datetime, timezone
4 from logging import getLogger
5 from struct import pack
9 from .gps303proto import *
10 from .zmsg import Bcast, Resp, topic
12 log = getLogger("gps303/termconfig")
16 termconfig = common.normconf(conf["termconfig"])
18 zsub = zctx.socket(zmq.SUB)
19 zsub.connect(conf.get("collector", "publishurl"))
23 POSITION_UPLOAD_INTERVAL.PROTO,
25 zsub.setsockopt(zmq.SUBSCRIBE, topic(proto))
26 zpush = zctx.socket(zmq.PUSH)
27 zpush.connect(conf.get("collector", "listenurl"))
31 zmsg = Bcast(zsub.recv())
32 msg = parse_message(zmsg.packet)
34 "IMEI %s from %s at %s: %s",
37 datetime.fromtimestamp(zmsg.when).astimezone(tz=timezone.utc),
40 if msg.RESPOND is not Respond.EXT:
42 "%s does not expect externally provided response", msg
45 if isinstance(msg, STATUS):
47 "upload_interval": termconfig.get(
48 "statusintervalminutes", 25
51 elif isinstance(msg, SETUP):
53 "uploadintervalseconds",
64 kwargs[key] = termconfig[key]
66 imei=zmsg.imei, when=zmsg.when, packet=msg.Out(**kwargs).packed
68 log.debug("Response: %s", resp)
69 zpush.send(resp.packed)
71 except KeyboardInterrupt:
75 if __name__.endswith("__main__"):
76 runserver(common.init(log))