Java通过JNative调用pi实时数据库dll类库piapi32.dll获取tag标签数据

首先需要把piapi32.dll 放在system32文件夹下,或者指定文件路径,我这里是放到system32文件夹

PIClientUtil.java 进行连接调用

package com.piutil;


import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

 



import org.xvolks.jnative.JNative;
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标签数据

 *

 */

public class PIClientUtil {

    private static PIClientUtil piClientUtil = new PIClientUtil();
    public static Map<String, String> maintags = new HashMap<String, String>();
    
    public static void GetAlltags(){
    	piClientUtil.GetMaintags();
    }


    public void GetMaintags(){
    	maintags.put("point1", piClientUtil.getPiTagValue("point1"));
    	maintags.put("point2", piClientUtil.getPiTagValue("point2"));
    }
   

    public static PIClientUtil getPIClientUtil(){

        return piClientUtil;

    }

 

    public  PIClientUtil()  {

        try {

            // *********************连接PI数据库**************************//

            // **********************************************************//

            JNative messageBox = new JNative("piapi32.dll", "piut_setservernode");

            messageBox.setRetVal(Type.INT);

            messageBox.setParameter(0, Type.STRING, "127.0.0.1");//服务器ip

            messageBox.invoke();

           

            //("piut_setservernode:"+messageBox.getRetValAsInt());

 

 

        } catch (NativeException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IllegalAccessException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

 

    }
    public  String  getTagValue(String tagName){
    	String value = maintags.get(tagName);
    	return value;
    }

    /**

     * 获取tag最新值

     * @param tagName

     * @return

     */

    public  String  getPiTagValue(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();

            int ptId=p.getAsInt(0);

            if(0==messageBox.getRetValAsInt()){

                ////("测点id:"+ptId);

                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.setParameter(4, new Pointer(new HeapMemoryBlock(1024)));

                messageBox.invoke();

                if(0==messageBox.getRetValAsInt()){
                	
                  ////(tagName+"测点值:"+ new DecimalFormat("0.00").format(pp.getAsFloat(0)));

                  return new DecimalFormat("0.00").format(pp.getAsFloat(0));

                }

                     

            }else{

              //("查询测点失败");

            }         

           

        } catch (NativeException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IllegalAccessException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        return "";

    }

   

    /**

     * 获取测点制定时间点的值

     * @param tagName

     * @return

     */

    public  float getTagValueByTime(String tagName,String time){

   

    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(8));

            messageBox.setParameter(1, p);

            messageBox.invoke();

            int ptId=p.getAsInt(0);

            if(0==messageBox.getRetValAsInt()){

                //("测点id:"+ptId);

                messageBox = new JNative("piapi32.dll", "piar_value");

                messageBox.setRetVal(Type.INT);

                messageBox.setParameter(0, Type.INT, "" + ptId);

                Pointer pp=new Pointer(new HeapMemoryBlock(1024));

                Pointer pp_status = new Pointer(new HeapMemoryBlock(1024));

                messageBox.setParameter(1,getTimeIntSec(time));

                messageBox.setParameter(2, Type.INT,3+"");

                messageBox.setParameter(3, pp);

                messageBox.setParameter(4, pp_status);

                messageBox.invoke();

                if(0==messageBox.getRetValAsInt()){

                  //(tagName+"测点值:"+pp.getAsFloat(0));

                  //(tagName+"status值:"+pp_status.getAsInt(0)); 

                  return pp.getAsFloat(0);

                }else{

                  //(tagName+"查询返回值:"+messageBox.getRetValAsInt()); 

                }

               

            }else{

              //("查询测点失败");

            }         

           

        } catch (NativeException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IllegalAccessException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        return 0F;

    }

   

    /**

     * 查询测点某时间段内最小值

     * @param tagName

     * @param time1

     * @param time2

     */

    public void getTagMinValue(String tagName,String time1,String time2){

    int code = 1;

    this.getTagValuesByTimeToTime(tagName, time1, time2, code);

    }

 

    /**

     * 查询测点某时间段内最大值

     * @param tagName

     * @param time1

     * @param time2

     */

    public void getTagMaxValue(String tagName,String time1,String time2){

    int code = 2;

    this.getTagValuesByTimeToTime(tagName, time1, time2, code);

    }

    /**

     * 查询测点某时间段内平均值

     * @param tagName

     * @param time1

     * @param time2

     */

    public void getTagAvgValue(String tagName,String time1,String time2){

    int code = 5;

    this.getTagValuesByTimeToTime(tagName, time1, time2, code);

    }

   

   

    /**

     *

     * @param tagName

     * @return

     */

