package com.nms.server.thread.monitor;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.nms.server.bean.DetectInfo;
import com.nms.server.bean.NodeModel;
import com.nms.server.bean.SetInfo;
import com.nms.server.common.Common;
import com.nms.server.common.Constants;
import com.nms.server.thread.monitor.util.MonitorUtil;
import com.nms.server.util.DateUtil;
/**
 * 时钟同步监测
 * @author dell
 *
 */
public class ServerDateThread implements Runnable{
	private static Logger logger = Logger.getLogger(ServerDateThread.class);
	private String name;
	private SetInfo setInfo;
	private List<NodeModel> nodeList;
	private Long startTime;
	
	public ServerDateThread(String name ,SetInfo setInfo,List<NodeModel> nodeList,Long startTime) {
		this.setInfo = setInfo;
		this.name = name;
		this.nodeList = nodeList;
		this.startTime = startTime;
	}
	
	@Override
	public void run() {
		Thread.currentThread().setName("ServerDateThread->" + name);
		try {
			for(NodeModel model : nodeList){
				if(model.getNodeType() != null && model.getNodeType().longValue() == 0l){	//服务器节点
					String nodeIp = model.getNodeIp();
					Boolean online = (Boolean)Common.getHbInfo(nodeIp, "online");
					Long localDate = (Long)Common.getHbInfo(nodeIp, "dcTime");
					Long sysDate = (Long)Common.getHbInfo(nodeIp, "ncTime");
					try {
						DetectInfo detectInfo = null;
						if(online != null && online && localDate != null && sysDate != null){//有心跳
							//-- 接收系统时间信息
							logger.debug("服务器系统时间：" + sysDate);
							//-- systemdate 监测设置 详细数据整理
							long timeDelay = Math.abs(getMinutesFromBeginToEnd(sysDate, localDate));
							//-- 信息保存到监测信息对象中
							detectInfo = new DetectInfo();
							detectInfo.getDetailDatas().add(localDate + MonitorUtil.SEPARATOR + sysDate + MonitorUtil.SEPARATOR + timeDelay);
							detectInfo.setDescInfo("server:"
									+ DateUtil.getStingDate(DateUtil.YYYY_MM_DD_HH24_MM_SS,new Date(localDate))
									+ " agent:" + DateUtil.getStingDate(DateUtil.YYYY_MM_DD_HH24_MM_SS,new Date(sysDate)));
						}
						if(detectInfo == null){
							detectInfo = new DetectInfo();
							detectInfo.setDescInfo("i18n_server.SystemDateThread.getSysTimeFail_n81i");
						}
						detectInfo.setTestTimes(1);
						detectInfo.setCheckTime(System.currentTimeMillis());
						MonitorUtil.createMonitorData(nodeIp, setInfo, startTime, Common.getAlarmInfoMap().get(setInfo.getId()+ ""), detectInfo);
					} catch (Exception ex) {
						logger.error("Getting the time anomaly of NC system",ex);
						String[] alarm = Common.alarmExceptionInfo(setInfo.getId(), nodeIp, Constants.DETEC_SYSTEMDATE_STR, setInfo.getProcessIden(), startTime, 1,Constants.DETECTION_INFO_ALARM_WRONG, ex.getMessage());
						Common.addAlarmData(alarm);
					}
				}else{		//非服务器节点
					logger.info("监测目标:"+model.getNodeIp()+" 是非服务器节点，不进行"+Constants.DETEC_SYSTEMDATE_STR+"监测");
				}
			}
		} catch (Exception e) {
			logger.error("error",e);
		}
	}

	
	public static long getMinutesFromBeginToEnd(long begin, long end) {
		return (end - begin) / (60 * 1000);
	}
	
}
