class _GT06pkt:
PROTO: int
+ CONFIG = None
def __init__(self, *args, **kwargs):
assert len(args) == 0
def make_response(msg):
return msg.response()
+
+def set_config(config): # Note that we are setting _class_ attribute
+ _GT06pkt.CONFIG = config
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"
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)
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))
def initdb(dbname):
global DB
+ log.info("Using Sqlite3 database \"%s\"", dbname)
DB = connect(dbname)
DB.execute(SCHEMA)
(str(clntaddr), timestamp, imei, proto, payload),
)
)
- log.debug("inserting %s", parms)
DB.execute(
"""insert or ignore into events
(timestamp, imei, clntaddr, proto, payload)