package com.szpg.rmi;

import com.opensymphony.xwork2.ActionSupport;
import com.szpg.db.dao.PgAcuDao;
import com.szpg.db.dao.impl.PgAcuDaoImpl;
import com.szpg.db.data.PgAcu;
import com.szpg.plc.message.AppMessageConstants;
import com.szpg.plc.message.command.ReadMemoryCommand;
import com.szpg.plc.message.command.write.SetZmOffBitCommand;
import com.szpg.plc.message.command.write.SetZmOnBitCommand;
import com.szpg.plc.protocol.DTProtocolInterface;
import com.szpg.plc.protocol.ProtocolFactory;
import com.szpg.plc.protocol.fins.FINSConstants;
import com.szpg.plc.server.ACUClient;
import com.szpg.plc.server.ACUClientUtil;
import com.szpg.plc.util.ByteUtil;
import com.szpg.service.ReadControllerStatusService;
import com.szpg.service.RemoteDeviceControlService;
import com.szpg.service.command.LightCommandService;
import com.szpg.util.Configure;
import com.szpg.util.HttpRequest;
import com.szpg.util.UnicodeConvertor;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import z.json.JSONArray;
import z.json.JSONObject;

import java.net.URLEncoder;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class PgInterface extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2221187086461756012L;

	private String params;

	private String format;
	private String jsoncallback;

	public String getParams() {
		return params;
	}

	public void setParams(String params) {
		this.params = params;
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public String getJsoncallback() {
		return jsoncallback;
	}

	public void setJsoncallback(String jsoncallback) {
		this.jsoncallback = jsoncallback;
	}

	/**
	 * 查询/PgInterface/rs/pgDevice/getPgDevice并转发
	 * @return
	 * @throws Exception
	 */
	public String getPgDevice() throws Exception {
		// 返回结果
		JSONObject jResult = new JSONObject();
		JSONArray jList = new JSONArray();

		// 跨域查询接口
		String baseURL = Configure.getProperty("sys", "PGINTERFACE_API_URL");
		String response = HttpRequest.sendPostByHttp(baseURL + "pgDevice/getPgDev", params);

		JSONObject jsonObject = new JSONObject(response);
		JSONArray jArray = jsonObject.getJSONArray("result");
		for (int i = 0; i < jArray.length(); i++) {
			JSONObject item = jArray.getJSONObject(i);
			JSONObject result = new JSONObject();
			result.put("id", item.getString("id"));
			result.put("assetCode", item.getString("assetCode"));
			result.put("partition", URLEncoder.encode(item.getString("partition"), "UTF-8"));
			result.put("assetName", URLEncoder.encode(item.getString("assetName"), "UTF-8"));

			jList.put(result);
		}

		jResult.put("data", jList);

		returnToFront(jResult);
		return null;
	}
	
	/**
	 * 给前端返回
	 * @param jResult
	 * @throws Exception
	 */
	private void returnToFront(JSONObject jResult) throws Exception {
		// 返回jsonp格式的数据
		if (null != format && format.equalsIgnoreCase("jsonp") == true) {
			ServletActionContext.getResponse().getWriter().write(jsoncallback + "(" + jResult.toString() + ");");
		}

		ServletActionContext.getResponse().getWriter().write(jResult.toString());
	}

}
