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.HDDataAnalysis;
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;
import com.baosight.hdsdk.common.HDStatsType;
import com.baosight.hdsdk.common.HDSampleType;

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

	public static void main(String[] args) {
		DataAnalysisExam dataExam = new DataAnalysisExam();
		dataExam.getTagMax();
	}
	
	/**
	 * 查询Tag点在一段时间内的最大值
	 *
	 */
	public void getTagMax() {
		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");
			HDDataAnalysis dataA = dataConn.getDataAnalysis();
			
			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);
	    	
			double dvalue = dataA.getTagStats(bTag.getId(), HDStatsType.HD_STATS_TYPE_MAX, startTime, endTime,
					HDSampleType.HD_STATS_SAMPLE_TYPE_RAW, 0, 0, 0, 0, "", 0, false);
			System.out.println(dvalue);
		} 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;
	}
}
