def end_task(self, task_id) -> Response: try: self.taskManager.remove_task(task_id) return Response(True, None) except IdError as e: Log.error(e) return Response(False, None, e)
def register_task(self, config) -> Response: try: validate_attributes(['task_id', 'task_class', 'task_type'], config, 'Task') task = self.taskManager.create_task(config) task.start() return Response(True, None) except (IdError, TypeError, AttributeError) as e: Log.error(e) return Response(False, None, e)
def register_device(self, config: dict) -> Response: try: validate_attributes( ['device_id', 'device_class', 'device_type', 'address'], config, 'Device') device = self.deviceManager.new_device(config) self.dataManager.save_device(device) except (IdError, ModuleNotFoundError, AttributeError) as e: Log.error(e) return Response(False, None, e) return Response(device is not None, None)
def get_latest_data(self, config) -> Response: 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)) except (IdError, AttributeError) as e: Log.error(e) return Response(False, None, e)
def end_device(self, device_id: str) -> Response: try: self.deviceManager.remove_device(device_id) # TEMPORAL HACK !!! self.dataManager.update_experiment(device_id) return Response(True, None) except AttributeError: exc = IdError('Connector with given ID: %s was not found' % device_id) Log.error(exc) return Response(False, None, exc)
def command(self, config) -> Response: 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') priority = 1 if config.get('priority', False) else 2 cmd = Command(device_id, command_id, eval(args), source) self.deviceManager.get_device(device_id).post_command( cmd, priority=priority) cmd.save_command_to_db() return Response(True, None) except AttributeError as e: Log.error(e) return Response(False, None, e)
def get_data(self, config) -> Response: 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 = process_time(time) return Response( True, self.dataManager.get_data(log_id, time, device_id, data_type)) except (IdError, AttributeError, SyntaxError) as e: Log.error(e) return Response(False, None, e)
def end(): data = request.get_json() _type = data.get("type", None) target_id = data.get("target_id", None) if _type == "device": response = self.app_manager.end_device(target_id) elif _type == "task": response = self.app_manager.end_task(target_id) elif _type == "all": response = self.app_manager.end() else: e = TypeError("Invalid type to end: {}".format( _type if _type is not None else "not given")) response = Response(False, None, e) return response.to_json()
def ping(self) -> Response: return Response(True, { 'devices': self.deviceManager.ping(), 'tasks': self.taskManager.ping() })
def end(self) -> Response: self.deviceManager.end() self.taskManager.end() return Response(True, None)