dk.opi.io
Interface IOServer

All Known Subinterfaces:
IOServer2
All Known Implementing Classes:
OpcClientApi

public interface IOServer

This interface describes IO servers.

The example below shows how to use this interface in order to monitor values from an OPC server. The way IOServer, IOGroup and IOItem objects are created depends on the device you are monitoring, for instance OPI supplies IO packages for monitoring of both OPC servers and digital IO cards.

By consistently referring to IOServer, IOGroup and IOItem interfaces in your code, IO devices can be seamlessly interchanged, without having to change any of your code.

 import dk.opi.io.*;
 import dk.opi.io.opc.opcclient.*;
 
 public class SimpleExample implements IOListener {
   protected IOItem boolItem;
   protected IOItem intItem;
   protected IOItem stringItem;
   IOServer is = null;
 
   public static void main(String[] args) {
     SimpleExample opct = new SimpleExample();
   }
 
   public SimpleExample() {
     try {
       // Connect to OPC server and create items
       connect();
 
       // Create test group and add items to it. This object is assigned as
       // listener when values change
       IOGroup g1 = createGroup1(this);
 
       // Fire an onValueChange() event for all added items in order to
       // get the initial item values
       g1.fireAllItems(this);
 
       // Try to change value of one of the registers and note that the
       // onValueChanged() method is called whenever the value change
       boolean bVals[] = {false, true, false};
       int iVals[] = {10, 20, 30};
       String sVals[] = {"string1", "string2", "string3"};
 
       for (int i = 0; i < 3; i++) {
         try {
           System.out.println("Writing loop=" + i);
 
           boolItem.writeValue(new Boolean(bVals[i]));
           intItem.writeValue(iVals[i]);
           stringItem.writeValue(sVals[i]);
         }
         catch (RbxIOException e) {
           e.printStackTrace();
         }
 
         try {Thread.sleep(2000);} catch(Exception e){};
       }
     }
     catch (RbxIOException e) {
       e.printStackTrace();
     }
 
     // release connection to OPC server again. It's important to call this
     // method. If it's not called the OPC server will not shut down
     // automatically
     if (is != null)
       is.releaseConnection();
 
     System.exit(0);
   }
 
   // This method implements the specific details of connecting to the OPC
   // server
   public void connect() throws RbxIOException {
     // Initialize the OPC client interface to a Iconics simulated OPC server
     this.is = new OpcClientApi("ICONICS.Simulator");
   }
 
   // This method implements the specific details of adding groups and
   // items to the already open OPC server
   public IOGroup createGroup1(IOListener iol) throws RbxIOException {
     // Get the reference to the OPC Server
     OpcClientApi oa1 = (OpcClientApi)this.is;
 
     // Add a group to this server
     OpcGroup og1 = oa1.addGroup("testgrp", true, 500, 0f);
 
     // Sign up specified listener for value change events. The
     // onValueChanged() method of the listener will be callled. It's
     // important to sign up as listener before items are added, otherwise
     // notifications may be lost
     og1.addIOListener(iol);
 
     // Add three items - the item name is specific for the type of device
     // you are going to read from. The specified item name (ie.
     // "Device1.boolItem") must match the item name as it is known to the
     // OPC server.
     boolItem = og1.addItem("Device1.boolItem", null, true,
                            IOItem.DATATYPE_BOOL, "myBoolItem");
     intItem = og1.addItem("Device1.intItem", null, true,
                           IOItem.DATATYPE_INT, "myIntItem");
     stringItem = og1.addItem("Device1.stringItem", null, true,
                              IOItem.DATATYPE_STRING, "myStringItem");
 
     return(og1);
   }
 
   // Called when a data value has changed
   public void onValueChanged(IOItem i) {
     System.out.println("onValueChanged action=" + i.getActionCommand() + " " +
                        i + "=" + i.getOldValue());
   }
 }
 
The following is sample ouput generated by the above example:
 C:\>java OPCTest2
 onValueChanged action=myBoolItem Device1.boolItem=false
 onValueChanged action=myIntItem Device1.intItem=0
 onValueChanged action=myStringItem Device1.stringItem=
 Writing loop=0
 onValueChanged action=myStringItem Device1.stringItem=string1
 onValueChanged action=myIntItem Device1.intItem=10
 Writing loop=1
 onValueChanged action=myStringItem Device1.stringItem=string2
 onValueChanged action=myIntItem Device1.intItem=20
 onValueChanged action=myBoolItem Device1.boolItem=true
 Writing loop=2
 onValueChanged action=myStringItem Device1.stringItem=string3
 onValueChanged action=myIntItem Device1.intItem=30
 onValueChanged action=myBoolItem Device1.boolItem=false
 


Method Summary
 Vector getGroups()
          Return Vector of all IOGroups currently added to this server.
 void releaseConnection()
          Close client connections and release all resources.
 void releaseConnection(int msecTimeout)
          Close client connections and release all resources.
 void removeGroup(IOGroup iog)
          Remove a group from this server
 

Method Detail

getGroups

public Vector getGroups()
Return Vector of all IOGroups currently added to this server. Don't change anything in this Vector since it most likely is used by the implementing IOServer class for critical IO stuff

Returns:
Vector object containing list of all IOGroups added to this server

removeGroup

public void removeGroup(IOGroup iog)
                 throws RbxIOException
Remove a group from this server

Parameters:
iog - The group to remove
Throws:
RbxIOException - thrown if it was not possible to remove the specified group, for instance if the specified group is not a member of this server.

releaseConnection

public void releaseConnection()
Close client connections and release all resources. After this method has been executed no further read or write request can be made to the server. Create a new instance if you want to reconnect.


releaseConnection

public void releaseConnection(int msecTimeout)
                       throws RbxIOException
Close client connections and release all resources. After this method has been executed no further read or write request can be made to the server. Create a new instance if you want to reconnect.

Parameters:
msecTimeout - number of milliseconds to wait before timing out in case connection could not be released within specified timeout period. A timeout may occur in the event that the implementing class delegates the releaseConnection call to eg. an OPC server, which is currently unavailable on the network.

Even in the event of a timeout all claimed resources will be released, thus the caller should not attempt any further calls to releaseConnection(), even after a timeout

Throws:
RbxIOException - thrown in case of timeout, however connection will still be released.