]> www.average.org Git - loctrkd.git/commitdiff
Use config from the config file to config
authorEugene Crosser <crosser@average.org>
Fri, 22 Apr 2022 14:46:04 +0000 (16:46 +0200)
committerEugene Crosser <crosser@average.org>
Fri, 22 Apr 2022 14:46:04 +0000 (16:46 +0200)
gps303.conf
gps303/common.py
gps303/gps303proto.py
gps303/termconfig.py

index 1a4f1813e783245cbc0ce7556b0da292a08c78a7..bb7b6c74f6e88209031e40076deff9fe858025d2 100644 (file)
@@ -9,7 +9,8 @@ dbfn = gps303.sqlite
 [opencellid]
 dbfn = opencellid.sqlite
 
-[device]
+[termconfig]
+statusIntervalMinutes = 25
 uploadIntervalSeconds = 0x0300
 binarySwitch = 0b00110001
 alarms =
index 0e7aac12e11d29f31f96b786e6386406f98d9d0c..44c301d41c591bbd785ef5b077d09a9ac8f338e3 100644 (file)
@@ -9,6 +9,7 @@ CONF = "/etc/gps303.conf"
 PORT = 4303
 DBFN = "/var/lib/gps303/gps303.sqlite"
 
+
 def init(log):
     opts, _ = getopt(argv[1:], "c:d")
     opts = dict(opts)
@@ -21,6 +22,7 @@ def init(log):
     log.info("starting with options: %s", opts)
     return conf
 
+
 def readconfig(fname):
     config = ConfigParser()
     config["collector"] = {
@@ -29,13 +31,31 @@ def readconfig(fname):
     config["storage"] = {
         "dbfn": DBFN,
     }
-    config["device"] = {}
-    #_print_config(config)
-    #print("now reading", fname)
+    config["termconfig"] = {}
     config.read(fname)
-    #_print_config(config)
     return config
 
+
+def normconf(section):
+    result = {}
+    for key, val in section.items():
+        vals = val.split("\n")
+        if len(vals) > 1 and vals[0] == "":
+            vals = vals[1:]
+        lst = []
+        for el in vals:
+            try:
+                el = int(el, 0)
+            except ValueError:
+                if el[0] == '"' and el[-1] == '"':
+                    el = el.strip('"').rstrip('"')
+            lst.append(el)
+        if len(lst) == 1:
+            [lst] = lst
+        result[key] = lst
+    return result
+
+
 if __name__ == "__main__":
     from sys import argv
 
@@ -47,4 +67,4 @@ if __name__ == "__main__":
 
     conf = readconfig(argv[1])
     _print_config(conf)
-    print("binaryswitch", int(conf.get("device", "binaryswitch"), 0))
+    print(normconf(conf["termconfig"]))
index ad8fd7168fe4951d75eda0bf19f87e33bd9229e3..46f12899e62b2d0f38cc087f774da20d6d6d1530 100755 (executable)
@@ -314,35 +314,35 @@ class SETUP(GPS303Pkt):
 
     def response(
         self,
-        uploadIntervalSeconds=0x0300,
-        binarySwitch=0b00110001,
+        uploadintervalseconds=0x0300,
+        binaryswitch=0b00110001,
         alarms=[0, 0, 0],
-        dndTimeSwitch=0,
-        dndTimes=[0, 0, 0],
-        gpsTimeSwitch=0,
-        gpsTimeStart=0,
-        gpsTimeStop=0,
-        phoneNumbers=["", "", ""],
+        dndtimeswitch=0,
+        dndtimes=[0, 0, 0],
+        gpstimeswitch=0,
+        gpstimestart=0,
+        gpstimestop=0,
+        phonenumbers=["", "", ""],
     ):
         def pack3b(x):
             return pack("!I", x)[1:]
 
         payload = b"".join(
             [
-                pack("!H", uploadIntervalSeconds),
-                pack("B", binarySwitch),
+                pack("!H", uploadintervalseconds),
+                pack("B", binaryswitch),
             ]
             + [pack3b(el) for el in alarms]
             + [
-                pack("B", dndTimeSwitch),
+                pack("B", dndtimeswitch),
             ]
-            + [pack3b(el) for el in dndTimes]
+            + [pack3b(el) for el in dndtimes]
             + [
-                pack("B", gpsTimeSwitch),
-                pack("!H", gpsTimeStart),
-                pack("!H", gpsTimeStop),
+                pack("B", gpstimeswitch),
+                pack("!H", gpstimestart),
+                pack("!H", gpstimestop),
             ]
-            + [b";".join([el.encode() for el in phoneNumbers])]
+            + [b";".join([el.encode() for el in phonenumbers])]
         )
         return self.make_packet(payload)
 
index 10c6286d544a76722a707a0cd0cdc73ad3e884ea..4bf148bb9a6a76378947d8f70efd7b63d22d6ae8 100644 (file)
@@ -6,13 +6,14 @@ from struct import pack
 import zmq
 
 from . import common
-from .gps303proto import parse_message, proto_by_name
+from .gps303proto import *
 from .zmsg import Bcast, Resp
 
 log = getLogger("gps303/termconfig")
 
 
 def runserver(conf):
+    termconfig = common.normconf(conf["termconfig"])
     zctx = zmq.Context()
     zsub = zctx.socket(zmq.SUB)
     zsub.connect(conf.get("collector", "publishurl"))
@@ -41,8 +42,28 @@ def runserver(conf):
                 datetime.fromtimestamp(zmsg.when).astimezone(tz=timezone.utc),
                 msg,
             )
-            # TODO get data from the config
-            resp = Resp(imei=zmsg.imei, packet=msg.response())
+            kwargs = {}
+            if isinstance(msg, STATUS):
+                kwargs = {
+                    "upload_interval": termconf.get(
+                        "statusintervalminutes", 25
+                    )
+                }
+            elif isinstance(msg, SETUP):
+                for key in (
+                    "uploadintervalseconds",
+                    "binaryswitch",
+                    "alarms",
+                    "dndtimeswitch",
+                    "dndtimes",
+                    "gpstimeswitch",
+                    "gpstimestart",
+                    "gpstimestop",
+                    "phonenumbers",
+                ):
+                    if key in termconfig:
+                        kwargs[key] = termconfig[key]
+            resp = Resp(imei=zmsg.imei, packet=msg.response(**kwargs))
             log.debug("Response: %s", resp)
             zpush.send(resp.packed)