use of org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice in project open-smart-grid-platform by OSGP.
the class InvocationCounterManager method initializeWithInvocationCounterStoredOnDeviceTask.
void initializeWithInvocationCounterStoredOnDeviceTask(final DlmsDevice device, final DlmsConnectionManager connectionManager) {
try {
final Long previousKnownInvocationCounter = device.getInvocationCounter();
final Long invocationCounterFromDevice = this.getInvocationCounter(connectionManager);
if (Objects.equals(previousKnownInvocationCounter, invocationCounterFromDevice)) {
LOGGER.warn("Initializing invocationCounter of device {} with the value that was already known: {}", device.getDeviceIdentification(), previousKnownInvocationCounter);
} else {
device.setInvocationCounter(invocationCounterFromDevice);
/*
* After saving the version is incremented. To prevent optimistic locking failures if the
* device gets saved again (for instance from updateInvocationCounterForDevice, called from
* doConnectionPostProcessing in DlmsConnectionMessageProcessor) set the version on the
* device that was passed on as a parameter to the InvocationCounterManager.
*/
final DlmsDevice updatedDevice = this.deviceRepository.save(device);
device.setVersion(updatedDevice.getVersion());
LOGGER.info("Property invocationCounter of device {} initialized to the value of the invocation counter " + "stored on the device: {}{}", device.getDeviceIdentification(), device.getInvocationCounter(), previousKnownInvocationCounter == null ? "" : " (previous known value: " + previousKnownInvocationCounter + ")");
}
} catch (final FunctionalException e) {
LOGGER.warn("Something went wrong while trying to get the invocation counter", e);
}
}
use of org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice in project open-smart-grid-platform by OSGP.
the class SetEncryptionKeyExchangeOnGMeterCommandExecutor method getTransferKeyMethodParameter.
private MethodParameter getTransferKeyMethodParameter(final String mbusDeviceIdentification, final int channel, final byte[] gMeterUserKey, final MessageMetadata messageMetadata) throws ProtocolAdapterException {
final DlmsDevice mbusDevice = this.dlmsDeviceRepository.findByDeviceIdentification(mbusDeviceIdentification);
if (mbusDevice == null) {
throw new ProtocolAdapterException("Unknown M-Bus device: " + mbusDeviceIdentification);
}
final byte[] mbusDefaultKey = this.secretManagementService.getKey(messageMetadata, mbusDeviceIdentification, G_METER_MASTER);
final byte[] encryptedUserKey = this.encryptMbusUserKey(mbusDefaultKey, gMeterUserKey);
final DataObject methodParameter = DataObject.newOctetStringData(encryptedUserKey);
final MBusClientMethod method = MBusClientMethod.TRANSFER_KEY;
return new MethodParameter(method.getInterfaceClass().id(), OBIS_HASHMAP.get(channel), method.getMethodId(), methodParameter);
}
use of org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice in project open-smart-grid-platform by OSGP.
the class DeviceRequestMessageProcessor method processMessageTasks.
public void processMessageTasks(final Serializable messageObject, final MessageMetadata messageMetadata, final DlmsConnectionManager connectionManager, final DlmsDevice device) throws OsgpException {
DlmsDevice device = null;
try {
if (this.maxScheduleTimeExceeded(messageMetadata)) {
log.info("Processing message of type {} for correlation UID {} exceeded max schedule time: {} ({})", messageMetadata.getMessageType(), messageMetadata.getCorrelationUid(), messageMetadata.getMaxScheduleTime(), Instant.ofEpochMilli(messageMetadata.getMaxScheduleTime()));
this.sendErrorResponse(messageMetadata, new FunctionalException(FunctionalExceptionType.MAX_SCHEDULE_TIME_EXCEEDED, ComponentType.PROTOCOL_DLMS), messageObject);
return;
}
log.info("{} called for device: {} for organisation: {}, correlationUID: {}", messageMetadata.getMessageType(), messageMetadata.getDeviceIdentification(), messageMetadata.getOrganisationIdentification(), messageMetadata.getCorrelationUid());
final Serializable response = this.getResponse(connectionManager, device, messageObject, messageMetadata);
this.sendResponse(messageMetadata, response);
} finally {
this.doConnectionPostProcessing(device, connectionManager, messageMetadata);
}
}
use of org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice in project open-smart-grid-platform by OSGP.
the class GetGsmDiagnosticCommandExecutor method execute.
@Override
public GetGsmDiagnosticResponseDto execute(final DlmsConnectionManager conn, final DlmsDevice device, final GetGsmDiagnosticRequestDto getGsmDiagnosticQuery, final MessageMetadata messageMetadata) throws ProtocolAdapterException {
final DlmsObject dlmsObject = this.dlmsObjectConfigService.getDlmsObjectForCommunicationMethod(device, DlmsObjectType.GSM_DIAGNOSTIC);
final AttributeAddress[] addresses = this.createAttributeAddresses(dlmsObject);
final String addressesDescriptions = JdlmsObjectToStringUtil.describeAttributes(addresses);
conn.getDlmsMessageListener().setDescription("Get GsmDiagnostic, retrieve attributes: " + addressesDescriptions);
LOGGER.info("Get GsmDiagnostic, retrieve attributes: {}", addressesDescriptions);
final List<GetResult> getResultList = this.dlmsHelper.getAndCheck(conn, device, "Get GsmDiagnostic", addresses);
LOGGER.info("GetResultList: {}", describeGetResults(getResultList));
if (!getResultList.stream().allMatch(result -> result.getResultCode() == AccessResultCode.SUCCESS)) {
throw new ProtocolAdapterException("Get gsm diagnostic failed for " + device.getDeviceId());
}
return this.createGetGsmDiagnosticResponse(getResultList);
}
use of org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice in project open-smart-grid-platform by OSGP.
the class AbstractReplaceKeyCommandExecutor method replaceKeys.
protected ActionResponseDto replaceKeys(final DlmsConnectionManager conn, final DlmsDevice device, final SetKeysRequestDto setKeysRequestDto, final MessageMetadata messageMetadata) throws OsgpException {
log.info("Keys set on device :{}", device.getDeviceIdentification());
SetKeysRequestDto setDecryptedKeysRequestDto = setKeysRequestDto;
if (!setKeysRequestDto.isGeneratedKeys()) {
setDecryptedKeysRequestDto = this.decryptRsaKeys(setKeysRequestDto);
}
// if keys are generated, then they are unencrypted by the GenerateAndReplaceKeyCommandExecutor
final DlmsDevice devicePostSave = this.storeAndSendToDevice(conn, device, wrap(setDecryptedKeysRequestDto.getAuthenticationKey(), KeyId.AUTHENTICATION_KEY, SecurityKeyType.E_METER_AUTHENTICATION, setDecryptedKeysRequestDto.isGeneratedKeys()), messageMetadata);
this.storeAndSendToDevice(conn, devicePostSave, wrap(setDecryptedKeysRequestDto.getEncryptionKey(), KeyId.GLOBAL_UNICAST_ENCRYPTION_KEY, SecurityKeyType.E_METER_ENCRYPTION, setDecryptedKeysRequestDto.isGeneratedKeys()), messageMetadata);
return new ActionResponseDto(String.format("Replace keys for device %s was successful", device.getDeviceIdentification()));
}
Aggregations