Search in sources :

Example 6 with ChannelScanInfo

use of org.openmuc.framework.config.ChannelScanInfo in project OpenMUC by isc-konstanz.

the class ColumnScanner method scan.

@Override
public void scan(List<ChannelScanInfo> channels) throws ArgumentSyntaxException, ScanException, ConnectionException {
    logger.info("Scan for columns in {}.{}", database, table);
    try (Connection connection = source.getConnection()) {
        try (Statement statement = connection.createStatement()) {
            try (ResultSet result = statement.executeQuery(String.format("SELECT * FROM %s LIMIT 1", table))) {
                if (result.first()) {
                    ResultSetMetaData metaData = result.getMetaData();
                    for (int i = 1; i <= metaData.getColumnCount(); i++) {
                        String column = metaData.getColumnName(i);
                        Integer typeLength = null;
                        ValueType type;
                        switch(metaData.getColumnTypeName(i)) {
                            case "BIT":
                                type = ValueType.BOOLEAN;
                                break;
                            case "TINYINT":
                                type = ValueType.BYTE;
                                break;
                            case "SMALLINT":
                                type = ValueType.SHORT;
                                break;
                            case "INTEGER":
                                type = ValueType.INTEGER;
                                break;
                            case "BIGINT":
                                type = ValueType.LONG;
                                break;
                            case "REAL":
                                type = ValueType.FLOAT;
                                break;
                            case "DOUBLE":
                            case "NUMERIC":
                                type = ValueType.DOUBLE;
                                break;
                            case "VARBINARY":
                            case "LONGVARBINARY":
                                typeLength = metaData.getColumnDisplaySize(i);
                                type = ValueType.BYTE_ARRAY;
                                break;
                            case "VARCHAR":
                            case "LONGVARCHAR":
                            default:
                                typeLength = metaData.getColumnDisplaySize(i);
                                type = ValueType.STRING;
                        }
                        channels.add(new ChannelScanInfo(column, column, type, typeLength));
                    }
                }
            } catch (SQLException e) {
                throw new ScanException(e);
            }
        }
    } catch (SQLException e) {
        throw new ConnectionException(e);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ChannelScanInfo(org.openmuc.framework.config.ChannelScanInfo) ScanException(org.openmuc.framework.config.ScanException) ValueType(org.openmuc.framework.data.ValueType) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException)

Example 7 with ChannelScanInfo

use of org.openmuc.framework.config.ChannelScanInfo in project OpenMUC by isc-konstanz.

the class CsvScanDeviceTest method testChannelScan.

@Test
public void testChannelScan() {
    CsvDriver csvDriver = new CsvDriver();
    String deviceAddress = dir + "/src/test/resources/test_data.csv";
    try {
        String settings = "SAMPLINGMODE=hhmmss";
        CsvFile csvConnection = (CsvFile) csvDriver.connect(deviceAddress, settings);
        List<ChannelScanInfo> channelsScanInfos = csvConnection.scanForChannels("");
        for (ChannelScanInfo info : channelsScanInfos) {
            System.out.println("Channel: " + info.getChannelAddress());
        }
    } catch (ArgumentSyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ConnectionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedOperationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ScanException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : ChannelScanInfo(org.openmuc.framework.config.ChannelScanInfo) ScanException(org.openmuc.framework.config.ScanException) CsvFile(org.openmuc.framework.driver.csv.CsvFile) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) CsvDriver(org.openmuc.framework.driver.csv.CsvDriver) ArgumentSyntaxException(org.openmuc.framework.config.ArgumentSyntaxException) Test(org.junit.Test)

Example 8 with ChannelScanInfo

use of org.openmuc.framework.config.ChannelScanInfo in project OpenMUC by isc-konstanz.

the class DeviceResourceServlet method scanForAllChannels.

private List<ChannelScanInfo> scanForAllChannels(String deviceId, String settings, HttpServletResponse response) {
    List<ChannelScanInfo> channelList = new ArrayList<>();
    List<ChannelScanInfo> scannedDevicesList;
    try {
        scannedDevicesList = configService.scanForChannels(deviceId, settings);
        for (ChannelScanInfo scannedDevice : scannedDevicesList) {
            channelList.add(scannedDevice);
        }
    } catch (UnsupportedOperationException e) {
        ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, logger, "Device does not support scanning.", REST_ID, deviceId);
    } catch (DriverNotAvailableException e) {
        ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_FOUND, logger, REQUESTED_ID_IS_NOT_AVAILABLE, REST_ID, deviceId);
    } catch (ArgumentSyntaxException e) {
        ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_ACCEPTABLE, logger, "Argument syntax was wrong.", REST_ID, deviceId, " Settings = ", settings);
    } catch (ScanException e) {
        ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, logger, "Error while scan device channels", REST_ID, deviceId, " Settings = ", settings);
    }
    return channelList;
}
Also used : ChannelScanInfo(org.openmuc.framework.config.ChannelScanInfo) ScanException(org.openmuc.framework.config.ScanException) ArrayList(java.util.ArrayList) DriverNotAvailableException(org.openmuc.framework.config.DriverNotAvailableException) ArgumentSyntaxException(org.openmuc.framework.config.ArgumentSyntaxException)

