package com.baosight.hdsdk.example;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.tuple.ImmutablePair;

import com.baosight.hdsdk.HDConnectionFactory;
import com.baosight.hdsdk.HDDataProvider;
import com.baosight.hdsdk.HDServerFactory;
import com.baosight.hdsdk.HDTagManager;
import com.baosight.hdsdk.common.HDInterpolationMode;
import com.baosight.hdsdk.domain.data.HDBasicTag;
import com.baosight.hdsdk.domain.data.HDDataConnection;
import com.baosight.hdsdk.domain.data.HDDataServer;
import com.baosight.hdsdk.domain.data.HDRecord;
import com.baosight.hdsdk.exception.HDSdkException;
import com.baosight.hdjni.HDErrcode;

/**
 * 实时数据库JavaSDK记录相关操作示例程序
 * 
 * @author kongxiaoyang
 * @since 2013-07-02
 */
public class DataOperationExam {

	public static void main(String[] args) {
		DataOperationExam dataExam = new DataOperationExam();
		dataExam.getSnapshot();
	}
	
	/**
	 * 实时值查询
	 *
	 */
	public void getSnapshot() {
		String serverIP = "192.168.128.115";
		int serverPort = 5673;
		
		HDDataServer dataServer = HDServerFactory.getHDDataServer(serverIP, serverPort, "", 5673);
		try {
			HDDataConnection dataConn = HDConnectionFactory.getHDDataConnection(dataServer);
			HDTagManager tagMgr = dataConn.getTagManager();
			HDBasicTag bTag = tagMgr.getBasicTagByName("test_0");
			HDDataProvider dataP = dataConn.getDataProvider();
			HDRecord record = dataP.querySnapshotByTagID(bTag.getId());
			System.out.println(record.toString());
		} catch (HDSdkException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 插值历史记录查询
	 *
	 */
	public void queryInterRecords() {
		
		//设置服务器信息
		String serverIP = "168.2.8.104";
		int serverPort = 5678;
		//获取服务器对象
		HDDataServer server = HDServerFactory.getHDDataServer(serverIP, serverPort);
		
    	int tagID = 7;		//tag id
    	//设置起止时间
    	String time1 = "2013-06-19 06:00:00";
    	Date startTime = this.String2Date(time1); 
    	String time2 = "2013-06-19 13:00:00";
    	Date endTime = this.String2Date(time2);
    	
    	//设置插值间隔 3分钟
    	int intervalInMs = 180000; //3 mins
    	//设置插值方式
    	HDInterpolationMode mode = HDInterpolationMode.FORWARD;
    	//设置查询结果是否包含查询出错的记录
    	HDErrcode err = new HDErrcode();
    	err.Value = 0;
    	
    	try {
			//获取后台连接
			HDDataConnection dataConn = HDConnectionFactory.getHDDataConnection(server);
			//获取报警管理器对象
			HDDataProvider hdProvider = dataConn.getDataProvider();
			//查询插值记录
			
			List<ImmutablePair<HDRecord,Integer>> listRecs = hdProvider.queryTagHisInterRecordsByMode(tagID, startTime, endTime, intervalInMs, mode, err);
			//处理查询结果
			for (ImmutablePair<HDRecord,Integer> record : listRecs) {
				System.out.println(record.left.toString());
			}
		} catch (HDSdkException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
    /**
	 * 将时间字符串转为Date 默认传入的格式为："yyyy-MM-dd HH:mm:ss"
	 */
	private  java.util.Date String2Date(String str) {
		java.util.Date date = null;
		// 如果是空串，或者不合适的串的话，返回null
		if (str.trim().length() == 0)// regex
		{
			return null;
		}
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			date = format.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
}
