Simple java client with monitored item

Playing around with client programming, which is great fun. This is an example of a very rudimentary JAVA client. But it works, right out of the box. Remember to set the security level accrodingly. And also to set the build path correctly, with the API as external jars.

The small piece of code is made with the API from http://www.prosysopc.com/

package dk.opcua; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.URISyntaxException; 
import java.util.EnumSet; 
import java.util.Iterator; 
import java.util.Locale; 

import org.opcfoundation.ua.builtintypes.DataValue; 
import org.opcfoundation.ua.builtintypes.LocalizedText; 
import org.opcfoundation.ua.builtintypes.NodeId; 
import org.opcfoundation.ua.core.ApplicationDescription; 
import org.opcfoundation.ua.core.ApplicationType; 
import org.opcfoundation.ua.core.Attributes; 
import org.opcfoundation.ua.core.MonitoringMode; 
import org.opcfoundation.ua.transport.security.Cert; 
import org.opcfoundation.ua.transport.security.SecurityMode; 
import com.prosysopc.ua.ApplicationIdentity; 
import com.prosysopc.ua.CertificateValidationListener; 
import com.prosysopc.ua.PkiFileBasedCertificateValidator; 
import com.prosysopc.ua.PkiFileBasedCertificateValidator.CertificateCheck;
import com.prosysopc.ua.PkiFileBasedCertificateValidator.ValidationResult;
import com.prosysopc.ua.SecureIdentityException; 
import com.prosysopc.ua.ServiceException; 
import com.prosysopc.ua.SessionActivationException; 
import com.prosysopc.ua.StatusException; 
import com.prosysopc.ua.client.InvalidServerEndpointException;
import com.prosysopc.ua.client.MonitoredDataItem; 
import com.prosysopc.ua.client.MonitoredDataItemListener; 
import com.prosysopc.ua.client.MonitoredItem; 
import com.prosysopc.ua.client.Subscription; 
import com.prosysopc.ua.client.UaClient; 

public class OpcUaSimpleClient {
     
    private  UaClient client; 
     
    /** * @param args */ 
    public static void main(String[] args) {
        OpcUaSimpleClient ouct = new OpcUaSimpleClient(); 
        ouct.clientTest(args[0], Integer.parseInt(args[1]), args[2]);
    } 
     
    private static MonitoredDataItemListener dataChangeListener = new MonitoredDataItemListener() {
        @Override 
        public void onDataChange(MonitoredDataItem sender, DataValue prevValue, DataValue value) {
            MonitoredItem i = sender;  
            System.out.println(i.getNodeId().toString()+ " TS: "+ value.getSourceTimestamp()+" Value: "+ value.getValue().toNumber()); 
        } }; 
         
    private static CertificateValidationListener validationListener = new
         CertificateValidationListener() { 

                @Override 
                public ValidationResult onValidate(Cert arg0,ApplicationDescription arg1,EnumSet<CertificateCheck> arg2) {
                    // TODO Auto-generated method stub 
                    System.out.println("1: " + arg0.certificate.getIssuerDN()); 
                    Iterator it = arg2.iterator(); 
                    CertificateCheck cc; 
                    while( it.hasNext()){ 
                         
                            System.out.println("2: " + it.next().toString() ); 
                    } 
                     
                    System.out.println("3: " + arg1.getApplicationType()); 
                    System.out.println("4: " + arg1.getApplicationName()); 
                    System.out.println("5: " + arg1.getApplicationUri()); 
                    System.out.println("6: " + arg0.getEncodedThumbprint().toString()); 
                    return ValidationResult.AcceptPermanently;
                }}; 
                 
     
     
    public void clientTest( String serverUrn, int ns, String nodeId)
    {     
        try  
        { 
             
            client = new UaClient(serverUrn); 
            final PkiFileBasedCertificateValidator validator = new PkiFileBasedCertificateValidator();
            validator.setValidationListener(validationListener); 
            client.setCertificateValidator(validator); 
            ApplicationDescription appDescription = new ApplicationDescription(); 
            appDescription.setApplicationName(new LocalizedText("SampleConsoleClient",Locale.ENGLISH)); 
                    appDescription.setApplicationUri("urn:localhost:UA:SampleConsoleClient"); 
                    appDescription.setProductUri("urn:prosysopc.com:UA:SampleConsoleClient"); 
                    appDescription.setApplicationType(ApplicationType.Client); 
             
            final ApplicationIdentity identity =  
                ApplicationIdentity.loadOrCreateCertificate(appDescription, "Sample Organisation", "",new File(validator.getBaseDir(), "private"),true); 
            client.setApplicationIdentity(identity); 
            client.setLocale(Locale.ENGLISH); 
            client.setTimeout(30000); 
            client.setSecurityMode(SecurityMode.NONE); 
             
             
             
             
            client.connect(); 
            String test = client.getHost(); 
            System.out.println( test ); 
            NodeId nid = new NodeId(ns,nodeId); 
             
            Subscription subscription = new Subscription(); 
             
            MonitoredDataItem item = new MonitoredDataItem(nid,Attributes.Value, MonitoringMode.Reporting);
            subscription.addItem(item); 
            client.addSubscription(subscription); 
            item.addChangeListener(dataChangeListener); 
            while(true){ 
                BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
                String s = null; 
                try { 
                    s = stdin.readLine(); 
                } catch (IOException e) { 
                    System.out.println(e); 
                } 
                if( s.equals("q") ) 
                { 
                    client.disconnect(); 
                    break; 
                } 
                Thread.sleep(500); 
            } 
             
        } catch (InvalidServerEndpointException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } catch (SessionActivationException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } catch (ServiceException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } catch (StatusException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } catch (InterruptedException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } catch (SecureIdentityException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } catch (IOException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
        catch (URISyntaxException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        }
    } 

}