+from getopt import getopt
from logging import getLogger, StreamHandler, DEBUG, INFO
from logging.handlers import SysLogHandler
from select import poll, POLLIN, POLLERR, POLLHUP, POLLPRI
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)
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()
--- /dev/null
+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))