]> www.average.org Git - loctrkd.git/blob - gps303/__main__.py
38d21fe6eea049c7e17030a4028976ba827b1898
[loctrkd.git] / gps303 / __main__.py
1 """ Command line tool for sending requests to the terminal """
2
3 from datetime import datetime, timezone
4 from getopt import getopt
5 from logging import getLogger
6 from sys import argv
7 import zmq
8
9 from . import common
10 from .gps303proto import *
11 from .zmsg import Bcast, Resp
12
13 log = getLogger("gps303")
14
15
16 def main(conf, opts, args):
17     zctx = zmq.Context()
18     zpush = zctx.socket(zmq.PUSH)
19     zpush.connect(conf.get("collector", "listenurl"))
20
21     if len(args) < 2:
22         raise ValueError("Too few args, need IMEI and command min: " + str(args))
23     imei = args[0]
24     cmd = args[1]
25     args = args[2:]
26     cls = class_by_prefix(cmd)
27     if isinstance(cls, list):
28         raise ValueError("Prefix does not select a single class: " + str(cls))
29     kwargs = dict([arg.split("=") for arg in args])
30     for arg in args:
31         k, v = arg.split("=")
32         kwargs[k] = v
33     resp = Resp(imei=imei, packet=cls.Out(**kwargs).packed)
34     log.debug("Response: %s", resp)
35     zpush.send(resp.packed)
36
37
38 if __name__.endswith("__main__"):
39     opts, args = getopt(argv[1:], "c:d")
40     main(common.init(log, opts=opts), opts, args)