From 5d056391aed17388fb8c2bfe71fd9eade2fe5c55 Mon Sep 17 00:00:00 2001 From: Eugene Crosser Date: Fri, 22 Apr 2022 23:27:15 +0200 Subject: [PATCH] introduce command-line forntend to send cmds --- gps303/__main__.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 gps303/__main__.py diff --git a/gps303/__main__.py b/gps303/__main__.py new file mode 100644 index 0000000..45deb2f --- /dev/null +++ b/gps303/__main__.py @@ -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) -- 2.43.0