use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException in project open-smart-grid-platform by OSGP.
the class Lls0Connector method connect.
@Override
public DlmsConnection connect(final MessageMetadata messageMetadata, final DlmsDevice device, final DlmsMessageListener dlmsMessageListener) throws OsgpException {
// Make sure neither device or device.getIpAddress() is null.
this.checkDevice(device);
this.checkIpAddress(device);
// Setup connection to device
final TcpConnectionBuilder tcpConnectionBuilder;
try {
tcpConnectionBuilder = new TcpConnectionBuilder(InetAddress.getByName(device.getIpAddress())).setResponseTimeout(this.responseTimeout).setLogicalDeviceId(this.logicalDeviceAddress).setClientId(this.clientId).setReferencingMethod(device.isUseSn() ? ReferencingMethod.SHORT : ReferencingMethod.LOGICAL);
if (device.isUseHdlc()) {
tcpConnectionBuilder.useHdlc();
}
} catch (final UnknownHostException e) {
LOGGER.error("The IP address is not found: {}", device.getIpAddress(), e);
// Unknown IP, unrecoverable.
throw new TechnicalException(ComponentType.PROTOCOL_DLMS, "The IP address is not found: " + device.getIpAddress());
}
this.setOptionalValues(device, tcpConnectionBuilder);
if (device.isInDebugMode()) {
tcpConnectionBuilder.setRawMessageListener(dlmsMessageListener);
}
try {
return tcpConnectionBuilder.build();
} catch (final IOException e) {
final String msg = String.format("Error creating connection for device %s with Ip address:%s Port:%d UseHdlc:%b UseSn:%b Message:%s", device.getDeviceIdentification(), device.getIpAddress(), device.getPort(), device.isUseHdlc(), device.isUseSn(), e.getMessage());
LOGGER.error(msg);
throw new ConnectionException(msg, e);
}
}
use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException in project open-smart-grid-platform by OSGP.
the class GetAllAttributeValuesCommandExecutor method execute.
@Override
public String execute(final DlmsConnectionManager conn, final DlmsDevice device, final DataObject object, final MessageMetadata messageMetadata) throws OsgpException {
final AttributeAddress attributeAddress = new AttributeAddress(CLASS_ID, OBIS_CODE, ATTRIBUTE_ID);
conn.getDlmsMessageListener().setDescription("RetrieveAllAttributeValues, retrieve attribute: " + JdlmsObjectToStringUtil.describeAttributes(attributeAddress));
LOGGER.debug("Retrieving all attribute values for class id: {}, obis code: {}, attribute id: {}", CLASS_ID, OBIS_CODE, ATTRIBUTE_ID);
final DataObject objectList = this.dlmsHelper.getAttributeValue(conn, attributeAddress);
if (!objectList.isComplex()) {
this.throwUnexpectedTypeProtocolAdapterException();
}
final List<DataObject> objectListElements = objectList.getValue();
final List<ClassIdObisAttr> allObisCodes = this.getAllObisCodes(objectListElements);
this.logAllObisCodes(allObisCodes);
try {
final String output = this.createOutput(conn, allObisCodes);
LOGGER.debug("Total output is: {}", output);
return output;
} catch (final IOException e) {
throw new ConnectionException(e);
}
}
use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException in project open-smart-grid-platform by OSGP.
the class GetOutagesCommandExecutor method execute.
@Override
public List<OutageDto> execute(final DlmsConnectionManager conn, final DlmsDevice device, final GetOutagesRequestDto getOutagesRequestDto, final MessageMetadata messageMetadata) throws ProtocolAdapterException {
final AttributeAddress eventLogBuffer = new AttributeAddress(CLASS_ID, new ObisCode(OBIS_CODE), ATTRIBUTE_ID);
conn.getDlmsMessageListener().setDescription("RetrieveOutages, retrieve attribute: " + JdlmsObjectToStringUtil.describeAttributes(eventLogBuffer));
final GetResult getResult;
try {
getResult = conn.getConnection().get(eventLogBuffer);
} catch (final IOException e) {
throw new ConnectionException(e);
}
if (getResult == null) {
throw new ProtocolAdapterException("No GetResult received while retrieving event register POWER_FAILURE_EVENT_LOG");
}
if (!AccessResultCode.SUCCESS.equals(getResult.getResultCode())) {
log.info("Result of getting events for POWER_FAILURE_EVENT_LOG is {}", getResult.getResultCode());
throw new ProtocolAdapterException("Getting the outages from POWER_FAILURE_EVENT_LOG from the meter resulted in: " + getResult.getResultCode());
}
final DataObject resultData = getResult.getResultData();
return this.dataObjectToOutageListConverter.convert(resultData);
}
use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException in project open-smart-grid-platform by OSGP.
the class SetAdministrativeStatusCommandExecutor method execute.
@Override
public AccessResultCode execute(final DlmsConnectionManager conn, final DlmsDevice device, final AdministrativeStatusTypeDto administrativeStatusType, final MessageMetadata messageMetadata) throws ProtocolAdapterException {
LOGGER.info("Set administrative status by issuing get request for class id: {}, obis code: {}, attribute id: {}", CLASS_ID, OBIS_CODE, ATTRIBUTE_ID);
final AttributeAddress attributeAddress = new AttributeAddress(CLASS_ID, OBIS_CODE, ATTRIBUTE_ID);
final DataObject value = DataObject.newEnumerateData(this.configurationMapper.map(administrativeStatusType, Integer.class));
final SetParameter setParameter = new SetParameter(attributeAddress, value);
conn.getDlmsMessageListener().setDescription("SetAdminstrativeStatus to " + administrativeStatusType + ", set attribute: " + JdlmsObjectToStringUtil.describeAttributes(attributeAddress));
try {
return conn.getConnection().set(setParameter);
} catch (final IOException e) {
throw new ConnectionException(e);
}
}
use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException 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);
}
}
Aggregations