Skip to content

Commit 4b0957c

Browse files
committed
#52 Board Configuration and Connection persistence integrated to the MLC API
1 parent 4957bb9 commit 4b0957c

File tree

5 files changed

+169
-15
lines changed

5 files changed

+169
-15
lines changed

MLC/GUI/Experiment/ExperimentWindow.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,7 @@ def __init__(self, mlc_local,
109109
mlc_local=self._mlc_local)
110110

111111
# Arduino board configurations
112-
# FIXME Board configuration must come from the experiment DB
113-
self._board_config = ProtocolConfig(None)
114-
self._serial_conn = SerialConnectionConfig('/dev/ttyACM0')
112+
self._board_config, self._serial_conn = mlc_local.get_board_configuration(self._experiment_name)
115113

116114
def closeEvent(self, event):
117115
logger.debug('[EXPERIMENT {0}] [CLOSE_DIALOG] - Executing overriden closeEvent function'.format(self._experiment_name))
@@ -750,6 +748,11 @@ def _store_board_configuration(self, board_config, serial_conn):
750748
try:
751749
ArduinoInterfaceSingleton.get_instance(protocol_config=self._board_config,
752750
conn_setup=self._serial_conn._asdict())
751+
752+
753+
# Update board configuration in the DB
754+
self._mlc_local.save_board_configuration(self._experiment_name, self._board_config, self._serial_conn)
755+
753756
except Exception, err:
754757
logger.debug('[EXPERIMENT {0}] [BOARD_CONFIG] - '
755758
'Serial port could not be initialized. Error Msg: {1}'
@@ -764,3 +767,6 @@ def _store_board_configuration(self, board_config, serial_conn):
764767
self.on_board_config_button_clicked()
765768
else:
766769
self._board_config = self._board_config._replace(connection=BaseConnection())
770+
self._mlc_local.save_board_configuration(self._experiment_name,
771+
self._board_config,
772+
self._serial_conn)

MLC/api/MLCLocal.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
from MLC.Log.log import get_gui_logger
2727
from MLC.mlc_parameters.mlc_parameters import Config
2828
from MLC.Simulation import Simulation
29+
from MLC.arduino.protocol import ProtocolConfig
30+
from MLC.arduino.connection.serialconnection import SerialConnectionConfig
2931

3032
logger = get_gui_logger()
3133

@@ -219,6 +221,57 @@ def get_experiment_info(self, experiment_name):
219221
experiment_info["best_indiv_value"] = min_indiv_data.get_value()
220222
return experiment_info
221223

224+
def get_board_configuration(self, experiment_name):
225+
if experiment_name not in self._experiments:
226+
raise ExperimentNotExistException(experiment_name)
227+
228+
if experiment_name not in self._open_experiments:
229+
raise ClosedExperimentException("get_experiment_info", experiment_name)
230+
231+
mlc_repo = MLCRepository.get_instance()
232+
233+
boards = mlc_repo.get_board_configuration_ids()
234+
235+
if len(boards) == 0:
236+
# creates default board configuration if not exists
237+
serial_connection = SerialConnectionConfig('/dev/ttyACM0')
238+
board_configuration = ProtocolConfig(serial_connection)
239+
240+
# save default board configuration and serial connection
241+
board_id = mlc_repo.save_board_configuration(board_configuration)
242+
mlc_repo.save_serial_connection(serial_connection, board_id)
243+
244+
return board_configuration, serial_connection
245+
else:
246+
# take first board configuration
247+
board_id = boards[0]
248+
249+
# load board configuration and serial connection
250+
board_configuration = mlc_repo.load_board_configuration(board_id)
251+
serial_connection = mlc_repo.load_serial_connection(board_id)
252+
253+
return board_configuration, serial_connection
254+
255+
def save_board_configuration(self, experiment_name, board_configuration, connection):
256+
if experiment_name not in self._experiments:
257+
raise ExperimentNotExistException(experiment_name)
258+
259+
if experiment_name not in self._open_experiments:
260+
raise ClosedExperimentException("get_experiment_info", experiment_name)
261+
262+
mlc_repo = MLCRepository.get_instance()
263+
264+
boards = mlc_repo.get_board_configuration_ids()
265+
266+
if len(boards) == 0:
267+
# save board configuration and serial connection
268+
board_id = mlc_repo.save_board_configuration(board_configuration)
269+
mlc_repo.save_serial_connection(connection, board_id)
270+
else:
271+
board_id = boards[0]
272+
mlc_repo.save_board_configuration(board_configuration, board_id=board_id)
273+
mlc_repo.save_serial_connection(connection, board_id=board_id, connection_id=board_id)
274+
222275
def go(self, experiment_name, to_generation, from_generation=0,
223276
callbacks={}, gen_creator=None):
224277
if experiment_name not in self._experiments:

MLC/db/sqlite/sql_statements_board_configuration.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ def stmt_create_table_board():
1111
def stmt_create_table_serial_connection():
1212
return ''' CREATE TABLE serial_connection(id INTEGER PRIMARY KEY AUTOINCREMENT,
1313
board_id INTEGER,
14-
port INTEGER,
14+
port TEXT,
1515
baudrate INTEGER,
16-
parity INTEGER,
16+
parity TEXT,
1717
stopbits INTEGER,
1818
bytesize INTEGER,
1919
FOREIGN KEY(board_id) REFERENCES board(id))'''
@@ -118,19 +118,19 @@ def stmt_get_pwm_pins(board_id):
118118

119119
def stmt_insert_serial_connection(board_id, port, baudrate, parity, stopbits, bytesize):
120120
return '''INSERT INTO serial_connection (board_id, port, baudrate, parity, stopbits, bytesize)
121-
VALUES (%s, %s, %s, %s, %s, %s)''' % (board_id,
122-
port,
123-
baudrate,
124-
parity,
125-
stopbits,
126-
bytesize)
121+
VALUES (%s, "%s", %s, "%s", %s, %s)''' % (board_id,
122+
port,
123+
baudrate,
124+
parity,
125+
stopbits,
126+
bytesize)
127127

128128
def stmt_update_serial_connection(connection_id, board_id, port, baudrate, parity, stopbits, bytesize):
129129
return '''UPDATE serial_connection SET
130130
board_id = "%s",
131-
port = %s,
131+
port = "%s",
132132
baudrate = %s,
133-
parity = %s,
133+
parity = "%s",
134134
stopbits = %s,
135135
bytesize = %s
136136
WHERE id = %s''' % (board_id,
@@ -141,6 +141,11 @@ def stmt_update_serial_connection(connection_id, board_id, port, baudrate, parit
141141
bytesize,
142142
connection_id)
143143

144+
144145
def stmt_get_serial_connection(board_id):
145146
return '''SELECT port, baudrate, parity, stopbits, bytesize
146-
FROM serial_connection WHERE board_id = %s''' % board_id
147+
FROM serial_connection WHERE board_id = %s''' % board_id
148+
149+
150+
def stmt_get_board_configuration_ids():
151+
return '''SELECT id FROM board'''

MLC/db/sqlite/sqlite_repository.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,15 @@ def __get_pins(self, cursor, stmt_get_pins, board_id):
374374
output_pins.append(pin_id)
375375
return input_pins, output_pins
376376

377+
def get_board_configuration_ids(self):
378+
board_ids = []
379+
conn = self.__get_db_connection()
380+
cursor = conn.execute(stmt_get_board_configuration_ids())
381+
for row in cursor:
382+
board_ids.append(row[0])
383+
cursor.close()
384+
return board_ids
385+
377386
def load_board_configuration(self, board_id):
378387
protocol = None
379388
conn = self.__get_db_connection()

tests/mlc/api/test_mlc.py

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
from MLC.Population.Population import Population as MLCPopulation
1919
from MLC.Simulation import Simulation
2020

21-
21+
from MLC.arduino import boards
22+
from MLC.arduino.protocol import ProtocolConfig
23+
from MLC.arduino.connection.serialconnection import SerialConnectionConfig
2224

2325
class MLCWorkspaceTest(unittest.TestCase):
2426
WORKSPACE_DIR = os.path.abspath("/tmp/mlc_workspace/")
@@ -201,6 +203,85 @@ def test_get_info_empty_simulation(self):
201203

202204
mlc.close_experiment(MLCWorkspaceTest.ORIGINAL_EXPERIMENT)
203205

206+
def test_create_experiment_and_obtain_configuration(self):
207+
try:
208+
mlc = MLCLocal(working_dir=MLCWorkspaceTest.WORKSPACE_DIR)
209+
mlc.new_experiment(MLCWorkspaceTest.NEW_EXPERIMENT,
210+
MLCWorkspaceTest.NEW_CONFIGURATION)
211+
mlc.open_experiment(MLCWorkspaceTest.NEW_EXPERIMENT)
212+
configuration = mlc.get_experiment_configuration(
213+
MLCWorkspaceTest.NEW_EXPERIMENT)
214+
print configuration
215+
mlc.close_experiment(MLCWorkspaceTest.NEW_EXPERIMENT)
216+
217+
# check configuration structure
218+
self.assertIsInstance(configuration, dict)
219+
self.assertTrue(configuration.has_key("PARAMS"))
220+
self.assertIsInstance(configuration["PARAMS"], dict)
221+
self.assertTrue(configuration["PARAMS"].has_key("test_param"))
222+
self.assertEqual(configuration["PARAMS"]["test_param"],
223+
"test_value")
224+
225+
finally:
226+
# FIXME: use Setup/TearDown testcase
227+
mlc.delete_experiment(MLCWorkspaceTest.NEW_EXPERIMENT)
228+
229+
def test_get_default_board_configuration(self):
230+
try:
231+
mlc = MLCLocal(working_dir=MLCWorkspaceTest.WORKSPACE_DIR)
232+
mlc.new_experiment(MLCWorkspaceTest.NEW_EXPERIMENT,
233+
MLCWorkspaceTest.NEW_CONFIGURATION)
234+
mlc.open_experiment(MLCWorkspaceTest.NEW_EXPERIMENT)
235+
236+
board_configuration, serial_connection = mlc.get_board_configuration(MLCWorkspaceTest.NEW_EXPERIMENT)
237+
238+
self.assertEqual(board_configuration.board_type, boards.Due)
239+
self.assertEqual(board_configuration.read_count, 2)
240+
self.assertEqual(board_configuration.read_delay, 0)
241+
self.assertEqual(board_configuration.report_mode, 0)
242+
self.assertEqual(board_configuration.analog_resolution, 12)
243+
244+
self.assertEqual(serial_connection.port, "/dev/ttyACM0")
245+
self.assertEqual(serial_connection.baudrate, 115200)
246+
self.assertEqual(serial_connection.parity, "N")
247+
self.assertEqual(serial_connection.stopbits, 1)
248+
self.assertEqual(serial_connection.bytesize, 8)
249+
250+
finally:
251+
# FIXME: use Setup/TearDown testcase
252+
mlc.delete_experiment(MLCWorkspaceTest.NEW_EXPERIMENT)
253+
254+
def test_update_board_configuration(self):
255+
try:
256+
mlc = MLCLocal(working_dir=MLCWorkspaceTest.WORKSPACE_DIR)
257+
mlc.new_experiment(MLCWorkspaceTest.NEW_EXPERIMENT, MLCWorkspaceTest.NEW_CONFIGURATION)
258+
mlc.open_experiment(MLCWorkspaceTest.NEW_EXPERIMENT)
259+
260+
board_configuration, serial_connection = mlc.get_board_configuration(MLCWorkspaceTest.NEW_EXPERIMENT)
261+
262+
new_board_config = ProtocolConfig(None, report_mode=12, read_count=10, read_delay=11, board_type=boards.Leonardo, analog_resolution=13)
263+
new_connection = SerialConnectionConfig("/dev/ttyACM1", baudrate=5000, parity="P", stopbits=2, bytesize=6)
264+
mlc.save_board_configuration(MLCWorkspaceTest.NEW_EXPERIMENT, new_board_config, new_connection)
265+
266+
loaded_board_configuration, loaded_connection = mlc.get_board_configuration(MLCWorkspaceTest.NEW_EXPERIMENT)
267+
268+
269+
self.assertEqual(loaded_board_configuration.board_type, boards.Leonardo)
270+
self.assertEqual(loaded_board_configuration.read_count, 10)
271+
self.assertEqual(loaded_board_configuration.read_delay, 11)
272+
self.assertEqual(loaded_board_configuration.report_mode, 12)
273+
self.assertEqual(loaded_board_configuration.analog_resolution, 13)
274+
275+
self.assertEqual(loaded_connection.port, "/dev/ttyACM1")
276+
self.assertEqual(loaded_connection.baudrate, 5000)
277+
self.assertEqual(loaded_connection.parity, "P")
278+
self.assertEqual(loaded_connection.stopbits, 2)
279+
self.assertEqual(loaded_connection.bytesize, 6)
280+
281+
finally:
282+
# FIXME: use Setup/TearDown testcase
283+
mlc.delete_experiment(MLCWorkspaceTest.NEW_EXPERIMENT)
284+
204285
@nottest
205286
def test_go_and_check_simulation_info(self):
206287
try:

0 commit comments

Comments
 (0)