package com.saga.test;

import org.zillion.util.log.LogUtil;

import com.persagy.communication.entity.Packet;
import com.persagy.communication.entity.PacketEntity;
import com.persagy.communication.mina.udp.server.UDPServerManager;
import com.persagy.communication.util.IServerManager;
import com.saga.util.Constant;

public class Simulator {

	private static final String MAC = "AC-CF-23-E9-3C-22";
	private static long needSend = 0l;
	private static IServerManager simulator = new UDPServerManager("0.0.0.0", 9756, "utf-8", 1000);

	private static boolean needSend() {
		if (System.currentTimeMillis() - needSend > Constant.Millisecond.MINUTE_1) {
			needSend = System.currentTimeMillis();
			return true;
		}
		return false;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		simulator.Start();
		simulator.AllowJustSend_UDP(true);
		while (true) {
			PacketEntity p;
			p = simulator.PopRece();
			returnReport(p);
			sendReport();

		}
	}

	private static void returnReport(PacketEntity p) {
		// TODO Auto-generated method stub
		try {
			if ((p != null) && (p.content != null) && (p.content.packetString != null)
					&& (p.content.packetString.length() > 0)) {
				LogUtil.info(simulator.Name() + " PopRece:" + p.address + " " + p.content.packetString);
				String[] rList = p.content.packetString.replace("(", "").replace(")", "").replace(" ", "").split(";");
				// (00-50-56-C0-00-08; control;1255581314001;2;21;0;22;1;)
				// (00-50-56-C0-00-08;controlack;1255581314001;1;)
				if ("control".equalsIgnoreCase(rList[1])) {
					simulator.AppendToSend(p.address,
							new Packet("(" + rList[0] + ";" + "controlack" + ";" + rList[2] + ";1;" + ")"));
					LogUtil.info(simulator.Name() + " AppendToSend:" + p.address + " " + "(" + rList[0] + ";"
							+ "reportack" + ";" + rList[2] + ")");
					// (00-50-56-C0-00-08; get;1255581314001;)
					// (00-50-56-C0-00-08;getack;1255581314001;9;11;22.3;12;33.3;13;32.3;14;22.2;
					// 21;1;22;0;23;0;24;0;25;1)
				} else if ("get".equalsIgnoreCase(rList[1])) {
					simulator.AppendToSend(p.address, new Packet("(" + rList[0] + ";" + "getack" + ";" + rList[2] + ";"
							+ "9;11;22.3;12;33.3;13;32.3;14;22.2;21;1;22;0;23;0;24;0;25;1)"));
					LogUtil.info(simulator.Name() + " AppendToSend:" + p.address + " " + "(" + rList[0] + ";" + "getack"
							+ ";" + rList[2] + ";" + "9;11;22.3;12;33.3;13;32.3;14;22.2;21;1;22;0;23;0;24;0;25;1)");

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

	private static void sendReport() {
		// TODO Auto-generated method stub
		if (needSend()) {
			StringBuffer sb = new StringBuffer();
			sb.append("(");
			sb.append(MAC);
			sb.append(";report;");
			sb.append(System.currentTimeMillis());
			sb.append(";9;11;22.3;12;33.3;13;32.3;14;22.2;21;1;22;0;23;0;24;0;25;1)");
			simulator.AppendToSend("/192.168.20.159:8756", new Packet(sb.toString()));
			LogUtil.info(simulator.Name() + " AppendToSend:" + "/192.168.20.159:80" + " " + sb.toString());
		}
	}

}
