Reputation: 3349
I found out the code in the Utgard documentation to access signals individually by defining their callback functions.
server.connect();
// add sync access, poll every 500 ms
final AccessBase access = new SyncAccess(server, 500);
access.addItem(itemId, new DataCallback() {
@Override
public void changed(Item item, ItemState state) {
System.out.println(state);
}
});
// start reading
access.bind();
// Sleeping thread infinitely to listen continuously
while(true){
Thread.sleep(10 * 1000);
}
// never comes here
access.unbind();
But, in my application I need to get signals in order of 1000s. Hence, defining 1000 callback functions wouldn't be a good approach to handle such large amount of signals.
Is their any way to get values of all 1000 signals in a single callback function ?
Please throw your views/opinions & enlighten me. Thanks !
Upvotes: 2
Views: 440
Reputation: 1
package com.freud.dcom.utgard.cases;
import com.freud.opc.utgard.BaseConfiguration;
import org.jinterop.dcom.common.JIException;
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 java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Executors;
/**
* 同步读取某个点位的值
*
* @author cc1500
*
*/
public class OpcValuesByItemIDs {
private static HashMap<String, Float> map3= new HashMap<>();
private static OpcValuesByItemIDs test =new OpcValuesByItemIDs();;
private static int wai=1000;
private static ItemState itemall;
private static Server server;
private static Group group ;
private static int Quality ,count,countarr;
public static void main(String[] args) throws Exception {
OpcCc( );
}
public static Server CreOpcCc( ) throws InterruptedException {
try {
server = new Server(
BaseConfiguration.getCLSIDConnectionInfomation(),
Executors.newSingleThreadScheduledExecutor());
server.connect();
group = server.addGroup();
}
catch ( Exception ee ) {
System.out.println ( "1 重新连接....1" );
synchronized(test) {
test.wait(10000);
}
CreOpcCc( );
}
return server;
}
public static void OpcCc( ) throws Exception {
CreOpcCc( );
try {
GetMapByFloa ssa= new GetMapByFloa();
///**
* Flat形式获取Item的信息 //GetMapByFloa
*/
map3= ssa.GetMapByFloa();
String[] vedioPics = new String[map3.size()];
int icon=0;
for (String key : map3.keySet()) {
vedioPics[icon] = key;
icon++;
}
Map<String, Item> items = group.addItems(vedioPics);
while (true) {
synchronized(test) {
test.wait(wai);
}
long startTime = System.currentTimeMillis();
countarr=1;
loop: for (Entry<String, Item> temp : items.entrySet()) {
dumpItem(temp.getValue());
countarr++;
if( wai==10000) {
break loop;
}
}
long endTime = System.currentTimeMillis();
System.out.println("===============================================");
System.out.println("轮询时间:(" + (endTime - startTime) + " + "+wai+")ms");
System.out.println("===============================================");
}
}
catch ( final JIException e ) {
wai=10000;
System.out.println("2 重新连接 2");
System.out.println ( String.format ( "%08X: %s", e.getErrorCode (), server.getErrorMessage ( e.getErrorCode () ) ) );
}
}
private static void dumpItem(Item sa ) {
try {
itemall= sa.read(false);
Quality =itemall.getQuality();
float val=0;
wai=1000;
if(Quality>0){
val= itemall.getValue().getObjectAsFloat();
}
else{
val=0;
}
System.out.println("[" + (++count) +"]["+countarr+ "]ItemID:[" + sa.getId()
+ "] ,value: " + Quality
+ "]\nvalue: " +val);
} catch ( final JIException e ) {
// wai=10000;
try {
OpcCc( );
}
catch ( Exception ee ) {
System.out.println ( "2 数据采集异常" );
}
System.out.println ( "1 重新连接...." );
}
}
}
Upvotes: 0