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 java.util.Map;
import java.util.ArrayList;
import com.baosight.hdjni.HdAlarmQuery;
import com.baosight.hdjni.HDJniTime;

import com.baosight.hdsdk.HDAlarmManager;
import com.baosight.hdsdk.HDConnectionFactory;
import com.baosight.hdsdk.HDServerFactory;
import com.baosight.hdsdk.common.HDAlarmRecordPropMask;
import com.baosight.hdsdk.domain.alarm.HDAlarmRecord;
import com.baosight.hdsdk.domain.data.HDDataConnection;
import com.baosight.hdsdk.domain.data.HDDataServer;
import com.baosight.hdsdk.exception.HDSdkException;


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

	public static void main(String[] args) {
		AlarmOperationExam amExam = new AlarmOperationExam();
		amExam.ackAlarm();
	}
	
	public void ackAlarm(){
		//设置服务器信息
		String serverIP = "168.2.237.64";
		int serverPort = 5673;
		//获取服务器对象
		HDDataServer server = HDServerFactory.getHDDataServer(serverIP, serverPort, "", 5673);
		long reaultPropMask = HDAlarmRecordPropMask.MASK_ALARM_RECORD_ALL;
		
		try {
			//获取后台连接
			HDDataConnection dataConn = HDConnectionFactory.getHDDataConnection(server);
			//获取报警管理器对象
			HDAlarmManager amManager = dataConn.getAlarmManager();
			
			HDAlarmRecord record = amManager.queryAlarmSnapshot(107, reaultPropMask);
			
			
			HDJniTime alarmTime = new HDJniTime();
			alarmTime.nSec = record.getStartTimeSec();
			alarmTime.nMsec = record.getStartTimeMSec();
			amManager.acknowledgeAlarm(107, alarmTime);
			record = amManager.queryAlarmSnapshot(107, reaultPropMask);
		} catch (HDSdkException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 查询历史报警记录
	 *
	 */
	public void queryAlarmRecords() {
		//设置服务器信息
		String serverIP = "168.2.8.104";
		int serverPort = 5673;
		//获取服务器对象
		HDDataServer server = HDServerFactory.getHDDataServer(serverIP, serverPort, "", 5673);
		
		//////////////////////////////////////// 整理参数 ///////////////////////////////////
		List<Integer> amIDs = new ArrayList<Integer>();
		amIDs.add(1);
		amIDs.add(2);// am id
		HdAlarmQuery amQuery = new HdAlarmQuery();
		//设置起止时间
		String time1 = "2013-06-20 06:00:00";
    	Date startTime = this.String2Date(time1);   	
    	String time2 = "2013-06-20 13:00:00";
    	Date endTime = this.String2Date(time2);
    	
    	amQuery.nStartSec = HDJniTime.valueOf(startTime).nSec;
    	amQuery.nStartMsec = HDJniTime.valueOf(startTime).nMsec;
    	amQuery.nEndSec = HDJniTime.valueOf(endTime).nSec;
    	amQuery.nEndMsec = HDJniTime.valueOf(endTime).nMsec;
		
    	//设置报警过滤项
    	amQuery.nAckMask = HDAlarmRecordPropMask.MASK_ALARM_STATUS_ALL;
    	amQuery.nCommentMask = HDAlarmRecordPropMask.MASK_ALARM_STATUS_ALL;
    	amQuery.nRestoreMask = HDAlarmRecordPropMask.MASK_ALARM_STATUS_ALL;
    	amQuery.nPriorMask = HDAlarmRecordPropMask.MASK_ALARM_STATUS_ALL;
    	amQuery.nTypeMask = HDAlarmRecordPropMask.MASK_ALARM_STATUS_ALL;
    	
		long reaultPropMask = HDAlarmRecordPropMask.MASK_ALARM_RECORD_ALL;
		
		try {
			//获取后台连接
			HDDataConnection dataConn = HDConnectionFactory.getHDDataConnection(server);
			//获取报警管理器对象
			HDAlarmManager amManager = dataConn.getAlarmManager();
			//调用接口后台查询
			Map<Integer, List<HDAlarmRecord>> retMap = amManager.queryAlarmRecords(amIDs, amQuery, reaultPropMask);
			//处理查询结果
			for (Map.Entry<Integer, List<HDAlarmRecord>> entry : retMap.entrySet()) {
				System.out.println("AlarmID = " + entry.getKey());
				System.out.println("AlarmRecordList = " + entry.getValue());
			}
		} 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;
	}
}
