package com.saga.thread.up;

import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.zillion.util.log.LogUtil;

import com.persagy.communication.entity.Packet;
import com.persagy.communication.entity.PacketEntity;
import com.persagy.communication.util.IServerManager;
import com.saga.entity.PointSet;
import com.saga.entity.Record;
import com.saga.util.Constant;
import com.saga.util.HBaseUtil;

public class SendThread extends Thread {

	private IServerManager uploadServer = null;
	private String ip = null;
	private Map<String, String> pointMac_UP = new HashMap<String, String>();

	public SendThread(IServerManager uploadServer) {
		// TODO Auto-generated constructor stub
		try {
			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();
			}
			this.uploadServer = uploadServer;
			this.uploadServer.Start();
			LogUtil.info("SendThread" + ": " + this.uploadServer.Name() + " Created ...");
		} catch (Exception e) {
			LogUtil.error("SendThread.Exception: " + e.toString());
			LogUtil.error("SendThread" + ": " + this.uploadServer.Name() + " failed to excute, exit!");
			System.exit(0);
		}
	}

	public void run() {
		try {
			this.uploadServer.Start();
		} catch (Exception e) {
			LogUtil.error("SendThread.run.Exception: " + e.toString());
		}
		LogUtil.info("SendThread" + ": " + this.uploadServer.Name() + " Starting ...");
		while (true) {
			try {
				// Constant.setlog = new File(
				// System.getProperty("user.dir") + "/setlog" +
				// Constant.format_day(new Date()) + ".log");
				// // V1.0点位日志结构
				// {
				// // 返pointset结果包
				// this.returnPointSet();
				// // 处理PopRece包
				// PacketEntity packageRece = this.uploadServer.PopRece();
				// if (packageRece != null) {
				// this.handlePopRece(packageRece);
				// }
				// }
				// V2.0点位日志结构
				{
					// 返pointset结果包
					this.returnPointSet2();
					// 处理PopRece包
					PacketEntity packageRece = this.uploadServer.PopRece();
					if (packageRece != null) {
						this.handlePopRece2(packageRece);
					}
				}

				Thread.sleep(1L);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				LogUtil.error("SendThread.run.Exception: " + e.toString());
			}
		}
	}

	private void returnPointSet() {
		// TODO Auto-generated method stub
		try {
			synchronized (Constant.pointSetList) {
				if (Constant.pointSetList.size() > 0) {
					Iterator<PointSet> it = Constant.pointSetList.iterator();
					while (it.hasNext()) {
						StringBuffer sb = new StringBuffer();
						PointSet ps = it.next();
						// String address =
						// Constant.CollectorPointSetList.get(ps.buildingSign +
						// ";"
						// + ps.ID + ";");
						// String address = ps.address;

						if (!ps.uploadName.equalsIgnoreCase(this.uploadServer.Name())) {
							break;
						}

						if (ps.address == null) {
							break;
						}
						String MAC = ps.mac;
						if (this.pointMac_UP.containsKey(ps.buildingSignUP + "-" + ps.meterSign)) {
							MAC = this.pointMac_UP.get(ps.buildingSignUP + "-" + ps.meterSign);
						}
						// 已得到控制设备的反馈状态，移除
						if (ps.recTime > 0 && ps.sendTime > 0 && (ps.status != null && ps.status.length() > 0)) {
							sb.append(ps.buildingSignUP).append(";").append(MAC).append(";").append("pointsetack")
									.append(";").append(Constant.format(new Date())).append(";").append(ps.ID)
									.append(";").append(ps.meterSign).append(";").append(ps.funcID).append(";")
									.append(ps.status).append(";");
							this.uploadServer.AppendToSend(ps.address, new Packet(sb.toString()));
							Constant.sendSend += sb.toString().length();
							HBaseUtil.BatchInsert_Set2(Constant.agent, Constant.BuildingDB.get(ps.buildingSign), ps);
							LogUtil.info(
									this.uploadServer.Name() + " AppendToSend: " + ps.address + " " + sb.toString());

							String id = ps.buildingSignUP + "-" + ps.mac + "-" + ps.ID;
							HBaseUtil.InsertPointCommunicationLog(Constant.agent,
									Constant.BuildingDB.get(ps.buildingSignUP), ps.buildingSignUP, this.ip, ps.mac,
									ps.meterSign, String.valueOf(ps.funcID), "down", "pointsetack",
									String.valueOf(ps.ID), Constant.format(new Date()), "finish:finish:" + ps.status);
							synchronized (Constant.ps_ID2Status) {
								Constant.ps_ID2Status.put(id, "finish:finish:" + ps.status);
							}

							// CSVUtil.insert(Constant.setlog, new
							// StringBuffer(Constant.format_Log(new Date())
							// + ": "
							// + ps.meterSign + "." + ps.funcID + " set " +
							// ps.dataSet + " " + ps.status + "!"
							// + " Consuming:" + (ps.recTime - ps.sendTime <
							// 0 ? 0 : ps.recTime - ps.sendTime)
							// + "ms!" + "\r\n"));
							it.remove();

						} // 超时未得到设备的反馈状态，移除
						else if (ps.recTime == 0 && ps.sendTime > 0
								&& (System.currentTimeMillis() - ps.sendTime > Constant.getWaitingtime())) {
							sb.append(ps.buildingSignUP).append(";").append(MAC).append(";").append("pointsetack")
									.append(";").append(Constant.format(new Date())).append(";").append(ps.ID)
									.append(";").append(ps.meterSign).append(";").append(ps.funcID).append(";")
									.append("fail:timeout").append(";");
							this.uploadServer.AppendToSend(ps.address, new Packet(sb.toString()));
							Constant.sendSend += sb.toString().length();
							HBaseUtil.BatchInsert_Set3(Constant.agent, Constant.BuildingDB.get(ps.buildingSign), ps);
							LogUtil.info(
									this.uploadServer.Name() + " AppendToSend: " + ps.address + " " + sb.toString());
							String id = ps.buildingSignUP + "-" + ps.mac + "-" + ps.ID;

							// LogUtil.info("InsertPointCommunicationLog" + "
							// begin");
							HBaseUtil.InsertPointCommunicationLog(Constant.agent,
									Constant.BuildingDB.get(ps.buildingSignUP), ps.buildingSignUP, this.ip, ps.mac,
									ps.meterSign, String.valueOf(ps.funcID), "down", "pointsetack",
									String.valueOf(ps.ID), Constant.format(new Date()),
									"finish:finish:" + "fail:timeout");

							synchronized (Constant.ps_ID2Status) {
								Constant.ps_ID2Status.put(id, "finish:finish:" + "fail:timeout");
							}
							// LogUtil.info("InsertPointCommunicationLog" + "
							// end");

							// CSVUtil.insert(Constant.setlog,
							// new StringBuffer(
							// Constant.format_Log(new Date()) + ": " +
							// ps.meterSign + "." + ps.funcID
							// + " set " + ps.dataSet + " fail(timeout) " +
							// "!" + " Consuming:"
							// + (System.currentTimeMillis() - ps.sendTime)
							// + "ms!" + "\r\n"));
							it.remove();
						}
						if (sb.length() > 0) {
							// Constant.CollectorPointSetList.remove(ps.buildingSign
							// + ";" + ps.ID + ";");
							break;
							// CSVUtil.insert(Constant.setlog, new
							// StringBuffer(Constant.format_Log(new Date())
							// + ": "
							// + this.uploadClient.Name() + " AppendToSend:
							// " + sb.toString() + "\r\n"));
						}
					}
				}
			}

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

	private void handlePopRece2(PacketEntity packageRece) {
		// TODO Auto-generated method stub
		if ((packageRece != null) && (packageRece.content.packetString != null)
				&& (packageRece.content.packetString.length() > 0)) {
			LogUtil.info(this.uploadServer.Name() + " PopRece: " + packageRece.address + " "
					+ packageRece.content.packetString);
			Constant.sendRec += packageRece.content.packetString.length();
			String results = packageRece.content.packetString.replace("(", "").replace(")", "").replace(" ", "");
			String[] resultArray = results.split("&");

			for (String result : resultArray) {
				String[] rList = result.split(";");
				if (rList.length > 6) {
					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);
					}
					String id = rList[0] + "-" + MAC + "-" + rList[4];
					String key = rList[0] + "-" + MAC + "-" + rList[5] + "." + rList[6];
					// Constant.id2time.put(id, System.currentTimeMillis());
					// Constant.id2point.put(id, key);
					// Constant.id2function.put(id, rList[2]);
					// Constant.id2status.put(id, "processing:rcvd");

					try {
						HBaseUtil.Insert_ps_virtual_point_log(Constant.agent, Constant.BuildingDB.get(rList[0]),
								rList[0], rList[5], Long.valueOf(rList[6]), this.getupdown(rList[2]),
								Constant.format(new Date()), Long.valueOf(rList[4]), rList[2], "processing:rcvd");
					} catch (UnknownHostException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

					if ("pointset".equalsIgnoreCase(rList[2])) {
						// 1101070037;1;pointset;;123;1001;11;3.1
						synchronized (Constant.ps_ID2Time) {
							Constant.ps_ID2Time.put(id, System.currentTimeMillis());
						}
						synchronized (Constant.ps_ID2Point) {
							Constant.ps_ID2Point.put(id, key);
						}
						synchronized (Constant.ps_ID2Function) {
							Constant.ps_ID2Function.put(id, rList[2]);
						}
						synchronized (Constant.ps_ID2Status) {
							Constant.ps_ID2Status.put(id, "processing:rcvd");
						}

						PointSet ps = new PointSet();
						ps.uploadName = this.uploadServer.Name();
						ps.address = packageRece.address;
						ps.content = rList;
						ps.sendTime = 0;
						ps.recTime = 0;
						ps.buildingSign = rList[0];
						ps.buildingSignUP = rList[0];
						// 楼号有转换规则，且转换后的楼-表有数据才进行转换
						// 解决一楼多楼号时总有部分仪表无法下发的问题
						if (Constant.BuildingConv.containsKey(ps.buildingSign) && Constant.Meter_Collector_Control
								.containsKey(Constant.BuildingConv.get(ps.buildingSign) + "-" + ps.meterSign)) {
							ps.buildingSign = Constant.BuildingConv.get(ps.buildingSign);
						}

						ps.ID = Integer.valueOf(rList[4]);
						ps.meterSign = rList[5];
						ps.mac = MAC;
						this.pointMac_UP.put(ps.buildingSignUP + "-" + ps.meterSign, rList[1]);
						ps.funcID = Integer.valueOf(rList[6]);
						ps.dataSet = Double.valueOf(rList[7]);
						// String adress = packageRece.address;
						// String cmd = ps.buildingSign + ";" + ps.ID + ";";
						// +ps.meterSign + ";" + ps.funcID + ";"
						// + ps.dataSet;

						// Constant.CollectorPointSetList.put(cmd, adress);
						synchronized (Constant.pointSetList) {
							Constant.pointSetList.add(ps);
						}

					} else if (rList.length > 6 && "pointread".equalsIgnoreCase(rList[2])) {
						try {
							StringBuffer sb = new StringBuffer();
							sb = this.getCacheData(rList);
							if (sb != null && sb.toString().length() > 0) {
								this.uploadServer.AppendToSend(packageRece.address, new Packet(sb.toString()));
								LogUtil.info(this.uploadServer.Name() + " AppendToSend: " + packageRece.address + " "
										+ sb.toString());
								// Constant.id2status.put(id, "finish:finish:" +
								// "success");
								HBaseUtil.Insert_ps_virtual_point_log(Constant.agent, Constant.BuildingDB.get(rList[0]),
										rList[0], rList[5], Long.valueOf(rList[6]), this.getupdown(rList[2]),
										Constant.format(new Date()), Long.valueOf(rList[4]), "pointreadack",
										"finish:finish:" + "success");

							} else {
								// Constant.id2status.put(id, "finish:finish:" +
								// "fail");
								HBaseUtil.Insert_ps_virtual_point_log(Constant.agent, Constant.BuildingDB.get(rList[0]),
										rList[0], rList[5], Long.valueOf(rList[6]), this.getupdown(rList[2]),
										Constant.format(new Date()), Long.valueOf(rList[4]), "pointreadack",
										"finish:finish:" + "fail");
							}

						} catch (Exception e) {
							e.printStackTrace();
							LogUtil.error("SendThread.handlePopRece2.pointread.Exception: " + e.toString());
						}

					} else if (rList.length > 6 && "realtimepointdata".equalsIgnoreCase(rList[2])) {
						try {
							StringBuffer sb = new StringBuffer();
							sb = this.getCacheData2(rList);
							if (sb != null && sb.toString().length() > 0) {
								this.uploadServer.AppendToSend(packageRece.address, new Packet(sb.toString()));
								LogUtil.info(this.uploadServer.Name() + " AppendToSend: " + packageRece.address + " "
										+ sb.toString());
								// Constant.id2status.put(id, "finish:finish:" +
								// "success");
								HBaseUtil.Insert_ps_virtual_point_log(Constant.agent, Constant.BuildingDB.get(rList[0]),
										rList[0], rList[5], Long.valueOf(rList[6]), this.getupdown(rList[2]),
										Constant.format(new Date()), Long.valueOf(rList[4]), "realtimepointdataack",
										"finish:finish:" + "success");

							} else {
								// Constant.id2status.put(id, "finish:finish:" +
								// "fail");
								HBaseUtil.Insert_ps_virtual_point_log(Constant.agent, Constant.BuildingDB.get(rList[0]),
										rList[0], rList[5], Long.valueOf(rList[6]), this.getupdown(rList[2]),
										Constant.format(new Date()), Long.valueOf(rList[4]), "realtimepointdataack",
										"finish:finish:" + "fail");
							}

						} catch (Exception e) {
							e.printStackTrace();
							LogUtil.error("SendThread.handlePopRece2.realtimepointdata.Exception: " + e.toString());
						}
					}
				}
			}
		}
	}

	private void returnPointSet2() {
		// TODO Auto-generated method stub
		try {
			synchronized (Constant.pointSetList) {
				if (Constant.pointSetList.size() > 0) {
					Iterator<PointSet> it = Constant.pointSetList.iterator();
					while (it.hasNext()) {
						StringBuffer sb = new StringBuffer();
						PointSet ps = it.next();
						// String address =
						// Constant.CollectorPointSetList.get(ps.buildingSign +
						// ";"
						// + ps.ID + ";");
						// String address = ps.address;

						if (!ps.uploadName.equalsIgnoreCase(this.uploadServer.Name())) {
							break;
						}

						if (ps.address == null) {
							break;
						}
						String MAC = ps.mac;
						if (this.pointMac_UP.containsKey(ps.buildingSignUP + "-" + ps.meterSign)) {
							MAC = this.pointMac_UP.get(ps.buildingSignUP + "-" + ps.meterSign);
						}
						// 已得到控制设备的反馈状态，移除
						if (ps.recTime > 0 && ps.sendTime > 0 && (ps.status != null && ps.status.length() > 0)) {
							sb.append(ps.buildingSignUP).append(";").append(MAC).append(";").append("pointsetack")
									.append(";").append(Constant.format(new Date())).append(";").append(ps.ID)
									.append(";").append(ps.meterSign).append(";").append(ps.funcID).append(";")
									.append(ps.status).append(";");
							this.uploadServer.AppendToSend(ps.address, new Packet(sb.toString()));
							Constant.sendSend += sb.toString().length();
							HBaseUtil.BatchInsert_Set2(Constant.agent, Constant.BuildingDB.get(ps.buildingSign), ps);
							LogUtil.info(
									this.uploadServer.Name() + " AppendToSend: " + ps.address + " " + sb.toString());

							String id = ps.buildingSignUP + "-" + ps.mac + "-" + ps.ID;
							HBaseUtil.Insert_ps_virtual_point_log(Constant.agent,
									Constant.BuildingDB.get(ps.buildingSignUP), ps.buildingSignUP, ps.meterSign,
									Long.valueOf(ps.funcID), this.getupdown("pointsetack"), Constant.format(new Date()),
									Long.valueOf(ps.ID), "pointsetack", "finish:finish:" + ps.status);

							synchronized (Constant.ps_ID2Status) {
								Constant.ps_ID2Status.put(id, "finish:finish:" + ps.status);
							}

							// CSVUtil.insert(Constant.setlog, new
							// StringBuffer(Constant.format_Log(new Date())
							// + ": "
							// + ps.meterSign + "." + ps.funcID + " set " +
							// ps.dataSet + " " + ps.status + "!"
							// + " Consuming:" + (ps.recTime - ps.sendTime <
							// 0 ? 0 : ps.recTime - ps.sendTime)
							// + "ms!" + "\r\n"));
							it.remove();

						} // 超时未得到设备的反馈状态，移除
						else if (ps.recTime == 0 && ps.sendTime > 0
								&& (System.currentTimeMillis() - ps.sendTime > Constant.getWaitingtime())) {
							sb.append(ps.buildingSignUP).append(";").append(MAC).append(";").append("pointsetack")
									.append(";").append(Constant.format(new Date())).append(";").append(ps.ID)
									.append(";").append(ps.meterSign).append(";").append(ps.funcID).append(";")
									.append("fail:timeout").append(";");
							this.uploadServer.AppendToSend(ps.address, new Packet(sb.toString()));
							Constant.sendSend += sb.toString().length();
							HBaseUtil.BatchInsert_Set3(Constant.agent, Constant.BuildingDB.get(ps.buildingSign), ps);
							LogUtil.info(
									this.uploadServer.Name() + " AppendToSend: " + ps.address + " " + sb.toString());
							String id = ps.buildingSignUP + "-" + ps.mac + "-" + ps.ID;

							// LogUtil.info("InsertPointCommunicationLog" + "
							// begin");
							HBaseUtil.Insert_ps_virtual_point_log(Constant.agent,
									Constant.BuildingDB.get(ps.buildingSignUP), ps.buildingSignUP, ps.meterSign,
									Long.valueOf(ps.funcID), this.getupdown("pointsetack"), Constant.format(new Date()),
									Long.valueOf(ps.ID), "pointsetack", "finish:finish:" + "fail:timeout");

							synchronized (Constant.ps_ID2Status) {
								Constant.ps_ID2Status.put(id, "finish:finish:" + "fail:timeout");
							}
							// LogUtil.info("InsertPointCommunicationLog" + "
							// end");

							// CSVUtil.insert(Constant.setlog,
							// new StringBuffer(
							// Constant.format_Log(new Date()) + ": " +
							// ps.meterSign + "." + ps.funcID
							// + " set " + ps.dataSet + " fail(timeout) " +
							// "!" + " Consuming:"
							// + (System.currentTimeMillis() - ps.sendTime)
							// + "ms!" + "\r\n"));
							it.remove();
						}
						if (sb.length() > 0) {
							// Constant.CollectorPointSetList.remove(ps.buildingSign
							// + ";" + ps.ID + ";");
							break;
							// CSVUtil.insert(Constant.setlog, new
							// StringBuffer(Constant.format_Log(new Date())
							// + ": "
							// + this.uploadClient.Name() + " AppendToSend:
							// " + sb.toString() + "\r\n"));
						}
					}
				}
			}

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

	private void handlePopRece(PacketEntity packageRece) {
		// TODO Auto-generated method stub
		if ((packageRece != null) && (packageRece.content.packetString != null)
				&& (packageRece.content.packetString.length() > 0)) {
			LogUtil.info(this.uploadServer.Name() + " PopRece: " + packageRece.address + " "
					+ packageRece.content.packetString);
			Constant.sendRec += packageRece.content.packetString.length();
			String results = packageRece.content.packetString.replace("(", "").replace(")", "").replace(" ", "");
			String[] resultArray = results.split("&");

			for (String result : resultArray) {
				String[] rList = result.split(";");
				if (rList.length > 6) {
					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);
					}
					String id = rList[0] + "-" + MAC + "-" + rList[4];
					String key = rList[0] + "-" + MAC + "-" + rList[5] + "." + rList[6];
					// Constant.id2time.put(id, System.currentTimeMillis());
					// Constant.id2point.put(id, key);
					// Constant.id2function.put(id, rList[2]);
					// Constant.id2status.put(id, "processing:rcvd");

					try {
						HBaseUtil.InsertPointCommunicationLog(Constant.agent, Constant.BuildingDB.get(rList[0]),
								rList[0], this.ip, MAC, rList[5], (rList[6]), this.getupdown(rList[2]), rList[2],
								String.valueOf(rList[4]), Constant.format(new Date()), "processing:rcvd");
					} catch (UnknownHostException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

					if ("pointset".equalsIgnoreCase(rList[2])) {
						// 1101070037;1;pointset;;123;1001;11;3.1
						synchronized (Constant.ps_ID2Time) {
							Constant.ps_ID2Time.put(id, System.currentTimeMillis());
						}
						synchronized (Constant.ps_ID2Point) {
							Constant.ps_ID2Point.put(id, key);
						}
						synchronized (Constant.ps_ID2Function) {
							Constant.ps_ID2Function.put(id, rList[2]);
						}
						synchronized (Constant.ps_ID2Status) {
							Constant.ps_ID2Status.put(id, "processing:rcvd");
						}

						PointSet ps = new PointSet();
						ps.uploadName = this.uploadServer.Name();
						ps.address = packageRece.address;
						ps.content = rList;
						ps.sendTime = 0;
						ps.recTime = 0;
						ps.buildingSign = rList[0];
						ps.buildingSignUP = rList[0];
						// 楼号有转换规则，且转换后的楼-表有数据才进行转换
						// 解决一楼多楼号时总有部分仪表无法下发的问题
						if (Constant.BuildingConv.containsKey(ps.buildingSign) && Constant.Meter_Collector_Control
								.containsKey(Constant.BuildingConv.get(ps.buildingSign) + "-" + ps.meterSign)) {
							ps.buildingSign = Constant.BuildingConv.get(ps.buildingSign);
						}

						ps.ID = Integer.valueOf(rList[4]);
						ps.meterSign = rList[5];
						ps.mac = MAC;
						this.pointMac_UP.put(ps.buildingSignUP + "-" + ps.meterSign, rList[1]);
						ps.funcID = Integer.valueOf(rList[6]);
						ps.dataSet = Double.valueOf(rList[7]);
						// String adress = packageRece.address;
						// String cmd = ps.buildingSign + ";" + ps.ID + ";";
						// +ps.meterSign + ";" + ps.funcID + ";"
						// + ps.dataSet;

						// Constant.CollectorPointSetList.put(cmd, adress);
						synchronized (Constant.pointSetList) {
							Constant.pointSetList.add(ps);
						}

					} else if (rList.length > 6 && "pointread".equalsIgnoreCase(rList[2])) {
						try {
							StringBuffer sb = new StringBuffer();
							sb = this.getCacheData(rList);
							if (sb != null && sb.toString().length() > 0) {
								this.uploadServer.AppendToSend(packageRece.address, new Packet(sb.toString()));
								LogUtil.info(this.uploadServer.Name() + " AppendToSend: " + packageRece.address + " "
										+ sb.toString());
								// Constant.id2status.put(id, "finish:finish:" +
								// "success");
								HBaseUtil.InsertPointCommunicationLog(Constant.agent, Constant.BuildingDB.get(rList[0]),
										rList[0], this.ip, rList[1], rList[5], (rList[6]), "down", "pointreadack",
										String.valueOf(rList[4]), Constant.format(new Date()),
										"finish:finish:" + "success");
							} else {
								// Constant.id2status.put(id, "finish:finish:" +
								// "fail");
								HBaseUtil.InsertPointCommunicationLog(Constant.agent, Constant.BuildingDB.get(rList[0]),
										rList[0], this.ip, rList[1], rList[5], (rList[6]), "down", "pointreadack",
										String.valueOf(rList[4]), Constant.format(new Date()),
										"finish:finish:" + "fail");
							}

						} catch (Exception e) {
							e.printStackTrace();
							LogUtil.error("SendThread.handlePopRece.pointread.Exception: " + e.toString());
						}

					} else if (rList.length > 6 && "realtimepointdata".equalsIgnoreCase(rList[2])) {
						try {
							StringBuffer sb = new StringBuffer();
							sb = this.getCacheData2(rList);
							if (sb != null && sb.toString().length() > 0) {
								this.uploadServer.AppendToSend(packageRece.address, new Packet(sb.toString()));
								LogUtil.info(this.uploadServer.Name() + " AppendToSend: " + packageRece.address + " "
										+ sb.toString());
								// Constant.id2status.put(id, "finish:finish:" +
								// "success");
								HBaseUtil.InsertPointCommunicationLog(Constant.agent, Constant.BuildingDB.get(rList[0]),
										rList[0], this.ip, rList[1], rList[5], (rList[6]), "down",
										"realtimepointdataack", String.valueOf(rList[4]), Constant.format(new Date()),
										"finish:finish:" + "success");
							} else {
								// Constant.id2status.put(id, "finish:finish:" +
								// "fail");
								HBaseUtil.InsertPointCommunicationLog(Constant.agent, Constant.BuildingDB.get(rList[0]),
										rList[0], this.ip, rList[1], rList[5], (rList[6]), "down",
										"realtimepointdataack", String.valueOf(rList[4]), Constant.format(new Date()),
										"finish:finish:" + "fail");
							}

						} catch (Exception e) {
							e.printStackTrace();
							LogUtil.error("SendThread.handlePopRece.realtimepointdata.Exception: " + e.toString());
						}
					}
				}
			}
		}
	}

	private String getupdown(String function) {
		// TODO Auto-generated method stub
		if ("report".equalsIgnoreCase(function)) {
			return "up";
		} else if ("reportaddtion".equalsIgnoreCase(function)) {
			return "up";
		} else {
			return "down";
		}

	}

	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;
	}
}