package com.saga.thread.down;

import java.util.ArrayList;
import java.util.Arrays;
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.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Joiner;
import com.persagy.communication.entity.Packet;
import com.persagy.communication.entity.PacketEntity;
import com.persagy.communication.mina.tcp.server.TCPServerManager;
import com.persagy.communication.mina.udp.server.UDPServerManager;
import com.persagy.communication.util.IServerManager;
import com.saga.entity.PointSet;
import com.saga.entity.Record;
import com.saga.entity.RecordMsg;
import com.saga.entity.Server;
import com.saga.util.Constant;
import com.saga.util.MySQLUtil;

/**
 * 下层通信
 *
 * @author 李井强
 */
public class DownThread extends Thread {
	// private int port = 0;
	// private long getPointList = 0;
	private Map<String, Long> getPointList = new HashMap<String, Long>();
	// private String ip = null;
	// private List<IServerManager> serverList = new
	// ArrayList<IServerManager>();
	private Map<String, IServerManager> serverList = new HashMap<String, IServerManager>();

	public DownThread(Server server) {
		try {
			this.serverList.put("UDP", new UDPServerManager(server.IP, server.Port, "gbk", server.compress, 1000));
			this.serverList.put("TCP",
					new TCPServerManager(server.IP, server.Port, "gbk", server.compress, 1000, (byte) '(', (byte) ')'));

		} catch (Exception e) {
			LogUtil.error("DownThread.Exception: " + e.toString());
			LogUtil.error("DownThread(" + server.IP + ":" + server.Port + ") failed to excute, exit!");
			System.exit(0);

		}
	}

	public void run() {
		for (Entry<String, IServerManager> server : this.serverList.entrySet()) {
			LogUtil.info("DownThread(" + server.getValue().Name() + "): " + " Created ...");
			server.getValue().Start();
			LogUtil.info("DownThread(" + server.getValue().Name() + "): " + " Starting ...");
		}
		while (true) {
			try {
				for (Entry<String, IServerManager> server : this.serverList.entrySet()) {
					PacketEntity p = server.getValue().PopRece();
					// // V1.0点位日志结构
					// {
					// // LogUtil.info(server.getKey() + " " +
					// // "DownThread.handlePacket begin");
					// this.handlePacket(server.getValue(), p);
					// // LogUtil.info(server.getKey() + " " +
					// // "DownThread.handlePacket end");
					// // LogUtil.info(server.getKey() + " " +
					// // "DownThread.handlePointSet begin");
					// this.handlePointSet();
					// // LogUtil.info(server.getKey() + " " +
					// // "DownThread.handlePointSet end");
					// }
					// V2.0点位日志结构
					{
						this.handlePacket2(server.getValue(), p);
						this.handlePointSet2();
					}
					Thread.sleep(1L);
				}
				// Thread.sleep(3L);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				LogUtil.error("DownThread.run.Exception: " + e.toString());
			}
		}
	}

