Java通过jnative调用pi实时数据库dll
Java通过jnative调用pi实时数据库dll
JNative : Java to native interface,This library allows developers to access native libraries (DLL and lib.so) from java. You do NOT need to compile a line of C/C++ to use it: it's dynamic !! Now Win32 DLL and shared libraries are usable with Java ;)
PI System, the world's leading highly-scalable and secure infrastructure for the management of real-time data and events
import org.xvolks.jnative.JNative;
import java.text.SimpleDateFormat;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;
import org.xvolks.jnative.pointers.Pointer;
import org.xvolks.jnative.pointers.memory.HeapMemoryBlock;
import org.xvolks.jnative.pointers.memory.MemoryBlock;
import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;
/**
* Java通过jnative调用pi实时数据库dll类库piapi32.dll获取tag标签数据
* <a href="http://itindex.net">http://itindex.net</a>
*/
public class PIClientUtil {
private static PIClientUtil piClientUtil=new PIClientUtil();
public static void main(String[] args) throws Exception {
PIClientUtil.getPIClientUtil().getTagValue("SCADA#ME-60331");
PIClientUtil.getPIClientUtil().getTagValue("SCADA#ME-60332");
PIClientUtil.getPIClientUtil().getTagValue("SCADA#ME-60333");
PIClientUtil.getPIClientUtil().getTagValue("SCADA#ME-60334");
PIClientUtil.getPIClientUtil().getTagValue("SCADA#ME-60335");
}
public static PIClientUtil getPIClientUtil(){
return piClientUtil;
}
private PIClientUtil() {
try {
MemoryBlock[] tim = new MemoryBlock[3];
tim[0] = MemoryBlockFactory.createMemoryBlock(8);
Pointer[] timed = new Pointer[100];
timed[0] = new Pointer(tim[0]);
tim[1] = MemoryBlockFactory.createMemoryBlock(8);
timed[99] = new Pointer(tim[1]);
MemoryBlock mm = MemoryBlockFactory.createMemoryBlock(4);
Pointer count1 = new Pointer(mm);
count1.setIntAt(0, 100);
int[] pt = { 17999, 18000, 18001 };
float[] rvals = new float[3];
int[] istats = new int[3];
System.out.println(System.currentTimeMillis());
// *********************连接PI数据库**************************//
// **********************************************************//
JNative messageBox = new JNative("piapi32.dll", "piut_setservernode");
messageBox.setRetVal(Type.INT);
messageBox.setParameter(0, Type.STRING, "88.148.76.208");
messageBox.invoke();
System.out.println("piut_setservernode:"+messageBox.getRetValAsInt());
// ***********************获取时间点**************************//
// **********************************************************//
String tt = "07-Aug-17 18:00:00 ";
messageBox = new JNative("piapi32.dll", "pitm_parsetime");
messageBox.setRetVal(Type.INT);
messageBox.setParameter(0, Type.STRING, tt);
messageBox.setParameter(1, Type.INT, " " + 0);
messageBox.setParameter(2, timed[0]);
messageBox.invoke();
System.out.println(messageBox.getRetValAsInt());
System.out.println(timed[0].getAsInt(0));
messageBox = new JNative("piapi32.dll", "pitm_parsetime");
messageBox.setRetVal(Type.INT);
messageBox.setParameter(0, Type.STRING, "+10h ");
messageBox.setParameter(1, Type.INT, " " + timed[0].getAsInt(0));
messageBox.setParameter(2, timed[99]);
messageBox.invoke();
System.out.println(messageBox.getRetValAsInt());
System.out.println(timed[99].getAsInt(0));
} catch (NativeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获取tag最新值,可能需要线程同步synchronized
* @param tagName
* @return
*/
public float getTagValue(String tagName){
try {
JNative messageBox = new JNative("piapi32.dll", "pipt_findpoint");
messageBox.setRetVal(Type.INT);
messageBox.setParameter(0, Type.STRING, tagName);
Pointer p=new Pointer(new HeapMemoryBlock(1024));
messageBox.setParameter(1, p);
messageBox.invoke();
System.out.println("pipt_findpoint:"+messageBox.getRetValAsInt());
int ptId=p.getAsInt(0);
messageBox = new JNative("piapi32.dll", "pisn_getsnapshot");
messageBox.setRetVal(Type.INT);
messageBox.setParameter(0, Type.INT, "" + ptId);
Pointer pp=new Pointer(new HeapMemoryBlock(1024));
messageBox.setParameter(1, pp);
messageBox.setParameter(2, new Pointer(new HeapMemoryBlock(1024)));
messageBox.setParameter(3, new Pointer(new HeapMemoryBlock(1024)));
messageBox.invoke();
System.out.println(tagName+"retValue:"+messageBox.getRetVal());
System.out.println(tagName+"tagValue:"+pp.getAsFloat(0));
return pp.getAsFloat(0);
} catch (NativeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
}