From c08595626e4e339c8d31272c0d7d6c7d52b8d069 Mon Sep 17 00:00:00 2001 From: Eugene Crosser Date: Wed, 16 Mar 2022 00:50:55 +0100 Subject: [PATCH] introduce config --- gps303.conf | 23 +++++++++++++++++++++++ gps303/__main__.py | 13 ++++++++++--- gps303/config.py | 24 ++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 gps303.conf create mode 100644 gps303/config.py diff --git a/gps303.conf b/gps303.conf new file mode 100644 index 0000000..ab4befa --- /dev/null +++ b/gps303.conf @@ -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 = + "" + "" + "" diff --git a/gps303/__main__.py b/gps303/__main__.py index 0f6484a..a3d4f07 100755 --- a/gps303/__main__.py +++ b/gps303/__main__.py @@ -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 index 0000000..6102e4a --- /dev/null +++ b/gps303/config.py @@ -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)) -- 2.43.0