use of org.openmuc.framework.driver.spi.ConnectionException in project OpenMUC by isc-konstanz.
the class DataManager method scanForChannels.
@Override
public List<ChannelScanInfo> scanForChannels(String deviceId, String settings) throws DriverNotAvailableException, UnsupportedOperationException, ArgumentSyntaxException, ScanException {
// TODO this function is probably not thread safe
DeviceConfigImpl config = (DeviceConfigImpl) this.rootConfig.getDevice(deviceId);
if (config == null) {
throw new ScanException("No device with ID \"" + deviceId + "\" found.");
}
DriverService activeDriver = activeDrivers.get(config.driverParent.id);
if (activeDriver == null) {
throw new DriverNotAvailableException();
}
waitTilDeviceIsConnected(config.device);
try {
return config.device.connection.scanForChannels(settings);
} catch (ConnectionException e) {
config.device.disconnectedSignal();
throw new ScanException(e.getMessage(), e);
}
}
use of org.openmuc.framework.driver.spi.ConnectionException in project OpenMUC by isc-konstanz.
the class DriverConnection method setRecords.
private boolean setRecords(List<ChannelRecordContainer> containers, MBusConnection mBusConnection, long timestamp, List<DataRecord> dataRecords, String[] dibvibs) throws ConnectionException {
boolean selectForReadoutSet = false;
for (ChannelRecordContainer container : containers) {
String channelAddress = container.getChannelAddress();
if (channelAddress.startsWith("X")) {
String[] dibAndVib = channelAddress.split(":");
if (dibAndVib.length != 2) {
container.setRecord(new Record(Flag.DRIVER_ERROR_CHANNEL_ADDRESS_SYNTAX_INVALID));
}
List<DataRecord> dataRecordsToSelectForReadout = new ArrayList<>(1);
selectForReadoutSet = true;
try {
mBusConnection.selectForReadout(mBusAddress, dataRecordsToSelectForReadout);
sleep(delay);
} catch (SerialPortTimeoutException e) {
container.setRecord(new Record(Flag.DRIVER_ERROR_TIMEOUT));
continue;
} catch (IOException e) {
connectionInterface.close();
throw new ConnectionException(e);
}
VariableDataStructure variableDataStructure2 = null;
try {
variableDataStructure2 = mBusConnection.read(mBusAddress);
} catch (SerialPortTimeoutException e1) {
container.setRecord(new Record(Flag.DRIVER_ERROR_TIMEOUT));
continue;
} catch (IOException e1) {
connectionInterface.close();
throw new ConnectionException(e1);
}
DataRecord dataRecord = variableDataStructure2.getDataRecords().get(0);
setContainersRecord(timestamp, container, dataRecord);
continue;
}
int j = 0;
for (DataRecord dataRecord : dataRecords) {
if (dibvibs[j++].equalsIgnoreCase(channelAddress)) {
setContainersRecord(timestamp, container, dataRecord);
break;
}
}
if (container.getRecord() == null) {
container.setRecord(new Record(Flag.DRIVER_ERROR_CHANNEL_WITH_THIS_ADDRESS_NOT_FOUND));
}
}
return selectForReadoutSet;
}
use of org.openmuc.framework.driver.spi.ConnectionException in project OpenMUC by isc-konstanz.
the class OpcConnection method connect.
@Connect
public void connect() throws ConnectionException {
try {
Path securityTempDir = Paths.get(System.getProperty("java.io.tmpdir"), "security");
Files.createDirectories(securityTempDir);
if (!Files.exists(securityTempDir)) {
throw new ConnectionException("Unable to create security dir: " + securityTempDir);
}
logger.debug("Security temp dir: {}", securityTempDir.toAbsolutePath());
KeyStoreLoader loader = new KeyStoreLoader().load(securityTempDir);
if (!address.contains("opc.tcp://")) {
host = address.split(":")[0];
port = Integer.parseInt(address.split(":")[1]);
address = "opc.tcp://" + address;
} else {
host = address.split("//|:")[1];
port = Integer.parseInt(address.split("//|:")[2]);
}
List<EndpointDescription> endpoints = DiscoveryClient.getEndpoints(address).get();
EndpointDescription endpoint = endpoints.stream().filter(e -> true).findFirst().orElseThrow(() -> new UaException(StatusCodes.Bad_ConfigurationError, "No endpoint selected"));
logger.info("OPC Client connecting to {}.", address);
endpoint = EndpointUtil.updateUrl(endpoints.get(0), host, port);
OpcUaClientConfigBuilder clientBuilder = new OpcUaClientConfigBuilder().setEndpoint(endpoint).setApplicationName(LocalizedText.english("OpenMUC OPC UA Client")).setApplicationUri("urn:openmuc:client").setCertificate(loader.getClientCertificate()).setKeyPair(loader.getClientKeyPair()).setIdentityProvider(new AnonymousProvider()).setRequestTimeout(uint(5000));
client = OpcUaClient.create(clientBuilder.build());
client.connect().get();
// Get a typed reference to the Server object: ServerNode
ServerTypeNode serverNode = client.getAddressSpace().getObjectNode(Identifiers.Server, ServerTypeNode.class).get();
if (namespaceUri != null && !namespaceUri.isEmpty()) {
try {
namespaceIndex = Integer.parseInt(namespaceUri);
} catch (NumberFormatException e) {
namespaceIndex = Arrays.asList(serverNode.getNamespaceArray().get()).indexOf(namespaceUri);
}
}
} catch (Exception e) {
logger.error("OPC connection to server failed {}", e);
}
}
use of org.openmuc.framework.driver.spi.ConnectionException in project OpenMUC by isc-konstanz.
the class RestChannelScanner method scan.
@Override
public void scan(List<ChannelScanInfo> channelScanInfos) throws ArgumentSyntaxException, ScanException, ConnectionException {
try (RestConnection connection = new RestConnection((RestConfigs) getContext())) {
FromJson json = new FromJson(connection.get(""));
List<RestChannel> channels = json.getRestChannelList();
for (RestChannel channel : channels) {
// TODO: get channel config list with valueTypeLength, description, ...
ChannelScanInfo channelScanInfo = new ChannelScanInfo(channel.getId(), "", channel.getValueType(), 0);
channelScanInfos.add(channelScanInfo);
}
} catch (IOException e) {
throw new ConnectionException(e.getMessage());
}
}
use of org.openmuc.framework.driver.spi.ConnectionException in project OpenMUC by isc-konstanz.
the class ModbusRTUTCPConnection method write.
@Override
public Object write(List<ChannelValueContainer> containers, Object containerListHandle) throws UnsupportedOperationException, ConnectionException {
for (ChannelValueContainer container : containers) {
ModbusChannel channel = getModbusChannel(container.getChannelAddress(), EAccess.WRITE);
try {
writeChannel(channel, container.getValue());
container.setFlag(Flag.VALID);
} catch (ModbusIOException e) {
logger.error("ModbusIOException while writing channel:" + channel.getChannelAddress(), e);
disconnect();
throw new ConnectionException("Try to solve issue with reconnect.");
} catch (ModbusException e) {
logger.error("ModbusException while writing channel: " + channel.getChannelAddress(), e);
container.setFlag(Flag.DRIVER_ERROR_CHANNEL_NOT_ACCESSIBLE);
} catch (Exception e) {
logger.error("Exception while writing channel: " + channel.getChannelAddress(), e);
container.setFlag(Flag.UNKNOWN_ERROR);
}
}
return null;
}
Aggregations