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