]> average.org Git - loctrkd.git/blobdiff - gps303/__main__.py
introduce command-line forntend to send cmds
[loctrkd.git] / gps303 / __main__.py
diff --git a/gps303/__main__.py b/gps303/__main__.py
new file mode 100644 (file)
index 0000000..45deb2f
--- /dev/null
@@ -0,0 +1,37 @@
+""" Command line tool for sending requests to the terminal """
+
+from datetime import datetime, timezone
+from getopt import getopt
+from logging import getLogger
+from sys import argv
+import zmq
+
+from . import common
+from .gps303proto import *
+from .zmsg import Bcast, Resp
+
+log = getLogger("gps303")
+
+
+def main(conf, opts, args):
+    zctx = zmq.Context()
+    zpush = zctx.socket(zmq.PUSH)
+    zpush.connect(conf.get("collector", "listenurl"))
+
+    if len(args) < 2:
+        raise ValueError("Too few args, need IMEI and command min: " + str(args))
+    imei = args[0]
+    cmd = args[1]
+    args = args[2:]
+    cls = class_by_prefix(cmd)
+    if isinstance(cls, list):
+        raise ValueError("Prefix does not select a single class: " + str(cls))
+    kwargs = {}
+    resp = Resp(imei=imei, packet=cls.response(**kwargs))
+    log.debug("Response: %s", resp)
+    zpush.send(resp.packed)
+
+
+if __name__.endswith("__main__"):
+    opts, args = getopt(argv[1:], "c:d")
+    main(common.init(log, opts=opts), opts, args)