	/**
	 * 指令处理
	 */
	private void handlePointSet2() {
		// TODO Auto-generated method stub
		try {
			synchronized (Constant.pointSetList) {
				for (int i = 0; i < Constant.pointSetList.size(); i++) {
					PointSet ps = new PointSet();
					ps = Constant.pointSetList.get(i);
					String address = null;
					if (ps.sendTime == 0) {
						address = Constant.Meter_Collector_Control.get(ps.buildingSign + "-" + ps.meterSign);
						// LogUtil.info("DownThread.handlePointSet " +
						// ps.buildingSign + " " + ps.meterSign + "." +
						// ps.funcID
						// + " " + address);
						if (address != null) {
							for (Entry<String, IServerManager> server : this.serverList.entrySet()) {
								try {
									// String cmd = StringUtils.join(ps.content,
									// ";").replace("pointset", "senddownset")
									// .replace(ps.buildingSignUP,
									// ps.buildingSign);
									// 1101070037;1;senddownset;;123;1001;11;3.1
									StringBuffer sb = new StringBuffer();

									sb.append(ps.buildingSign).append(";").append(ps.mac).append(";senddownset;;")
											.append(ps.ID).append(";").append(ps.meterSign).append(";")
											.append(ps.funcID).append(";").append(ps.dataSet).append(";");

									String cmd = sb.toString();

									String id = ps.buildingSignUP + "-" + ps.mac + "-" + ps.ID;
									Constant.ps_ID2Status.put(id, "processing:sent");
									// HBaseUtil.Insert_ps_virtual_point_log(Constant.agent,
									// Constant.BuildingDB.get(ps.buildingSignUP),
									// ps.buildingSignUP, ps.meterSign,
									// Long.valueOf(ps.funcID), "down",
									// Constant.format(new Date()),
									// Long.valueOf(ps.ID), "senddownset",
									// "processing:sent");
									Constant.Insert_ps_virtual_point_log(Constant.Database, ps.buildingSignUP,
											ps.meterSign, Long.valueOf(ps.funcID), "down", Constant.format(new Date()),
											Long.valueOf(ps.ID), "senddownset", "processing:sent");
									server.getValue().AppendToSend(address, new Packet(cmd));
									Constant.collectSend += cmd.length();
									Constant.pointSetList.get(i).sendTime = System.currentTimeMillis();
									// HBaseUtil.BatchInsert_Set1(Constant.agent,
									// Constant.BuildingDB.get(ps.buildingSign),
									// ps);
									Constant.BatchInsert_Set1(Constant.Database, ps);
									LogUtil.info(server.getValue().Name() + " AppendToSend:" + address + " " + cmd);
									break;
								} catch (Exception e) {
									// TODO Auto-generated catch block
									LogUtil.error("DownThread.handlePointSet.Foreach.Exception: " + e.toString());
								}
							}

						} else {
							LogUtil.warn("DownThread.handlePointSet " + ps.buildingSign + " " + ps.meterSign + "."
									+ ps.funcID + " " + "address is null");
							if (Constant.pointSetList.get(i).sendTime == 0) {
								Constant.pointSetList.get(i).sendTime = System.currentTimeMillis();
							}
						}
						Constant.pointSetList.get(i).sendTime = System.currentTimeMillis();
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			LogUtil.error("DownThread.handlePointSet.Exception: " + e.toString());
		}

	}

	// private void handlePointSet() {
	// // TODO Auto-generated method stub
	// try {
	// synchronized (Constant.pointSetList) {
	// for (int i = 0; i < Constant.pointSetList.size(); i++) {
	// PointSet ps = new PointSet();
	// ps = Constant.pointSetList.get(i);
	// String address = null;
	// if (ps.sendTime == 0) {
	// address = Constant.Meter_Collector_Control.get(ps.buildingSign + "-" +
	// ps.meterSign);
	// // LogUtil.info("DownThread.handlePointSet " +
	// // ps.buildingSign + " " + ps.meterSign + "." +
	// // ps.funcID
	// // + " " + address);
	// if (address != null) {
	// for (Entry<String, IServerManager> server : this.serverList.entrySet()) {
	// try {
	// // String cmd = StringUtils.join(ps.content,
	// // ";").replace("pointset", "senddownset")
	// // .replace(ps.buildingSignUP,
	// // ps.buildingSign);
	// // 1101070037;1;senddownset;;123;1001;11;3.1
	// StringBuffer sb = new StringBuffer();
	//
	// sb.append(ps.buildingSign).append(";").append(ps.mac).append(";senddownset;;")
	// .append(ps.ID).append(";").append(ps.meterSign).append(";")
	// .append(ps.funcID).append(";").append(ps.dataSet).append(";");
	//
	// String cmd = sb.toString();
	//
	// String id = ps.buildingSignUP + "-" + ps.mac + "-" + ps.ID;
	// Constant.ps_ID2Status.put(id, "processing:sent");
	//
	// HBaseUtil.InsertPointCommunicationLog(Constant.agent,
	// Constant.BuildingDB.get(ps.buildingSignUP), ps.buildingSignUP, this.ip,
	// ps.mac, ps.meterSign, String.valueOf(ps.funcID), "down", "senddownset",
	// String.valueOf(ps.ID), Constant.format(new Date()), "processing:sent");
	// server.getValue().AppendToSend(address, new Packet(cmd));
	// Constant.collectSend += cmd.length();
	// Constant.pointSetList.get(i).sendTime = System.currentTimeMillis();
	// HBaseUtil.BatchInsert_Set1(Constant.agent,
	// Constant.BuildingDB.get(ps.buildingSign),
	// ps);
	// LogUtil.info(server.getValue().Name() + " AppendToSend:" + address + " "
	// + cmd);
	// break;
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// LogUtil.error("DownThread.handlePointSet.Exception: " + e.toString());
	// }
	// }
	// }
	// if (Constant.pointSetList.get(i).sendTime == 0) {
	// Constant.pointSetList.get(i).sendTime = System.currentTimeMillis();
	// }
	// }
	// }
	// }
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// LogUtil.error("DownThread.handlePointSet.Exception: " + e.toString());
	// }
	//
	// }

	/**
	 * 数据报文处理
	 */
	private void handlePacket2(IServerManager server, PacketEntity p) {
		// TODO Auto-generated method stub
		try {
			if ((p != null) && (p.content.packetString != null)) {
				// LogUtil.info("handlePacket begin...");
				Constant.collectRec += p.content.packetString.length();
				// p.content.packetString = new
				// String(p.content.packetString.getBytes("utf-8"), "GBK");
				LogUtil.info(server.Name() + " PopRece:" + p.address + " " + p.content.packetString);

				// LogUtil.info("handlePacket split begin...");
				String[] list = p.content.packetString.replace("(", "").replace(")", "").replace(" ", "").split("&");
				// LogUtil.info("handlePacket split end...");
				StringBuffer sb = new StringBuffer();
				// LogUtil.info("DownThread Challenge
				// Constant.CollectReportList begin...");

				// LogUtil.info("DownThread Challenge
				// Constant.CollectReportList get...");
				for (String s : list) {
					// LogUtil.info("handlePacket " + s);
					// LogUtil.info(server.Name() + " PopRece.Split:" +
					// p.address + " " + s);
					if (s.startsWith("/") && s.contains("$")) {
						s = s.substring(s.indexOf("$") + 1);
					}
					String[] rList = s.split(";");

					if ("report".equalsIgnoreCase(rList[2]) && rList.length > 7) {
						// 1101070037;1;report;20010203040506;123;1001;2;11;3.1;12;3.2
						Constant.pointMAC.put(rList[0] + "-" + rList[5], rList[1]);
						Constant.collectCount++;
						Constant.ps_PhysicalStatus.put(rList[0] + ";" + rList[1] + ";;;", System.currentTimeMillis());
						// LogUtil.info("DownThread Challenge
						// Constant.CollectReportList begin...");
						// synchronized (Constant.CollectReportList) {
						// LogUtil.info("DownThread Challenge
						// Constant.CollectReportList get...");
						synchronized (Constant.CollectReportList) {
							if (Constant.BuildingAdds.containsKey(rList[0])) {
								for (String building : Constant.BuildingAdds.get(rList[0])) {
									String newData = s.replace(rList[0], building);
									if (Constant.saveTypeList.contains("report")) {
										Constant.CollectReportList.add(newData);
									}
									if (Constant.TransferReportList.size() < 100000) {
										Constant.TransferReportList.add(newData);
									}

									// LogUtil.info("Constant.CollectReportList
									// add:" + newData);
									// String key = building + "-" + rList[1] +
									// "-" + rList[4];
									// Constant.pointListUp.put(key,
									// System.currentTimeMillis());
								}
							} else {
								if (Constant.saveTypeList.contains("report")) {
									Constant.CollectReportList.add(s);
								}
								if (Constant.TransferReportList.size() < 100000) {
									Constant.TransferReportList.add(s);
								}

								// LogUtil.info("rList[0]:" + rList[0]);
								// String key = rList[0] + "-" + rList[1] + "-"
								// + rList[4];
								// Constant.pointListUp.put(key,
								// System.currentTimeMillis());
							}
						}
						// }
						// LogUtil.info("DownThread Challenge
						// Constant.CollectReportList release...");
						// LogUtil.info("handlePacket pushData begin...");
						this.pushData(rList);
						/// LogUtil.info("handlePacket pushData end...");
						// LogUtil.info("handlePacket updateMeterCollector
						/// begin...");
						this.updateMeterCollector(rList[0], rList[5], p.address);
						// LogUtil.info("handlePacket updateMeterCollector
						// end...");
						// if (this.needAck(server)) {
						this.waitAck();
						if (sb.length() > 0) {
							sb.append("&" + rList[0] + ";" + rList[1] + ";" + "reportack" + ";"
									+ Constant.format(new Date()) + ";" + rList[4]);
						} else {
							sb.append(rList[0] + ";" + rList[1] + ";" + "reportack" + ";" + Constant.format(new Date())
									+ ";" + rList[4]);
						}
						// }
					} else if ("reportaddtion".equalsIgnoreCase(rList[2]) && rList.length > 7) {
						Constant.pointMAC.put(rList[0] + "-" + rList[5], rList[1]);
						Constant.collectCount++;
						Constant.ps_PhysicalStatus.put(rList[0] + ";" + rList[1] + ";;;", System.currentTimeMillis());
						// LogUtil.info("DownThread Challenge
						// Constant.CollectReportList begin...");
						// synchronized (Constant.CollectReportList) {
						// LogUtil.info("DownThread Challenge
						// Constant.CollectReportList get...");
						synchronized (Constant.CollectReportList) {
							if (Constant.BuildingAdds.containsKey(rList[0])) {
								for (String building : Constant.BuildingAdds.get(rList[0])) {
									String newData = s.replace(rList[0], building);
									if (Constant.saveTypeList.contains("report")) {
										Constant.CollectReportList.add(newData);
									}

									if (Constant.TransferReportList.size() < 100000) {
										Constant.TransferReportList.add(newData);
									}

									// LogUtil.info("Constant.CollectReportList
									// add:" + newData);
									// String key = building + "-" + rList[1] +
									// "-" + rList[4];
									// Constant.pointListUp.put(key,
									// System.currentTimeMillis());
								}
							} else {
								if (Constant.saveTypeList.contains("report")) {
									Constant.CollectReportList.add(s);
								}

								if (Constant.TransferReportList.size() < 100000) {
									Constant.TransferReportList.add(s);
								}

								// LogUtil.info("rList[0]:" + rList[0]);
								// String key = rList[0] + "-" + rList[1] + "-"
								// + rList[4];
								// Constant.pointListUp.put(key,
								// System.currentTimeMillis());
							}
						}

						// }
						// LogUtil.info("DownThread Challenge
						// Constant.CollectReportList release...");
						// LogUtil.info("handlePacket pushData begin...");
						this.pushDataAddtion(rList);
						// LogUtil.info("handlePacket pushData end...");
						// LogUtil.info("handlePacket updateMeterCollector
						// begin...");
						this.updateMeterCollector(rList[0], rList[5], p.address);
						// LogUtil.info("handlePacket updateMeterCollector
						// end...");
						// LogUtil.info("handlePacket Ack begin...");
						// if (this.needAck(server)) {
						this.waitAck();
						if (sb.length() > 0) {
							sb.append("&" + rList[0] + ";" + rList[1] + ";" + "reportaddtionack" + ";"
									+ Constant.format(new Date()) + ";" + rList[4]);
						} else {
							sb.append(rList[0] + ";" + rList[1] + ";" + "reportaddtionack" + ";"
									+ Constant.format(new Date()) + ";" + rList[4]);
						}
						// }
						// LogUtil.info("handlePacket Ack end...");
					} else if ("message".equalsIgnoreCase(rList[2]) && rList.length > 7) {
						this.pushMsgData(rList);
						if (Constant.TransferReportList.size() < 100000) {
							Constant.TransferReportList.add(s);
						}
					} else if ("alarmReport".equalsIgnoreCase(rList[2]) && rList.length > 5) {
						this.pushAlarmData(rList);
					} else if ("recoveryReport".equalsIgnoreCase(rList[2]) && rList.length > 5) {
						this.pushAlarmRecoveryData(rList);
					} else if ("senddownsetack".equalsIgnoreCase(rList[2])) {
						String id = rList[0] + "-" + rList[1] + "-" + rList[4];
						synchronized (Constant.ps_ID2Status) {
							Constant.ps_ID2Status.put(id, "processing:finish:" + rList[7]);
						}
						this.updatePointSet(rList);
						// 1101070037;1;senddownsetack;20010203040506;123;1001;11;success
						String MAC = rList[1];

						if (Constant.pointMAC.containsKey(rList[0] + "-" + rList[5])) {
							MAC = Constant.pointMAC.get(rList[0] + "-" + rList[5]);
							LogUtil.info("MAC:" + (rList[0] + "-" + rList[5]) + " " + rList[1] + "-->" + MAC);
						}
						// HBaseUtil.Insert_ps_virtual_point_log(Constant.agent,
						// Constant.BuildingDB.get(rList[0]),
						// rList[0], rList[5], Long.valueOf(rList[6]), "down",
						// Constant.format(new Date()),
						// Long.valueOf(rList[4]), "senddownsetack",
						// "processing:finish:" + rList[7]);
						Constant.Insert_ps_virtual_point_log(Constant.Database, rList[0], rList[5],
								Long.valueOf(rList[6]), "down", Constant.format(new Date()), Long.valueOf(rList[4]),
								"senddownsetack", "processing:finish:" + rList[7]);

					} else if ("connect".equalsIgnoreCase(rList[2])) {
						sb.append(rList[0] + ";" + rList[1] + ";" + "connectack" + ";" + Constant.format(new Date()));
						// server.AppendToSend(p.address, new Packet(
						// rList[0] + ";" + rList[1] + ";" + "connectack" +
						// ";"
						// + Constant.format(new Date())));
						// LogUtil.info(server.Name() + " AppendToSend:" +
						// p.address + " " + rList[0] + ";" + rList[1]
						// + ";" + "connectack" + ";" + Constant.format(new
						// Date()));
						Constant.ps_PhysicalStatus.put(rList[0] + ";" + rList[1] + ";;;", System.currentTimeMillis());
						// this.insertCollectorCommunicationLog(rList);

						// 1101070037;1;pointcount;;19317
						int id = Constant.getID();
						server.AppendToSend(p.address,
								new Packet(rList[0] + ";" + rList[1] + ";" + "pointcount" + ";" + "" + ";" + id));
						LogUtil.info(server.Name() + " AppendToSend:" + p.address + " " + rList[0] + ";" + rList[1]
								+ ";" + "pointcount" + ";" + "" + ";" + id);
					} else if ("synchronization".equalsIgnoreCase(rList[2])) {
						//// 1101070037;1;synchronizationack;;123;50;1001;11;20181213000000;1...

						for (Record r : Constant.RecordList) {
							if (rList[0].equalsIgnoreCase(r.buildingSign)) {

								int id = Constant.getID();
								server.AppendToSend(p.address,
										new Packet(rList[0] + ";" + rList[1] + ";" + "synchronizationack" + ";" + ""
												+ ";" + id + ";1;" + r.meterSign + ";" + r.funcID + ";"
												+ Constant.format(r.receivetime) + ";" + r.data + ";"));
								LogUtil.info(server.Name() + " AppendToSend:" + p.address + " "
										+ (rList[0] + ";" + rList[1] + ";" + "synchronizationack" + ";" + "" + ";" + id
												+ ";1;" + r.meterSign + ";" + r.funcID + ";"
												+ Constant.format(r.receivetime) + ";" + r.data + ";"));
							}
						}

					} else if ("heart".equalsIgnoreCase(rList[2])) {
						sb.append(rList[0] + ";" + rList[1] + ";" + "heartack" + ";" + Constant.format(new Date()));
						// server.AppendToSend(p.address, new Packet(
						// rList[0] + ";" + rList[1] + ";" + "heartack" +
						// ";" +
						// Constant.format(new Date())));
						// LogUtil.info(server.Name() + " AppendToSend:" +
						// p.address + " " + rList[0] + ";" + rList[1]
						// + ";" + "heartack" + ";" + Constant.format(new
						// Date()));
						Constant.ps_PhysicalStatus.put(rList[0] + ";" + rList[1] + ";;;", System.currentTimeMillis());
						// this.insertCollectorCommunicationLog(rList);
						this.getPointList(p.address, rList);

					} else if ("pointcountack".equalsIgnoreCase(rList[2])) {
						int count = Integer.valueOf(rList[5]);
						int loop = (count / 50) + 1;
						for (int i = 0; i < loop; i++) {
							// 1101070037;1;pointlist;;19317;50;11;2
							int id = Constant.getID();
							server.AppendToSend(p.address, new Packet(rList[0] + ";" + rList[1] + ";" + "pointlist"
									+ ";" + "" + ";" + id + ";" + 50 + ";" + loop + ";" + (i + 1)));
							LogUtil.info(server.Name() + " AppendToSend:" + p.address + " " + rList[0] + ";" + rList[1]
									+ ";" + "pointlist" + ";" + "" + ";" + id + ";" + 50 + ";" + loop + ";" + (i + 1));

						}
					} else if ("pointlistack".equalsIgnoreCase(rList[2])) {
						// 6666666666;1;pointlistack;;4353;50;11;1;50;206007;11021;206007;11014;206007;11015;206007;11025;206007;11024;206007;11011;206007;11023;206007;11022;206007;11013;206007;11012;208002;11011;208002;11013;208002;11024;208002;11023;208002;11015;208002;11012;208002;11022;208002;11014;208002;11025;208002;11021;503001;11012;503001;11021;503001;11022;503001;11015;503001;11011;503001;11025;503001;11014;503001;11023;503001;11013;503001;11024;201004;11022;201004;11025;201004;11013;201004;11011;201004;11015;201004;11023;201004;11024;201004;11021;201004;11012;201004;11014;204001;11021;204001;11023;204001;11011;204001;11025;204001;11022;204001;11012;204001;11015;204001;11013;204001;11014;204001;11024;
						int count = Integer.valueOf(rList[8]);
						String building = rList[0];
						// String mac = rList[1];
						Map<String, String> meterList = new HashMap<String, String>();
						for (int i = 0; i < count; i++) {
							meterList.put((rList[9 + 2 * i]), "");
							String meter = rList[9 + 2 * i];
							String funcid = rList[10 + 2 * i];
							String key = building + "-" + meter + "." + funcid;
							// synchronized (Constant.pointListUp) {
							if (!Constant.ps_PointStatusUp.containsKey(key)) {
								Constant.ps_PointStatusUp.put(key, null);
							}
							// }
							// if (!Constant.pointListDown.containsKey(key))
							// {
							// Constant.pointListDown.put(key, null);
							// }

						}
						this.updateMeterCollectorControl(rList[0], meterList, p.address);
					} else if (rList.length > 6 && "pointread".equalsIgnoreCase(rList[2])) {
						// 1101070037;1;pointread;;123;1001;11
						// String key = rList[0] + "-" + rList[1] + "-" +
						// rList[5] + "." + rList[6];
						// String id = rList[0] + "-" + rList[1] + "-" +
						// rList[4];
						// Constant.id2time.put(key,
						// System.currentTimeMillis());
						// Constant.id2point.put(id, key);
						// Constant.id2function.put(id, "pointread");
						// Constant.id2status.put(id, "processing:rcvd");

						StringBuffer sb1 = new StringBuffer();
						sb1 = this.getCacheData(rList);
						server.AppendToSend(p.address, new Packet(sb1.toString()));
						LogUtil.info(server.Name() + " AppendToSend: " + p.address + " " + sb.toString());
					} else if (rList.length > 6 && "realtimepointdata".equalsIgnoreCase(rList[2])) {
						// String key = rList[0] + "-" + rList[1] + "-" +
						// rList[5] + "." + rList[6];
						// String id = rList[0] + "-" + rList[1] + "-" +
						// rList[4];
						// Constant.id2time.put(key,
						// System.currentTimeMillis());
						// Constant.id2point.put(id, key);
						// Constant.id2function.put(id, "realtimepointdata");
						// Constant.id2status.put(id, "processing:rcvd");

						StringBuffer sb1 = new StringBuffer();
						sb1 = this.getCacheData2(rList);
						server.AppendToSend(p.address, new Packet(sb1.toString()));
						LogUtil.info(server.Name() + " AppendToSend: " + p.address + " " + sb.toString());
					} else if ("getdbpointlist".equalsIgnoreCase(rList[2])) {
						// 1101070037;1;getdbpointlist
						// 1101070037;1;getdbpointlistack;1001;2;0;1
						String building = rList[0];
						// for (Entry<String, List<Integer>> mfs :
						// HBaseUtil.GetDBPointList(Constant.agent,
						// Constant.BuildingDB.get(building), "dy_pointlist",
						// building).entrySet()) {
						for (Entry<String, List<Integer>> mfs : Constant
								.GetDBPointList2Cache(Constant.Database, "dy_pointlist", building).entrySet()) {
							StringBuffer sb1 = new StringBuffer();
							sb1.append(building).append(";").append(rList[1]).append(";").append("getdbpointlistack")
									.append(";").append(mfs.getKey()).append(";").append(mfs.getValue().size());
							for (Integer f : mfs.getValue()) {
								sb1.append(";").append(f);
							}

							server.AppendToSend(p.address, new Packet(sb1.toString()));
							LogUtil.info(server.Name() + " AppendToSend: " + p.address + " " + sb.toString());
						}

					} else if ("updatedbpointlist".equalsIgnoreCase(rList[2])) {
						// 1101070037;1;updatedbpointlist;1001;2;0;Acc;1;Inst
						String building = rList[0];
						String meter = rList[3];
						int count = Integer.valueOf(rList[4]);
						for (int i = 0; i < count; i++) {
							// HBaseUtil.Insert_dy_pointlist(Constant.agent,
							// Constant.BuildingDB.get(building), building,
							// meter, Integer.valueOf(rList[5 + 2 * i]), "其他",
							// "第三方", p.address.split(":")[0],
							// p.address, p.address.split(":")[1], "", "",
							// "15min", rList[6 + 2 * i], "");
							Constant.Insert_dy_pointlist(building, meter, Integer.valueOf(rList[5 + 2 * i]), "其他",
									"第三方", p.address.split(":")[0], p.address, p.address.split(":")[1], "", "", "15min",
									rList[6 + 2 * i], "");
						}
					}
				}
				LogUtil.info("DownThread Challenge Constant.CollectReportList release...");

				if (sb.length() > 0) {
					server.AppendToSend(p.address, new Packet(sb.toString()));
					LogUtil.info(server.Name() + " AppendToSend:" + p.address + " " + sb.toString());
					Constant.collectSend += sb.toString().length();
				}
				// LogUtil.info("handlePacket end...");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			LogUtil.error("DownThread.handlePacket.Exception: " + e.toString());
		}
	}

	private void waitAck() {
		// TODO Auto-generated method stub
		while (true) {
			try {
				if (Constant.CollectReportList.size() < 500000) {
					return;
				}
				LogUtil.warn("wait Report to DB ...");
				Thread.sleep(3L);
			} catch (Exception e) {
			}
		}
	}

	/**
	 * 生成pointlist
	 */
	private void getPointList(String address, String[] rList) {
		// TODO Auto-generated method stub
		String key = rList[0] + "-" + rList[1] + "-" + address;
		Long getTime = 0l;
		if (!this.getPointList.containsKey(key)) {
			this.getPointList.put(key, getTime);
		}
		if (this.needGetPointList(this.getPointList.get(key))) {
			LogUtil.warn(key + " needGetPointList true ...");
			int id = Constant.getID();
			for (IServerManager server : this.serverList.values()) {
				try {
					server.AppendToSend(address,
							new Packet(rList[0] + ";" + rList[1] + ";" + "pointcount" + ";" + "" + ";" + id));
					LogUtil.info(server.Name() + " AppendToSend:" + address + " " + rList[0] + ";" + rList[1] + ";"
							+ "pointcount" + ";" + "" + ";" + id);
				} catch (Exception e) {
					// TODO Auto-generated catch block

				}
			}
			this.getPointList.put(key, System.currentTimeMillis());
		}
	}

	// private void handlePacket(IServerManager server, PacketEntity p) {
	// // TODO Auto-generated method stub
	// try {
	// if ((p != null) && (p.content.packetString != null)) {
	// // LogUtil.info("handlePacket begin...");
	// Constant.collectRec += p.content.packetString.length();
	// LogUtil.info(server.Name() + " PopRece:" + p.address + " " +
	// p.content.packetString);
	// // LogUtil.info("handlePacket split begin...");
	// String[] list = p.content.packetString.replace("(", "").replace(")",
	// "").replace(" ", "").split("&");
	// // LogUtil.info("handlePacket split end...");
	// StringBuffer sb = new StringBuffer();
	// // LogUtil.info("DownThread Challenge
	// // Constant.CollectReportList begin...");
	//
	// // LogUtil.info("DownThread Challenge
	// // Constant.CollectReportList get...");
	// for (String s : list) {
	// // LogUtil.info("handlePacket " + s);
	// // LogUtil.info(server.Name() + " PopRece.Split:" +
	// // p.address + " " + s);
	// String[] rList = s.split(";");
	// if ("report".equalsIgnoreCase(rList[2]) && rList.length > 7) {
	// // 1101070037;1;report;20010203040506;123;1001;2;11;3.1;12;3.2
	// Constant.pointMAC.put(rList[0] + "-" + rList[5], rList[1]);
	// Constant.collectCount++;
	// Constant.ps_NodeStatus.put(rList[0] + "-" + rList[1],
	// System.currentTimeMillis());
	// // LogUtil.info("DownThread Challenge
	// // Constant.CollectReportList begin...");
	// // synchronized (Constant.CollectReportList) {
	// // LogUtil.info("DownThread Challenge
	// // Constant.CollectReportList get...");
	// synchronized (Constant.CollectReportList) {
	// if (Constant.BuildingAdds.containsKey(rList[0])) {
	// for (String building : Constant.BuildingAdds.get(rList[0])) {
	// String newData = s.replace(rList[0], building);
	// Constant.CollectReportList.add(newData);
	// // LogUtil.info("Constant.CollectReportList
	// // add:" + newData);
	// // String key = building + "-" + rList[1] +
	// // "-" + rList[4];
	// // Constant.pointListUp.put(key,
	// // System.currentTimeMillis());
	// }
	// } else {
	// Constant.CollectReportList.add(s);
	// // LogUtil.info("rList[0]:" + rList[0]);
	// // String key = rList[0] + "-" + rList[1] + "-"
	// // + rList[4];
	// // Constant.pointListUp.put(key,
	// // System.currentTimeMillis());
	// }
	// }
	// // }
	// // LogUtil.info("DownThread Challenge
	// // Constant.CollectReportList release...");
	// // LogUtil.info("handlePacket pushData begin...");
	// this.pushData(rList);
	// /// LogUtil.info("handlePacket pushData end...");
	// // LogUtil.info("handlePacket updateMeterCollector
	// /// begin...");
	// this.updateMeterCollector(rList[0], rList[5], p.address);
	// // LogUtil.info("handlePacket updateMeterCollector
	// // end...");
	// // if (this.needAck(server)) {
	// if (sb.length() > 0) {
	// sb.append("&" + rList[0] + ";" + rList[1] + ";" + "reportack" + ";"
	// + Constant.format(new Date()) + ";" + rList[4]);
	// } else {
	// sb.append(rList[0] + ";" + rList[1] + ";" + "reportack" + ";" +
	// Constant.format(new Date())
	// + ";" + rList[4]);
	// }
	// // }
	// } else if ("reportaddtion".equalsIgnoreCase(rList[2]) && rList.length >
	// 7) {
	// Constant.pointMAC.put(rList[0] + "-" + rList[5], rList[1]);
	// Constant.collectCount++;
	// Constant.ps_NodeStatus.put(rList[0] + "-" + rList[1],
	// System.currentTimeMillis());
	// // LogUtil.info("DownThread Challenge
	// // Constant.CollectReportList begin...");
	// // synchronized (Constant.CollectReportList) {
	// // LogUtil.info("DownThread Challenge
	// // Constant.CollectReportList get...");
	// synchronized (Constant.CollectReportList) {
	// if (Constant.BuildingAdds.containsKey(rList[0])) {
	// for (String building : Constant.BuildingAdds.get(rList[0])) {
	// String newData = s.replace(rList[0], building);
	// Constant.CollectReportList.add(newData);
	// // LogUtil.info("Constant.CollectReportList
	// // add:" + newData);
	// // String key = building + "-" + rList[1] +
	// // "-" + rList[4];
	// // Constant.pointListUp.put(key,
	// // System.currentTimeMillis());
	// }
	// } else {
	// Constant.CollectReportList.add(s);
	// // LogUtil.info("rList[0]:" + rList[0]);
	// // String key = rList[0] + "-" + rList[1] + "-"
	// // + rList[4];
	// // Constant.pointListUp.put(key,
	// // System.currentTimeMillis());
	// }
	// }
	//
	// // }
	// // LogUtil.info("DownThread Challenge
	// // Constant.CollectReportList release...");
	// // LogUtil.info("handlePacket pushData begin...");
	// this.pushDataAddtion(rList);
	// // LogUtil.info("handlePacket pushData end...");
	// // LogUtil.info("handlePacket updateMeterCollector
	// // begin...");
	// this.updateMeterCollector(rList[0], rList[5], p.address);
	// // LogUtil.info("handlePacket updateMeterCollector
	// // end...");
	// // LogUtil.info("handlePacket Ack begin...");
	// // if (this.needAck(server)) {
	// if (sb.length() > 0) {
	// sb.append("&" + rList[0] + ";" + rList[1] + ";" + "reportaddtionack" +
	// ";"
	// + Constant.format(new Date()) + ";" + rList[4]);
	// } else {
	// sb.append(rList[0] + ";" + rList[1] + ";" + "reportaddtionack" + ";"
	// + Constant.format(new Date()) + ";" + rList[4]);
	// }
	// // }
	// // LogUtil.info("handlePacket Ack end...");
	// } else if ("senddownsetack".equalsIgnoreCase(rList[2])) {
	// String id = rList[0] + "-" + rList[1] + "-" + rList[4];
	// synchronized (Constant.ps_ID2Status) {
	// Constant.ps_ID2Status.put(id, "processing:finish:" + rList[7]);
	// }
	// this.updatePointSet(rList);
	// // 1101070037;1;senddownsetack;20010203040506;123;1001;11;success
	// String MAC = rList[1];
	//
	// if (Constant.pointMAC.containsKey(rList[0] + "-" + rList[5])) {
	// MAC = Constant.pointMAC.get(rList[0] + "-" + rList[5]);
	// LogUtil.info("MAC:" + (rList[0] + "-" + rList[5]) + " " + rList[1] +
	// "-->" + MAC);
	// }
	// HBaseUtil.InsertPointCommunicationLog(Constant.agent,
	// Constant.BuildingDB.get(rList[0]),
	// rList[0], this.ip, MAC, rList[5], (rList[6]), "down", "senddownsetack",
	// rList[4],
	// Constant.format(new Date()), "processing:finish:" + rList[7]);
	//
	// // LogUtil.info("id: " + id);
	// // LogUtil.info("InsertPointCommunicationLog" + "
	// // begin");
	//
	// // if
	// // (!Constant.id2status.get(id).contains("finish:finish"))
	// // {
	// // if ("success".equalsIgnoreCase(rList[7])) {
	// // Constant.id2status.put(id, "finish:finish:" +
	// // rList[7]);
	// // } else {
	//
	// // }
	// // }
	//
	// // LogUtil.info("InsertPointCommunicationLog" + "
	// // end");
	//
	// } else if ("connect".equalsIgnoreCase(rList[2])) {
	// sb.append(rList[0] + ";" + rList[1] + ";" + "connectack" + ";" +
	// Constant.format(new Date()));
	// // server.AppendToSend(p.address, new Packet(
	// // rList[0] + ";" + rList[1] + ";" + "connectack" +
	// // ";"
	// // + Constant.format(new Date())));
	// // LogUtil.info(server.Name() + " AppendToSend:" +
	// // p.address + " " + rList[0] + ";" + rList[1]
	// // + ";" + "connectack" + ";" + Constant.format(new
	// // Date()));
	// Constant.ps_NodeStatus.put(rList[0] + "-" + rList[1],
	// System.currentTimeMillis());
	// // this.insertCollectorCommunicationLog(rList);
	//
	// // 1101070037;1;pointcount;;19317
	// int id = Constant.getID();
	// server.AppendToSend(p.address,
	// new Packet(rList[0] + ";" + rList[1] + ";" + "pointcount" + ";" + "" +
	// ";" + id));
	// LogUtil.info(server.Name() + " AppendToSend:" + p.address + " " +
	// rList[0] + ";" + rList[1]
	// + ";" + "pointcount" + ";" + "" + ";" + id);
	// } else if ("heart".equalsIgnoreCase(rList[2])) {
	// sb.append(rList[0] + ";" + rList[1] + ";" + "heartack" + ";" +
	// Constant.format(new Date()));
	// // server.AppendToSend(p.address, new Packet(
	// // rList[0] + ";" + rList[1] + ";" + "heartack" +
	// // ";" +
	// // Constant.format(new Date())));
	// // LogUtil.info(server.Name() + " AppendToSend:" +
	// // p.address + " " + rList[0] + ";" + rList[1]
	// // + ";" + "heartack" + ";" + Constant.format(new
	// // Date()));
	// Constant.ps_NodeStatus.put(rList[0] + "-" + rList[1],
	// System.currentTimeMillis());
	// // this.insertCollectorCommunicationLog(rList);
	//
	// // if (this.needGetPointList()) {
	// int id = Constant.getID();
	// server.AppendToSend(p.address,
	// new Packet(rList[0] + ";" + rList[1] + ";" + "pointcount" + ";" + "" +
	// ";" + id));
	// LogUtil.info(server.Name() + " AppendToSend:" + p.address + " " +
	// rList[0] + ";" + rList[1]
	// + ";" + "pointcount" + ";" + "" + ";" + id);
	// // }
	//
	// } else if ("pointcountack".equalsIgnoreCase(rList[2])) {
	// int count = Integer.valueOf(rList[5]);
	// int loop = (count / 50) + 1;
	// for (int i = 0; i < loop; i++) {
	// // 1101070037;1;pointlist;;19317;50;11;2
	// int id = Constant.getID();
	// server.AppendToSend(p.address, new Packet(rList[0] + ";" + rList[1] + ";"
	// + "pointlist"
	// + ";" + "" + ";" + id + ";" + 50 + ";" + loop + ";" + (i + 1)));
	// LogUtil.info(server.Name() + " AppendToSend:" + p.address + " " +
	// rList[0] + ";" + rList[1]
	// + ";" + "pointlist" + ";" + "" + ";" + id + ";" + 50 + ";" + loop + ";" +
	// (i + 1));
	//
	// }
	// } else if ("pointlistack".equalsIgnoreCase(rList[2])) {
	// //
	// 6666666666;1;pointlistack;;4353;50;11;1;50;206007;11021;206007;11014;206007;11015;206007;11025;206007;11024;206007;11011;206007;11023;206007;11022;206007;11013;206007;11012;208002;11011;208002;11013;208002;11024;208002;11023;208002;11015;208002;11012;208002;11022;208002;11014;208002;11025;208002;11021;503001;11012;503001;11021;503001;11022;503001;11015;503001;11011;503001;11025;503001;11014;503001;11023;503001;11013;503001;11024;201004;11022;201004;11025;201004;11013;201004;11011;201004;11015;201004;11023;201004;11024;201004;11021;201004;11012;201004;11014;204001;11021;204001;11023;204001;11011;204001;11025;204001;11022;204001;11012;204001;11015;204001;11013;204001;11014;204001;11024;
	// int count = Integer.valueOf(rList[8]);
	// String building = rList[0];
	// String mac = rList[1];
	// Map<String, String> meterList = new HashMap<String, String>();
	// for (int i = 0; i < count; i++) {
	// meterList.put((rList[9 + 2 * i]), "");
	// String meter = rList[9 + 2 * i];
	// String funcid = rList[10 + 2 * i];
	// String key = building + "-" + mac + "-" + meter + "." + funcid;
	// // synchronized (Constant.pointListUp) {
	// if (!Constant.ps_PointStatusUp.containsKey(key)) {
	// Constant.ps_PointStatusUp.put(key, null);
	// }
	// // }
	// // if (!Constant.pointListDown.containsKey(key))
	// // {
	// // Constant.pointListDown.put(key, null);
	// // }
	//
	// }
	// this.updateMeterCollectorControl(rList[0], meterList, p.address);
	// } else if (rList.length > 6 && "pointread".equalsIgnoreCase(rList[2])) {
	// // 1101070037;1;pointread;;123;1001;11
	// // String key = rList[0] + "-" + rList[1] + "-" +
	// // rList[5] + "." + rList[6];
	// // String id = rList[0] + "-" + rList[1] + "-" +
	// // rList[4];
	// // Constant.id2time.put(key,
	// // System.currentTimeMillis());
	// // Constant.id2point.put(id, key);
	// // Constant.id2function.put(id, "pointread");
	// // Constant.id2status.put(id, "processing:rcvd");
	//
	// StringBuffer sb1 = new StringBuffer();
	// sb1 = this.getCacheData(rList);
	// server.AppendToSend(p.address, new Packet(sb1.toString()));
	// LogUtil.info(server.Name() + " AppendToSend: " + p.address + " " +
	// sb.toString());
	// } else if (rList.length > 6 &&
	// "realtimepointdata".equalsIgnoreCase(rList[2])) {
	// // String key = rList[0] + "-" + rList[1] + "-" +
	// // rList[5] + "." + rList[6];
	// // String id = rList[0] + "-" + rList[1] + "-" +
	// // rList[4];
	// // Constant.id2time.put(key,
	// // System.currentTimeMillis());
	// // Constant.id2point.put(id, key);
	// // Constant.id2function.put(id, "realtimepointdata");
	// // Constant.id2status.put(id, "processing:rcvd");
	//
	// StringBuffer sb1 = new StringBuffer();
	// sb1 = this.getCacheData2(rList);
	// server.AppendToSend(p.address, new Packet(sb1.toString()));
	// LogUtil.info(server.Name() + " AppendToSend: " + p.address + " " +
	// sb.toString());
	// } else if ("getdbpointlist".equalsIgnoreCase(rList[2])) {
	// // 1101070037;1;getdbpointlist
	// // 1101070037;1;getdbpointlistack;1001;2;0;1
	// String building = rList[0];
	// for (Entry<String, List<Integer>> mfs :
	// HBaseUtil.GetDBPointList(Constant.agent,
	// Constant.BuildingDB.get(building), "dy_pointlist", building).entrySet())
	// {
	// StringBuffer sb1 = new StringBuffer();
	// sb1.append(building).append(";").append(rList[1]).append(";").append("getdbpointlistack")
	// .append(";").append(mfs.getKey()).append(";").append(mfs.getValue().size());
	// for (Integer f : mfs.getValue()) {
	// sb1.append(";").append(f);
	// }
	//
	// server.AppendToSend(p.address, new Packet(sb1.toString()));
	// LogUtil.info(server.Name() + " AppendToSend: " + p.address + " " +
	// sb.toString());
	// }
	//
	// } else if ("updatedbpointlist".equalsIgnoreCase(rList[2])) {
	// // 1101070037;1;updatedbpointlist;1001;2;0;Acc;1;Inst
	// String building = rList[0];
	// String meter = rList[3];
	// int count = Integer.valueOf(rList[4]);
	// Map<Integer, String> funcidList = new HashMap<Integer, String>();
	// for (int i = 0; i < count; i++) {
	// funcidList.put(Integer.valueOf(rList[5 + 2 * i]), rList[6 + 2 * i]);
	// }
	// HBaseUtil.InsertDBPointList(Constant.agent,
	// Constant.BuildingDB.get(building), building, meter,
	// funcidList);
	// }
	// }
	// LogUtil.info("DownThread Challenge Constant.CollectReportList
	// release...");
	//
	// if (sb.length() > 0) {
	// server.AppendToSend(p.address, new Packet(sb.toString()));
	// LogUtil.info(server.Name() + " AppendToSend:" + p.address + " " +
	// sb.toString());
	// Constant.collectSend += sb.toString().length();
	// }
	// // LogUtil.info("handlePacket end...");
	// }
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// LogUtil.error("DownThread.handlePacket.Exception: " + e.toString());
	// }
	// }

	/**
	 * 是否需要获取pointlist
	 */
	private boolean needGetPointList(Long getTime) {
		// TODO Auto-generated method stub
		LogUtil.info(System.currentTimeMillis() + " " + getTime + " "
				+ (System.currentTimeMillis() - getTime) / Constant.Millisecond.MINUTE_1);
		if (System.currentTimeMillis() - getTime > Constant.Millisecond.MINUTE_5) {
			return true;
		} else {
			return false;
		}
	}

	// private void insertCollectorStatus(String[] rList) {
	// // TODO Auto-generated method stub
	// try {
	// HBaseUtil.InsertCollectorStatus(Constant.agent,
	// Constant.BuildingDB.get(rList[0]), rList[0],
	// Constant.getLocalHostLANAddress().getHostAddress(), rList[1], "");
	// } catch (UnknownHostException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// }

	// private void insertCollectorCommunicationLog(String[] rList) {
	// // TODO Auto-generated method stub
	// try {
	// HBaseUtil.InsertCollectorCommunicationLog(Constant.agent,
	// Constant.BuildingDB.get(rList[0]), rList[0],
	// this.ip, rList[1], Constant.format(new Date()), rList[2]);
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// LogUtil.error("DownThread.insertCollectorCommunicationLog.Exception: "
	// + e.toString());
	// }
	// }

	/**
	 * 数据解析
	 */
	private void pushData(String[] rList) {
		// TODO Auto-generated method stub
		// 1101070037;1;report;20010203040506;123;1001;2;11;3.1;12;3.2
		int count = Integer.valueOf(rList[6]);
		String buildingSign = rList[0];
		String mac = rList[1];
		String meterSign = rList[5];
		String receivetime = rList[3];
		String seq = rList[4];

		for (int i = 0; i < count; i++) {
			try {
				if (rList[8 + i * 2].contains("ff")) {
					continue;
				}
				int funcID = Integer.valueOf(rList[7 + i * 2]);
				double data = Double.valueOf(rList[8 + i * 2]);
				// this.handleData(buildingSign, mac, meterSign, funcID,
				// receivetime, data);
				this.handleDataAddtion(buildingSign, mac, meterSign, funcID, receivetime, data, seq, "");
				if (Constant.BuildingAdds.containsKey(buildingSign)) {
					for (String building : Constant.BuildingAdds.get(buildingSign)) {
						// this.handleData(building, mac, meterSign, funcID,
						// receivetime, data);
						this.handleDataAddtion(building, mac, meterSign, funcID, receivetime, data, seq, "");
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				LogUtil.error("DownThread.pushData.Exception: " + e.toString());
			}
		}
	}

	/**
	 * 数据解析
	 */
	private void pushDataAddtion(String[] rList) {
		// TODO Auto-generated method stub
		// 1101070037;1;report;20010203040506;123;1001;2;11;3.1;12;3.2
		int count = Integer.valueOf(rList[6]);
		String buildingSign = rList[0];
		String mac = rList[1];
		String meterSign = rList[5];
		String receivetime = rList[3];
		String seq = rList[4];
		for (int i = 0; i < count; i++) {
			try {
				if (rList[8 + i * 2].contains("ff")) {
					continue;
				}

				int funcID = Integer.valueOf(rList[7 + i * 3]);
				double data = Double.valueOf(rList[8 + i * 3]);
				String addtion = "null".equalsIgnoreCase(rList[9 + i * 3]) ? "" : (rList[9 + i * 3]);
				// HBaseUtil.InsertPointCommunicationLog(Constant.agent,
				// Constant.BuildingDB.get(buildingSign),
				// buildingSign,
				// Constant.getLocalHostLANAddress().getHostAddress(), mac,
				// meterSign + "." + funcID,
				// "reportaddtion", id, Constant.format(new Date()), addtion);
				// this.handleData(buildingSign, mac, meterSign, funcID,
				// receivetime, data);
				this.handleDataAddtion(buildingSign, mac, meterSign, funcID, receivetime, data, seq, addtion);
				if (Constant.BuildingAdds.containsKey(buildingSign)) {
					for (String building : Constant.BuildingAdds.get(buildingSign)) {
						// this.handleData(building, mac, meterSign, funcID,
						// receivetime, data);
						this.handleDataAddtion(building, mac, meterSign, funcID, receivetime, data, seq, addtion);
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				LogUtil.error("DownThread.pushData2.Exception: " + e.toString());
			}
		}
	}

	private void pushMsgData(String[] rList) {
		int count = Integer.valueOf(rList[6]);
		String buildingSign = rList[0];
		// String mac = rList[1];
		String meterSign = rList[5];
		String receivetime = rList[3];
		// String seq = rList[4];
		if (!Constant.saveTypeList.contains("message")) {
			return;
		}
		try {
			List<RecordMsg> dataList = new ArrayList<RecordMsg>();

			int funcID = Integer.valueOf(rList[7]);
			String data = Joiner.on(";").join(Arrays.copyOfRange(rList, 8, rList.length));
			RecordMsg record = new RecordMsg();
			record.buildingSign = buildingSign;
			record.meterSign = meterSign;
			record.funcID = funcID;
			record.receivetime = Constant.parse(receivetime);
			record.data = data;
			dataList.add(record);

			Constant.MsgBatchInsert(Constant.Database, dataList);

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

	private void pushAlarmData(String[] rList) {
		try {
			String buildingSign = rList[0];
			// String mac = rList[1];
			String receivetime = rList[3];
			// String seq = rList[4];
			// String data = rList[5];
			String data = (new String(rList[5].getBytes("gbk"), "utf-8"));
			Constant.unicodeDecode(rList[5]);
			Map d = (Map) JSON.parse(data);
			// meterId
			String meterSign = d.get("meterId").toString();
			int funcID = 932;
			List<RecordMsg> dataList = new ArrayList<RecordMsg>();
			List<RecordMsg> dataList2 = new ArrayList<RecordMsg>();
			RecordMsg record = new RecordMsg();
			record.buildingSign = buildingSign;
			record.meterSign = meterSign;
			record.funcID = funcID;
			record.receivetime = Constant.parse(receivetime);
			String alarmPositionNameOld = d.get("alarmPositionName").toString();
			String alarmOwnerPositionNameOld = d.get("alarmOwnerPositionName").toString();
			String alarmPositionName = Constant.xiangsidu(alarmPositionNameOld, Constant.xuzhousuning());
			String alarmOwnerPositionName = Constant.xiangsidu(alarmOwnerPositionNameOld, Constant.xuzhousuning());
			data = data.replace(alarmPositionNameOld, alarmPositionName);
			data = data.replace(alarmOwnerPositionNameOld, alarmOwnerPositionName);
			record.data = data;
			dataList.add(record);
			// Constant.MsgBatchInsert(Constant.Database, dataList);
			MySQLUtil.MsgBatchInsertMonth(Constant.Database, dataList);
			String presentData = MySQLUtil.MsgBatchSelectPresent(Constant.Database, record);
			List l = new ArrayList();
			if (presentData != null) {
				l = (List) JSON.parse(presentData);
			}
			Map dc = (Map) JSON.parse(record.data);
			l.add(dc);
			record.data = JSON.toJSONString(l);
			dataList2.add(record);
			MySQLUtil.MsgBatchInsertPresent(Constant.Database, dataList2);

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

	private void pushAlarmRecoveryData(String[] rList) {
		// 3203020001;1;recoveryReport;20191214010544;13;ada86ed3-1adf-42b2-8d51-075b039d7989
		try {
			String buildingSign = rList[0];
			// String mac = rList[1];
			String receivetime = rList[3];
			// String seq = rList[4];
			String data = rList[5];
			RecordMsg record = new RecordMsg();
			record.data = data;
			record.buildingSign = buildingSign;
			// String data = (new String(rList[5].getBytes("gbk"), "utf-8"));
			String meterSign = MySQLUtil.MsgBatchSelectPresentByID(Constant.Database, record);
			// Map d = (Map) JSON.parse(data);
			// String meterSign = d.get("meterId").toString();
			int funcID = 932;
			List<RecordMsg> dataList = new ArrayList<RecordMsg>();
			List<RecordMsg> dataList2 = new ArrayList<RecordMsg>();

			record.meterSign = meterSign;
			record.funcID = funcID;
			record.receivetime = Constant.parse(receivetime);
			// record.data = data;
			dataList.add(record);

			// Constant.MsgBatchDelte(Constant.Database, dataList);
			MySQLUtil.MsgBatchInsertMonth(Constant.Database, dataList);
			String presentData = MySQLUtil.MsgBatchSelectPresent(Constant.Database, record);
			// Map dc = (Map) JSON.parse(record.data);
			if (presentData != null) {
				List l = (List) JSON.parse(presentData);
				Iterator it = l.iterator();
				while (it.hasNext()) {
					JSONObject msg = (JSONObject) it.next();
					if (msg.get("id").toString().equalsIgnoreCase(data)) {
						it.remove();
					}
				}
				record.data = JSON.toJSONString(l);
				dataList2.add(record);
				MySQLUtil.MsgBatchInsertPresent(Constant.Database, dataList2);
			}

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

	// private void handleData(String buildingSign, String mac, String
	// meterSign, int funcID, String receivetime,
	// double data) {
	// // TODO Auto-generated method stub
	// try {
	// if (Constant.RecordAddress.get(buildingSign + "-" + meterSign + "-" +
	// funcID) != null) {
	// int address = Constant.RecordAddress.get(buildingSign + "-" + meterSign +
	// "-" + funcID);
	//
	// Constant.RecordList.get(address).receivetime =
	// Constant.parse(receivetime);
	// Constant.RecordList.get(address).data = data;
	// String key = buildingSign + "-" + mac + "-" + meterSign + "." + funcID;
	// Constant.pointListUp.put(key, System.currentTimeMillis());
	// // LogUtil.info((address + 1) + "/" + Constant.RecordList.size()
	// // + " UPDATE "
	// // + Constant.RecordList.get(address).buildingSign + "."
	// // + Constant.RecordList.get(address).meterSign + "." +
	// // Constant.RecordList.get(address).funcID
	// // + ":" +
	// // Constant.format(Constant.RecordList.get(address).receivetime)
	// // + " "
	// // + Constant.RecordList.get(address).data);
	// } else {
	// Record record = new Record();
	// record.buildingSign = buildingSign;
	// record.meterSign = meterSign;
	// record.funcID = funcID;
	// record.receivetime = Constant.parse(receivetime);
	// record.data = data;
	// Constant.RecordAddress.put(buildingSign + "-" + meterSign + "-" + funcID,
	// Constant.RecordList.size());
	// Constant.RecordList.add(record);
	// String key = buildingSign + "-" + mac + "-" + meterSign + "." + funcID;
	// Constant.pointListUp.put(key, System.currentTimeMillis());
	// // LogUtil.info(Constant.RecordList.size() + "/" +
	// // Constant.RecordList.size() + " ADD "
	// // + record.buildingSign + "." + record.meterSign + "." +
	// // record.funcID + ":"
	// // + Constant.format(record.receivetime) + " " + record.data);
	//
	// }
	//
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// LogUtil.error("DownThread.handleData.Exception: " + e.toString());
	// }
	// }

	/**
	 * 数据写入缓存
	 */
	private void handleDataAddtion(String buildingSign, String mac, String meterSign, int funcID, String receivetime,
			double data, String seq, String addtion) {
		// TODO Auto-generated method stub
		try {
			if (Constant.RecordAddress.get(buildingSign + "-" + meterSign + "-" + funcID) != null) {
				int address = Constant.RecordAddress.get(buildingSign + "-" + meterSign + "-" + funcID);
				// if (Constant.RecordList.get(address).receivetime.getTime() >
				// Constant.parse(receivetime).getTime()) {
				// return;
				// }
				Constant.RecordList.get(address).receivetime = Constant.parse(receivetime);
				Constant.RecordList.get(address).data = data;
				Constant.RecordList.get(address).seq = seq;
				// String key = buildingSign + "-" + mac + "-" + meterSign + "."
				// + funcID;
				// synchronized (Constant.pointListUp) {
				// Constant.pointListUp.put(key, System.currentTimeMillis());
				// }
				// LogUtil.info((address + 1) + "/" + Constant.RecordList.size()
				// + " UPDATE "
				// + Constant.RecordList.get(address).buildingSign + "."
				// + Constant.RecordList.get(address).meterSign + "." +
				// Constant.RecordList.get(address).funcID
				// + ":" +
				// Constant.format(Constant.RecordList.get(address).receivetime)
				// + " "
				// + Constant.RecordList.get(address).data);
			} else {
				Record record = new Record();
				record.buildingSign = buildingSign;
				record.meterSign = meterSign;
				record.funcID = funcID;
				record.receivetime = Constant.parse(receivetime);
				record.data = data;
				record.seq = seq;
				record.addtion = addtion;
				Constant.RecordAddress.put(buildingSign + "-" + meterSign + "-" + funcID, Constant.RecordList.size());
				Constant.RecordList.add(record);
				// String key = buildingSign + "-" + mac + "-" + meterSign + "."
				// + funcID;
				// synchronized (Constant.pointListUp) {
				// Constant.pointListUp.put(key, System.currentTimeMillis());
				// }
				// LogUtil.info(Constant.RecordList.size() + "/" +
				// Constant.RecordList.size() + " ADD "
				// + record.buildingSign + "." + record.meterSign + "." +
				// record.funcID + ":"
				// + Constant.format(record.receivetime) + " " + record.data);

			}

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

	/**
	 * 更新设定信息
	 */
	private void updatePointSet(String[] rList) {
		// TODO Auto-generated method stub
		// 7777777777;1;senddownsetack;20171102152549;123;1001;0;success
		synchronized (Constant.pointSetList) {
			for (int i = 0; i < Constant.pointSetList.size(); i++) {
				PointSet ps = Constant.pointSetList.get(i);
				if (ps.sendTime > 0 && ps.buildingSign.equalsIgnoreCase(rList[0])
						&& ps.ID == Integer.valueOf(rList[4])) {
					Constant.pointSetList.get(i).recTime = System.currentTimeMillis();
					Constant.pointSetList.get(i).status = rList[7];
					break;
				}
			}
		}
	}

	// private void updateMeterCollector(String building, Map<String, String>
	// meterList, String address) {
	// // TODO Auto-generated method stub
	// for (Entry<String, String> meter : meterList.entrySet()) {
	// this.updateMeterCollector(building, meter.getKey(), address);
	// }
	// }

	/**
	 * 更新Meter-Collector记录
	 */
	private void updateMeterCollectorControl(String building, Map<String, String> meterList, String address) {
		// TODO Auto-generated method stub
		for (Entry<String, String> meter : meterList.entrySet()) {
			this.updateMeterCollectorControl(building, meter.getKey(), address);
		}
	}

	/**
	 * 更新Meter-Collector记录
	 */
	private void updateMeterCollector(String building, String meter, String address) {
		// TODO Auto-generated method stub
		try {
			// if (Constant.Meter_Collector.get(meter) != null) {
			// Constant.Meter_Collector.get(meter).PROTOCOL = protocol;
			// Constant.Meter_Collector.get(meter).IP = address.split(":")[0];
			// Constant.Meter_Collector.get(meter).Port =
			// Integer.valueOf(address.split(":")[1]);
			// } else {
			// Collector c = new Collector();
			// c.PROTOCOL = protocol;
			// c.IP = address.split(":")[0];
			// c.Port = Integer.valueOf(address.split(":")[1]);
			// Constant.Meter_Collector.put(meter, c);
			// }
			Constant.Meter_Collector.put(building + "-" + meter, address);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			LogUtil.error("DownThread.updateMeterCollector.Exception: " + e.toString());
		}
	}

	/**
	 * 更新Meter-Collector记录
	 */
	private void updateMeterCollectorControl(String building, String meter, String address) {
		// TODO Auto-generated method stub
		try {
			// if (Constant.Meter_Collector.get(meter) != null) {
			// Constant.Meter_Collector.get(meter).PROTOCOL = protocol;
			// Constant.Meter_Collector.get(meter).IP = address.split(":")[0];
			// Constant.Meter_Collector.get(meter).Port =
			// Integer.valueOf(address.split(":")[1]);
			// } else {
			// Collector c = new Collector();
			// c.PROTOCOL = protocol;
			// c.IP = address.split(":")[0];
			// c.Port = Integer.valueOf(address.split(":")[1]);
			// Constant.Meter_Collector.put(meter, c);
			// }
			Constant.Meter_Collector_Control.put(building + "-" + meter, address);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			LogUtil.error("DownThread.updateMeterCollector.Exception: " + e.toString());
		}
	}

	// private boolean needAck(IServerManager server) {
	// // TODO Auto-generated method stub
	// try {
	// if (server.Name().contains("8887")) {
	// return false;
	// }
	// if (Constant.DBConn = false) {
	// return false;
	// }
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// LogUtil.error("DownThread.needAck.Exception: " + e.toString());
	// }
	// return true;
	// }

	/**
	 * 获取缓存数据
	 */
	private StringBuffer getCacheData(String[] rList) {
		// TODO Auto-generated method stub
		Record record = new Record();
		StringBuffer sb = new StringBuffer();
		if (Constant.RecordAddress.get(rList[0] + "-" + rList[5] + "-" + rList[6]) != null) {
			int address = Constant.RecordAddress.get(rList[0] + "-" + rList[5] + "-" + rList[6]);
			record = Constant.RecordList.get(address);
			// Constant.info("get pointread by address !");
		} else {
			for (Record r : Constant.RecordList) {
				if (rList[0].equalsIgnoreCase(r.buildingSign) && rList[5].equalsIgnoreCase(r.meterSign)
						&& Integer.valueOf(rList[6]) == r.funcID) {
					record = r;
					// Constant.info("get pointread by for circle !");
					break;
				}
			}
		}
		sb.append(rList[0]).append(";").append(rList[1]).append(";").append("pointreadack").append(";")
				.append(Constant.format(record.receivetime)).append(";").append(rList[4]).append(";")
				.append(record.meterSign).append(";").append(record.funcID).append(";").append(record.data).append(";");
		return sb;
	}

	/**
	 * 获取缓存数据
	 */
	private StringBuffer getCacheData2(String[] rList) {
		// TODO Auto-generated method stub
		Record record = new Record();
		StringBuffer sb = new StringBuffer();
		if (Constant.RecordAddress.get(rList[0] + "-" + rList[5] + "-" + rList[6]) != null) {
			int address = Constant.RecordAddress.get(rList[0] + "-" + rList[5] + "-" + rList[6]);
			record = Constant.RecordList.get(address);
			// Constant.info("get pointread by address !");
		} else {
			for (Record r : Constant.RecordList) {
				if (rList[0].equalsIgnoreCase(r.buildingSign) && rList[5].equalsIgnoreCase(r.meterSign)
						&& Integer.valueOf(rList[6]) == r.funcID) {
					record = r;
					// Constant.info("get pointread by for circle !");
					break;
				}
			}
		}
		sb.append(rList[0]).append(";").append(rList[1]).append(";").append("realtimepointdataack").append(";")
				.append(Constant.format(record.receivetime)).append(";").append(rList[4]).append(";")
				.append(record.meterSign).append(";").append(record.funcID).append(";").append(record.data).append(";");
		return sb;
	}

	// private boolean needHandle(Date receivetime) {
	// // TODO Auto-generated method stub
	// try {
	// if (receivetime.getSeconds() % this.collectInterval < 5) {
	// return true;
	// }
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return false;
	// }
	private Integer getUUIDInOrderId(String uuid) {
		Integer orderId = uuid.replace("-", "").hashCode();
		orderId = orderId < 0 ? -orderId : orderId;
		return orderId;
	}
}