]> www.average.org Git - loctrkd.git/commitdiff
introduce config
authorEugene Crosser <crosser@average.org>
Tue, 15 Mar 2022 23:50:55 +0000 (00:50 +0100)
committerEugene Crosser <crosser@average.org>
Tue, 15 Mar 2022 23:50:55 +0000 (00:50 +0100)
gps303.conf [new file with mode: 0644]
gps303/__main__.py
gps303/config.py [new file with mode: 0644]

diff --git a/gps303.conf b/gps303.conf
new file mode 100644 (file)
index 0000000..ab4befa
--- /dev/null
@@ -0,0 +1,23 @@
+[daemon]
+port = 4303
+dbfn = /tmp/gps303.sqlite
+
+[device]
+uploadIntervalSeconds = 0x0300
+binarySwitch = 0b00110001
+alarms =
+ 0
+ 0
+ 0
+dndTimeSwitch = 0
+dndTimes =
+ 0
+ 0
+ 0
+gpsTimeSwitch = 0
+gpsTimeStart = 0
+gpsTimeStop = 0
+phoneNumbers =
+ ""
+ ""
+ ""
index 0f6484a01fb8d1712933b42ba0326ac7de3280d4..a3d4f07d6d1d4a702dd5cab573fa4e538510b69f 100755 (executable)
@@ -1,3 +1,4 @@
+from getopt import getopt
 from logging import getLogger, StreamHandler, DEBUG, INFO
 from logging.handlers import SysLogHandler
 from select import poll, POLLIN, POLLERR, POLLHUP, POLLPRI
@@ -5,13 +6,19 @@ from socket import socket, AF_INET, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR
 import sys
 from time import time
 
+from .config import readconfig
 from .GT06mod import handle_packet, make_response, LOGIN
 from .evstore import initdb, stow
 
-PORT = 4303
+CONF = "/etc/gps303.conf"
+
 log = getLogger("gps303")
 
 if __name__.endswith("__main__"):
+    opts, _ = getopt(sys.argv[1:], "c:p:")
+    opts = dict(opts)
+    conf = readconfig(opts["c"] if "c" in opts else CONF)
+
     if sys.stdout.isatty():
         log.addHandler(StreamHandler(sys.stderr))
         log.setLevel(DEBUG)
@@ -19,11 +26,11 @@ if __name__.endswith("__main__"):
         log.addHandler(SysLogHandler(address="/dev/log"))
         log.setLevel(INFO)
 
-    initdb("/tmp/gps303.sqlite")
+    initdb(conf.get("daemon", "port"))
 
     ctlsock = socket(AF_INET, SOCK_STREAM)
     ctlsock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
-    ctlsock.bind(("", PORT))
+    ctlsock.bind(("", conf.getint("daemon", "port")))
     ctlsock.listen(5)
     ctlfd = ctlsock.fileno()
     pollset = poll()
diff --git a/gps303/config.py b/gps303/config.py
new file mode 100644 (file)
index 0000000..6102e4a
--- /dev/null
@@ -0,0 +1,24 @@
+from configparser import ConfigParser
+
+PORT = 4303
+DBFN = "/var/lib/gps303/gps303.sqlite"
+
+def readconfig(fname):
+    config = ConfigParser()
+    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
+    return config
+
+if __name__ == "__main__":
+    from sys import argv
+    conf = readconfig(argv[1])
+    for section in conf.sections():
+        print("section", section)
+        for option in conf.options(section):
+            print("    ", option, conf[section][option])
+    print("binaryswitch", int(conf.get("device", "binaryswitch"), 0))