Search in sources :

Example 6 with DlmsConnectionManager

use of org.opensmartgridplatform.adapter.protocol.dlms.domain.factories.DlmsConnectionManager in project open-smart-grid-platform by OSGP.

the class SetAlarmNotificationsCommandExecutor method executeForAlarmFilter.

private AccessResultCode executeForAlarmFilter(final DlmsConnectionManager conn, final AttributeAddress alarmFilterAttributeAddress, final AlarmNotificationsDto alarmNotifications, final DlmsObjectType alarmRegisterDlmsObjectType) throws ProtocolAdapterException {
    try {
        final AlarmNotificationsDto alarmNotificationsOnDevice = this.retrieveCurrentAlarmNotifications(conn, alarmFilterAttributeAddress, alarmRegisterDlmsObjectType);
        LOGGER.info("Alarm Filter on device before setting notifications: {}", alarmNotificationsOnDevice);
        final Set<AlarmTypeDto> alarmTypesForRegister = this.alarmHelperService.alarmTypesForRegister(alarmRegisterDlmsObjectType);
        final Set<AlarmNotificationDto> alarmNotificationsSet = alarmNotifications.getAlarmNotificationsSet().stream().filter(alarmNotificationDto -> alarmTypesForRegister.contains(alarmNotificationDto.getAlarmType())).collect(Collectors.toSet());
        final long alarmFilterLongValueOnDevice = this.alarmFilterLongValue(alarmNotificationsOnDevice);
        final long updatedAlarmFilterLongValue = this.calculateAlarmFilterLongValue(alarmNotificationsOnDevice, alarmNotificationsSet);
        if (alarmFilterLongValueOnDevice == updatedAlarmFilterLongValue) {
            return AccessResultCode.SUCCESS;
        }
        LOGGER.info("Modified Alarm Filter long value for device: {}", updatedAlarmFilterLongValue);
        return this.writeUpdatedAlarmNotifications(conn, updatedAlarmFilterLongValue, alarmFilterAttributeAddress);
    } catch (final IOException e) {
        throw new ConnectionException(e);
    }
}
Also used : DlmsObjectType(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.DlmsObjectType) ActionResponseDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.ActionResponseDto) GetResult(org.openmuc.jdlms.GetResult) LoggerFactory(org.slf4j.LoggerFactory) MessageMetadata(org.opensmartgridplatform.shared.infra.jms.MessageMetadata) Autowired(org.springframework.beans.factory.annotation.Autowired) TreeSet(java.util.TreeSet) DlmsObjectConfigService(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.DlmsObjectConfigService) AttributeAddress(org.openmuc.jdlms.AttributeAddress) ProtocolAdapterException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException) AlarmTypeDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.AlarmTypeDto) SetAlarmNotificationsRequestDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.SetAlarmNotificationsRequestDto) AccessResultCode(org.openmuc.jdlms.AccessResultCode) DlmsConnectionManager(org.opensmartgridplatform.adapter.protocol.dlms.domain.factories.DlmsConnectionManager) Logger(org.slf4j.Logger) ConnectionException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException) SetParameter(org.openmuc.jdlms.SetParameter) AbstractCommandExecutor(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.AbstractCommandExecutor) Set(java.util.Set) DataObject(org.openmuc.jdlms.datatypes.DataObject) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ActionRequestDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.ActionRequestDto) Component(org.springframework.stereotype.Component) AlarmNotificationsDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.AlarmNotificationsDto) Optional(java.util.Optional) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) AlarmNotificationDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.AlarmNotificationDto) BitSet(java.util.BitSet) JdlmsObjectToStringUtil(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.utils.JdlmsObjectToStringUtil) AlarmNotificationDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.AlarmNotificationDto) AlarmTypeDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.AlarmTypeDto) IOException(java.io.IOException) AlarmNotificationsDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.AlarmNotificationsDto) ConnectionException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException)

Example 7 with DlmsConnectionManager

use of org.opensmartgridplatform.adapter.protocol.dlms.domain.factories.DlmsConnectionManager in project open-smart-grid-platform by OSGP.

the class OsgpResponseMessageProcessor method processMessage.

