package com.saga.thread.common;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.zillion.util.log.LogUtil;

import com.saga.util.Constant;
import com.saga.util.HBaseUtil;
import com.zillion.database.agent.ZillionAgent;

public class CheckThread extends Thread {
	ZillionAgent agent;
	long realCheck = 0;
	private String ip = null;
	// private Map<String, String> nodeStatus = new HashMap<String, String>();
	private Map<String, String> pointStatus = new HashMap<String, String>();
	private Map<String, String> meterStatus = new HashMap<String, String>();
	private Map<String, String> projectStatus = new HashMap<String, String>();
	private Map<String, String> physicalStatus = new HashMap<String, String>();

	public CheckThread(ZillionAgent agent) {
		try {
			this.agent = agent;
			try {
				this.ip = Constant.getLocalHostLANAddress().getHostAddress() + ":" + Constant.TypePort.get("Down");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				this.ip = "0.0.0.0" + ":" + Constant.TypePort.get("Down");
				e.printStackTrace();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			LogUtil.error("CheckThread.Exception: " + e.toString());
			System.exit(0);
		}
	}

	public void run() {
		LogUtil.info("CheckThread-" + this.getName() + " Starting ...");
		// try {
		// Thread.sleep(Constant.Millisecond.MINUTE_1);
		// } catch (Exception e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// 初次加载生成当前状态
		this.excuteDBStatus();

		while (true) {
			try {
				// // V1.0点位日志结构
				// this.realCheck();
				// V2.0点位日志结构
				this.realCheck2();
				// Constant.showStatus();
				Thread.sleep(Constant.Millisecond.SECOND_1);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				LogUtil.error("CheckThread.run.Exception: " + e.toString());
			}
		}
	}

	private void excuteDBStatus() {
		// TODO Auto-generated method stub
		// for (Entry<String, Long> pns : Constant.ps_NodeStatus.entrySet()) {
		// if (System.currentTimeMillis() - pns.getValue() <
		// Constant.Millisecond.MINUTE_1) {
		// this.nodeStatus.put(pns.getKey(), "conn");
		// } else {
		// this.nodeStatus.put(pns.getKey(), "disconn");
		// }
		// }
		for (Entry<String, Long> ppsu : Constant.ps_PointStatusUp.entrySet()) {
			if (ppsu.getValue() != null) {
				if (System.currentTimeMillis() - ppsu.getValue() < Constant.Millisecond.MINUTE_10) {
					this.pointStatus.put(ppsu.getKey(), "conn");
				} else {
					this.pointStatus.put(ppsu.getKey(), "disconn");
				}
			} else {
				this.pointStatus.put(ppsu.getKey(), null);
			}
		}

		for (Entry<String, Long> pps : Constant.ps_PhysicalStatus.entrySet()) {
			// if (!pps.getKey().endsWith(";;;")) {
			if (pps.getValue() != null) {
				if (System.currentTimeMillis() - pps.getValue() < Constant.Millisecond.MINUTE_10) {
					this.physicalStatus.put(pps.getKey(), "conn");
				} else {
					this.physicalStatus.put(pps.getKey(), "disconn");
				}
			} else {
				this.physicalStatus.put(pps.getKey(), null);
			}

			// }
		}
		for (Entry<String, Long> pps : Constant.ps_ProjectStatus.entrySet()) {
			if (pps.getValue() != null) {
				if (System.currentTimeMillis() - pps.getValue() < Constant.Millisecond.MINUTE_10) {
					this.projectStatus.put(pps.getKey(), "conn");
				} else {
					this.projectStatus.put(pps.getKey(), "disconn");
				}
			} else {
				this.projectStatus.put(pps.getKey(), null);
			}

		}
		for (Entry<String, Long> pmsu : Constant.ps_MeterStatusUp.entrySet()) {
			if (pmsu.getValue() != null) {
				if (System.currentTimeMillis() - pmsu.getValue() < Constant.Millisecond.MINUTE_10) {
					this.meterStatus.put(pmsu.getKey(), "conn");
				} else {
					this.meterStatus.put(pmsu.getKey(), "disconn");
				}
			} else {
				this.meterStatus.put(pmsu.getKey(), null);
			}
		}
	}

	private void realCheck2() {
		// TODO Auto-generated method stub

		if (System.currentTimeMillis() - this.realCheck > Constant.Millisecond.SECOND_3) {
			this.realCheck = System.currentTimeMillis();

			try {
				this.virtual_point_status_up_Check();
				this.virtual_point_status_down_Check();
				this.physical_status_address_Check();

				this.virtual_meter_status_up_Check();
				this.virtual_project_status_up_Check();

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				LogUtil.error("CheckThread.realCheck2.Exception: " + e.toString());
			}
		}
	}

	private void virtual_project_status_up_Check() {
		// TODO Auto-generated method stub
		for (Entry<String, List<String>> ps : Constant.ps_ProjectList.entrySet()) {
			String project = ps.getKey();
			List<String> l = ps.getValue();
			String status = this.gatherStatus(l);
			try {
				if ("conn".equalsIgnoreCase(status)) {
					HBaseUtil.Insert_ps_virtual_project_status(Constant.agent, Constant.BuildingDB.get(project),
							project, status, Constant.format(new Date()), "");
				}

				if (
				// 首次
				!this.projectStatus.containsKey(project) || null == this.projectStatus.get(project) ||
				// 已有记录
						!status.equalsIgnoreCase(this.projectStatus.get(project))) {

					HBaseUtil.Insert_ps_virtual_project_log(Constant.agent, Constant.BuildingDB.get(project), project,
							Constant.format(new Date()), status);
					if ("disconn".equalsIgnoreCase(status)) {
						HBaseUtil.Insert_ps_virtual_project_status(Constant.agent, Constant.BuildingDB.get(project),
								project, status, Constant.format(new Date()), "");
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.projectStatus.put(project, status);
		}
	}

	private void virtual_meter_status_up_Check() {
		// TODO Auto-generated method stub
		for (Entry<String, List<String>> ms : Constant.ps_MeterList.entrySet()) {
			String pm = ms.getKey();
			List<String> l = ms.getValue();
			String status = this.gatherStatus(l);
			String[] ml = pm.split("-");
			String project = ml[0];
			String meter = ml[1];
			try {
				if ("conn".equalsIgnoreCase(status)) {
					HBaseUtil.Insert_ps_virtual_meter_status(Constant.agent, Constant.BuildingDB.get(project), project,
							meter, "up", status, Constant.format(new Date()), "");
				}

				if (
				// 首次
				!this.meterStatus.containsKey(pm) || null == this.meterStatus.get(pm) ||
				// 已有记录
						!status.equalsIgnoreCase(this.meterStatus.get(pm))) {

					HBaseUtil.Insert_ps_virtual_meter_log(Constant.agent, Constant.BuildingDB.get(project), project,
							meter, "up", Constant.format(new Date()), status);
					if ("disconn".equalsIgnoreCase(status)) {
						HBaseUtil.Insert_ps_virtual_meter_status(Constant.agent, Constant.BuildingDB.get(project),
								project, meter, "up", status, Constant.format(new Date()), "");
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.meterStatus.put(pm, status);
		}
	}

	private void physical_status_address_Check() {
		// TODO Auto-generated method stub
		for (Entry<String, List<String>> ps : Constant.ps_PhysicalList.entrySet()) {
			String physical = ps.getKey();
			String[] pl = physical.split(";");
			String project = pl[0];
			String address_1 = pl.length < 2 ? "" : pl[1];
			if ("".equalsIgnoreCase(address_1)) {
				continue;
			}
			String address_2 = pl.length < 3 ? "" : pl[2];
			String address_3 = pl.length < 4 ? "" : pl[3];
			String address_4 = pl.length < 5 ? "" : pl[4];
			String status = "disconn";
			if (!physical.endsWith(";;;")) {
				List<String> l = ps.getValue();
				status = this.gatherStatus(l);
			} else {
				if (Constant.ps_PhysicalStatus.get(physical) != null) {
					if (this.realCheck - Constant.ps_PhysicalStatus.get(physical) < Constant.Millisecond.MINUTE_1) {
						status = "conn";
					}
				} else {
					status = "disconn";
				}
			}
			try {
				if ("conn".equalsIgnoreCase(status)) {
					HBaseUtil.Insert_ps_physical_status(Constant.agent, Constant.BuildingDB.get(project), project,
							address_1, address_2, address_3, address_4, status, Constant.format(new Date()), "");
				}

				if (
				// 首次
				!this.physicalStatus.containsKey(physical) || null == this.physicalStatus.get(physical) ||
				// 已有记录
						!status.equalsIgnoreCase(this.physicalStatus.get(physical))) {

					HBaseUtil.Insert_ps_physical_log(Constant.agent, Constant.BuildingDB.get(project), project,
							address_1, address_2, address_3, address_4, Constant.format(new Date()), status);
					if ("disconn".equalsIgnoreCase(status)) {
						HBaseUtil.Insert_ps_physical_status(Constant.agent, Constant.BuildingDB.get(project), project,
								address_1, address_2, address_3, address_4, status, Constant.format(new Date()), "");
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.physicalStatus.put(physical, status);
		}
	}

	private String gatherStatus(List<String> l) {
		// TODO Auto-generated method stub
		for (String s : l) {
			if ("conn".equalsIgnoreCase(this.pointStatus.get(s))) {
				return "conn";
			}
		}
		return "disconn";
	}

	private void virtual_point_status_down_Check() {
		// TODO Auto-generated method stub
		// down
		try {
			List<String> nk = new ArrayList<String>();
			List<String> ps = new ArrayList<String>();
			synchronized (Constant.ps_ID2Time) {
				Iterator<Entry<String, Long>> it = Constant.ps_ID2Time.entrySet().iterator();
				while (it.hasNext()) {
					try {
						Entry<String, Long> n = it.next();

						String building = Constant.ps_ID2Point.get(n.getKey()).split("-")[0];
						String mac = Constant.ps_ID2Point.get(n.getKey()).split("-")[1];
						String point = Constant.ps_ID2Point.get(n.getKey()).split("-")[2];

						String[] slist = point.split("\\.");
						StringBuffer sb = new StringBuffer();
						for (int i = 0; i < slist.length - 1; i++) {
							if (i > 0) {
								sb.append(".");
							}
							sb.append(slist[i]);
						}
						String meter = sb.toString();
						String funcid = slist[slist.length - 1];

						// String function =
						// Constant.id2function.get(n.getKey());

						if (n.getValue() != null
								&& System.currentTimeMillis() - n.getValue() > Constant.Millisecond.SECOND_10) {
							try {
								if ("finish:finish:success".equalsIgnoreCase(Constant.ps_ID2Status.get(n.getKey()))) {
									// HBaseUtil.InsertPointStatus(Constant.agent,
									// Constant.BuildingDB.get(building),
									// building, this.ip, mac, meter,
									// funcid, "down", "conn",
									// // function + ":" +
									// Constant.format(new
									// Date(Constant.id2time.get(n.getKey()))),
									// Constant.id2status.get(n.getKey()));
									ps.add(Constant.BuildingDB.get(building) + ";" + building + ";" + this.ip + ";"
											+ mac + ";" + meter + ";" + funcid + ";" + "down" + ";" + "conn" + ";"
											+ Constant.format(new Date(Constant.ps_ID2Time.get(n.getKey()))) + ";"
											+ Constant.ps_ID2Status.get(n.getKey()));
								} else {
									// Constant.pointListDown.put(n.getKey(),
									// null);
									// HBaseUtil.InsertPointStatus(Constant.agent,
									// Constant.BuildingDB.get(building),
									// building, this.ip, mac, meter,
									// funcid, "down", "disconn",
									// // function + ":" +
									// Constant.format(new
									// Date(Constant.id2time.get(n.getKey()))),
									// Constant.id2status.get(n.getKey()));
									ps.add(Constant.BuildingDB.get(building) + ";" + building + ";" + this.ip + ";"
											+ mac + ";" + meter + ";" + funcid + ";" + "down" + ";" + "disconn" + ";"
											+ Constant.format(new Date(Constant.ps_ID2Time.get(n.getKey()))) + ";"
											+ Constant.ps_ID2Status.get(n.getKey()));
								}

							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							nk.add(n.getKey());
							it.remove();
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}

			synchronized (Constant.ps_ID2Point) {
				for (String k : nk) {
					if (Constant.ps_ID2Point.containsKey(k)) {
						Constant.ps_ID2Point.remove(k);
					}
				}
			}
			synchronized (Constant.ps_ID2Status) {
				for (String k : nk) {
					if (Constant.ps_ID2Status.containsKey(k)) {
						Constant.ps_ID2Status.remove(k);
					}
				}
			}
			synchronized (Constant.ps_ID2Function) {
				for (String k : nk) {
					if (Constant.ps_ID2Function.containsKey(k)) {
						Constant.ps_ID2Function.remove(k);
					}
				}
			}

			nk = new ArrayList<String>();
			for (String s : ps) {
				String[] a = s.split(";");
				HBaseUtil.Insert_ps_virtual_point_status(Constant.agent, a[0], a[1], a[4], Long.valueOf(a[5]), a[6],
						a[7], a[8], a[9]);
				Thread.sleep(1L);
			}
			ps = new ArrayList<String>();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			LogUtil.error("CheckThread.realCheck2.PointStatus.down.Exception:" + e.toString());
		}
	}

	private void virtual_point_status_up_Check() {
		// TODO Auto-generated method stub
		// PointStatus
		// up
		// LogUtil.info("Constant.pointListUp:" + Constant.pointListUp);
		// synchronized (Constant.pointListUp) {
		// if (Constant.pointListUp.size() > 0) {

		// Iterator<Entry<String, Long>> pu =
		// Constant.pointListUp.entrySet().iterator();
		Map<String, Long> pu = new HashMap<String, Long>();
		synchronized (Constant.ps_PointStatusUp) {
			if (Constant.ps_PointStatusUp.size() > 0) {
				pu.putAll(Constant.ps_PointStatusUp);
			}
		}
		for (Entry<String, Long> pointUp : pu.entrySet()) {
			try {
				String building = pointUp.getKey().split("-")[0];
				String point = pointUp.getKey().split("-")[1];

				String[] slist = point.split("\\.");
				StringBuffer sb = new StringBuffer();
				for (int i = 0; i < slist.length - 1; i++) {
					if (i > 0) {
						sb.append(".");
					}
					sb.append(slist[i]);
				}
				String meter = sb.toString();
				String funcid = slist[slist.length - 1];
				String status = "";

				if (pointUp.getValue() != null) {
					if (this.realCheck - pointUp.getValue() < Constant.Millisecond.MINUTE_10) {
						status = "conn";
					} else {
						status = "disconn";
					}
				} else {
					// 首次
					status = "disconn";

				}
				if ("conn".equalsIgnoreCase(status)) {
					this.Insert_ps_virtual_point_status(building, meter, Long.valueOf(funcid), "up", status,
							pointUp.getValue(), "");
				}

				if (
				// 首次
				!this.pointStatus.containsKey(pointUp.getKey()) || null == this.pointStatus.get(pointUp.getKey()))

				{
					this.Insert_ps_virtual_point_log(building, meter, Long.valueOf(funcid), "up",
							System.currentTimeMillis(), 0l, status, "");
					if ("disconn".equalsIgnoreCase(status)) {
						this.Insert_ps_virtual_point_status(building, meter, Long.valueOf(funcid), "up", status,
								pointUp.getValue(), "");
					}
				} else if (
				// 已有记录
				(!status.equalsIgnoreCase(this.pointStatus.get(pointUp.getKey())))) {
					// 添加Log记录
					if (Constant.RecordAddress.containsKey(building + "-" + point.replace(".", "-"))) {
						if (Constant.RecordAddress.get(building + "-" + point.replace(".", "-")) != null) {
							int address = Constant.RecordAddress.get(building + "-" + point.replace(".", "-"));
							this.Insert_ps_virtual_point_log(building, meter, Long.valueOf(funcid), "up",
									pointUp.getValue(), Long.valueOf(Constant.RecordList.get(address).seq), status, "");
							if ("disconn".equalsIgnoreCase(status)) {
								this.Insert_ps_virtual_point_status(building, meter, Long.valueOf(funcid), "up", status,
										pointUp.getValue(), "");
							}
						}
					}
				}
				// 更新Status
				this.pointStatus.put(pointUp.getKey(), status);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				LogUtil.error("CheckThread.virtual_point_status_up_Check.Exception:" + e.toString());
			}
		}
	}

	private void Insert_ps_virtual_point_log(String building, String meter, Long funcid, String updown, Long time,
			Long seq, String status, String feedback) {
		// TODO Auto-generated method stub
		try {
			if (time == null) {
				HBaseUtil.Insert_ps_virtual_point_log(Constant.agent, Constant.BuildingDB.get(building), building,
						meter, Long.valueOf(funcid), updown, Constant.format(new Date()), seq, status, feedback);
			} else {
				HBaseUtil.Insert_ps_virtual_point_log(Constant.agent, Constant.BuildingDB.get(building), building,
						meter, Long.valueOf(funcid), updown, Constant.format(new Date(time)), seq, status, feedback);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// private void physical_status_address_1_Check() {
	// // TODO Auto-generated method stub
	// // physical_status
	// // address_1
	// for (Entry<String, Long> node : Constant.ps_NodeStatus.entrySet()) {
	// try {
	// String building = node.getKey().split("-")[0];
	// String mac = node.getKey().split("-")[1];
	// String status = "";
	// if (node.getValue() != null && this.realCheck - node.getValue() <
	// Constant.Millisecond.MINUTE_1) {
	// status = "conn";
	// HBaseUtil.Insert_ps_physical_status(Constant.agent,
	// Constant.BuildingDB.get(building), building,
	// mac, "", "", "", status, Constant.format(new Date(node.getValue())), "");
	// } else {
	// status = "disconn";
	// HBaseUtil.Insert_ps_physical_status(Constant.agent,
	// Constant.BuildingDB.get(building), building,
	// mac, "", "", "", status, Constant.format(new Date(node.getValue())), "");
	// }
	//
	// if ((!this.nodeStatus.containsKey(node.getKey()))
	// || (!status.equalsIgnoreCase(this.nodeStatus.get(node.getKey())))) {
	// // 添加Log记录
	// HBaseUtil.Insert_ps_physical_log(Constant.agent,
	// Constant.BuildingDB.get(building), building, mac,
	// "", "", "", Constant.format(new Date(node.getValue())), (status));
	// }
	// // 更新Status
	// this.nodeStatus.put(node.getKey(), status);
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// LogUtil.error("CheckThread.realCheck.CollectorStatus.Exception: " +
	// e.toString());
	// }
	// }
	// }

	private void Insert_ps_virtual_point_status(String building, String meter, Long funcid, String updown,
			String status, Long time, String detail) {
		// TODO Auto-generated method stub
		try {
			if (time == null) {
				HBaseUtil.Insert_ps_virtual_point_status(Constant.agent, Constant.BuildingDB.get(building), building,
						meter, Long.valueOf(funcid), "up", status, Constant.format(new Date()), "");

			} else {
				HBaseUtil.Insert_ps_virtual_point_status(Constant.agent, Constant.BuildingDB.get(building), building,
						meter, Long.valueOf(funcid), "up", status, Constant.format(new Date(time)), "");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// private void realCheck() {
	// // TODO Auto-generated method stub
	//
	// if (System.currentTimeMillis() - this.realCheck >
	// Constant.Millisecond.SECOND_3) {
	// this.realCheck = System.currentTimeMillis();
	//
	// try {
	// // CollectorStatus
	// // LogUtil.info("Constant.nodeList:" + Constant.nodeList);
	// for (Entry<String, Long> node : Constant.ps_NodeStatus.entrySet()) {
	// try {
	// String building = node.getKey().split("-")[0];
	// String mac = node.getKey().split("-")[1];
	// String status = "";
	// if (node.getValue() != null
	// && this.realCheck - node.getValue() < Constant.Millisecond.MINUTE_1) {
	// HBaseUtil.InsertCollectorStatus(Constant.agent,
	// Constant.BuildingDB.get(building), building,
	// this.ip, mac, "conn", Constant.format(new Date(node.getValue())));
	// status = "conn";
	// } else {
	// HBaseUtil.InsertCollectorStatus(Constant.agent,
	// Constant.BuildingDB.get(building), building,
	// this.ip, mac, "disconn", Constant.format(new Date(node.getValue())));
	// status = "disconn";
	// }
	//
	// if ((!this.nodeStatus.containsKey(node.getKey()))
	// || (!status.equalsIgnoreCase(this.nodeStatus.get(node.getKey())))) {
	// // 添加Log记录
	// HBaseUtil.InsertCollectorCommunicationLog(Constant.agent,
	// Constant.BuildingDB.get(building),
	// building, this.ip, mac, Constant.format(new Date(node.getValue())),
	// (status));
	// }
	// // 更新Status
	// this.nodeStatus.put(node.getKey(), status);
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// LogUtil.error("CheckThread.realCheck.CollectorStatus.Exception: " +
	// e.toString());
	// }
	// }
	// // PointStatus
	// // up
	// // LogUtil.info("Constant.pointListUp:" + Constant.pointListUp);
	// // synchronized (Constant.pointListUp) {
	// // if (Constant.pointListUp.size() > 0) {
	//
	// // Iterator<Entry<String, Long>> pu =
	// // Constant.pointListUp.entrySet().iterator();
	// Map<String, Long> pu = new HashMap<String, Long>();
	// synchronized (Constant.ps_PointStatusUp) {
	// if (Constant.ps_PointStatusUp.size() > 0) {
	// pu.putAll(Constant.ps_PointStatusUp);
	// }
	// }
	// for (Entry<String, Long> pointUp : pu.entrySet()) {
	// try {
	// String building = pointUp.getKey().split("-")[0];
	// String mac = pointUp.getKey().split("-")[1];
	// String point = pointUp.getKey().split("-")[2];
	// // System.out.println(point);
	// String[] slist = point.split("\\.");
	// StringBuffer sb = new StringBuffer();
	// for (int i = 0; i < slist.length - 1; i++) {
	// if (i > 0) {
	// sb.append(".");
	// }
	// sb.append(slist[i]);
	// }
	// String meter = sb.toString();
	// String funcid = slist[slist.length - 1];
	// // System.out.println(meter);
	// // System.out.println(funcid);
	// String status = "";
	//
	// if (pointUp.getValue() != null
	// && this.realCheck - pointUp.getValue() < Constant.Millisecond.MINUTE_10)
	// {
	// HBaseUtil.InsertPointStatus(Constant.agent,
	// Constant.BuildingDB.get(building), building,
	// this.ip, mac, meter, funcid, "up", "conn",
	// Constant.format(new Date(pointUp.getValue())), "");
	// status = "conn";
	// } else {
	// if (pointUp.getValue() != null) {
	// HBaseUtil.InsertPointStatus(Constant.agent,
	// Constant.BuildingDB.get(building), building,
	// this.ip, mac, meter, funcid, "up", "disconn",
	// Constant.format(new Date(pointUp.getValue())), "");
	// status = "disconn";
	// }
	// }
	// if ((!this.pointStatus.containsKey(pointUp.getKey()))
	// || (!status.equalsIgnoreCase(this.pointStatus.get(pointUp.getKey())))) {
	// // 添加Log记录
	// if (Constant.RecordAddress.containsKey(building + "-" +
	// point.replace(".", "-"))) {
	// int address = Constant.RecordAddress.get(building + "-" +
	// point.replace(".", "-"));
	// if (pointUp.getValue() != null) {
	// HBaseUtil.InsertPointCommunicationLog(Constant.agent,
	// Constant.BuildingDB.get(building), building, this.ip, mac, meter, funcid,
	// "up", (status), Constant.RecordList.get(address).seq,
	// Constant.format(new Date(pointUp.getValue())), "");
	// }
	// }
	//
	// }
	// // 更新Status
	// this.pointStatus.put(pointUp.getKey(), status);
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// LogUtil.error("CheckThread.realCheck.PointStatus.up.Exception:" +
	// e.toString());
	// }
	// }
	// // down
	// try {
	// List<String> nk = new ArrayList<String>();
	// List<String> ps = new ArrayList<String>();
	// synchronized (Constant.ps_ID2Time) {
	// Iterator<Entry<String, Long>> it =
	// Constant.ps_ID2Time.entrySet().iterator();
	// while (it.hasNext()) {
	// try {
	// Entry<String, Long> n = it.next();
	//
	// String building = Constant.ps_ID2Point.get(n.getKey()).split("-")[0];
	// String mac = Constant.ps_ID2Point.get(n.getKey()).split("-")[1];
	// String point = Constant.ps_ID2Point.get(n.getKey()).split("-")[2];
	//
	// String[] slist = point.split("\\.");
	// StringBuffer sb = new StringBuffer();
	// for (int i = 0; i < slist.length - 1; i++) {
	// if (i > 0) {
	// sb.append(".");
	// }
	// sb.append(slist[i]);
	// }
	// String meter = sb.toString();
	// String funcid = slist[slist.length - 1];
	//
	// // String function =
	// // Constant.id2function.get(n.getKey());
	//
	// if (n.getValue() != null
	// && System.currentTimeMillis() - n.getValue() >
	// Constant.Millisecond.SECOND_10) {
	// try {
	// if ("finish:finish:success"
	// .equalsIgnoreCase(Constant.ps_ID2Status.get(n.getKey()))) {
	// // HBaseUtil.InsertPointStatus(Constant.agent,
	// // Constant.BuildingDB.get(building),
	// // building, this.ip, mac, meter,
	// // funcid, "down", "conn",
	// // // function + ":" +
	// // Constant.format(new
	// // Date(Constant.id2time.get(n.getKey()))),
	// // Constant.id2status.get(n.getKey()));
	// ps.add(Constant.BuildingDB.get(building) + ";" + building + ";" + this.ip
	// + ";" + mac + ";" + meter + ";" + funcid + ";" + "down" + ";"
	// + "conn" + ";"
	// + Constant.format(new Date(Constant.ps_ID2Time.get(n.getKey())))
	// + ";" + Constant.ps_ID2Status.get(n.getKey()));
	// } else {
	// // Constant.pointListDown.put(n.getKey(),
	// // null);
	// // HBaseUtil.InsertPointStatus(Constant.agent,
	// // Constant.BuildingDB.get(building),
	// // building, this.ip, mac, meter,
	// // funcid, "down", "disconn",
	// // // function + ":" +
	// // Constant.format(new
	// // Date(Constant.id2time.get(n.getKey()))),
	// // Constant.id2status.get(n.getKey()));
	// ps.add(Constant.BuildingDB.get(building) + ";" + building + ";" + this.ip
	// + ";" + mac + ";" + meter + ";" + funcid + ";" + "down" + ";"
	// + "disconn" + ";"
	// + Constant.format(new Date(Constant.ps_ID2Time.get(n.getKey())))
	// + ";" + Constant.ps_ID2Status.get(n.getKey()));
	// }
	//
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// nk.add(n.getKey());
	// it.remove();
	// }
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// }
	// }
	//
	// synchronized (Constant.ps_ID2Point) {
	// for (String k : nk) {
	// if (Constant.ps_ID2Point.containsKey(k)) {
	// Constant.ps_ID2Point.remove(k);
	// }
	// }
	// }
	// synchronized (Constant.ps_ID2Status) {
	// for (String k : nk) {
	// if (Constant.ps_ID2Status.containsKey(k)) {
	// Constant.ps_ID2Status.remove(k);
	// }
	// }
	// }
	// synchronized (Constant.ps_ID2Function) {
	// for (String k : nk) {
	// if (Constant.ps_ID2Function.containsKey(k)) {
	// Constant.ps_ID2Function.remove(k);
	// }
	// }
	// }
	//
	// nk = new ArrayList<String>();
	// for (String s : ps) {
	// String[] a = s.split(";");
	// HBaseUtil.InsertPointStatus(Constant.agent, a[0], a[1], a[2], a[3], a[4],
	// a[5], a[6], a[7],
	// a[8], a[9]);
	// Thread.sleep(1L);
	// }
	// ps = new ArrayList<String>();
	//
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// LogUtil.error("CheckThread.realCheck.PointStatus.down.Exception:" +
	// e.toString());
	// }
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// LogUtil.error("CheckThread.realCheck.Exception: " + e.toString());
	// }
	// }
	// }

}
