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
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",
25 topic = pack("B", proto_by_name(protoname))
26 zsub.setsockopt(zmq.SUBSCRIBE, topic)
27 zpush = zctx.socket(zmq.PUSH)
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.DIR is not Dir.EXT:
43 "%s does not expect externally provided response", msg
46 if isinstance(msg, STATUS):
48 "upload_interval": termconfig.get(
49 "statusintervalminutes", 25
52 elif isinstance(msg, SETUP):
54 "uploadintervalseconds",
65 kwargs[key] = termconfig[key]
66 resp = Resp(imei=zmsg.imei, packet=msg.response(**kwargs))
67 log.debug("Response: %s", resp)
68 zpush.send(resp.packed)
70 except KeyboardInterrupt:
74 if __name__.endswith("__main__"):
75 runserver(common.init(log))