    public  void getTagValuesByTimeToTime(String tagName,String time1,String time2, int code){

   

    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();

            int ptId=p.getAsInt(0);

            if(0==messageBox.getRetValAsInt()){

                //("测点id:"+ptId);

                messageBox = new JNative("piapi32.dll", "piar_summary");

                messageBox.setRetVal(Type.INT);

                //下面开始设置参数

                messageBox.setParameter(0, Type.INT, "" + ptId);

               

                Pointer p_startTime = this.getTimeIntSec(time1);

                Pointer p_endTime = this.getTimeIntSec(time2);

              

                Pointer p_retVal = new Pointer(new HeapMemoryBlock(8));

               

                Pointer p_pctGood = new Pointer(new HeapMemoryBlock(8));

               

                messageBox.setParameter(1, p_startTime);

                messageBox.setParameter(2, p_endTime);

                messageBox.setParameter(3, p_retVal);

                messageBox.setParameter(4, p_pctGood);

                messageBox.setParameter(5, Type.INT,code+"");

                messageBox.invoke();

                if(0==messageBox.getRetValAsInt()){

                  //(tagName+"测点返回值:"+p_retVal.getAsFloat(0));

                }else{

                  //(tagName+"查询状态值:"+messageBox.getRetValAsInt()); 

                }

               

            }else{

              //("查询测点失败");

            }         

           

        } catch (NativeException e) {

            e.printStackTrace();

        } catch (IllegalAccessException e) {

            e.printStackTrace();

        }

    }

   

    /**

     * 将时间串转换为int

     * 格式:11-Aug-17   18:00:00

     * @param time

     * @return

     */

    public int getPiTime(String time){

        // 获取时间点

    try {

            String tt = time;

            Pointer pointer    = new Pointer(MemoryBlockFactory.createMemoryBlock(8));

          

            JNative 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, pointer);

            messageBox.invoke();

            if(0 == messageBox.getRetValAsInt()){

            //("执行成功,getPiTime结果是:"+ pointer.getAsInt(0));

            return pointer.getAsInt(0);

            }else{

            //("执行失败");

            return 0;

            }

    } catch (NativeException e) {

           e.printStackTrace();

           return 0;

       } catch (IllegalAccessException e) {

            e.printStackTrace();

            return 0;

        }

    }

   

    /**

     * 将整数转换为时间,同上方法互逆

     * @param time

     * @return

     */

    public String getTimeFromInt(int time){

 

    try {

            Pointer pointer    = new Pointer(MemoryBlockFactory.createMemoryBlock(8));

          

            JNative messageBox = new JNative("piapi32.dll", "pitm_formtime");

            messageBox.setRetVal(Type.INT);

            messageBox.setParameter(0, Type.INT,this.getPiTime("")+"");

            messageBox.setParameter(1, pointer);

            messageBox.setParameter(2,19);

            messageBox.invoke();

          

            //("结果是:"+ pointer.getAsString());

            return pointer.getAsString();

    } catch (NativeException e) {

           e.printStackTrace();

       } catch (IllegalAccessException e) {

            e.printStackTrace();

        }

       return "";

    }

   

    /**

     * 将PI日期转为数组型

     * @param time

     * @return

     */

    public int[]  getTimeSecint(int time){

    int [] time_arrays = new int[6];

    try {

            Pointer pointer    = new Pointer(MemoryBlockFactory.createMemoryBlock(4* time_arrays.length));

            JNative messageBox = new JNative("piapi32.dll", "pitm_secint");

      

            messageBox.setParameter(0, Type.INT,this.getTimeIntSec("2012-05-17 11:11:11").getAsInt(0)+"");

            messageBox.setParameter(1, pointer);

            messageBox.invoke();

           

            for(int i=0;i<time_arrays.length;i++){

            time_arrays[i] = pointer.getAsInt(i*4);

            }
            return time_arrays;

    } catch (NativeException e) {

           e.printStackTrace();

       } catch (IllegalAccessException e) {

            e.printStackTrace();

        }

       return null;

    }

    /**

     * 将日期转为数组型,与pitm_secint互相逆

     * @param time

     * @return

     */

    public Pointer  getTimeIntSec(String time){

   

    PIDate date = PIDate.getPIDate(time);

    try {
        int time_array[] = new int[]{date.getMonth() , date.getDay() , date.getYear() , date.getHour(),  date.getMinutes(), date.getSeconds()};

       

            Pointer pointer    = new Pointer(MemoryBlockFactory.createMemoryBlock(8));

            Pointer pointer_array = new Pointer(MemoryBlockFactory.createMemoryBlock(time_array.length*4));

            JNative messageBox = new JNative("piapi32.dll", "pitm_intsec");

            /**这种方法也可以

            pointer_array.setIntAt(0, 2012);

            pointer_array.setIntAt(4, 5);

            pointer_array.setIntAt(8, 16);

            pointer_array.setIntAt(12, 21);

            pointer_array.setIntAt(16, 33);

            pointer_array.setIntAt(20, 33);

            **/

            // 初始化数组  

            for (int i = 0;i < time_array.length; i++) {  

            pointer_array.setIntAt(4 * i, time_array[i]);  

            }
            messageBox.setParameter(0, pointer);

            messageBox.setParameter(1, pointer_array);

            messageBox.invoke();

            //("getTimeIntSec="+pointer.getAsInt(0));

            return pointer;

    } catch (NativeException e) {

           e.printStackTrace();

       } catch (IllegalAccessException e) {

            e.printStackTrace();

        }

       return null;

    }

    /**

     * 不能直接返回int[0]

     * @param time

     * @return

     */

    public int  getTimeIntSecForInt(String time){

    PIDate date = PIDate.getPIDate(time);

        try {

            

        int time_array[] = new int[]{date.getMonth() , date.getDay() , date.getYear() , date.getHour(),  date.getMinutes(), date.getSeconds()};

            Pointer pointer    = new Pointer(MemoryBlockFactory.createMemoryBlock(8));

            Pointer pointer_array = new Pointer(MemoryBlockFactory.createMemoryBlock(time_array.length*4));

            JNative messageBox = new JNative("piapi32.dll", "pitm_intsec");
            // 初始化数组  

            for (int i = 0;i < time_array.length; i++) {  

            pointer_array.setIntAt(4 * i, time_array[i]);  

            }
            messageBox.setParameter(0, pointer);

            messageBox.setParameter(1, pointer_array);

            messageBox.invoke();

            //("getTimeIntSec="+pointer.getAsInt(0));

            return pointer.getAsInt(0);
    } catch (NativeException e) {
           e.printStackTrace();
       } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
       return 0;
    }
}

  

