Search in sources :

Example 1 with ConnectionException

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);
    }
}
Also used : ScanException(org.openmuc.framework.config.ScanException) DriverNotAvailableException(org.openmuc.framework.config.DriverNotAvailableException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) DriverService(org.openmuc.framework.driver.spi.DriverService)

Example 2 with ConnectionException

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;
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) ChannelRecordContainer(org.openmuc.framework.driver.spi.ChannelRecordContainer) SerialPortTimeoutException(org.openmuc.jrxtx.SerialPortTimeoutException) DataRecord(org.openmuc.jmbus.DataRecord) Record(org.openmuc.framework.data.Record) DataRecord(org.openmuc.jmbus.DataRecord) VariableDataStructure(org.openmuc.jmbus.VariableDataStructure) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException)

Example 3 with ConnectionException

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);
    }
}
Also used : Path(java.nio.file.Path) OpcUaClientConfigBuilder(org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfigBuilder) ServerTypeNode(org.eclipse.milo.opcua.sdk.client.model.nodes.objects.ServerTypeNode) UaException(org.eclipse.milo.opcua.stack.core.UaException) AnonymousProvider(org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ExecutionException(java.util.concurrent.ExecutionException) UaException(org.eclipse.milo.opcua.stack.core.UaException) Connect(org.openmuc.framework.driver.annotation.Connect)

Example 4 with ConnectionException

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());
    }
}
Also used : ChannelScanInfo(org.openmuc.framework.config.ChannelScanInfo) FromJson(org.openmuc.framework.lib.rest.FromJson) RestChannel(org.openmuc.framework.lib.rest.objects.RestChannel) IOException(java.io.IOException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException)

Example 5 with ConnectionException

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;
}
Also used : ModbusChannel(org.openmuc.framework.driver.modbus.ModbusChannel) ModbusIOException(com.ghgande.j2mod.modbus.ModbusIOException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ArgumentSyntaxException(org.openmuc.framework.config.ArgumentSyntaxException) ScanException(org.openmuc.framework.config.ScanException) UnknownHostException(java.net.UnknownHostException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ModbusIOException(com.ghgande.j2mod.modbus.ModbusIOException) ModbusException(com.ghgande.j2mod.modbus.ModbusException) ChannelValueContainer(org.openmuc.framework.driver.spi.ChannelValueContainer) ModbusException(com.ghgande.j2mod.modbus.ModbusException)

Aggregations

ConnectionException (org.openmuc.framework.driver.spi.ConnectionException)55 IOException (java.io.IOException)23 ArgumentSyntaxException (org.openmuc.framework.config.ArgumentSyntaxException)16 ScanException (org.openmuc.framework.config.ScanException)14 Record (org.openmuc.framework.data.Record)11 ChannelRecordContainer (org.openmuc.framework.driver.spi.ChannelRecordContainer)10 ModbusException (com.ghgande.j2mod.modbus.ModbusException)9 ModbusIOException (com.ghgande.j2mod.modbus.ModbusIOException)9 ArrayList (java.util.ArrayList)9 ModbusChannel (org.openmuc.framework.driver.modbus.ModbusChannel)6 ChannelValueContainer (org.openmuc.framework.driver.spi.ChannelValueContainer)6 ChannelScanInfo (org.openmuc.framework.config.ChannelScanInfo)5 ServiceError (com.beanit.iec61850bean.ServiceError)4 Connect (org.openmuc.framework.driver.annotation.Connect)4 FcModelNode (com.beanit.iec61850bean.FcModelNode)3 InterruptedIOException (java.io.InterruptedIOException)3 SQLException (java.sql.SQLException)3 Value (org.openmuc.framework.data.Value)3 VariableDataStructure (org.openmuc.jmbus.VariableDataStructure)3 BasicDataAttribute (com.beanit.iec61850bean.BasicDataAttribute)2