@Override
public void processMessage(final ObjectMessage message) throws JMSException {
    LOGGER.debug("Processing {} request message", this.messageType);
    final MessageMetadata messageMetadata = MessageMetadata.fromMessage(message);
    final Serializable messageObject = message.getObject();
    final ThrowingConsumer<DlmsConnectionManager> taskForConnectionManager = conn -> this.processMessageTask(messageObject, messageMetadata, conn);
    try {
        if (this.usesDeviceConnection()) {
            this.createAndHandleConnectionForDevice(this.domainHelperService.findDlmsDevice(messageMetadata), messageMetadata, taskForConnectionManager);
        } else {
            this.processMessageTask(messageObject, messageMetadata, null);
        }
    } catch (final OsgpException e) {
        LOGGER.error("Something went wrong with the DlmsConnection", e);
    }
}
Also used : DomainHelperService(org.opensmartgridplatform.adapter.protocol.dlms.application.services.DomainHelperService) MessageProcessorMap(org.opensmartgridplatform.shared.infra.jms.MessageProcessorMap) MessageProcessor(org.opensmartgridplatform.shared.infra.jms.MessageProcessor) DlmsConnectionManager(org.opensmartgridplatform.adapter.protocol.dlms.domain.factories.DlmsConnectionManager) Logger(org.slf4j.Logger) ResponseMessageResultType(org.opensmartgridplatform.shared.infra.jms.ResponseMessageResultType) OsgpException(org.opensmartgridplatform.shared.exceptionhandling.OsgpException) LoggerFactory(org.slf4j.LoggerFactory) MessageMetadata(org.opensmartgridplatform.shared.infra.jms.MessageMetadata) Autowired(org.springframework.beans.factory.annotation.Autowired) ObjectMessage(javax.jms.ObjectMessage) MessageType(org.opensmartgridplatform.shared.infra.jms.MessageType) JMSException(javax.jms.JMSException) Serializable(java.io.Serializable) PostConstruct(javax.annotation.PostConstruct) DeviceResponseMessageSender(org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.DeviceResponseMessageSender) Qualifier(org.springframework.beans.factory.annotation.Qualifier) ThrowingConsumer(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ThrowingConsumer) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) DlmsConnectionMessageProcessor(org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.DlmsConnectionMessageProcessor) Message(javax.jms.Message) SilentException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.SilentException) OsgpException(org.opensmartgridplatform.shared.exceptionhandling.OsgpException) MessageMetadata(org.opensmartgridplatform.shared.infra.jms.MessageMetadata) Serializable(java.io.Serializable) DlmsConnectionManager(org.opensmartgridplatform.adapter.protocol.dlms.domain.factories.DlmsConnectionManager)

Example 8 with DlmsConnectionManager

use of org.opensmartgridplatform.adapter.protocol.dlms.domain.factories.DlmsConnectionManager in project open-smart-grid-platform by OSGP.

the class DlmsHelperTest method assertGetWithListException.

private void assertGetWithListException(final Class<? extends Exception> jdlmsExceptionClazz, final Class<? extends Exception> exceptionClazz) throws IOException {
    final DlmsConnection dlmsConnection = mock(DlmsConnection.class);
    final DlmsConnectionManager connectionManager = mock(DlmsConnectionManager.class);
    final DlmsDevice dlmsDevice = mock(DlmsDevice.class);
    when(dlmsDevice.getDeviceIdentification()).thenReturn("666");
    when(connectionManager.getConnection()).thenReturn(dlmsConnection);
    final AttributeAddress[] attrAddresses = new AttributeAddress[1];
    attrAddresses[0] = mock(AttributeAddress.class);
    when(dlmsDevice.isWithListSupported()).thenReturn(true);
    when(dlmsConnection.get(Arrays.asList(attrAddresses))).thenThrow(jdlmsExceptionClazz);
    final Exception exception = assertThrows(exceptionClazz, () -> {
        this.dlmsHelper.getWithList(connectionManager, dlmsDevice, attrAddresses);
    });
    assertThat(exception.getMessage()).contains(dlmsDevice.getDeviceIdentification());
}
Also used : AttributeAddress(org.openmuc.jdlms.AttributeAddress) DlmsConnectionManager(org.opensmartgridplatform.adapter.protocol.dlms.domain.factories.DlmsConnectionManager) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) DlmsConnection(org.openmuc.jdlms.DlmsConnection) ConnectionException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException) IOException(java.io.IOException) ProtocolAdapterException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException)

Example 9 with DlmsConnectionManager

use of org.opensmartgridplatform.adapter.protocol.dlms.domain.factories.DlmsConnectionManager in project open-smart-grid-platform by OSGP.

the class DeviceRequestMessageProcessor method processMessage.