PIDate.java 对日期类型进行处理

package com.piutil;
 

import java.util.regex.Matcher;

import java.util.regex.Pattern;

 

public class PIDate {

         private int year;

         private int month;

         private int day;

         private int hour;

         private int minutes;

         private int seconds;

        

         public int getYear() {

                   return year;

         }

         public void setYear(int year) {

                   this.year = year;

         }

         public int getMonth() {

                   return month;

         }

         public void setMonth(int month) {

                   this.month = month;

         }

         public int getDay() {

                   return day;

         }

         public void setDay(int day) {

                   this.day = day;

         }

         public int getHour() {

                   return hour;

         }

         public void setHour(int hour) {

                   this.hour = hour;

         }

         public int getMinutes() {

                   return minutes;

         }

         public void setMinutes(int minutes) {

                   this.minutes = minutes;

         }

         public int getSeconds() {

                   return seconds;

         }

         public void setSeconds(int seconds) {

                   this.seconds = seconds;

         }

        

         public static PIDate getPIDate(String time){

                   Pattern p=Pattern.compile("(\\d{4})-(\\d{1,2})-(\\d{1,2}) (\\d{1,2}):(\\d{1,2}):(\\d{1,2})");  

                   Matcher m=p.matcher(time);  

                   PIDate date = new PIDate();

                   if(m.find()){  

                       /*System.out.println("日期:"+m.group());  

                       System.out.println("年:"+m.group(1));  

                       System.out.println("月:"+m.group(2));  

                       System.out.println("日:"+m.group(3));

                       System.out.println("时:"+m.group(4));

                       System.out.println("分:"+m.group(5));

                       System.out.println("秒:"+m.group(6));*/

                       date.setYear(Integer.parseInt(m.group(1)));

                       date.setMonth(Integer.parseInt(m.group(2)));

                       date.setDay(Integer.parseInt(m.group(3)));

                       date.setHour(Integer.parseInt(m.group(4)));

                       date.setMinutes(Integer.parseInt(m.group(5)));

                       date.setSeconds(Integer.parseInt(m.group(6)));

                   } 
         return date;
   }
}

PISearchAjaxServlet.java servlet便于前端调用。

package com.piutil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import com.piutil.PIClientUtil;

import cn.myapps.constans.Web;
import cn.myapps.core.user.action.WebUser;

public class PISearchAjaxServlet extends HttpServlet {

	HashMap<String, String> maintags =  (HashMap<String, String>) PIClientUtil.maintags;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		

		HttpSession session = request.getSession();
		WebUser user = (WebUser) session.getAttribute(Web.SESSION_ATTRIBUTE_FRONT_USER);
		if(user!=null){
			response.setContentType("text/html;charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			String tag = request.getParameter("tag");
	
			tag = new String(tag.getBytes("GBK"), "UTF-8");
			
			List<Map<String, String>> list = new ArrayList<Map<String,String>>();
			Map<String, String> map = new HashMap();
			String tags[] = tag.split(";");
			for(int i=0;i<=tags.length-1;i++){
				String  mytag = maintags.get(tags[i]);
				map.put(tags[i], mytag);
			}
			list.add(map);
			String str = JSONArray.fromObject(list).toString();
			System.out.println(str);
			// 把数据直接响应到客户端
			response.getWriter().write(str);
		}else{
			request.getRequestDispatcher("/portal/phone/timeOut.jsp").forward(request, response);
		}
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		this.doGet(request, response);
	}
}

注意:因为是32位的dll只能运行在 tomcat jdk 都是32位的环境中 最好单独做一个小应用使用。

此方式可以调用dll文件中的接口。比较远古的应用办法了。。

现在应该比较少会用到。。