java - 使用 OPCFoundation/UA-Java 项目的 OPC UA 模拟器工作示例

标签 java git apache-nifi opc opc-ua

谁有 OPC UA 的模拟器示例,我正在使用 OPC UA 项目 https://github.com/OPCFoundation/UA-Java .

我已经尝试了此 git hub 页面上提到的所有服务器,但没有一个对我有用 https://github.com/node-opcua/node-opcua/wiki/publicly-available-OPCUA-servers .

我正在使用 org.opcfoundation.ua.examples.SampleClient 实用程序来检查连接和示例值,但无法执行此操作。如果有人有关于此的工作示例,请与代码一起分享。一旦此工作正常,我需要在 apache Nifi 中配置此设置以创建数据管道。

代码:

public class SampleClient {


    public static final Locale ENGLISH = Locale.ENGLISH;
    public static final Locale ENGLISH_FINLAND = new Locale("en", "FI");
    public static final Locale ENGLISH_US = new Locale("en", "US");

    public static final Locale FINNISH = new Locale("fi");
    public static final Locale FINNISH_FINLAND = new Locale("fi", "FI");

    public static final Locale GERMAN = Locale.GERMAN;
    public static final Locale GERMAN_GERMANY = new Locale("de", "DE");

    public static void main(String[] args) 
    throws Exception {
//      if (args.length==0) {
//          System.out.println("Usage: SampleClient [server uri]");
//          return;
//      }
        //String url = /*args[0]*/"opc.tcp://uademo.prosysopc.com:53530/OPCUA/SimulationServer";
        //String url = /*args[0]*/"opc.tcp://uademo.prosysopc.com:53530";
        //String url = /*args[0]*/"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer";
        String url="opc.tcp://mfactorengineering.com:4840";
        //String url="opc.tcp://commsvr.com:51234/UA/CAS_UA_Server";
        //String url="opc.tcp://uademo.prosysopc.com:53530/OPCUA/SimulationServer";
        //String url="opc.tcp://opcua.demo-this.com:51210/UA/SampleServer";
        //String url="opc.tcp://opcua.demo-this.com:51211/UA/SampleServer";
        //String url="opc.tcp://opcua.demo-this.com:51212/UA/SampleServer";
        //String url="opc.tcp://demo.ascolab.com:4841";
        //String url="opc.tcp://alamscada.dynu.com:4096";

        System.out.print("SampleClient: Connecting to "+url+" .. ");

        //////////////  CLIENT  //////////////
        // Create Client
        Application myApplication = new Application();
        Client myClient = new Client(myApplication);
        myApplication.addLocale( ENGLISH );
        myApplication.setApplicationName( new LocalizedText("Java Sample Client", Locale.ENGLISH) );
        myApplication.setProductUri( "urn:JavaSampleClient" );

        CertificateUtils.setKeySize(1024); // default = 1024
        KeyPair pair = ExampleKeys.getCert("SampleClient");
        myApplication.addApplicationInstanceCertificate( pair );        

        // The HTTPS SecurityPolicies are defined separate from the endpoint securities
        myApplication.getHttpsSettings().setHttpsSecurityPolicies(HttpsSecurityPolicy.ALL);

        // Peer verifier
        myApplication.getHttpsSettings().setHostnameVerifier( SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER );
        myApplication.getHttpsSettings().setCertificateValidator( CertificateValidator.ALLOW_ALL );

        // The certificate to use for HTTPS
        KeyPair myHttpsCertificate = ExampleKeys.getHttpsCert("SampleClient"); 
        myApplication.getHttpsSettings().setKeyPair( myHttpsCertificate );

        // Connect to the given uri
        SessionChannel mySession = myClient.createSessionChannel(url);
//      mySession.activate("username", "123");
        mySession.activate();
        //////////////////////////////////////      

        /////////////  EXECUTE  //////////////      
        // Browse Root
        BrowseDescription browse = new BrowseDescription();
        browse.setNodeId( Identifiers.RootFolder );
        browse.setBrowseDirection( BrowseDirection.Forward );
        browse.setIncludeSubtypes( true );
        browse.setNodeClassMask( NodeClass.Object, NodeClass.Variable );
        browse.setResultMask( BrowseResultMask.All );
        BrowseResponse res3 = mySession.Browse( null, null, null, browse );             
        System.out.println(res3);

        // Read Namespace Array
        ReadResponse res5 = mySession.Read(
            null, 
            null, 
            TimestampsToReturn.Neither,                 
            new ReadValueId(Identifiers.Server_NamespaceArray, Attributes.Value, null, null ) 
        );
        String[] namespaceArray = (String[]) res5.getResults()[0].getValue().getValue();
        System.out.println(Arrays.toString(namespaceArray));

        // Read a variable
        ReadResponse res4 = mySession.Read(
            null, 
            500.0, 
            TimestampsToReturn.Source, 
            new ReadValueId(new NodeId(6, 1710), Attributes.Value, null, null ) 
        );      
        System.out.println(res4);

        res4 = mySession.Read(
            null, 
            500.0, 
            TimestampsToReturn.Source, 
            new ReadValueId(new NodeId(6, 1710), Attributes.DataType, null, null ) 
        );      
        System.out.println(res4);


        /////////////  SHUTDOWN  /////////////
        mySession.close();
        mySession.closeAsync();
        //////////////////////////////////////  

    }

}

异常(exception):