@Override
public void processMessage(final ObjectMessage message) throws JMSException {
    log.debug("Processing {} request message", this.messageType);
    final MessageMetadata messageMetadata = MessageMetadata.fromMessage(message);
    final Serializable messageObject = message.getObject();
    try {
        final DlmsDevice device;
        if (this.requiresExistingDevice()) {
            device = this.domainHelperService.findDlmsDevice(messageMetadata);
        } else {
            device = null;
        }
        if (this.usesDeviceConnection()) {
            /*
         * Set up a consumer to be called back with a DlmsConnectionManager for which the connection
         * with the device has been created. Note that when usesDeviceConnection is true, in this
         * way all logic in processMessageTasks is executed only after the connection to the device
         * has successfully been established.
         */
            final ThrowingConsumer<DlmsConnectionManager> taskForConnectionManager = connectionManager -> this.processMessageTasks(messageObject, messageMetadata, connectionManager, device);
            this.createAndHandleConnectionForDevice(device, messageMetadata, taskForConnectionManager);
        } else {
            this.processMessageTasks(messageObject, messageMetadata, null, device);
        }
    } catch (final ThrottlingPermitDeniedException exception) {
        /*
       * Throttling permit for network access not granted, send the request back to the queue to be
       * picked up again a little later by the message listener for device requests.
       */
        this.deviceRequestMessageSender.send(messageObject, messageMetadata, this.throttlingClientConfig.permitRejectedDelay());
    } catch (final DeviceKeyProcessAlreadyRunningException exception) {
        this.deviceRequestMessageSender.send(messageObject, messageMetadata, this.deviceKeyProcessingTimeout);
    } catch (final Exception exception) {
        this.sendErrorResponse(messageMetadata, exception, messageObject);
    }
}
Also used : MessageProcessor(org.opensmartgridplatform.shared.infra.jms.MessageProcessor) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) ResponseMessageResultType(org.opensmartgridplatform.shared.infra.jms.ResponseMessageResultType) DeviceKeyProcessAlreadyRunningException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.DeviceKeyProcessAlreadyRunningException) MessageMetadata(org.opensmartgridplatform.shared.infra.jms.MessageMetadata) Autowired(org.springframework.beans.factory.annotation.Autowired) ObjectMessage(javax.jms.ObjectMessage) FunctionalExceptionType(org.opensmartgridplatform.shared.exceptionhandling.FunctionalExceptionType) MessageType(org.opensmartgridplatform.shared.infra.jms.MessageType) ThrottlingPermitDeniedException(org.opensmartgridplatform.throttling.ThrottlingPermitDeniedException) Value(org.springframework.beans.factory.annotation.Value) Duration(java.time.Duration) Qualifier(org.springframework.beans.factory.annotation.Qualifier) ThrowingConsumer(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ThrowingConsumer) ComponentType(org.opensmartgridplatform.shared.exceptionhandling.ComponentType) DomainHelperService(org.opensmartgridplatform.adapter.protocol.dlms.application.services.DomainHelperService) MessageProcessorMap(org.opensmartgridplatform.shared.infra.jms.MessageProcessorMap) DlmsConnectionManager(org.opensmartgridplatform.adapter.protocol.dlms.domain.factories.DlmsConnectionManager) OsgpException(org.opensmartgridplatform.shared.exceptionhandling.OsgpException) Instant(java.time.Instant) JMSException(javax.jms.JMSException) Serializable(java.io.Serializable) Slf4j(lombok.extern.slf4j.Slf4j) PostConstruct(javax.annotation.PostConstruct) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) SilentException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.SilentException) DeviceKeyProcessAlreadyRunningException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.DeviceKeyProcessAlreadyRunningException) MessageMetadata(org.opensmartgridplatform.shared.infra.jms.MessageMetadata) Serializable(java.io.Serializable) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) DlmsConnectionManager(org.opensmartgridplatform.adapter.protocol.dlms.domain.factories.DlmsConnectionManager) ThrottlingPermitDeniedException(org.opensmartgridplatform.throttling.ThrottlingPermitDeniedException) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) DeviceKeyProcessAlreadyRunningException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.DeviceKeyProcessAlreadyRunningException) ThrottlingPermitDeniedException(org.opensmartgridplatform.throttling.ThrottlingPermitDeniedException) OsgpException(org.opensmartgridplatform.shared.exceptionhandling.OsgpException) JMSException(javax.jms.JMSException) SilentException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.SilentException)

Aggregations

DlmsConnectionManager (org.opensmartgridplatform.adapter.protocol.dlms.domain.factories.DlmsConnectionManager)9 DlmsDevice (org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice)8 AttributeAddress (org.openmuc.jdlms.AttributeAddress)5 MessageMetadata (org.opensmartgridplatform.shared.infra.jms.MessageMetadata)5 Autowired (org.springframework.beans.factory.annotation.Autowired)5 ProtocolAdapterException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 Serializable (java.io.Serializable)3 JMSException (javax.jms.JMSException)3 ObjectMessage (javax.jms.ObjectMessage)3 DlmsConnection (org.openmuc.jdlms.DlmsConnection)3 SilentException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.SilentException)3 ThrowingConsumer (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ThrowingConsumer)3 IOException (java.io.IOException)2 Collectors (java.util.stream.Collectors)2 PostConstruct (javax.annotation.PostConstruct)2 Test (org.junit.jupiter.api.Test)2 AccessResultCode (org.openmuc.jdlms.AccessResultCode)2 GetResult (org.openmuc.jdlms.GetResult)2