Search in sources :

Example 1 with MethodParameter

use of org.openmuc.jdlms.MethodParameter in project open-smart-grid-platform by OSGP.

the class SetActivityCalendarCommandActivationExecutor method execute.

@Override
public MethodResultCode execute(final DlmsConnectionManager conn, final DlmsDevice device, final Void v, final MessageMetadata messageMetadata) throws ProtocolAdapterException {
    LOGGER.info("ACTIVATING PASSIVE CALENDAR");
    final MethodParameter method = new MethodParameter(CLASS_ID, OBIS_CODE, METHOD_ID_ACTIVATE_PASSIVE_CALENDAR, DataObject.newInteger32Data(0));
    conn.getDlmsMessageListener().setDescription("SetActivityCalendarActivation, call method: " + JdlmsObjectToStringUtil.describeMethod(method));
    final MethodResult methodResultCode;
    try {
        methodResultCode = conn.getConnection().action(method);
    } catch (final IOException e) {
        throw new ConnectionException(e);
    }
    if (!MethodResultCode.SUCCESS.equals(methodResultCode.getResultCode())) {
        throw new ProtocolAdapterException("Activating the activity calendar failed. MethodResult is: " + methodResultCode.getResultCode() + " ClassId: " + CLASS_ID + " obisCode: " + OBIS_CODE + " method id: " + METHOD_ID_ACTIVATE_PASSIVE_CALENDAR);
    }
    return MethodResultCode.SUCCESS;
}
Also used : IOException(java.io.IOException) ProtocolAdapterException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException) MethodParameter(org.openmuc.jdlms.MethodParameter) ConnectionException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException) MethodResult(org.openmuc.jdlms.MethodResult)

Example 2 with MethodParameter

use of org.openmuc.jdlms.MethodParameter in project open-smart-grid-platform by OSGP.

the class ClearMBusStatusOnAllChannelsCommandExecutorTest method assertMethodResetAlarm.

private void assertMethodResetAlarm(final List<MethodParameter> allMethods) {
    for (int i = 1; i <= 4; i++) {
        final MethodParameter methodParameter = allMethods.get(i - 1);
        assertThat(((Number) methodParameter.getParameter().getValue()).longValue()).isEqualTo(0L);
        assertThat(methodParameter.getInstanceId().asDecimalString()).isEqualTo(OBIS_CODE_TEMPLATE_MBUS_CLIENT_SETUP.replaceAll("<c>", String.valueOf(i)));
        assertThat(methodParameter.getClassId()).isEqualTo(CLASS_ID_MBUS_CLIENT);
    }
}
Also used : MethodParameter(org.openmuc.jdlms.MethodParameter)

Example 3 with MethodParameter

use of org.openmuc.jdlms.MethodParameter in project open-smart-grid-platform by OSGP.

the class SetEncryptionKeyExchangeOnGMeterCommandExecutor method transferKey.

private MethodResult transferKey(final DlmsConnectionManager conn, final String mbusDeviceIdentification, final int channel, final byte[] encryptionKey, final MessageMetadata messageMetadata) throws ProtocolAdapterException, IOException {
    final MethodParameter methodTransferKey = this.getTransferKeyMethodParameter(mbusDeviceIdentification, channel, encryptionKey, messageMetadata);
    conn.getDlmsMessageListener().setDescription("SetEncryptionKeyExchangeOnGMeter for channel " + channel + ", call M-Bus Setup transfer_key method: " + JdlmsObjectToStringUtil.describeMethod(methodTransferKey));
    return conn.getConnection().action(methodTransferKey);
}
Also used : MethodParameter(org.openmuc.jdlms.MethodParameter)

Example 4 with MethodParameter

use of org.openmuc.jdlms.MethodParameter 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);
}
Also used : DataObject(org.openmuc.jdlms.datatypes.DataObject) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) ProtocolAdapterException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException) MethodParameter(org.openmuc.jdlms.MethodParameter) MBusClientMethod(org.opensmartgridplatform.dlms.interfaceclass.method.MBusClientMethod)

Example 5 with MethodParameter

use of org.openmuc.jdlms.MethodParameter in project open-smart-grid-platform by OSGP.

the class AbstractReplaceKeyCommandExecutor method sendToDevice.

/**
 * Send the key to the device.
 *
 * @param conn jDLMS connection.
 * @param deviceIdentification Device identification
 * @param keyWrapper Key data
 * @param messageMetadata
 */
private void sendToDevice(final DlmsConnectionManager conn, final String deviceIdentification, final ReplaceKeyInput keyWrapper, final MessageMetadata messageMetadata) throws ProtocolAdapterException {
    try {
        final byte[] decryptedKey = keyWrapper.getBytes();
        final byte[] decryptedMasterKey = this.secretManagementService.getKey(messageMetadata, deviceIdentification, SecurityKeyType.E_METER_MASTER);
        final MethodParameter methodParameterAuth = SecurityUtils.keyChangeMethodParamFor(decryptedMasterKey, decryptedKey, keyWrapper.getKeyId());
        conn.getDlmsMessageListener().setDescription("ReplaceKey for " + keyWrapper.getSecurityKeyType() + " " + keyWrapper.getKeyId() + ", call method: " + JdlmsObjectToStringUtil.describeMethod(methodParameterAuth));
        final MethodResultCode methodResultCode = conn.getConnection().action(methodParameterAuth).getResultCode();
        if (!MethodResultCode.SUCCESS.equals(methodResultCode)) {
            throw new ProtocolAdapterException("AccessResultCode for replace keys was not SUCCESS: " + methodResultCode);
        }
        if (keyWrapper.getSecurityKeyType() == SecurityKeyType.E_METER_AUTHENTICATION) {
            conn.getConnection().changeClientGlobalAuthenticationKey(decryptedKey);
        } else if (keyWrapper.getSecurityKeyType() == SecurityKeyType.E_METER_ENCRYPTION) {
            conn.getConnection().changeClientGlobalEncryptionKey(decryptedKey);
        }
    } catch (final IOException e) {
        throw new ConnectionException(e);
    } catch (final EncrypterException e) {
        throw new ProtocolAdapterException("Unexpected exception during decryption of security keys, reason = " + e.getMessage(), e);
    }
}
Also used : MethodResultCode(org.openmuc.jdlms.MethodResultCode) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) ProtocolAdapterException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException) IOException(java.io.IOException) MethodParameter(org.openmuc.jdlms.MethodParameter) ConnectionException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException)

Aggregations

MethodParameter (org.openmuc.jdlms.MethodParameter)8 ProtocolAdapterException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException)3 MBusClientMethod (org.opensmartgridplatform.dlms.interfaceclass.method.MBusClientMethod)3 IOException (java.io.IOException)2 DataObject (org.openmuc.jdlms.datatypes.DataObject)2 ConnectionException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException)2 MethodResult (org.openmuc.jdlms.MethodResult)1 MethodResultCode (org.openmuc.jdlms.MethodResultCode)1 DlmsDevice (org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice)1 EncrypterException (org.opensmartgridplatform.shared.exceptionhandling.EncrypterException)1