X-Git-Url: http://average.org/gitweb/?a=blobdiff_plain;f=gps303%2Ftermconfig.py;h=723e8f62766ef542dbdb337ae015c66203ff1b9c;hb=18eda7307e92eeee6a9e2fdd2e810f98d25df654;hp=10c6286d544a76722a707a0cd0cdc73ad3e884ea;hpb=ecb31fcdc354471235ba44edbad3b24a6bded905;p=loctrkd.git diff --git a/gps303/termconfig.py b/gps303/termconfig.py index 10c6286..723e8f6 100644 --- a/gps303/termconfig.py +++ b/gps303/termconfig.py @@ -1,33 +1,30 @@ """ For when responding to the terminal is not trivial """ +from configparser import ConfigParser from datetime import datetime, timezone from logging import getLogger from struct import pack import zmq from . import common -from .gps303proto import parse_message, proto_by_name -from .zmsg import Bcast, Resp +from .gps303proto import * +from .zmsg import Bcast, Resp, topic log = getLogger("gps303/termconfig") -def runserver(conf): - zctx = zmq.Context() - zsub = zctx.socket(zmq.SUB) +def runserver(conf: ConfigParser) -> None: + # Is this https://github.com/zeromq/pyzmq/issues/1627 still not fixed?! + zctx = zmq.Context() # type: ignore + zsub = zctx.socket(zmq.SUB) # type: ignore zsub.connect(conf.get("collector", "publishurl")) - for protoname in ( - "SUPERVISION", - "STATUS", - "RESET", - "WHITELIST_TOTAL", - "PROHIBIT_LBS", - "SETUP", - "POSITION_UPLOAD_INTERVAL", + for proto in ( + STATUS.PROTO, + SETUP.PROTO, + POSITION_UPLOAD_INTERVAL.PROTO, ): - topic = pack("B", proto_by_name(protoname)) - zsub.setsockopt(zmq.SUBSCRIBE, topic) - zpush = zctx.socket(zmq.PUSH) + zsub.setsockopt(zmq.SUBSCRIBE, topic(proto)) + zpush = zctx.socket(zmq.PUSH) # type: ignore zpush.connect(conf.get("collector", "listenurl")) try: @@ -41,13 +38,47 @@ def runserver(conf): datetime.fromtimestamp(zmsg.when).astimezone(tz=timezone.utc), msg, ) - # TODO get data from the config - resp = Resp(imei=zmsg.imei, packet=msg.response()) + if msg.RESPOND is not Respond.EXT: + log.error( + "%s does not expect externally provided response", msg + ) + if zmsg.imei is not None and conf.has_section(zmsg.imei): + termconfig = common.normconf(conf[zmsg.imei]) + elif conf.has_section("termconfig"): + termconfig = common.normconf(conf["termconfig"]) + else: + termconfig = {} + kwargs = {} + if isinstance(msg, STATUS): + kwargs = { + "upload_interval": termconfig.get( + "statusintervalminutes", 25 + ) + } + elif isinstance(msg, SETUP): + for key in ( + "uploadintervalseconds", + "binaryswitch", + "alarms", + "dndtimeswitch", + "dndtimes", + "gpstimeswitch", + "gpstimestart", + "gpstimestop", + "phonenumbers", + ): + if key in termconfig: + kwargs[key] = termconfig[key] + resp = Resp( + imei=zmsg.imei, when=zmsg.when, packet=msg.Out(**kwargs).packed + ) log.debug("Response: %s", resp) zpush.send(resp.packed) except KeyboardInterrupt: - pass + zsub.close() + zpush.close() + zctx.destroy() # type: ignore if __name__.endswith("__main__"):