WOW, ever heard about COSM

This is really very very cool. If you want to make some data public, se it graphs, make alarms and events, please have a look at COSM. The Tutorials on the site, uses CURL, but there are API's available in almost any high level language, including C# and JAVA. It is all about making data visible for a brader audience, and this site is made for that. I don't know the limits for data, but I find the concept a great inspiration. You can upload data in xml, csv or JSON. I made this small java extension (before I found the JAVA API) and it works. Be aware, you have to close the connection after every upload, so it is probably not the best solution for high resolution data, But COSM also have a solution for that, using pull instead of push.

private static void putToCosm( String timestamp, String value ){ 
        HttpURLConnection con; 
        OutputStreamWriter writer; 
        try { 
            con = (HttpURLConnection) new URL("http://api.cosm.com/v2/feeds/YOUR_ID_HERE/datastreams/datastream_id.csv").openConnection(); 
            con.setDoOutput(true); 
            con.setDoInput(true); 
            con.addRequestProperty("X-ApiKey","YOUR COSM KEY"); 
            con.setRequestMethod("PUT"); 
            writer = new OutputStreamWriter(con.getOutputStream());
            writer.write(value); 
            writer.flush(); 
            System.out.println( con.getResponseMessage() ); 
            writer.close(); 
            con.disconnect(); 

        } catch (MalformedURLException e) { 
            System.out.println( e.toString()); 
            //e.printStackTrace(); 
        } catch (IOException e) { 
            System.out.println( e.toString()); 
            //e.printStackTrace(); 
        } 
    }

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(); 
        }
    } 

} 

Site is up and running

The site is up and running with a cool metro look (yes I do think windows 8 looks really cool ). The site is build on joomla, with my own template on top.