def get_data(self, config: dict) -> Response: """ Retrieves data in supported format (see `details <https://github.com/SmartBioTech/DeviceControl/wiki/End-Points#get-data>`__). :param config: A dictionary with pre-defined keys :return: Response object """ try: validate_attributes(['device_id', 'type'], config, 'GetData') device_id = config.get('device_id') data_type = config.get('type') # (events/values) log_id = config.get('log_id', None) time = config.get('time', None) time = time_from_string(time) return Response( True, self.dataManager.get_data(log_id, time, device_id, data_type), None) except (IdError, AttributeError, SyntaxError) as e: Log.error(e) return Response(False, None, e)
def command(self, config: dict) -> Response: """ Run a specific command (see `details <https://github.com/SmartBioTech/DeviceControl/wiki/End-Points#command>`__). :param config: A dictionary with pre-defined keys :return: Response object """ try: validate_attributes(['device_id', 'command_id'], config, 'Command') device_id = config.get('device_id') command_id = config.get('command_id') args = config.get('arguments', '[]') source = config.get('source', 'external') await_result = config.get('await', False) cmd = self._create_command(device_id, command_id, args, source) if await_result: self.deviceManager.get_device(device_id).post_command(cmd) cmd.await_cmd() return Response(True, cmd.response, None) else: self.deviceManager.get_device(device_id).post_manual_command( cmd) return Response(True, None, None) except AttributeError as e: Log.error(e) return Response(False, None, e)
def test_get_latest_data(self): config = {'device_id': 23, 'type': 'values'} data = {'some random data': 123} self.AM.dataManager.get_latest_data = mock.Mock(return_value=data) # correct behaviour result = Response(True, data, None) self.assertEqual(self.AM.get_latest_data(config), result) # exception in progress e = AttributeError("Missing a key attribute.") result = Response(False, None, e) self.AM.dataManager.get_latest_data = mock.Mock(side_effect=e) self.assertEqual(self.AM.get_latest_data(config), result)
def test_end_task(self): task_id = 23 self.AM.taskManager.remove_task = mock.Mock() self.AM.dataManager.event_task_end = mock.Mock() # correct behaviour result = Response(True, None, None) self.assertEqual(self.AM.end_task(task_id), result) # exception in progress e = IdError("Task with requested ID: 23 was not found") result = Response(False, None, e) self.AM.taskManager.remove_task = mock.Mock(side_effect=KeyError) self.assertEqual(self.AM.end_task(task_id), result)
def test_end_device(self): device_id = 23 self.AM.deviceManager.remove_device = mock.Mock() self.AM.dataManager.update_experiment = mock.Mock() self.AM.dataManager.event_device_end = mock.Mock() # correct behaviour result = Response(True, None, None) self.assertEqual(self.AM.end_device(device_id), result) # exception in progress e = IdError('Connector with given ID: %s was not found' % device_id) self.AM.deviceManager.remove_device = mock.Mock(side_effect=KeyError) result = Response(False, None, e) self.assertEqual(self.AM.end_device(device_id), result)
def end_task(self, task_id) -> Response: """ Terminate an existing task. :param task_id: ID of an existing task :return: Response object """ try: device_id = self.taskManager.remove_task(task_id) self.dataManager.event_task_end(device_id, task_id) return Response(True, None, None) except KeyError: exc = IdError('Task with requested ID: %s was not found' % task_id) Log.error(exc) return Response(False, None, exc)
def get_latest_data(self, config) -> Response: """ Retrieves the last data entry for specified Device ID and Data Type. :param config: A dictionary which specifies the "device_id": string and "type": string :return: Response object """ try: validate_attributes(['device_id', 'type'], config, 'GetData') device_id = config.get('device_id') data_type = config.get('type') # (events/values) return Response( True, self.dataManager.get_latest_data(device_id, data_type), None) except (IdError, AttributeError) as e: Log.error(e) return Response(False, None, e)
def register_task(self, config: dict) -> Response: """ Register a new task (see `details <https://github.com/SmartBioTech/DeviceControl/wiki/End-Points#task-initiation>`__). :param config: A dictionary with pre-defined keys :return: Response object """ try: validate_attributes(['task_id', 'task_class', 'task_type'], config, 'Task') task = self.taskManager.create_task(config) task.start() self.dataManager.event_task_start(config) return Response(True, None, None) except (IdError, TypeError, AttributeError) as e: Log.error(e) return Response(False, None, e)
def register_device(self, config: dict) -> Response: """ Register a new device (see `details <https://github.com/SmartBioTech/DeviceControl/wiki/End-Points#device-initiation>`__). :param config: A dictionary with pre-defined keys :return: Response object """ try: validate_attributes( ['device_id', 'device_class', 'device_type', 'address'], config, 'Connector') device = self.deviceManager.new_device(config) self.dataManager.save_device(device) self.dataManager.event_device_start(config) except (IdError, ModuleNotFoundError, AttributeError, KeyError) as e: Log.error(e) return Response(False, None, e) return Response(device is not None, None, None)
def end(self) -> Response: """ Ends the application (see `details <https://github.com/SmartBioTech/DeviceControl/wiki/End-Points#end>`__). :return: Response object """ self.taskManager.end() self.deviceManager.end() return Response(True, None, None)
def test_register_task(self): config = { 'task_id': 23, 'task_class': 'PSI', 'task_type': 'PBR_measure_all' } task = mock.Mock() task.start = mock.Mock() self.AM.taskManager.create_task = mock.Mock(return_value=task) self.AM.dataManager.event_task_start = mock.Mock() # correct behaviour result = Response(True, None, None) self.assertEqual(self.AM.register_task(config), result) # exception in progress e = AttributeError("Task missing attribute") result = Response(False, None, e) self.AM.taskManager.create_task = mock.Mock(side_effect=e) self.assertEqual(self.AM.register_task(config), result)
def end_device(self, device_id: str) -> Response: """ Terminates an existing device. :param device_id: ID of an existing device :return: Response object """ try: self.deviceManager.remove_device(device_id) self.dataManager.event_device_end(device_id) # TEMPORAL HACK !!! self.dataManager.update_experiment(device_id) return Response(True, None, None) except KeyError: exc = IdError('Connector with given ID: %s was not found' % device_id) Log.error(exc) return Response(False, None, exc)
def ping(self) -> Response: """ Get status information about the running devices and tasks (see `details <https://github.com/SmartBioTech/DeviceControl/wiki/End-Points#ping>`__). :return: Response object """ return Response(True, { 'devices': self.deviceManager.ping(), 'tasks': self.taskManager.ping() }, None)
def test_command(self): config = {'device_id': 23, 'command_id': '3', 'arguments': '[30]'} result = Command(23, '3', [30], 'external') result.save_command_to_db = mock.Mock() device = mock.Mock() device.post_command = mock.Mock() self.AM.deviceManager.get_device = mock.Mock(return_value=device) # correct behaviour result = Response(True, None, None) command = mock.Mock() command.save_command_to_db = mock.Mock() self.AM._create_command = mock.Mock(return_value=device) self.assertEqual(self.AM.command(config), result) # exception in progress e = AttributeError("Command missing attribute") result = Response(False, None, e) self.AM.deviceManager.get_device = mock.Mock(side_effect=e) self.assertEqual(self.AM.command(config), result)
def test_ping(self): device_data = {'device_data': 'data01'} task_data = {'task_data': 'data02'} self.AM.deviceManager.ping = mock.Mock(return_value=device_data) self.AM.taskManager.ping = mock.Mock(return_value=task_data) # correct behaviour result = Response(True, { 'devices': device_data, 'tasks': task_data }, None) self.assertEqual(self.AM.ping(), result)
def test_register_device(self): config = { 'device_id': 15, 'device_class': 'name', 'device_type': 'car', 'address': 'home' } device = mock.Mock() self.AM.deviceManager.new_device = mock.Mock(return_value=device) self.AM.dataManager.save_device = mock.Mock() self.AM.dataManager.event_device_start = mock.Mock() # correct behaviour result = Response(True, None, None) self.assertEqual(self.AM.register_device(config), result) # exception in progress e = AttributeError("Device missing attribute") result = Response(False, None, e) self.AM.deviceManager.new_device = mock.Mock(side_effect=e) self.assertEqual(self.AM.register_device(config), result)