]> www.average.org Git - loctrkd.git/commitdiff
Use command line args and config more
authorEugene Crosser <crosser@average.org>
Wed, 16 Mar 2022 13:36:13 +0000 (14:36 +0100)
committerEugene Crosser <crosser@average.org>
Wed, 16 Mar 2022 13:36:13 +0000 (14:36 +0100)
gps303/GT06mod.py
gps303/__main__.py
gps303/config.py
gps303/evstore.py

index 31be5d3bc5bbe354a4f2ff5b94b4f88c7d2c9f93..3256d6ca5ae4f9e61fde001c3b3e8891b1e8fc74 100755 (executable)
@@ -15,6 +15,7 @@ log = getLogger("gps303")
 
 class _GT06pkt:
     PROTO: int
+    CONFIG = None
 
     def __init__(self, *args, **kwargs):
         assert len(args) == 0
@@ -263,3 +264,6 @@ def handle_packet(packet, addr, when):
 
 def make_response(msg):
     return msg.response()
+
+def set_config(config):  # Note that we are setting _class_ attribute
+    _GT06pkt.CONFIG = config
index c8c5a28c4a265096bc12f3aee7056355c5299a05..ef03c61eff92d9ef31dcdefba8f6920025dcb696 100755 (executable)
@@ -7,7 +7,7 @@ import sys
 from time import time
 
 from .config import readconfig
-from .GT06mod import handle_packet, make_response, LOGIN
+from .GT06mod import handle_packet, make_response, LOGIN, set_config
 from .evstore import initdb, stow
 
 CONF = "/etc/gps303.conf"
@@ -15,18 +15,19 @@ CONF = "/etc/gps303.conf"
 log = getLogger("gps303")
 
 if __name__.endswith("__main__"):
-    opts, _ = getopt(sys.argv[1:], "c:p:")
+    opts, _ = getopt(sys.argv[1:], "c:d")
     opts = dict(opts)
-    conf = readconfig(opts["c"] if "c" in opts else CONF)
+    conf = readconfig(opts["-c"] if "-c" in opts else CONF)
 
     if sys.stdout.isatty():
         log.addHandler(StreamHandler(sys.stderr))
-        log.setLevel(DEBUG)
     else:
         log.addHandler(SysLogHandler(address="/dev/log"))
-        log.setLevel(INFO)
+    log.setLevel(DEBUG if "-d" in opts else INFO)
+    log.info("starting with options: %s", opts)
 
     initdb(conf.get("daemon", "dbfn"))
+    set_config(conf)
 
     ctlsock = socket(AF_INET, SOCK_STREAM)
     ctlsock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
index 6102e4ab8c5f5fa6f3ef74f129f432696bd9f958..fe4f696faa201474ea03de47a80b8c46ab42c2fe 100644 (file)
@@ -5,20 +5,25 @@ DBFN = "/var/lib/gps303/gps303.sqlite"
 
 def readconfig(fname):
     config = ConfigParser()
+    config["daemon"] = {
+        "port": PORT,
+        "dbfn": DBFN,
+    }
+    config["device"] = {}
+    #_print_config(config)
+    #print("now reading", fname)
     config.read(fname)
-    if not config.has_section("daemon"):
-        config.add_section("daemon")
-    if not config.has_option("daemon", "port"):
-        config["daemon"]["port"] = str(PORT)
-    if not config.has_option("daemon", "dbfn"):
-        config["daemon"]["dbfn"] = DBFN
+    #_print_config(config)
     return config
 
-if __name__ == "__main__":
-    from sys import argv
-    conf = readconfig(argv[1])
+def _print_config(conf):
     for section in conf.sections():
         print("section", section)
         for option in conf.options(section):
             print("    ", option, conf[section][option])
+
+if __name__ == "__main__":
+    from sys import argv
+    conf = readconfig(argv[1])
+    _print_config(conf)
     print("binaryswitch", int(conf.get("device", "binaryswitch"), 0))
index 1b1ae2dc398f29063ffcddf2716da7b2424de01c..360ab359505c7a141cdf6a5857d7dbcc14c86b53 100644 (file)
@@ -18,6 +18,7 @@ SCHEMA = """create table if not exists events (
 
 def initdb(dbname):
     global DB
+    log.info("Using Sqlite3 database \"%s\"", dbname)
     DB = connect(dbname)
     DB.execute(SCHEMA)
 
@@ -30,7 +31,6 @@ def stow(clntaddr, timestamp, imei, proto, payload):
             (str(clntaddr), timestamp, imei, proto, payload),
         )
     )
-    log.debug("inserting %s", parms)
     DB.execute(
         """insert or ignore into events
                 (timestamp, imei, clntaddr, proto, payload)