package com.luguang.ba.service;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Random;
import javax.annotation.PostConstruct;
import javafish.clients.opc.JCustomOpc;
import javafish.clients.opc.JEasyOpc;
import javafish.clients.opc.JOpc;
import javafish.clients.opc.asynch.AsynchEvent;
import javafish.clients.opc.asynch.OpcAsynchGroupListener;
import javafish.clients.opc.component.OpcGroup;
import javafish.clients.opc.component.OpcItem;
import javafish.clients.opc.exception.CoInitializeException;
import javafish.clients.opc.exception.ComponentNotFoundException;
import javafish.clients.opc.exception.ConnectivityException;
import javafish.clients.opc.exception.SynchReadException;
import javafish.clients.opc.exception.UnableAddGroupException;
import javafish.clients.opc.exception.UnableAddItemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.luguang.ba.model.LgpIoR;
@Service
public class LgpOpcClient {
static private String host="localhost";
static private String serverPID="ICONICS.SimulatorOPCDA.2";
static private String serverClientHandler="JOPC1";
private JEasyOpc jopc;
private OpcGroup readGroup;
private OpcItem statusItem0,statusItem1,statusItem2;
private LgpIoR lgpIoR=new LgpIoR();
private LgpRealDataService lgpRealDataService;
@Autowired
public void setLgpRealDataService(LgpRealDataService service){
this.lgpRealDataService =service;
}
/**
* 初始化
*/
@PostConstruct
private void initOpc()
{
try
{
JOpc.coInitialize();//初始化服务器
//建立server对象
jopc = new JEasyOpc(host, serverPID, serverClientHandler);
jopc.connect();//建立连接
//建立Group对象
readGroup= new OpcGroup("kksrhsjz3639_1",true,200,.0f);//kksrhsjz3639_1:OPC Gorup
//建立Item对象
statusItem0=new OpcItem("kksrhsjz3639_1.test1",true,"");//kksrhsjz3639_1.test1:OPC item
statusItem1=new OpcItem("kksrhsjz3639_1.test2",true,"");
statusItem2=new OpcItem("kksrhsjz3639_1.test3",true,"");
//将Item对象添加到Group对象下
readGroup.addItem(statusItem0);
readGroup.addItem(statusItem1);
readGroup.addItem(statusItem2);
//将Group对象添加到JOPC对象下
jopc.addGroup(readGroup);
//注册Group对象
jopc.registerGroups();
jopc.registerItem(readGroup, statusItem2);
jopc.start();
readGroup.addAsynchListener(new OpcAsynchGroupListener(){
public void getAsynchEvent(AsynchEvent event)
{
System.out.println(((JCustomOpc) event.getSource()).getFullOpcServerName() + "=>");
System.out.println("Package: " + event.getID());
System.out.println(event.getOPCGroup());
try {
OpcGroup responseGroup = jopc.synchReadGroup(readGroup);
for (OpcItem i:responseGroup.getItems())
{
System.out.println(i.getItemName());
System.out.println(i.getTimeStamp());
System.out.println(i.isQuality());
if(i.getItemName().equals(statusItem0.getItemName()))
{
System.out.println(i.getValue());
}
if(i.getItemName().equals(statusItem1.getItemName()))
{
System.out.println(i.getValue());
}
if(i.getItemName().equals(statusItem2.getItemName()))
{
System.out.println(i.getValue());
}
}
} catch (ComponentNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SynchReadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
} catch (ConnectivityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnableAddGroupException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnableAddItemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(CoInitializeException e1)
{
e1.printStackTrace();
}
}
}
jeasyOPC开发包 需要资源分1分