package com.saga.test;

import java.io.File;
import java.io.FileInputStream;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import org.apache.hadoop.conf.Configuration;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import org.zillion.util.log.LogUtil;

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

public class Test {

	public Test() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("1101080002;1;pointset;;66;666666;11024;0".length());
		// String point = "8002.11201";
		// System.out.println(point);
		// String meter = point.split("\\.")[0];
		// String funcid = (point.split("\\.")[1]);
		// System.out.println(meter);
		// System.out.println(funcid);

		// getConfigFile(LogUtil.GetPath(), "config-", "port");
		// System.out.println(Constant.pointInfo);
		// for (Entry<String, String> pi : Constant.pointInfo.entrySet()) {
		// System.out.println(pi.getKey() + " " + pi.getValue());
		// }

		// try {
		// System.out.println(System.currentTimeMillis());
		// Constant.getLocalHostLANAddress();
		// System.out.println(System.currentTimeMillis());
		// } catch (UnknownHostException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// List<String> a = new ArrayList<String>();
		// List<String> b = new ArrayList<String>();
		// a.add("a");
		// a.add("b");
		// a.add("c");
		// b.addAll(a);
		// a = new ArrayList<String>();
		// for (String s : b) {
		// System.out.println(s);
		// }
		// try {
		// String resource = "/hbase-site.xml";
		// Configuration configuration = new Configuration();
		//
		// configuration.addResource(new
		// FileInputStream(System.getProperty("user.dir") + resource));
		// String zookeeper_connectionAddress =
		// "zookeeper1:2181,zookeeper2:2181,zookeeper3:2181";
		// int zookeeper_sessionTimeout = 300000;
		// Constant.agent = new ZillionAgent(configuration,
		// zookeeper_connectionAddress, zookeeper_sessionTimeout);
		//
		// Constant.agent.Start();
		// // LogUtil.info("" + Constant.BuildingDB);
		// // LogUtil.info("" + Constant.BuildingAdds);
		// // LogUtil.info("" + Constant.BuildingConv);
		//
		// } catch (Exception e) {
		// // TODO Auto-generated catch block
		// LogUtil.error("Main: " + e.toString());
		// }
		// while (true) {
		// // 加载采集器配置文件
		// // 文件夹命名格式：config+"-"+楼号+"-"+mac序号，示例：config-1101080002-1
		// Constant.getConfigFile(LogUtil.GetPath(), "config-", "port");
		// // 采集器点位位置信息写入数据库
		// for (Entry<String, String> pi : Constant.pointInfo.entrySet()) {
		// String building = pi.getKey().split("-")[0];
		// String mac = pi.getKey().split("-")[1];
		// String node = mac;
		// String point = pi.getKey().split("-")[2];
		// String busno = pi.getValue().split("-")[0];
		// String site = pi.getValue().split("-")[1];
		// try {
		// System.out.println(building + " " + node + " " + point + " " + busno
		// + " " + site);
		// // HBaseUtil.InsertPointInfo(Constant.agent,
		// // Constant.BuildingDB.get(building), building, node, point,
		// // busno, site);
		// } catch (Exception e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// // System.out.println(building + node + point + busno + site);
		// }
		// try {
		// Thread.sleep(Constant.Millisecond.MINUTE_1);
		// } catch (InterruptedException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// }

	}

	public static List<String> getConfigFile(String directoryPath, String directoryKey, String fileKey) {
		List<String> list = new ArrayList<String>();
		File baseFile = new File(directoryPath);
		if (baseFile.isFile() || !baseFile.exists()) {
			return list;
		}
		try {
			File[] files = baseFile.listFiles();
			SAXReader saxReader = new SAXReader();
			for (File file : files) {

				if (file.isDirectory() && file.getName().contains(directoryKey)) {
					list.addAll(getConfigFile(file.getAbsolutePath(), directoryKey, fileKey));
				} else {
					if (file.getName().contains(fileKey)) {
						list.add(file.getAbsolutePath());
						Document document = saxReader.read(file);
						List<Element> devList = document.selectNodes("/root/portdev/devtype");
						// System.out.println(file.getName());
						// System.out.println(file.getName().indexOf(fileKey));
						// System.out.println(file.getName().indexOf(".xml"));
						String port = file.getName().substring(file.getName().indexOf(fileKey) + fileKey.length(),
								file.getName().indexOf(".xml"));
						// System.out.println(port);
						// System.out.println(file.getParentFile().getName());
						String building = file.getParentFile().getName().replace(directoryKey, "").substring(0, 10);
						String mac = file.getParentFile().getName().replace(directoryKey, "").substring(10);
						// System.out.println(building);
						// System.out.println(mac);

						for (Element portdev : devList) {
							String meter = portdev.attributeValue("meterid");
							Iterator funcList = portdev.selectNodes("funset/funid").iterator();
							while (funcList.hasNext()) {
								Element func = (Element) funcList.next();
								String point = building + "-" + mac + "-" + meter + "." + func.getText();
								String info = port + "-" + portdev.element("addr").getText();
								Constant.pointInfo.put(point, info);
								// System.out.println(meter + "." +
								// func.getText());
							}
						}

					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return list;
	}
}