SampleClient: Connecting to opc.tcp://mfactorengineering.com:4840 .. 2017-07-20 11:24:34,909 [main] INFO  CryptoUtil  - SecurityProvider initialized from org.bouncycastle.jce.provider.BouncyCastleProvider
2017-07-20 11:24:34,909 [main] INFO  CryptoUtil  - Using SecurityProvider BC
2017-07-20 11:24:35,549 [main] INFO  TcpConnection  - mfactorengineering.com/184.173.118.46:4840 Connecting
2017-07-20 11:24:36,142 [main] INFO  TcpConnection  - mfactorengineering.com/184.173.118.46:4840 Connected
2017-07-20 11:24:36,753 [main] INFO  SecureChannelTcp  - 1804305022 Closed
2017-07-20 11:24:36,768 [TcpConnection/Read] INFO  TcpConnection  - mfactorengineering.com/184.173.118.46:4840 Closed (expected)
2017-07-20 11:24:36,768 [main] INFO  TcpConnection  - mfactorengineering.com/184.173.118.46:4840 Closed
2017-07-20 11:24:36,768 [main] INFO  TcpConnection  - mfactorengineering.com/184.173.118.46:4840 Connecting
2017-07-20 11:24:37,408 [main] INFO  TcpConnection  - mfactorengineering.com/184.173.118.46:4840 Connect failed
java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:209)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at java.net.SocketInputStream.read(SocketInputStream.java:223)
    at org.opcfoundation.ua.utils.bytebuffer.InputStreamReadable._get(InputStreamReadable.java:53)
    at org.opcfoundation.ua.utils.bytebuffer.InputStreamReadable.getInt(InputStreamReadable.java:144)
    at org.opcfoundation.ua.transport.tcp.io.TcpConnection.open(TcpConnection.java:500)
    at org.opcfoundation.ua.transport.tcp.io.SecureChannelTcp.open(SecureChannelTcp.java:565)
    at org.opcfoundation.ua.application.Client.createSecureChannel(Client.java:641)
    at org.opcfoundation.ua.application.Client.createSecureChannel(Client.java:555)
    at org.opcfoundation.ua.application.Client.createSessionChannel(Client.java:370)
    at org.opcfoundation.ua.application.Client.createSessionChannel(Client.java:345)
    at org.opcfoundation.ua.examples.SampleClient.main(SampleClient.java:120)
2017-07-20 11:24:37,464 [main] WARN  SecureChannelTcp  - Connection failed: Bad_CommunicationError (code=0x80050000, description="2147811328, Connection reset")
2017-07-20 11:24:37,464 [main] WARN  SecureChannelTcp  - Bad_CommunicationError: Retrying
2017-07-20 11:24:37,464 [main] INFO  TcpConnection  - mfactorengineering.com/184.173.118.46:4840 Connecting
2017-07-20 11:24:38,056 [main] INFO  TcpConnection  - mfactorengineering.com/184.173.118.46:4840 Connected
2017-07-20 11:24:38,368 [TcpConnection/Read] WARN  TcpConnection  - mfactorengineering.com/184.173.118.46:4840 Error
org.opcfoundation.ua.common.ServiceResultException: Bad_SecurityChecksFailed (code=0x80130000, description="An error occurred verifying security")
    at org.opcfoundation.ua.transport.tcp.io.TcpConnection$ReadThread.run(TcpConnection.java:782)
2017-07-20 11:24:38,368 [TcpConnection/Read] INFO  TcpConnection  - mfactorengineering.com/184.173.118.46:4840 Closed
Exception in thread "main" org.opcfoundation.ua.common.ServiceResultException: Bad_SecurityChecksFailed (code=0x80130000, description="An error occurred verifying security")
    at org.opcfoundation.ua.transport.tcp.io.TcpConnection$ReadThread.run(TcpConnection.java:782)

最佳答案

编写该示例时,它始终选择最安全的连接,另一方面,这要求服务器在启用连接之前接受客户端应用程序的应用程序实例证书。 Bad_SecurityChecksFailed 是服务器不接受客户端连接时发出的标准错误代码。

由于您无法控制这些公开可用的服务器以使它们信任您的客户端应用程序,因此您唯一的选择是在服务器允许的情况下尝试在不安全的情况下进行连接。

为此,您需要更改代码,以便它选择不安全的端点。

替换

SessionChannel mySession = myClient.createSessionChannel(url);

EndpointDescription[] endpoints = myClient.discoverEndpoints(url);
// Filter out all but opc.tcp protocol endpoints
endpoints = selectByProtocol(endpoints, "opc.tcp");
// Filter out all but Signed & Encrypted endpoints
endpoints = selectByMessageSecurityMode(endpoints, MessageSecurityMode.None);

// Choose one endpoint
if (endpoints.length == 0)
  throw new Exception("The server does not support insecure connections");
EndpointDescription endpoint = endpoints[0];
//////////////////////////////////////
SessionChannel mySession = myClient.createSessionChannel(endpoint);

(根据ClientExample1的行)

关于java - 使用 OPCFoundation/UA-Java 项目的 OPC UA 模拟器工作示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45204769/

相关文章:

java - 检查大型机 FTP 服务器上的路径是文件还是文件夹。

git - TortoiseGit修改标志(图标叠加)不更新

java - NIfi错误: OPCUA processor.服务器已停止并且无法处理任何请求

mysql - 如何使用nifi CaptureChangeMySQL?

java - 我可以对代码执行哪些操作来跳过输入文本文件的第一行?

java - 如何更新列表首选项摘要(findPreference() 不起作用)?

java - hibernate validator 4+。获取与类关联的所有约束

git - 删除所有更改并恢复到上次提交状态的 git 命令是什么?

python - 如何从 Jenkins 的特定构建中获取分支名称?

csv - 使用 NiFi 处理器对 csv 数据进行分组