Skip to content

Commit 607dc1b

Browse files
committed
#52 protocol board configuration persistence implemented
1 parent 712c93b commit 607dc1b

File tree

4 files changed

+370
-1
lines changed

4 files changed

+370
-1
lines changed

MLC/db/mlc_repository.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,19 @@ def update_individual_cost(self, individual_id, cost, evaluation_time, generatio
103103
def remove_unused_individuals(self):
104104
raise NotImplementedError("This method must be implemented")
105105

106+
# board configuration
107+
def save_board_configuration(self, board_config, board_id=None):
108+
raise NotImplementedError("This method must be implemented")
109+
110+
def load_board_configuration(self, board_id):
111+
raise NotImplementedError("This method must be implemented")
112+
113+
def save_connection(self, serial_connection, board_id, connection_id=None):
114+
raise NotImplementedError("This method must be implemented")
115+
116+
def load_connection(self, connection_id):
117+
raise NotImplementedError("This method must be implemented")
118+
106119
@staticmethod
107120
def get_instance():
108121
# FIXME: use factories instead of this
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
def stmt_create_table_board():
2+
return ''' CREATE TABLE board(id INTEGER PRIMARY KEY AUTOINCREMENT,
3+
board_type TEXT,
4+
connection_type INTEGER,
5+
read_count INTEGER,
6+
read_delay INTEGER,
7+
report_mode INTEGER,
8+
analog_resolution INTEGER)'''
9+
10+
11+
def stmt_create_table_serial_connection():
12+
return ''' CREATE TABLE serial_connection(id INTEGER PRIMARY KEY AUTOINCREMENT,
13+
board_id INTEGER,
14+
baud_rate INTEGER,
15+
parity INTEGER,
16+
stop_bits INTEGER,
17+
byte_size INTEGER,
18+
FOREIGN KEY(board_id) REFERENCES board(id))'''
19+
20+
21+
def stmt_create_table_digital_pin():
22+
return ''' CREATE TABLE digital_pin(pin_id INTEGER,
23+
board_id INTEGER,
24+
pin_type INTEGER,
25+
PRIMARY KEY (pin_id, board_id),
26+
FOREIGN KEY(board_id) REFERENCES board(id))'''
27+
28+
29+
def stmt_create_table_analog_pin():
30+
return ''' CREATE TABLE analog_pin(pin_id INTEGER,
31+
board_id INTEGER,
32+
pin_type INTEGER,
33+
PRIMARY KEY (pin_id, board_id),
34+
FOREIGN KEY(board_id) REFERENCES board(id))'''
35+
36+
37+
def stmt_create_table_pwm_pin():
38+
return ''' CREATE TABLE pwm_pin(pin_id INTEGER,
39+
board_id INTEGER,
40+
PRIMARY KEY (pin_id, board_id),
41+
FOREIGN KEY(board_id) REFERENCES board(id))'''
42+
43+
def stmt_insert_board(board_type, connection_type, read_count, read_delay, report_mode, analog_resolution):
44+
return '''INSERT INTO board (board_type, connection_type, read_count, read_delay, report_mode, analog_resolution)
45+
VALUES ("%s", %s, %s, %s, %s, %s)''' % (board_type,
46+
connection_type,
47+
read_count,
48+
read_delay,
49+
report_mode,
50+
analog_resolution)
51+
52+
def stmt_update_board(board_id, board_type, connection_type, read_count, read_delay, report_mode, analog_resolution):
53+
return '''UPDATE board SET
54+
board_type = "%s",
55+
connection_type = %s,
56+
read_count = %s,
57+
read_delay = %s,
58+
report_mode = %s,
59+
analog_resolution = %s
60+
WHERE id = %s''' % (board_type,
61+
connection_type,
62+
read_count,
63+
read_delay,
64+
report_mode,
65+
analog_resolution,
66+
board_id)
67+
68+
69+
def stmt_get_board(board_id):
70+
return '''SELECT board_type, report_mode, read_count, read_delay, analog_resolution
71+
FROM board WHERE id = %s''' % board_id
72+
73+
74+
def stmt_delete_digital_pin(board_id):
75+
return __stmt_delete_pin("digital_pin", board_id)
76+
77+
78+
def stmt_delete_analog_pin(board_id):
79+
return __stmt_delete_pin("analog_pin", board_id)
80+
81+
82+
def stmt_delete_pwm_pin(board_id):
83+
return __stmt_delete_pin("pwm_pin", board_id)
84+
85+
86+
def __stmt_delete_pin(pin_table, board_id):
87+
return '''DELETE FROM %s WHERE board_id = %s''' % (pin_table, board_id)
88+
89+
90+
def stmt_insert_digital_pin(board_id, pin_id, pin_type):
91+
return __stmt_insert_pin("digital_pin", board_id, pin_id, pin_type)
92+
93+
94+
def stmt_insert_analog_pin(board_id, pin_id, pin_type):
95+
return __stmt_insert_pin("analog_pin", board_id, pin_id, pin_type)
96+
97+
98+
def __stmt_insert_pin(pin_table, board_id, pin_id, pin_type):
99+
return '''INSERT INTO %s (pin_id, board_id, pin_type) VALUES (%s, %s, %s)''' % (pin_table, pin_id, board_id, pin_type)
100+
101+
102+
def stmt_insert_pwm_pin(board_id, pin_id):
103+
return '''INSERT INTO pwm_pin (pin_id, board_id) VALUES (%s, %s)''' % (pin_id, board_id)
104+
105+
106+
def stmt_get_analog_pins(board_id):
107+
return "SELECT pin_id, pin_type FROM analog_pin WHERE board_id = %s" % (board_id)
108+
109+
110+
def stmt_get_digital_pins(board_id):
111+
return "SELECT pin_id, pin_type FROM digital_pin WHERE board_id = %s" % (board_id)
112+
113+
114+
def stmt_get_pwm_pins(board_id):
115+
return "SELECT pin_id FROM pwm_pin WHERE board_id = %s" % (board_id)

MLC/db/sqlite/sqlite_repository.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from MLC.Log.log import get_gui_logger
88
from MLC.Simulation import Simulation
99
from sql_statements import *
10+
from sql_statements_board_configuration import *
11+
from MLC.arduino.protocol import ProtocolConfig
12+
from MLC.arduino.boards import types
1013

1114
logger = get_gui_logger()
1215

@@ -43,8 +46,18 @@ def __init__(self, database, init_db=False):
4346

4447
def __initialize_db(self):
4548
cursor = self._conn.cursor()
49+
50+
# MLC Population tables
4651
cursor.execute(stmt_create_table_individuals())
4752
cursor.execute(stmt_create_table_population())
53+
54+
# Board configuration tables
55+
cursor.execute(stmt_create_table_board())
56+
cursor.execute(stmt_create_table_serial_connection())
57+
cursor.execute(stmt_create_table_digital_pin())
58+
cursor.execute(stmt_create_table_analog_pin())
59+
cursor.execute(stmt_create_table_pwm_pin())
60+
4861
cursor.close()
4962
self._conn.commit()
5063

@@ -239,6 +252,7 @@ def update_individual_cost(self, individual_id, cost, evaluation_time, generatio
239252
self.__execute(stmt_to_update_cost)
240253

241254
def __execute(self, statement):
255+
# print ">>> %s" % statement
242256
conn = self.__get_db_connection()
243257
cursor = conn.cursor()
244258
cursor.execute(statement)
@@ -289,3 +303,113 @@ def __load_individuals(self):
289303
cursor.close()
290304
conn.commit()
291305
return individuals
306+
307+
# board configuration
308+
def save_board_configuration(self, board_config, board_id=None):
309+
310+
conn = self.__get_db_connection()
311+
cursor = conn.cursor()
312+
313+
try:
314+
# save/update board configuration
315+
if board_id is None:
316+
cursor.execute(stmt_insert_board(board_config.board_type["SHORT_NAME"],
317+
0, # serial connection hardcoded
318+
board_config.read_count,
319+
board_config.read_delay,
320+
board_config.report_mode,
321+
board_config.analog_resolution))
322+
board_id = cursor.lastrowid
323+
else:
324+
cursor.execute(stmt_update_board(board_id,
325+
board_config.board_type["SHORT_NAME"],
326+
0,
327+
board_config.read_count,
328+
board_config.read_delay,
329+
board_config.report_mode,
330+
board_config.analog_resolution))
331+
if cursor.rowcount < 1:
332+
raise KeyError("Board %s does not exist" % board_id)
333+
334+
# if board update is successful, update board pins
335+
# delete board pin configuration
336+
cursor.execute(stmt_delete_digital_pin(board_id))
337+
cursor.execute(stmt_delete_analog_pin(board_id))
338+
cursor.execute(stmt_delete_pwm_pin(board_id))
339+
340+
# update digital pins
341+
self.__insert_pins(board_config.digital_input_pins, cursor, stmt_insert_digital_pin, board_id, 0)
342+
self.__insert_pins(board_config.digital_output_pins, cursor, stmt_insert_digital_pin, board_id, 1)
343+
344+
# update analog pins
345+
self.__insert_pins(board_config.analog_input_pins, cursor, stmt_insert_analog_pin, board_id, 0)
346+
self.__insert_pins(board_config.analog_output_pins, cursor, stmt_insert_analog_pin, board_id, 1)
347+
348+
# update pwm pins
349+
for pin_id in board_config.pwm_pins:
350+
cursor.execute(stmt_insert_pwm_pin(board_id, pin_id))
351+
352+
except Exception:
353+
raise
354+
finally:
355+
cursor.close()
356+
conn.commit()
357+
358+
return board_id
359+
360+
def __insert_pins(self, pin_list, cursor, stmt_insert_pin, board_id, pin_type):
361+
for pin_id in pin_list:
362+
cursor.execute(stmt_insert_pin(board_id, pin_id, pin_type))
363+
364+
def __get_pins(self, cursor, stmt_get_pins, board_id):
365+
input_pins = []
366+
output_pins = []
367+
368+
for row in cursor.execute(stmt_get_pins(board_id)):
369+
pin_id, pin_type = row[0], row[1]
370+
if pin_type == 0:
371+
input_pins.append(pin_id)
372+
else:
373+
output_pins.append(pin_id)
374+
return input_pins, output_pins
375+
376+
def load_board_configuration(self, board_id):
377+
protocol = None
378+
conn = self.__get_db_connection()
379+
cursor = conn.execute(stmt_get_board(board_id))
380+
381+
for row in cursor:
382+
board_type = filter(lambda x: x["SHORT_NAME"] == row[0], types)
383+
384+
protocol = ProtocolConfig(connection=None,
385+
board_type=board_type[0],
386+
report_mode=row[1],
387+
read_count=row[2],
388+
read_delay=row[3],
389+
analog_resolution=row[4])
390+
break
391+
392+
if protocol is None:
393+
raise KeyError("Board %s dows not exists" % board_id)
394+
395+
# load pins
396+
input_pins, output_pins = self.__get_pins(cursor, stmt_get_analog_pins, board_id)
397+
protocol.analog_input_pins.extend(input_pins)
398+
protocol.analog_output_pins.extend(output_pins)
399+
400+
input_pins, output_pins = self.__get_pins(cursor, stmt_get_digital_pins, board_id)
401+
protocol.digital_input_pins.extend(input_pins)
402+
protocol.digital_output_pins.extend(output_pins)
403+
404+
for row in cursor.execute(stmt_get_pwm_pins(board_id)):
405+
protocol.pwm_pins.append(row[0])
406+
407+
cursor.close()
408+
409+
return protocol
410+
411+
def save_connection(self, serial_connection, board_id, connection_id=None):
412+
raise NotImplementedError("This method must be implemented")
413+
414+
def load_connection(self, connection_id):
415+
raise NotImplementedError("This method must be implemented")

0 commit comments

Comments
 (0)