]> www.average.org Git - loctrkd.git/blob - gps303/evstore.py
Use command line args and config more
[loctrkd.git] / gps303 / evstore.py
1 from logging import getLogger
2 from sqlite3 import connect
3
4 __all__ = ("initdb", "stow")
5
6 log = getLogger("gps303")
7
8 DB = None
9
10 SCHEMA = """create table if not exists events (
11     timestamp real not null,
12     imei text,
13     clntaddr text not null,
14     proto int not null,
15     payload blob
16 )"""
17
18
19 def initdb(dbname):
20     global DB
21     log.info("Using Sqlite3 database \"%s\"", dbname)
22     DB = connect(dbname)
23     DB.execute(SCHEMA)
24
25
26 def stow(clntaddr, timestamp, imei, proto, payload):
27     assert DB is not None
28     parms = dict(
29         zip(
30             ("clntaddr", "timestamp", "imei", "proto", "payload"),
31             (str(clntaddr), timestamp, imei, proto, payload),
32         )
33     )
34     DB.execute(
35         """insert or ignore into events
36                 (timestamp, imei, clntaddr, proto, payload)
37                 values (:timestamp, :imei, :clntaddr, :proto, :payload)""",
38         parms,
39     )
40     DB.commit()