Example 9 with ChannelScanInfo

use of org.openmuc.framework.config.ChannelScanInfo in project OpenMUC by isc-konstanz.

the class Iec61850ConnectionTest method testWrite.

@Test
public void testWrite() throws IOException, ServiceError, ConfigurationException, javax.naming.ConfigurationException, SclParseException, InterruptedException, UnsupportedOperationException, ConnectionException {
    System.out.println("Attempting to connect to server " + host + " on port " + port);
    clientAssociation = clientSap.associate(InetAddress.getByName(host), port, null, this);
    ServerModel serverModel = SclParser.parse("src/test/resources/testOpenmuc.icd").get(0);
    clientAssociation.setServerModel(serverModel);
    getAllBdas(serverModel, clientAssociation);
    // ------------SCAN FOR CHANNELS-------------------
    Iec61850Connection testIec61850Connection = new Iec61850Connection(clientAssociation, serverModel);
    List<ChannelScanInfo> testChannelScanList = testIec61850Connection.scanForChannels("");
    // ----------WRITE-----------------
    List<ChannelValueContainer> testChannelValueContainers = new ArrayList<>();
    byte[] newValue = { 0x44 };
    testChannelValueContainers.add(new ChannelValueContainerImpl(testChannelScanList.get(14).getChannelAddress(), new ByteArrayValue(newValue)));
    testChannelValueContainers.add(new ChannelValueContainerImpl(testChannelScanList.get(25).getChannelAddress(), new FloatValue((float) 12.5)));
    testChannelValueContainers.add(new ChannelValueContainerImpl(testChannelScanList.get(24).getChannelAddress(), new BooleanValue(true)));
    testIec61850Connection.write(testChannelValueContainers, null);
    // Create record container to read the changes made by "write"
    List<ChannelRecordContainer> testRecordContainers = new ArrayList<>();
    for (int i = 0; i < 34; i++) {
        testRecordContainers.add(new ChannelRecordContainerImpl(testChannelScanList.get(i).getChannelAddress()));
    }
    testIec61850Connection.read(testRecordContainers, null, "");
    Assert.assertEquals("[68]", testRecordContainers.get(14).getRecord().getValue().toString());
    Assert.assertEquals("12.5", testRecordContainers.get(25).getRecord().getValue().toString());
    Assert.assertEquals("true", testRecordContainers.get(24).getRecord().getValue().toString());
}
Also used : ArrayList(java.util.ArrayList) ChannelScanInfo(org.openmuc.framework.config.ChannelScanInfo) ChannelRecordContainer(org.openmuc.framework.driver.spi.ChannelRecordContainer) ServerModel(com.beanit.iec61850bean.ServerModel) BooleanValue(org.openmuc.framework.data.BooleanValue) ByteArrayValue(org.openmuc.framework.data.ByteArrayValue) FloatValue(org.openmuc.framework.data.FloatValue) ChannelValueContainer(org.openmuc.framework.driver.spi.ChannelValueContainer) Test(org.junit.jupiter.api.Test)

