mirror of
https://github.com/luoyan35714/OPC_Client.git
synced 2026-04-18 00:01:30 +08:00
Add the Utgard resources
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* This file is part of the OpenSCADA project
|
||||
* Copyright (C) 2006-2009 TH4 SYSTEMS GmbH (http://th4-systems.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
package org.openscada.opc.lib;
|
||||
|
||||
import org.jinterop.dcom.common.JIException;
|
||||
import org.openscada.opc.lib.da.DataCallback;
|
||||
import org.openscada.opc.lib.da.Item;
|
||||
import org.openscada.opc.lib.da.ItemState;
|
||||
|
||||
public class DataCallbackDumper implements DataCallback
|
||||
{
|
||||
|
||||
public void changed ( final Item item, final ItemState itemState )
|
||||
{
|
||||
System.out.println ( String.format ( "Item: %s, Value: %s, Timestamp: %tc, Quality: %d", item.getId (), itemState.getValue (), itemState.getTimestamp (), itemState.getQuality () ) );
|
||||
|
||||
try
|
||||
{
|
||||
VariantDumper.dumpValue ( "\t", itemState.getValue () );
|
||||
}
|
||||
catch ( final JIException e )
|
||||
{
|
||||
e.printStackTrace ();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* This file is part of the OpenSCADA project
|
||||
* Copyright (C) 2006-2009 TH4 SYSTEMS GmbH (http://th4-systems.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
package org.openscada.opc.lib;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.jinterop.dcom.common.JIException;
|
||||
import org.openscada.opc.lib.common.ConnectionInformation;
|
||||
import org.openscada.opc.lib.da.Group;
|
||||
import org.openscada.opc.lib.da.Item;
|
||||
import org.openscada.opc.lib.da.ItemState;
|
||||
import org.openscada.opc.lib.da.Server;
|
||||
import org.openscada.opc.lib.da.browser.Branch;
|
||||
import org.openscada.opc.lib.da.browser.Leaf;
|
||||
|
||||
public class OPCTest1
|
||||
{
|
||||
public static void dumpItemState ( final Item item, final ItemState state )
|
||||
{
|
||||
System.out.println ( String.format ( "Item: %s, Value: %s, Timestamp: %tc, Quality: %d", item.getId (), state.getValue (), state.getTimestamp (), state.getQuality () ) );
|
||||
}
|
||||
|
||||
public static void dumpTree ( final Branch branch, final int level )
|
||||
{
|
||||
final StringBuilder sb = new StringBuilder ();
|
||||
for ( int i = 0; i < level; i++ )
|
||||
{
|
||||
sb.append ( " " );
|
||||
}
|
||||
final String indent = sb.toString ();
|
||||
|
||||
for ( final Leaf leaf : branch.getLeaves () )
|
||||
{
|
||||
System.out.println ( indent + "Leaf: " + leaf.getName () + " [" + leaf.getItemId () + "]" );
|
||||
}
|
||||
for ( final Branch subBranch : branch.getBranches () )
|
||||
{
|
||||
System.out.println ( indent + "Branch: " + subBranch.getName () );
|
||||
dumpTree ( subBranch, level + 1 );
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings ( "unused" )
|
||||
public static void main ( final String[] args ) throws Throwable
|
||||
{
|
||||
// create connection information
|
||||
final ConnectionInformation ci = new ConnectionInformation ();
|
||||
ci.setHost ( args[0] );
|
||||
ci.setDomain ( args[1] );
|
||||
ci.setUser ( args[2] );
|
||||
ci.setPassword ( args[3] );
|
||||
ci.setClsid ( args[4] );
|
||||
|
||||
// create a new server
|
||||
final Server server = new Server ( ci, Executors.newSingleThreadScheduledExecutor () );
|
||||
try
|
||||
{
|
||||
// connect to server
|
||||
server.connect ();
|
||||
|
||||
// browse
|
||||
dumpTree ( server.getTreeBrowser ().browse (), 0 );
|
||||
|
||||
// add sync reader
|
||||
|
||||
// Add a new group
|
||||
Group group = server.addGroup ( "test" );
|
||||
// group is initially active ... just for demonstration
|
||||
group.setActive ( true );
|
||||
|
||||
// We already have our group ... just for demonstration
|
||||
group = server.findGroup ( "test" );
|
||||
|
||||
// Add a new item to the group
|
||||
final Item item = group.addItem ( "Saw-toothed Waves.Int2" );
|
||||
// Items are initially active ... just for demonstration
|
||||
item.setActive ( true );
|
||||
|
||||
// Add some more items ... including one that is already existing
|
||||
final Map<String, Item> items = group.addItems ( "Saw-toothed Waves.Int2", "Saw-toothed Waves.Int4" );
|
||||
|
||||
// sync-read some values
|
||||
for ( int i = 0; i < 10; i++ )
|
||||
{
|
||||
Thread.sleep ( 100 );
|
||||
dumpItemState ( item, item.read ( false ) );
|
||||
}
|
||||
}
|
||||
catch ( final JIException e )
|
||||
{
|
||||
System.out.println ( String.format ( "%08X: %s", e.getErrorCode (), server.getErrorMessage ( e.getErrorCode () ) ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* This file is part of the OpenSCADA project
|
||||
* Copyright (C) 2006-2009 TH4 SYSTEMS GmbH (http://th4-systems.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
package org.openscada.opc.lib;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.jinterop.dcom.common.JIException;
|
||||
import org.openscada.opc.lib.common.ConnectionInformation;
|
||||
import org.openscada.opc.lib.da.AccessBase;
|
||||
import org.openscada.opc.lib.da.Server;
|
||||
import org.openscada.opc.lib.da.SyncAccess;
|
||||
|
||||
/**
|
||||
* Another test showing the "Access" interface with the SyncAccess implementation.
|
||||
* @author Jens Reimann <jens.reimann@th4-systems.com>
|
||||
*
|
||||
*/
|
||||
public class OPCTest2
|
||||
{
|
||||
public static void main ( final String[] args ) throws Throwable
|
||||
{
|
||||
// create connection information
|
||||
final ConnectionInformation ci = new ConnectionInformation ();
|
||||
ci.setHost ( args[0] );
|
||||
ci.setDomain ( args[1] );
|
||||
ci.setUser ( args[2] );
|
||||
ci.setPassword ( args[3] );
|
||||
ci.setClsid ( args[4] );
|
||||
|
||||
String itemId = "Saw-toothed Waves.Int2";
|
||||
if ( args.length >= 6 )
|
||||
{
|
||||
itemId = args[5];
|
||||
}
|
||||
|
||||
// create a new server
|
||||
final Server server = new Server ( ci, Executors.newSingleThreadScheduledExecutor () );
|
||||
try
|
||||
{
|
||||
// connect to server
|
||||
server.connect ();
|
||||
|
||||
// add sync access
|
||||
|
||||
final AccessBase access = new SyncAccess ( server, 100 );
|
||||
access.addItem ( itemId, new DataCallbackDumper () );
|
||||
|
||||
// start reading
|
||||
access.bind ();
|
||||
|
||||
// wait a little bit
|
||||
Thread.sleep ( 10 * 1000 );
|
||||
|
||||
// stop reading
|
||||
access.unbind ();
|
||||
}
|
||||
catch ( final JIException e )
|
||||
{
|
||||
System.out.println ( String.format ( "%08X: %s", e.getErrorCode (), server.getErrorMessage ( e.getErrorCode () ) ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* This file is part of the OpenSCADA project
|
||||
* Copyright (C) 2006-2009 TH4 SYSTEMS GmbH (http://th4-systems.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
package org.openscada.opc.lib;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.jinterop.dcom.common.JIException;
|
||||
import org.openscada.opc.lib.common.ConnectionInformation;
|
||||
import org.openscada.opc.lib.da.Server;
|
||||
import org.openscada.opc.lib.da.browser.BaseBrowser;
|
||||
import org.openscada.opc.lib.da.browser.Branch;
|
||||
import org.openscada.opc.lib.da.browser.Leaf;
|
||||
import org.openscada.opc.lib.da.browser.TreeBrowser;
|
||||
|
||||
/**
|
||||
* Another test showing the browser interfaces
|
||||
* @author Jens Reimann <jens.reimann@th4-systems.com>
|
||||
*
|
||||
*/
|
||||
public class OPCTest3
|
||||
{
|
||||
|
||||
private static void dumpLeaf ( final String prefix, final Leaf leaf )
|
||||
{
|
||||
System.out.println ( prefix + "Leaf: " + leaf.getName () + " [" + leaf.getItemId () + "]" );
|
||||
}
|
||||
|
||||
private static void dumpBranch ( final String prefix, final Branch branch )
|
||||
{
|
||||
System.out.println ( prefix + "Branch: " + branch.getName () );
|
||||
}
|
||||
|
||||
public static void dumpTree ( final Branch branch, final int level )
|
||||
{
|
||||
final StringBuilder sb = new StringBuilder ();
|
||||
for ( int i = 0; i < level; i++ )
|
||||
{
|
||||
sb.append ( " " );
|
||||
}
|
||||
final String indent = sb.toString ();
|
||||
|
||||
for ( final Leaf leaf : branch.getLeaves () )
|
||||
{
|
||||
dumpLeaf ( indent, leaf );
|
||||
}
|
||||
for ( final Branch subBranch : branch.getBranches () )
|
||||
{
|
||||
dumpBranch ( indent, subBranch );
|
||||
dumpTree ( subBranch, level + 1 );
|
||||
}
|
||||
}
|
||||
|
||||
public static void main ( final String[] args ) throws Throwable
|
||||
{
|
||||
// create connection information
|
||||
final ConnectionInformation ci = new ConnectionInformation ();
|
||||
ci.setHost ( args[0] );
|
||||
ci.setDomain ( args[1] );
|
||||
ci.setUser ( args[2] );
|
||||
ci.setPassword ( args[3] );
|
||||
ci.setClsid ( args[4] );
|
||||
|
||||
// create a new server
|
||||
final Server server = new Server ( ci, Executors.newSingleThreadScheduledExecutor () );
|
||||
try
|
||||
{
|
||||
// connect to server
|
||||
server.connect ();
|
||||
|
||||
// browse flat
|
||||
final BaseBrowser flatBrowser = server.getFlatBrowser ();
|
||||
if ( flatBrowser != null )
|
||||
{
|
||||
for ( final String item : server.getFlatBrowser ().browse ( "" ) )
|
||||
{
|
||||
System.out.println ( item );
|
||||
}
|
||||
}
|
||||
|
||||
// browse tree
|
||||
final TreeBrowser treeBrowser = server.getTreeBrowser ();
|
||||
if ( treeBrowser != null )
|
||||
{
|
||||
dumpTree ( treeBrowser.browse (), 0 );
|
||||
}
|
||||
|
||||
// browse tree manually
|
||||
browseTree ( "", treeBrowser, new Branch () );
|
||||
}
|
||||
catch ( final JIException e )
|
||||
{
|
||||
e.printStackTrace ();
|
||||
System.out.println ( String.format ( "%08X: %s", e.getErrorCode (), server.getErrorMessage ( e.getErrorCode () ) ) );
|
||||
}
|
||||
}
|
||||
|
||||
private static void browseTree ( final String prefix, final TreeBrowser treeBrowser, final Branch branch ) throws IllegalArgumentException, UnknownHostException, JIException
|
||||
{
|
||||
treeBrowser.fillLeaves ( branch );
|
||||
treeBrowser.fillBranches ( branch );
|
||||
|
||||
for ( final Leaf leaf : branch.getLeaves () )
|
||||
{
|
||||
dumpLeaf ( "M - " + prefix + " ", leaf );
|
||||
}
|
||||
for ( final Branch subBranch : branch.getBranches () )
|
||||
{
|
||||
dumpBranch ( "M - " + prefix + " ", subBranch );
|
||||
browseTree ( prefix + " ", treeBrowser, subBranch );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* This file is part of the OpenSCADA project
|
||||
* Copyright (C) 2006-2009 TH4 SYSTEMS GmbH (http://th4-systems.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
package org.openscada.opc.lib;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.jinterop.dcom.common.JIException;
|
||||
import org.openscada.opc.lib.common.ConnectionInformation;
|
||||
import org.openscada.opc.lib.da.AccessBase;
|
||||
import org.openscada.opc.lib.da.Async20Access;
|
||||
import org.openscada.opc.lib.da.Server;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Another test showing the "Access" interface with the Async20Access implementation.
|
||||
* @author Jens Reimann <jens.reimann@th4-systems.com>
|
||||
*
|
||||
*/
|
||||
public class OPCTest4
|
||||
{
|
||||
private static Logger _log = LoggerFactory.getLogger ( OPCTest4.class );
|
||||
|
||||
public static void main ( final String[] args ) throws Throwable
|
||||
{
|
||||
// create connection information
|
||||
final ConnectionInformation ci = new ConnectionInformation ();
|
||||
ci.setHost ( args[0] );
|
||||
ci.setDomain ( args[1] );
|
||||
ci.setUser ( args[2] );
|
||||
ci.setPassword ( args[3] );
|
||||
ci.setClsid ( args[4] );
|
||||
|
||||
final Set<String> items = new HashSet<String> ();
|
||||
for ( int i = 5; i < args.length; i++ )
|
||||
{
|
||||
items.add ( args[i] );
|
||||
}
|
||||
if ( items.isEmpty () )
|
||||
{
|
||||
items.add ( "Saw-toothed Waves.Int2" );
|
||||
}
|
||||
|
||||
// create a new server
|
||||
final Server server = new Server ( ci, Executors.newSingleThreadScheduledExecutor () );
|
||||
try
|
||||
{
|
||||
// connect to server
|
||||
server.connect ();
|
||||
|
||||
// add sync access
|
||||
final AccessBase access = new Async20Access ( server, 100, false );
|
||||
for ( final String itemId : items )
|
||||
{
|
||||
access.addItem ( itemId, new DataCallbackDumper () );
|
||||
}
|
||||
|
||||
// start reading
|
||||
access.bind ();
|
||||
|
||||
// wait a little bit
|
||||
_log.info ( "Sleep for some seconds to give events a chance..." );
|
||||
Thread.sleep ( 10 * 1000 );
|
||||
_log.info ( "Returned from sleep" );
|
||||
|
||||
// stop reading
|
||||
access.unbind ();
|
||||
}
|
||||
catch ( final JIException e )
|
||||
{
|
||||
System.out.println ( String.format ( "%08X: %s", e.getErrorCode (), server.getErrorMessage ( e.getErrorCode () ) ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* This file is part of the OpenSCADA project
|
||||
* Copyright (C) 2006-2009 TH4 SYSTEMS GmbH (http://th4-systems.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
package org.openscada.opc.lib;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.jinterop.dcom.common.JIException;
|
||||
import org.openscada.opc.lib.common.ConnectionInformation;
|
||||
import org.openscada.opc.lib.da.Async20Access;
|
||||
import org.openscada.opc.lib.da.Server;
|
||||
|
||||
/**
|
||||
* Another test showing the "Access" interface with
|
||||
* the Async20Access implementation. Testing two connections at the same time.
|
||||
*
|
||||
* @author Jens Reimann <jens.reimann@th4-systems.com>
|
||||
*/
|
||||
public class OPCTest5
|
||||
{
|
||||
public static void main ( final String[] args ) throws Throwable
|
||||
{
|
||||
// create connection information
|
||||
final ConnectionInformation baseInfo = new ConnectionInformation ();
|
||||
baseInfo.setHost ( args[0] );
|
||||
baseInfo.setDomain ( args[1] );
|
||||
baseInfo.setUser ( args[2] );
|
||||
baseInfo.setPassword ( args[3] );
|
||||
|
||||
final List<OPCTestInfo> testInfo = new LinkedList<OPCTestInfo> ();
|
||||
int i = 0;
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
while ( args.length > i * 2 + 4 )
|
||||
{
|
||||
final ConnectionInformation ci = new ConnectionInformation ( baseInfo );
|
||||
ci.setClsid ( args[i * 2 + 4] );
|
||||
final OPCTestInfo ti = new OPCTestInfo ();
|
||||
ti._info = ci;
|
||||
ti._itemId = args[i * 2 + 5];
|
||||
ti._server = new Server ( ci, Executors.newSingleThreadScheduledExecutor () );
|
||||
|
||||
ti._server.connect ();
|
||||
ti._access = new Async20Access ( ti._server, 100, false );
|
||||
ti._access.addItem ( ti._itemId, new DataCallbackDumper () );
|
||||
ti._access.bind ();
|
||||
|
||||
testInfo.add ( ti );
|
||||
i++;
|
||||
}
|
||||
|
||||
// wait a little bit
|
||||
Thread.sleep ( 10 * 1000 );
|
||||
}
|
||||
catch ( final JIException e )
|
||||
{
|
||||
System.out.println ( String.format ( "%08X", e.getErrorCode () ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* This file is part of the OpenSCADA project
|
||||
* Copyright (C) 2006-2009 TH4 SYSTEMS GmbH (http://th4-systems.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
package org.openscada.opc.lib;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.jinterop.dcom.common.JIException;
|
||||
import org.openscada.opc.lib.common.ConnectionInformation;
|
||||
import org.openscada.opc.lib.da.AccessBase;
|
||||
import org.openscada.opc.lib.da.AutoReconnectController;
|
||||
import org.openscada.opc.lib.da.Server;
|
||||
import org.openscada.opc.lib.da.SyncAccess;
|
||||
|
||||
/**
|
||||
* Another test showing the "Access" interface with the SyncAccess implementation
|
||||
* including the AutoReconnectController which should re-establish the connection
|
||||
* if it breaks.
|
||||
* @author Jens Reimann <jens.reimann@th4-systems.com>
|
||||
*
|
||||
*/
|
||||
public class OPCTest6
|
||||
{
|
||||
public static void main ( final String[] args ) throws Throwable
|
||||
{
|
||||
// create connection information
|
||||
final ConnectionInformation ci = new ConnectionInformation ();
|
||||
ci.setHost ( args[0] );
|
||||
ci.setDomain ( args[1] );
|
||||
ci.setUser ( args[2] );
|
||||
ci.setPassword ( args[3] );
|
||||
ci.setClsid ( args[4] );
|
||||
|
||||
String itemId = "Saw-toothed Waves.Int2";
|
||||
if ( args.length >= 6 )
|
||||
{
|
||||
itemId = args[5];
|
||||
}
|
||||
|
||||
// create a new server
|
||||
final Server server = new Server ( ci, Executors.newSingleThreadScheduledExecutor () );
|
||||
final AutoReconnectController autoReconnectController = new AutoReconnectController ( server );
|
||||
try
|
||||
{
|
||||
// connect to server
|
||||
autoReconnectController.connect ();
|
||||
|
||||
// add sync access
|
||||
|
||||
final AccessBase access = new SyncAccess ( server, 100 );
|
||||
access.addItem ( itemId, new DataCallbackDumper () );
|
||||
|
||||
// start reading
|
||||
access.bind ();
|
||||
|
||||
// run forever
|
||||
final boolean running = true;
|
||||
while ( running )
|
||||
{
|
||||
Thread.sleep ( 10 * 1000 );
|
||||
}
|
||||
|
||||
/*
|
||||
// stop reading
|
||||
access.unbind ();
|
||||
|
||||
// disconnect
|
||||
autoReconnectController.disconnect ();
|
||||
*/
|
||||
}
|
||||
catch ( final JIException e )
|
||||
{
|
||||
System.out.println ( String.format ( "%08X: %s", e.getErrorCode (), server.getErrorMessage ( e.getErrorCode () ) ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* This file is part of the OpenSCADA project
|
||||
* Copyright (C) 2006-2009 TH4 SYSTEMS GmbH (http://th4-systems.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
package org.openscada.opc.lib;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.jinterop.dcom.common.JIException;
|
||||
import org.jinterop.dcom.core.JIArray;
|
||||
import org.jinterop.dcom.core.JIFlags;
|
||||
import org.jinterop.dcom.core.JIString;
|
||||
import org.jinterop.dcom.core.JIVariant;
|
||||
import org.openscada.opc.lib.common.ConnectionInformation;
|
||||
import org.openscada.opc.lib.da.Group;
|
||||
import org.openscada.opc.lib.da.Item;
|
||||
import org.openscada.opc.lib.da.ItemState;
|
||||
import org.openscada.opc.lib.da.Server;
|
||||
|
||||
/**
|
||||
* A sample that reads an item and writes back the result. You will need a
|
||||
* read/write item for this to work.
|
||||
* @author Jens Reimann <jens.reimann@th4-systems.com>
|
||||
*
|
||||
*/
|
||||
public class OPCTest7
|
||||
{
|
||||
@SuppressWarnings ( "unused" )
|
||||
public static void main ( final String[] args ) throws Throwable
|
||||
{
|
||||
// create connection information
|
||||
final ConnectionInformation ci = new ConnectionInformation ();
|
||||
ci.setHost ( args[0] );
|
||||
ci.setDomain ( args[1] );
|
||||
ci.setUser ( args[2] );
|
||||
ci.setPassword ( args[3] );
|
||||
ci.setClsid ( args[4] );
|
||||
|
||||
final String itemName = args[5];
|
||||
|
||||
// create a new server
|
||||
final Server server = new Server ( ci, Executors.newSingleThreadScheduledExecutor () );
|
||||
try
|
||||
{
|
||||
// connect to server
|
||||
server.connect ();
|
||||
|
||||
// Add a new group
|
||||
final Group group = server.addGroup ( "test" );
|
||||
|
||||
// Add a new item to the group
|
||||
final Item item = group.addItem ( itemName );
|
||||
|
||||
final JIString[] sdata = new JIString[] { new JIString ( "ab", JIFlags.FLAG_REPRESENTATION_STRING_BSTR ), new JIString ( "cd", JIFlags.FLAG_REPRESENTATION_STRING_BSTR ), new JIString ( "ef", JIFlags.FLAG_REPRESENTATION_STRING_BSTR ) };
|
||||
final Double[] ddata = new Double[] { 1.1, 2.2, 3.3 };
|
||||
final Boolean[] bdata = new Boolean[] { true, false, true, false };
|
||||
final Integer[] idata = new Integer[] { 1202, 1203, 1204 };
|
||||
final Long[] ldata = new Long[] { 12020001L, 12030001L, 12040001L };
|
||||
final Float[] fdata = new Float[] { 1.1f, 1.2f, 1.3f };
|
||||
final Byte[] bydata = new Byte[] { 1, 2, 3 };
|
||||
final Character[] cdata = new Character[] { 'A', 'B', 'C' };
|
||||
|
||||
final JIArray array = new JIArray ( ddata, true );
|
||||
final JIVariant value = new JIVariant ( array );
|
||||
|
||||
System.out.println ( "============= PHASE 1 ============= " );
|
||||
|
||||
// dump the value
|
||||
VariantDumper.dumpValue ( value );
|
||||
|
||||
System.out.println ( "============= PHASE 2 ============= " );
|
||||
|
||||
// now write it to the item
|
||||
item.write ( value );
|
||||
Thread.sleep ( 2500 );
|
||||
|
||||
System.out.println ( "============= PHASE 3 ============= " );
|
||||
|
||||
// now read the value back and dump it
|
||||
final ItemState itemState = item.read ( true );
|
||||
VariantDumper.dumpValue ( itemState.getValue () );
|
||||
|
||||
System.out.println ( "============= PHASE 4 ============= " );
|
||||
|
||||
// and write the value just read in
|
||||
item.write ( itemState.getValue () );
|
||||
|
||||
System.out.println ( "============= COMPLETE ============= " );
|
||||
}
|
||||
catch ( final JIException e )
|
||||
{
|
||||
System.out.println ( String.format ( "%08X: %s", e.getErrorCode (), server.getErrorMessage ( e.getErrorCode () ) ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* This file is part of the OpenSCADA project
|
||||
* Copyright (C) 2006-2009 TH4 SYSTEMS GmbH (http://th4-systems.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
package org.openscada.opc.lib;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.jinterop.dcom.common.JIException;
|
||||
import org.openscada.opc.dcom.list.ClassDetails;
|
||||
import org.openscada.opc.lib.list.Categories;
|
||||
import org.openscada.opc.lib.list.Category;
|
||||
import org.openscada.opc.lib.list.ServerList;
|
||||
|
||||
/**
|
||||
* A sample that queries the server browser interface
|
||||
* @author Jens Reimann <jens.reimann@th4-systems.com>
|
||||
*
|
||||
*/
|
||||
public class OPCTest8
|
||||
{
|
||||
protected static void showDetails ( final ServerList serverList, final String clsid ) throws JIException
|
||||
{
|
||||
final ClassDetails cd = serverList.getDetails ( clsid );
|
||||
if ( cd != null )
|
||||
{
|
||||
System.out.println ( cd.getProgId () + " = " + cd.getDescription () );
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println ( "unknown" );
|
||||
}
|
||||
}
|
||||
|
||||
public static void main ( final String[] args ) throws Throwable
|
||||
{
|
||||
final ServerList serverList = new ServerList ( args[0], args[2], args[3], args[1] );
|
||||
|
||||
final String cls = serverList.getClsIdFromProgId ( "Matrikon.OPC.Simulation.1" );
|
||||
System.out.println ( "Matrikon OPC Simulation Server: " + cls );
|
||||
showDetails ( serverList, cls );
|
||||
|
||||
final Collection<ClassDetails> detailsList = serverList.listServersWithDetails ( new Category[] { Categories.OPCDAServer20 }, new Category[] {} );
|
||||
|
||||
for ( final ClassDetails details : detailsList )
|
||||
{
|
||||
System.out.println ( String.format ( "Found: %s", details.getClsId () ) );
|
||||
System.out.println ( String.format ( "\tProgID: %s", details.getProgId () ) );
|
||||
System.out.println ( String.format ( "\tDescription: %s", details.getDescription () ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* This file is part of the OpenSCADA project
|
||||
* Copyright (C) 2006-2009 TH4 SYSTEMS GmbH (http://th4-systems.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
package org.openscada.opc.lib;
|
||||
|
||||
import org.openscada.opc.lib.common.ConnectionInformation;
|
||||
import org.openscada.opc.lib.da.AccessBase;
|
||||
import org.openscada.opc.lib.da.Server;
|
||||
|
||||
class OPCTestInfo
|
||||
{
|
||||
ConnectionInformation _info = null;
|
||||
|
||||
String _itemId = null;
|
||||
|
||||
Server _server = null;
|
||||
|
||||
AccessBase _access = null;
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
* This file is part of the OpenSCADA project
|
||||
* Copyright (C) 2006-2009 TH4 SYSTEMS GmbH (http://th4-systems.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
package org.openscada.opc.lib;
|
||||
|
||||
import org.jinterop.dcom.common.JIException;
|
||||
import org.jinterop.dcom.core.JIArray;
|
||||
import org.jinterop.dcom.core.JIFlags;
|
||||
import org.jinterop.dcom.core.JIString;
|
||||
import org.jinterop.dcom.core.JIVariant;
|
||||
|
||||
public class VariantDumper
|
||||
{
|
||||
|
||||
static protected void dumpArray ( final String prefix, final JIArray array ) throws JIException
|
||||
{
|
||||
System.out.println ( prefix + String.format ( "IsConformant: %s, IsVarying: %s", array.isConformant () ? "yes" : "no", array.isVarying () ? "yes" : "no" ) );
|
||||
System.out.println ( prefix + String.format ( "Dimensions: %d", array.getDimensions () ) );
|
||||
for ( int i = 0; i < array.getDimensions (); i++ )
|
||||
{
|
||||
System.out.println ( prefix + String.format ( "Dimension #%d: Upper Bound: %d", i, array.getUpperBounds ()[i] ) );
|
||||
}
|
||||
|
||||
final Object o = array.getArrayInstance ();
|
||||
System.out.println ( prefix + "Array Instance: " + o.getClass () );
|
||||
final Object[] a = (Object[])o;
|
||||
System.out.println ( prefix + "Array Size: " + a.length );
|
||||
|
||||
for ( final Object value : a )
|
||||
{
|
||||
dumpValue ( prefix + "\t", value );
|
||||
}
|
||||
}
|
||||
|
||||
static public void dumpValue ( final Object value ) throws JIException
|
||||
{
|
||||
dumpValue ( "", value );
|
||||
}
|
||||
|
||||
static protected void dumpValue ( final String prefix, final Object value ) throws JIException
|
||||
{
|
||||
if ( value instanceof JIVariant )
|
||||
{
|
||||
System.out.println ( prefix + "JIVariant" );
|
||||
final JIVariant variant = (JIVariant)value;
|
||||
System.out.println ( prefix + String.format ( "IsArray: %s, IsByRef: %s, IsNull: %s", variant.isArray () ? "yes" : "no", variant.isByRefFlagSet () ? "yes" : "no", variant.isNull () ? "yes" : "no" ) );
|
||||
|
||||
if ( variant.isArray () )
|
||||
{
|
||||
dumpArray ( prefix, variant.getObjectAsArray () );
|
||||
}
|
||||
else
|
||||
{
|
||||
dumpValue ( prefix + "\t", variant.getObject () );
|
||||
}
|
||||
}
|
||||
else if ( value instanceof JIString )
|
||||
{
|
||||
final JIString string = (JIString)value;
|
||||
|
||||
String strType;
|
||||
switch ( string.getType () )
|
||||
{
|
||||
case JIFlags.FLAG_REPRESENTATION_STRING_BSTR:
|
||||
strType = "BSTR";
|
||||
break;
|
||||
case JIFlags.FLAG_REPRESENTATION_STRING_LPCTSTR:
|
||||
strType = "LPCSTR";
|
||||
break;
|
||||
case JIFlags.FLAG_REPRESENTATION_STRING_LPWSTR:
|
||||
strType = "LPWSTR";
|
||||
break;
|
||||
default:
|
||||
strType = "unknown";
|
||||
break;
|
||||
}
|
||||
System.out.println ( prefix + String.format ( "JIString: '%s' (%s)", string.getString (), strType ) );
|
||||
}
|
||||
else if ( value instanceof Double )
|
||||
{
|
||||
System.out.println ( prefix + "Double: " + value );
|
||||
}
|
||||
else if ( value instanceof Float )
|
||||
{
|
||||
System.out.println ( prefix + "Float: " + value );
|
||||
}
|
||||
else if ( value instanceof Byte )
|
||||
{
|
||||
System.out.println ( prefix + "Byte: " + value );
|
||||
}
|
||||
else if ( value instanceof Character )
|
||||
{
|
||||
System.out.println ( prefix + "Character: " + value );
|
||||
}
|
||||
else if ( value instanceof Integer )
|
||||
{
|
||||
System.out.println ( prefix + "Integer: " + value );
|
||||
}
|
||||
else if ( value instanceof Short )
|
||||
{
|
||||
System.out.println ( prefix + "Short: " + value );
|
||||
}
|
||||
else if ( value instanceof Long )
|
||||
{
|
||||
System.out.println ( prefix + "Long: " + value );
|
||||
}
|
||||
else if ( value instanceof Boolean )
|
||||
{
|
||||
System.out.println ( prefix + "Boolean: " + value );
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
System.out.println ( prefix + String.format ( "Unknown value type (%s): %s", value.getClass (), value.toString () ) );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user