protected void closeManagementOperation(KapuaId scopeId, KapuaId deviceId, KapuaId operationId, KapuaResponseMessage<?, ?> responseMessageMessage) throws KapuaException { DeviceManagementOperationQuery query = DEVICE_MANAGEMENT_OPERATION_FACTORY.newQuery(scopeId); query.setPredicate( new AndPredicateImpl( new AttributePredicateImpl<>(DeviceManagementOperationAttributes.DEVICE_ID, deviceId), new AttributePredicateImpl<>(DeviceManagementOperationAttributes.OPERATION_ID, operationId) ) ); DeviceManagementOperation deviceManagementOperation = DEVICE_MANAGEMENT_OPERATION_REGISTRY_SERVICE.query(query).getFirstItem(); if (deviceManagementOperation == null) { throw new KapuaEntityNotFoundException(DeviceManagementOperation.TYPE, operationId); } deviceManagementOperation.setStatus(responseMessageMessage.getResponseCode().isAccepted() ? OperationStatus.COMPLETED : OperationStatus.FAILED); deviceManagementOperation.setEndedOn(responseMessageMessage.getReceivedOn()); KapuaSecurityUtils.doPrivileged(() -> DEVICE_MANAGEMENT_OPERATION_REGISTRY_SERVICE.update(deviceManagementOperation)); }
/** * Creates a {@link org.eclipse.kapua.service.device.registry.event.DeviceEvent} extracting data from the given {@link KapuaRequestMessage} and {@link KapuaResponseMessage}. * <p> * This operation is performed using {@link KapuaSecurityUtils#doPrivileged(ThrowingRunnable)} since {@link org.eclipse.kapua.service.device.registry.event.DeviceEventDomain} isn't a required * permission to use any of the Device Management Services. * * @param scopeId The scopeId in which to create the {@link org.eclipse.kapua.service.device.registry.event.DeviceEvent} * @param deviceId The {@link org.eclipse.kapua.service.device.registry.Device} id for which the {@link org.eclipse.kapua.service.device.registry.event.DeviceEvent} is created * @param requestMessage The {@link KapuaRequestMessage} of the DeviceManagementService operation from which to extract data. * @param responseMessage The {@link KapuaResponseMessage} of the DeviceManagementService operation from which to extract data. * @throws KapuaException If the creation of the {@link org.eclipse.kapua.service.device.registry.event.DeviceEvent} fails for some reasons * @since 1.0.0 */ protected void createDeviceEvent(KapuaId scopeId, KapuaId deviceId, KapuaRequestMessage<?, ?> requestMessage, KapuaResponseMessage responseMessage) throws KapuaException { DeviceEventCreator deviceEventCreator = DEVICE_EVENT_FACTORY.newCreator( scopeId, deviceId, responseMessage.getReceivedOn(), requestMessage.getChannel().getAppName().getValue()); deviceEventCreator.setPosition(responseMessage.getPosition()); deviceEventCreator.setSentOn(responseMessage.getSentOn()); deviceEventCreator.setAction(requestMessage.getChannel().getMethod()); deviceEventCreator.setResponseCode(responseMessage.getResponseCode()); deviceEventCreator.setEventMessage(responseMessage.getPayload().toDisplayString()); KapuaSecurityUtils.doPrivileged(() -> DEVICE_EVENT_SERVICE.create(deviceEventCreator)); }