1 """ Command line tool for sending requests to the terminal """
3 from configparser import ConfigParser
4 from datetime import datetime, timezone
5 from getopt import getopt
6 from importlib import import_module
7 from logging import getLogger
10 from typing import Any, cast, List, Tuple, Type, Union
14 from .protomodule import ProtoModule
15 from .zmsg import Bcast, Resp
17 log = getLogger("loctrkd")
20 pmods: List[ProtoModule] = []
24 conf: ConfigParser, opts: List[Tuple[str, str]], args: List[str]
28 cast(ProtoModule, import_module("." + modnm, __package__))
29 for modnm in conf.get("common", "protocols").split(",")
31 # Is this https://github.com/zeromq/pyzmq/issues/1627 still not fixed?!
32 zctx = zmq.Context() # type: ignore
33 zpush = zctx.socket(zmq.PUSH) # type: ignore
34 zpush.connect(conf.get("collector", "listenurl"))
38 "Too few args, need IMEI and command min: " + str(args)
45 if pmod.proto_handled(cmd):
49 raise NotImplementedError(f"No protocol can handle {cmd}")
50 cls = pmod.class_by_prefix(cmd)
51 if isinstance(cls, list):
52 raise ValueError("Prefix does not select a single class: " + str(cls))
53 kwargs = dict([arg.split("=") for arg in args])
57 resp = Resp(imei=imei, when=time(), packet=cls.Out(**kwargs).packed)
58 log.debug("Response: %s", resp)
59 zpush.send(resp.packed)
62 if __name__.endswith("__main__"):
63 opts, args = getopt(argv[1:], "c:d")
64 main(common.init(log, opts=opts), opts, args)