forked from hardbyte/python-can
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite.py
More file actions
127 lines (99 loc) · 3.65 KB
/
Copy pathsqlite.py
File metadata and controls
127 lines (99 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from can.listener import BufferedReader
from can.message import Message
import sys
import time
import threading
import sqlite3
import logging
log = logging.getLogger('can.io.sql')
if sys.version_info > (3,):
buffer = memoryview
class SqlReader:
def __init__(self, filename):
log.debug("Starting sqlreader with {}".format(filename))
conn = sqlite3.connect(filename)
self.c = conn.cursor()
@staticmethod
def create_frame_from_db_tuple(frame_data):
ts, id, is_extended, is_remote, is_error, dlc, data = frame_data
return Message(
ts, is_remote, is_extended, is_error, id, dlc, data
)
def __iter__(self):
log.debug("Iterating through messages from sql db")
for frame_data in self.c.execute("SELECT * FROM messages"):
yield SqlReader.create_frame_from_db_tuple(frame_data)
class SqliteWriter(BufferedReader):
"""Logs received CAN data to a simple SQL database.
The sqlite database may already exist, otherwise it will
be created when the first message arrives.
"""
insert_msg_template = '''
INSERT INTO messages VALUES
(?, ?, ?, ?, ?, ?, ?)
'''
GET_MESSAGE_TIMEOUT = 0.25
"""Number of seconds to wait for messages from internal queue"""
MAX_TIME_BETWEEN_WRITES = 5
"""Maximum number of seconds to wait between writes to the database"""
def __init__(self, filename):
super(SqliteWriter, self).__init__()
self.db_fn = filename
self.stop_running_event = threading.Event()
self.writer_thread = threading.Thread(target=self.db_writer_thread)
self.writer_thread.start()
def _create_db(self):
# Note you can't share sqlite3 connections between threads
# hence we setup the db here.
log.info("Creating sqlite db")
self.conn = sqlite3.connect(self.db_fn)
c = self.conn.cursor()
# create table structure
c.execute('''
CREATE TABLE IF NOT EXISTS messages
(
ts REAL,
arbitration_id INTEGER,
extended INTEGER,
remote INTEGER,
error INTEGER,
dlc INTEGER,
data BLOB
)
''')
self.conn.commit()
self.db_setup = True
def db_writer_thread(self):
num_frames = 0
last_write = time.time()
self._create_db()
while not self.stop_running_event.is_set():
messages = []
m = self.get_message(self.GET_MESSAGE_TIMEOUT)
while m is not None:
log.debug("sqlitewriter buffering message")
messages.append((
m.timestamp,
m.arbitration_id,
m.id_type,
m.is_remote_frame,
m.is_error_frame,
m.dlc,
buffer(m.data)
))
m = self.get_message(self.GET_MESSAGE_TIMEOUT)
if time.time() - last_write > self.MAX_TIME_BETWEEN_WRITES:
log.debug("Max timeout between writes reached")
break
if len(messages) > 0:
with self.conn:
log.debug("Writing %s frames to db", len(messages))
self.conn.executemany(SqliteWriter.insert_msg_template, messages)
num_frames += len(messages)
last_write = time.time()
self.conn.close()
log.info("Stopped sqlite writer after writing %s messages", num_frames)
def stop(self):
self.stop_running_event.set()
log.debug("Stopping sqlite writer")
self.writer_thread.join()