From: Eugene Crosser Date: Fri, 22 Apr 2022 21:27:15 +0000 (+0200) Subject: introduce command-line forntend to send cmds X-Git-Tag: 0.01~39 X-Git-Url: http://www.average.org/gitweb/?p=loctrkd.git;a=commitdiff_plain;h=5d056391aed17388fb8c2bfe71fd9eade2fe5c55 introduce command-line forntend to send cmds --- 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)