OpcuaPlcDriver implements PlcDriver { @Override public PlcConnection getConnection(String url) throws PlcConnectionException { Matcher matcher = OPCUA_URI_PATTERN.matcher(url); if (!matcher.matches()) { throw new PlcConnectionException( "Connection url doesn't match the format 'opcua:{type} } String host = matcher.group("host"); String portString = matcher.group("port"); Integer port = StringUtils.isNotBlank(portString) ? Integer.parseInt(portString) : null; String params = matcher.group("params") != null ? matcher.group("params").substring(1) : null; try { return opcuaConnectionFactory.opcuaTcpPlcConnectionOf(InetAddress.getByName(host), port, params, requestTimeout); } catch (UnknownHostException e) { throw new PlcConnectionException(e); } }  @Override String getProtocolCode(); @Override String getProtocolName(); @Override PlcConnection getConnection(String url); @Override PlcConnection getConnection(String url, PlcAuthentication authentication); @Override OpcuaField prepareField(String query); static final Pattern INET_ADDRESS_PATTERN; static final Pattern OPCUA_URI_PARAM_PATTERN; static final Pattern OPCUA_URI_PATTERN; }
@Test public void connectionNoParams(){ connectionStringValidSet.forEach(connectionAddress -> { String connectionString = connectionAddress; try { PlcConnection opcuaConnection = new PlcDriverManager().getConnection(connectionString); assert opcuaConnection.isConnected(); opcuaConnection.close(); assert !opcuaConnection.isConnected(); } catch (PlcConnectionException e) { fail("Exception during connectionNoParams while connecting Test EXCEPTION: " + e.getMessage()); } catch (Exception e) { fail("Exception during connectionNoParams while closing Test EXCEPTION: " + e.getMessage()); } }); }
@Test public void connectionWithDiscoveryParam(){ connectionStringValidSet.forEach(connectionAddress -> { discoveryParamValidSet.forEach(discoveryParam -> { String connectionString = connectionAddress + paramSectionDivider + discoveryParam; try { PlcConnection opcuaConnection = new PlcDriverManager().getConnection(connectionString); assert opcuaConnection.isConnected(); opcuaConnection.close(); assert !opcuaConnection.isConnected(); } catch (PlcConnectionException e) { fail("Exception during connectionWithDiscoveryParam while connecting Test EXCEPTION: " + e.getMessage()); } catch (Exception e) { fail("Exception during connectionWithDiscoveryParam while closing Test EXCEPTION: " + e.getMessage()); } }); }); }
@Test public void readVariables() { try { PlcConnection opcuaConnection = new PlcDriverManager().getConnection(tcpConnectionAddress); assert opcuaConnection.isConnected(); PlcReadRequest.Builder builder = opcuaConnection.readRequestBuilder(); builder.addItem("Bool", BOOL_IDENTIFIER_READ_WRITE); builder.addItem("Byte", BYTE_IDENTIFIER_READ_WRITE); builder.addItem("Double", DOUBLE_IDENTIFIER_READ_WRITE); builder.addItem("Float", FLOAT_IDENTIFIER_READ_WRITE); builder.addItem("Int16", INT16_IDENTIFIER_READ_WRITE); builder.addItem("Int32", INT32_IDENTIFIER_READ_WRITE); builder.addItem("Int64", INT64_IDENTIFIER_READ_WRITE); builder.addItem("Integer", INTEGER_IDENTIFIER_READ_WRITE); builder.addItem("SByte", SBYTE_IDENTIFIER_READ_WRITE); builder.addItem("String", STRING_IDENTIFIER_READ_WRITE); builder.addItem("UInt16", UINT16_IDENTIFIER_READ_WRITE); builder.addItem("UInt32", UINT32_IDENTIFIER_READ_WRITE); builder.addItem("UInt64", UINT64_IDENTIFIER_READ_WRITE); builder.addItem("UInteger", UINTEGER_IDENTIFIER_READ_WRITE); builder.addItem("DoesNotExists", DOES_NOT_EXIST_IDENTIFIER_READ_WRITE); PlcReadRequest request = builder.build(); PlcReadResponse response = request.execute().get(); assert response.getResponseCode("Bool").equals(PlcResponseCode.OK); assert response.getResponseCode("Byte").equals(PlcResponseCode.OK); assert response.getResponseCode("Double").equals(PlcResponseCode.OK); assert response.getResponseCode("Float").equals(PlcResponseCode.OK); assert response.getResponseCode("Int16").equals(PlcResponseCode.OK); assert response.getResponseCode("Int32").equals(PlcResponseCode.OK); assert response.getResponseCode("Int64").equals(PlcResponseCode.OK); assert response.getResponseCode("Integer").equals(PlcResponseCode.OK); assert response.getResponseCode("SByte").equals(PlcResponseCode.OK); assert response.getResponseCode("String").equals(PlcResponseCode.OK); assert response.getResponseCode("UInt16").equals(PlcResponseCode.OK); assert response.getResponseCode("UInt32").equals(PlcResponseCode.OK); assert response.getResponseCode("UInt64").equals(PlcResponseCode.OK); assert response.getResponseCode("UInteger").equals(PlcResponseCode.OK); assert response.getResponseCode("DoesNotExists").equals(PlcResponseCode.NOT_FOUND); opcuaConnection.close(); assert !opcuaConnection.isConnected(); } catch (Exception e) { fail("Exception during readVariables Test EXCEPTION: " + e.getMessage()); } }
@Test public void writeVariables() { try { PlcConnection opcuaConnection = new PlcDriverManager().getConnection(tcpConnectionAddress); assert opcuaConnection.isConnected(); PlcWriteRequest.Builder builder = opcuaConnection.writeRequestBuilder(); builder.addItem("Bool", BOOL_IDENTIFIER_READ_WRITE, true); builder.addItem("Double", DOUBLE_IDENTIFIER_READ_WRITE, 0.5d); builder.addItem("Float", FLOAT_IDENTIFIER_READ_WRITE, 0.5f); builder.addItem("Int32", INT32_IDENTIFIER_READ_WRITE, 42); builder.addItem("Int64", INT64_IDENTIFIER_READ_WRITE, 42L); builder.addItem("Integer", INTEGER_IDENTIFIER_READ_WRITE, 42); builder.addItem("String", STRING_IDENTIFIER_READ_WRITE, "Helllo Toddy!"); builder.addItem("UInt64", UINT64_IDENTIFIER_READ_WRITE, new BigInteger("1337")); PlcWriteRequest request = builder.build(); PlcWriteResponse response = request.execute().get(); assert response.getResponseCode("Bool").equals(PlcResponseCode.OK); assert response.getResponseCode("Double").equals(PlcResponseCode.OK); assert response.getResponseCode("Float").equals(PlcResponseCode.OK); assert response.getResponseCode("Int32").equals(PlcResponseCode.OK); assert response.getResponseCode("Int64").equals(PlcResponseCode.OK); assert response.getResponseCode("Integer").equals(PlcResponseCode.OK); assert response.getResponseCode("String").equals(PlcResponseCode.OK); assert response.getResponseCode("UInt64").equals(PlcResponseCode.OK); opcuaConnection.close(); assert !opcuaConnection.isConnected(); } catch (Exception e) { fail("Exception during readVariables Test EXCEPTION: " + e.getMessage()); } }