def process_delete(mods, session): for item in mods: dev = DeviceMdl.get_device_with_uuid(uuid=uuid.UUID(item), session=session) if dev is None: raise ApiOperationError("Cannot find specified device", item) try: session.delete(dev) session.flush() except IntegrityError: raise ApiOperationError(f"Cannot delete item: currently in use", item)
def delete_room(uuid, session): room = RoomMdl.get_room_with_uuid(uuid=uuid, session=session) if room is None: raise ApiOperationError("Room with this uuid is not found", uuid) else: session.delete(room) session.commit()
def process_edit(mods, session): eps = list() for item in mods: ep = EndpointMdl.get_endpoint_by_uuid(uuid=uuid.UUID(item['uuid']), session=session) ep.name = item['name'] if ep.driver_uuid != uuid.UUID(item['driver']['uuid']): raise ApiOperationError("Changing item driver is not available", item['uuid']) if ep.driver_type == DriverTypeEnum.mqtt: if item['parameters']['topic_read']['value'] == "": raise ApiOperationError("Cannot subscribe on empty topics", item['uuid']) ep.mqtt_params.topic_read = item['parameters']['topic_read'][ 'value'] ep.mqtt_params.topic_write = item['parameters']['topic_write'][ 'value'] if MqttTypeMdl.get_by_name( name=item['parameters']['type']['value'], session=session) is None: raise ApiOperationError("Cannot find specified mqtt type", item['uuid']) ep.mqtt_params.type = MqttTypeMdl.get_by_name( name=item['parameters']['type']['value'], session=session) elif ep.driver_type == DriverTypeEnum.alarm: try: ep.alarm_params.severity = AlarmSeverityEnum[ item['parameters']['severity']['value']] except KeyError: raise ApiOperationError( f"Incorrect alarm severity: {item['parameters']['severity']['value']}", item['uuid']) try: ep.alarm_params.acl = int(item['parameters']['acl']['value']) except ValueError: raise ApiOperationError( f"Incorrect value for acl: {item['parameters']['acl']['value']}", item['uuid']) elif ep.driver_type == DriverTypeEnum.setpoint: ep.setpoint_params.name = item['parameters']['name']['value'] value = tryeval(item['parameters']['value']['value']) ep.setpoint_params.value = pickle.dumps(value) session.add(ep) eps.append(ep) session.flush() return eps
def add_room(name, session): room = RoomMdl.get_room_with_name(name=name, session=session) if room is not None: raise ApiOperationError("Room with that name already exists", name) else: room = RoomMdl(name=name) session.add(room) session.commit()
def process_delete(mods, session): eps = list() for item in mods: ep = EndpointMdl.get_endpoint_by_uuid(uuid=uuid.UUID(item), session=session) try: session.delete(ep) session.flush() except IntegrityError: raise ApiOperationError(f"Cannot delete item: currently in use", item) eps.append(ep) return eps
def process_edit(mods, session): with session.no_autoflush: for item in mods: dev = DeviceMdl.get_device_with_uuid(uuid=uuid.UUID(item['uuid']), session=session) if item['name'] == "": raise ApiOperationError("Name cannot be empty", item['uuid']) dev.name = item['name'] if item['exported']: if item['interface'] is None: raise ApiOperationError( "Interface parameters not specified", item['uuid']) if dev.interface is None: dev.interface = InterfaceMdl() dev.interface.read_acl = item['interface']['read_acl'] dev.interface.write_acl = item['interface']['write_acl'] else: dev.interface = None if item['type'] == "": raise ApiOperationError("Type cannot be empty", item['uuid']) try: ent = DeviceEntEnum[item['type']] if ent != dev.dev_type: dev.dev_type = DeviceEntEnum[item['type']] except KeyError: raise ApiOperationError("Incorrect device type", item['uuid']) dev.sources = list() for param in item['sources'].keys(): if item['sources'][param]['ep_param'] == "": raise ApiOperationError('Endpoint parameter not specified', item['uuid']) if item['sources'][param]['ep_uuid'] == "": raise ApiOperationError('Endpoint uuid not specified', item['uuid']) if EndpointMdl.get_endpoint_by_uuid(uuid=uuid.UUID( item['sources'][param]['ep_uuid']), session=session) is None: raise ApiOperationError( 'Endpoint with selected uuid not found', item['uuid']) link = DeviceSourceMdl() link.device_param = param link.device_uuid = dev.uuid link.endpoint_param = item['sources'][param]['ep_param'] link.endpoint_uuid = uuid.UUID( item['sources'][param]['ep_uuid']) dev.sources.append(link) session.flush()
def process_add(mods, session): eps = list() for item in mods: if item['name'] == "": raise ApiOperationError("Name cannot be empty", item['uuid']) if item['driver']['uuid'] == "": raise ApiOperationError("Driver cannot be empty", item['uuid']) if DriverInstanceMdl.get_instance_by_uuid(uuid=item['driver']['uuid'], session=session) is None: raise ApiOperationError("Cannot find specified driver", item['uuid']) ep = EndpointMdl(uuid=uuid.UUID(item['uuid']), name=item['name'], driver=DriverInstanceMdl.get_instance_by_uuid( uuid=uuid.UUID(item['driver']['uuid']), session=session)) ep.driver_type = ep.driver.driver_type if ep.driver_type == DriverTypeEnum.mqtt: ep.mqtt_params = MqttParamsMdl() if "topic_read" not in item['parameters'].keys(): raise ApiOperationError( "Missing required parameter: topic_read", item['uuid']) if item['parameters']['topic_read']['value'] == "": raise ApiOperationError("Cannot subscribe on empty topics", item['uuid']) ep.mqtt_params.topic_read = item['parameters']['topic_read'][ 'value'] if "topic_write" in item['parameters'].keys(): ep.mqtt_params.topic_write = item['parameters']['topic_write'][ 'value'] if "type" not in item['parameters'].keys(): raise ApiOperationError("Missing required parameter: type", item['uuid']) if MqttTypeMdl.get_by_name( name=item['parameters']['type']['value'], session=session) is None: raise ApiOperationError("Cannot find specified mqtt type", item['uuid']) ep.mqtt_params.type = MqttTypeMdl.get_by_name( name=item['parameters']['type']['value'], session=session) elif ep.driver_type == DriverTypeEnum.alarm: ep.alarm_params = AlarmParamsMdl(triggered=False) if "msg_on" in item['parameters'].keys(): ep.alarm_params.topic_write = item['parameters']['msg_on'][ 'value'] if "msg_off" in item['parameters'].keys(): ep.alarm_params.topic_write = item['parameters']['msg_off'][ 'value'] if "severity" not in item['parameters'].keys(): ep.alarm_params.severity = AlarmSeverityEnum.Info else: try: ep.alarm_params.severity = AlarmSeverityEnum[ item['parameters']['severity']['value']] except KeyError: raise ApiOperationError( f"Incorrect alarm severity: {item['parameters']['severity']['value']}", item['uuid']) if "acl" not in item['parameters'].keys(): ep.alarm_params.acl = 100 else: try: ep.alarm_params.acl = int( item['parameters']['acl']['value']) except ValueError: raise ApiOperationError( f"Incorrect value for acl: {item['parameters']['acl']['value']}", item['uuid']) elif ep.driver_type == DriverTypeEnum.setpoint: ep.setpoint_params = SetpointParamsMdl() if "name" not in item['parameters'].keys(): raise ApiOperationError("Missing required parameter: name", item['uuid']) ep.setpoint_params.name = item['parameters']['name']['value'] if "value" not in item['parameters'].keys(): raise ApiOperationError("Missing required parameter: value", item['uuid']) value = tryeval(item['parameters']['value']['value']) ep.setpoint_params.value = pickle.dumps(value) session.add(ep) eps.append(ep) session.flush() return eps