package com.xt.hb.data.gps.svcimpl;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Service;

import com.xt.hb.data.gps.model.GpsInfo;
import com.xt.hb.data.gps.svc.GpsService;
import com.xt.hb.data.system.GlobalData;

@Service
public class GpsServiceImpl implements GpsService {
	@Override
	public List<GpsInfo> getGPSRealTime() {
		List<GpsInfo> gpsInfos = analyseGpsData();
		return gpsInfos;
	}

	/**
	 * 获取分析后的结果数据
	 * 
	 * @return
	 */
	public List<GpsInfo> analyseGpsData() {
		List<GpsInfo> modelList = new ArrayList<>();
		try {
			SAXReader reader = new SAXReader();
			Document doc = reader.read(callWebService());
			// FileWriter fw = new FileWriter(new File("gps" +
			// System.currentTimeMillis() + ".xml"));
			// doc.write(fw);
			// fw.close();
			@SuppressWarnings("unchecked")
			List<Element> elements = doc.selectNodes("//Table");
			for (Element el : elements) {
				GpsInfo model = new GpsInfo();
				model.setGpsid(el.element("GPSID").getText());
				model.setRecvdate(Long.parseLong(el.element("RECVDATE").getText()));
				model.setRecvtime(Long.parseLong(el.element("RECVTIME").getText()));
				model.setJd(Long.parseLong(el.element("JD").getText()));
				model.setWd(Long.parseLong(el.element("WD").getText()));
				model.setGd(Long.parseLong(el.element("GD").getText()));
				model.setSd(Long.parseLong(el.element("SD").getText()));
				model.setFx(Integer.parseInt(el.element("FX").getText()));
				Element alarm = el.element("ALARM");
				if (alarm != null) {
					model.setAlarm(alarm.getText());
				}
				Element segment = el.element("SEGMENT");
				if (segment != null) {
					model.setSegment(segment.getText());
				}
				Element areaid = el.element("AREAID");
				if (areaid != null) {
					model.setAreaid(areaid.getText());
				}
				Element status1 = el.element("STATUS1");
				if (status1 != null) {
					model.setStatus1(status1.getText());
				}
				Element status2 = el.element("STATUS2");
				if (status2 != null) {
					model.setStatus2(status2.getText());
				}
				modelList.add(model);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelList;
	}

	/**
	 * 获取测试数据
	 * 
	 * @return
	 */
	public InputStream getDataStream() {
		return GpsServiceImpl.class.getResourceAsStream("GetGPSRealTime.xml");
	}

	/**
	 * 获取真实请求数据
	 * 
	 * @param wsdl
	 * @return
	 * @throws Exception
	 */
	private InputStream callWebService() throws Exception {
		System.setProperty("sun.net.client.defaultConnectTimeout", "20000");
		System.setProperty("sun.net.client.defaultReadTimeout", "20000");
		// URL连接
		URL url = new URL(GlobalData.GPS_WSDL);
		// System.err.println("gps connect url:"+url.getPath());
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		// conn.setRequestMethod("GET");
		conn.setRequestMethod("POST");
		conn.setRequestProperty("Content-Length", String.valueOf(actionBySOAP().getBytes().length));
		conn.setRequestProperty("Content-Type", "text/xml; charset=GBK");
		conn.setDoOutput(true);
		conn.setDoInput(true);
		conn.setConnectTimeout(20000);
		// 请求输入内容
		OutputStream output = conn.getOutputStream();
		output.write(actionBySOAP().getBytes());
		output.flush();
		output.close();
		// 请求返回内容
		return conn.getInputStream();
	}

	/**
	 * soap请求头拼接
	 * 
	 * @return
	 */
	private String actionBySOAP() {
		StringBuilder sb = new StringBuilder();
		sb.append(
				"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" "
						+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">");
		sb.append("<SOAP-ENV:Body>");
		sb.append("<m:GetGPSRealTime xmlns:m=\"http://pdiwt.com.cn/webservices/Oracle\"/>");
		sb.append("</SOAP-ENV:Body>");
		sb.append("</SOAP-ENV:Envelope>");
		return sb.toString();
	}
}