Example 10 with ChannelScanInfo

use of org.openmuc.framework.config.ChannelScanInfo in project OpenMUC by isc-konstanz.

the class Iec61850ConnectionTest method testScanForChannels.

@Test
public void testScanForChannels() throws IOException, ServiceError, ConfigurationException, javax.naming.ConfigurationException, SclParseException, InterruptedException, UnsupportedOperationException, ConnectionException {
    System.out.println("Attempting to connect to server " + host + " on port " + port);
    clientAssociation = clientSap.associate(InetAddress.getByName(host), port, null, this);
    ServerModel serverModel = SclParser.parse("src/test/resources/testOpenmuc.icd").get(0);
    clientAssociation.setServerModel(serverModel);
    getAllBdas(serverModel, clientAssociation);
    Iec61850Connection testConnection = new Iec61850Connection(clientAssociation, serverModel);
    List<ChannelScanInfo> testChannelScanList = testConnection.scanForChannels("");
    for (int i = 14; i < 23; i++) {
        System.out.print(testChannelScanList.get(i).getChannelAddress() + "\n");
        Assert.assertEquals("BYTE_ARRAY", testChannelScanList.get(i).getValueType().toString());
    }
    Assert.assertEquals("LONG", testChannelScanList.get(23).getValueType().toString());
    Assert.assertEquals("BOOLEAN", testChannelScanList.get(24).getValueType().toString());
    Assert.assertEquals("FLOAT", testChannelScanList.get(25).getValueType().toString());
    Assert.assertEquals("DOUBLE", testChannelScanList.get(26).getValueType().toString());
    Assert.assertEquals("BYTE", testChannelScanList.get(27).getValueType().toString());
    Assert.assertEquals("SHORT", testChannelScanList.get(28).getValueType().toString());
    Assert.assertEquals("SHORT", testChannelScanList.get(29).getValueType().toString());
    Assert.assertEquals("INTEGER", testChannelScanList.get(30).getValueType().toString());
    Assert.assertEquals("INTEGER", testChannelScanList.get(31).getValueType().toString());
    Assert.assertEquals("LONG", testChannelScanList.get(32).getValueType().toString());
    Assert.assertEquals("LONG", testChannelScanList.get(33).getValueType().toString());
    Assert.assertEquals("BYTE_ARRAY", testChannelScanList.get(34).getValueType().toString());
}
Also used : ChannelScanInfo(org.openmuc.framework.config.ChannelScanInfo) ServerModel(com.beanit.iec61850bean.ServerModel) Test(org.junit.jupiter.api.Test)

Aggregations

ChannelScanInfo (org.openmuc.framework.config.ChannelScanInfo)21 ArrayList (java.util.ArrayList)10 ValueType (org.openmuc.framework.data.ValueType)6 IOException (java.io.IOException)5 ConnectionException (org.openmuc.framework.driver.spi.ConnectionException)5 ScanException (org.openmuc.framework.config.ScanException)4 ServerModel (com.beanit.iec61850bean.ServerModel)3 Test (org.junit.jupiter.api.Test)3 JsonArray (com.google.gson.JsonArray)2 JsonObject (com.google.gson.JsonObject)2 LinkedList (java.util.LinkedList)2 Test (org.junit.Test)2 ArgumentSyntaxException (org.openmuc.framework.config.ArgumentSyntaxException)2 ChannelRecordContainer (org.openmuc.framework.driver.spi.ChannelRecordContainer)2 FromJson (org.openmuc.framework.lib.rest.FromJson)2 RestChannel (org.openmuc.framework.lib.rest.objects.RestChannel)2 DataObject (org.openmuc.jdlms.datatypes.DataObject)2 MBusConnection (org.openmuc.jmbus.MBusConnection)2 VariableDataStructure (org.openmuc.jmbus.VariableDataStructure)2 BasicDataAttribute (com.beanit.iec61850bean.BasicDataAttribute)1