def testSQLWriterWritesToSameFile(self): f = tempfile.NamedTemporaryFile('w', delete=False) f.close() first_listener = can.SqliteWriter(f.name) first_listener(generate_message(0x01)) sleep(first_listener.MAX_TIME_BETWEEN_WRITES) first_listener.stop() second_listener = can.SqliteWriter(f.name) second_listener(generate_message(0x02)) sleep(second_listener.MAX_TIME_BETWEEN_WRITES) second_listener.stop() con = sqlite3.connect(f.name) with con: c = con.cursor() c.execute("select COUNT() from messages") self.assertEqual(2, c.fetchone()[0]) c.execute("select * from messages") msg1 = c.fetchone() msg2 = c.fetchone() assert msg1[1] == 0x01 assert msg2[1] == 0x02
def testSQLWriterReceives(self): f = tempfile.NamedTemporaryFile('w') a_listener = can.SqliteWriter(f.name) a_listener(generate_message(0xDADADA)) a_listener.stop() import sqlite3 con = sqlite3.connect(f.name) c = con.cursor() c.execute("select * from messages") msg = c.fetchone() assert msg[1] == 0xDADADA
def testSQLWriterReceives(self): f = tempfile.NamedTemporaryFile('w', delete=False) f.close() a_listener = can.SqliteWriter(f.name) a_listener(generate_message(0xDADADA)) # Small delay so we don't stop before we actually block trying to read sleep(0.5) a_listener.stop() con = sqlite3.connect(f.name) c = con.cursor() c.execute("select * from messages") msg = c.fetchone() con.close() self.assertEqual(msg[1], 0xDADADA)
def test_sql_reader(self): f = tempfile.NamedTemporaryFile('w', delete=False) f.close() a_listener = can.SqliteWriter(f.name) a_listener(generate_message(0xDADADA)) sleep(a_listener.MAX_TIME_BETWEEN_WRITES) while not a_listener.buffer.empty(): sleep(0.1) a_listener.stop() reader = can.SqlReader(f.name) ms = [] for m in reader: ms.append(m) self.assertEqual(len(ms), 1) self.assertEqual(0xDADADA, ms[0].arbitration_id)
if doprint: printer = can.Printer() if doupload: influxwriter = InfluxWriter(hostname, database=vehicle, measurement_name=vehicle, user=user, password=password, database_file=database_file) mqttwriter = MqttWriter(hostname, vehicle=vehicle, user=user, password=password, database_file=database_file) if dolog: sqlitewriter = can.SqliteWriter(sqlitefile, table_name=vehicle) while True: message = bus.recv() if doprint: printer(message) if dolog: sqlitewriter(message) if doupload: if influxwriter.buffer_is_full(): print("influxwriter buffer full") else: influxwriter(message) if mqttwriter.buffer_is_full(): print("mqttwriter buffer is full)") else: