Newer
Older
pgdsc / src / com / szpg / rmi / RemoteZMCommandAction.java
ty-pc\admin on 22 May 2019 13 KB 20190522 环境监测功能梳理
package com.szpg.rmi;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.szpg.db.dao.PgAcuCmdDao;
import com.szpg.db.dao.PgAcuDao;
import com.szpg.db.dao.impl.PgAcuCmdDaoImpl;
import com.szpg.db.dao.impl.PgAcuDaoImpl;
import com.szpg.db.data.PgAcu;
import com.szpg.db.data.PgAcuCmd;
import com.szpg.plc.message.AppMessageConstants;
import com.szpg.plc.message.CommandResponse;
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.command.LightCommandService;
import com.szpg.util.Configure;
import com.szpg.util.UnicodeConvertor;

import z.json.JSONObject;

public class RemoteZMCommandAction extends ActionSupport {

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

	private String zcbh;
	
	private String format;
	private String jsoncallback;
	
	private Logger logger = Logger.getLogger(this.getClass().getName());
	
	public String getZcbh() {
		return zcbh;
	}

	public void setZcbh(String zcbh) {
		this.zcbh = zcbh;
	}

	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;
	}
	
	/**
	 * 读取照明实时状态
	 * @return
	 * @throws Exception
	 */
	public String readZmStatus() throws Exception {
		// 返回结果
		JSONObject jResult = new JSONObject();
		
		PgAcuDao acuDao = new PgAcuDaoImpl();
		
		if (null == zcbh || zcbh.equals("") == true) {
			jResult.put("success", false);
			jResult.put("code", "1");
			jResult.put("resaon", UnicodeConvertor.string2Unicode("资产编号为空"));
			
			returnToFront(jResult);
			return null;
		}
		
		String acucode = zcbh.substring(0, zcbh.indexOf(".", 6));
		PgAcu acu = acuDao.findACUByCode(acucode);
		if (null == acu) {
			jResult.put("success", false);
			jResult.put("code", "2");
			jResult.put("resaon", UnicodeConvertor.string2Unicode("未找到资产对应的PLC主机"));
			
			returnToFront(jResult);
			return null;
		}
		
		ACUClient client = ACUClientUtil.getInstance().getClients().get(acu.getAcu_host() + ":" + acu.getAcu_port());
		if (null != client) {
			ReadMemoryCommand command = ReadMemoryCommand.getInstance(AppMessageConstants.CMD_TYPE_READZMSTAT);
			String sour = Configure.getProperty("sys", "LOCALHOST.NET") + 
						  Configure.getProperty("sys", "LOCALHOST.NODE") + 
						  Configure.getProperty("sys", "LOCALHOST.UNIT");
			command.setMessageProducerId(sour);
			
			String dest = client.getNet() + client.getNode() + client.getUnit();
			command.setDestinationId(dest);
			command.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_WORD); //按字读取内容
			
			try {
				// 设置读取的地址范围
				// 从配置文件读取而来
				command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMSTAT.START")), 2)) + "00");
				command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".ZMSTAT.WORDCOUNT")));
				
				// 调用服务过程执行命令发送服务
				ReadControllerStatusService service = new ReadControllerStatusService();
				service.executeService(client, command);
				logger.info(command);
			} catch (Exception ex) {
				logger.error("发送查询照明指令异常" + ex);
			}
			
			jResult.put("success", true);
			jResult.put("resaon", UnicodeConvertor.string2Unicode("发送成功,请等待响应"));
			
			returnToFront(jResult);
			return null;
		} else {
			jResult.put("success", false);
			jResult.put("code", "3");
			jResult.put("resaon", UnicodeConvertor.string2Unicode("PLC主机不在线"));
			
			returnToFront(jResult);
			return null;
		}
	}
	
	/**
	 * 远程打开照明
	 * @return
	 * @throws Exception
	 */
	public String turnOnZm() throws Exception {
		// 返回结果
		JSONObject jResult = new JSONObject();
		
		if (null == zcbh || zcbh.equals("") == true) {
			jResult.put("success", false);
			jResult.put("code", "1");
			jResult.put("resaon", UnicodeConvertor.string2Unicode("资产编号为空"));
			
			returnToFront(jResult);
			return null;
		}
		
		// 查找ACU的信息
		PgAcuDao acuDao = new PgAcuDaoImpl();
		
		String acucode = zcbh.substring(0, zcbh.indexOf(".", 6));
		PgAcu acu = acuDao.findACUByCode(acucode);
		if (null == acu) {
			jResult.put("success", false);
			jResult.put("code", "2");
			jResult.put("resaon", UnicodeConvertor.string2Unicode("未找到资产对应的PLC主机"));
			
			returnToFront(jResult);
			return null;
		}
		
		ACUClient client = ACUClientUtil.getInstance().getClients().get(acu.getAcu_host() + ":" + acu.getAcu_port());
		if (null != client) {
			// 源地址
			String sour = Configure.getProperty("sys", "LOCALHOST.NET") + 
						  Configure.getProperty("sys", "LOCALHOST.NODE") + 
						  Configure.getProperty("sys", "LOCALHOST.UNIT");
			
			// 目标地址
			String dest = client.getNet() + client.getNode() + client.getUnit();
			
			DTProtocolInterface finspi = ProtocolFactory.getDefaultDTProtocol();
			
			// 1首先将停止位置0
			// 该逻辑取消,由设备自行控制
			
			// 2 发送设置启动位的命令
			// 构建打开照明指令
			SetZmOnBitCommand setOnCmd = LightCommandService.buildTurnOnCommand(sour, dest, zcbh);
			if (null != setOnCmd) {
				// 解析命令对象为字节数组
				byte[] content = finspi.messageToBytes(setOnCmd);
				
				// 通过socket接口发送出去
				ACUClientUtil.getInstance().sendACUCommand(client, content);
			} else {
				jResult.put("success", false);
				jResult.put("code", "4");
				jResult.put("reason", UnicodeConvertor.string2Unicode("未找到资产对应的打开风机变量"));
				
				returnToFront(jResult);
				return null;
			}
			
			// 3将命令存入数据库
			PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl();
			PgAcuCmd cmd = new PgAcuCmd();
			cmd.setCmd_type(setOnCmd.getCommandType());
			cmd.setDest_acu_code(acucode);
			cmd.setTm(setOnCmd.getTime().getTime());
			cmdDao.addCmdRecord(cmd);
			
			// 4阻塞,循环查找响应消息池,找到对应的响应消息
			boolean flag = false;
			int times = 0;
			CommandResponse response = null;
			while (flag == false && times < 240) {
				response = ACUClientUtil.getInstance().responsePool.getResponse(cmd.getId());
				
				if (null != response && response.equals("") == false) {
					flag = true;
				}
				
				times++;
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// 目前的处理流程为1)记录日志;2)将命令置为超时
					logger.error("在响应池中查找命令的响应消息阻塞线程被异常打断", e);
					cmdDao.updateCmdRecordTimeout(cmd.getId());
					
					jResult.put("success", false);
					jResult.put("code", "5");
					jResult.put("reason", UnicodeConvertor.string2Unicode("查找命令的响应消息时异常"));
					
					returnToFront(jResult);
					return null;
				}
			}
			
			// 5若未超时,将值存入数据库
			if (null != response) {
				// 6根据命令类型的不同将监测值存入对应的数据库
				response.afterAction();
				
				// 成功返回
				jResult.put("success", true);
				jResult.put("code", "0");
				
				// 20190515增加的逻辑,在置1打开位一秒钟后,将该位复位
				setOnCmd = LightCommandService.buildResetTurnOnCommand(sour, dest, zcbh);
				if (null != setOnCmd) {
					byte[] content = finspi.messageToBytes(setOnCmd);
					ACUClientUtil.getInstance().sendACUCommand(client, content);
				}
				
				returnToFront(jResult);
				return null;
			} else {
				// 9超时,将命令的超时标志位置1
				logger.warn("命令超时" + cmd.getId());
				cmdDao.updateCmdRecordTimeout(cmd.getId());
				
				jResult.put("success", false);
				jResult.put("code", "6");
				jResult.put("reason", UnicodeConvertor.string2Unicode("命令发送超时"));
				
				returnToFront(jResult);
				return null;
			}
		} else {
			jResult.put("success", false);
			jResult.put("code", "3");
			jResult.put("resaon", UnicodeConvertor.string2Unicode("PLC主机不在线"));
			
			returnToFront(jResult);
			return null;
		}
	}
	
	/**
	 * 远程关闭照明
	 * @return
	 * @throws Exception
	 */
	public String turnOffZm() throws Exception {
		// 返回结果
		JSONObject jResult = new JSONObject();
		
		if (null == zcbh || zcbh.equals("") == true) {
			jResult.put("success", false);
			jResult.put("code", "1");
			jResult.put("resaon", UnicodeConvertor.string2Unicode("资产编号为空"));
			
			returnToFront(jResult);
			return null;
		}
		
		// 查找ACU的信息
		PgAcuDao acuDao = new PgAcuDaoImpl();
		
		String acucode = zcbh.substring(0, zcbh.indexOf(".", 6));
		PgAcu acu = acuDao.findACUByCode(acucode);
		if (null == acu) {
			jResult.put("success", false);
			jResult.put("code", "2");
			jResult.put("resaon", UnicodeConvertor.string2Unicode("未找到资产对应的PLC主机"));
			
			returnToFront(jResult);
			return null;
		}
		
		ACUClient client = ACUClientUtil.getInstance().getClients().get(acu.getAcu_host() + ":" + acu.getAcu_port());
		if (null != client) {
			// 源地址
			String sour = Configure.getProperty("sys", "LOCALHOST.NET") + 
						  Configure.getProperty("sys", "LOCALHOST.NODE") + 
						  Configure.getProperty("sys", "LOCALHOST.UNIT");
			
			// 目标地址
			String dest = client.getNet() + client.getNode() + client.getUnit();
			
			DTProtocolInterface finspi = ProtocolFactory.getDefaultDTProtocol();
			
			// 1首先将启动位置0
			// 逻辑取消,由PLC设备自行控制
			
			// 2 发送设置停止位的命令
			SetZmOffBitCommand setOffCmd = LightCommandService.buildTurnOffCommand(sour, dest, zcbh);
			if (null != setOffCmd) {
				// 解析命令对象为字节数组
				byte[] content = finspi.messageToBytes(setOffCmd);
				
				// 通过socket接口发送出去
				ACUClientUtil.getInstance().sendACUCommand(client, content);
			} else {
				jResult.put("success", false);
				jResult.put("code", "4");
				jResult.put("reason", UnicodeConvertor.string2Unicode("未找到资产对应的关闭照明变量"));
				
				returnToFront(jResult);
				return null;
			}
			
			// 3将命令存入数据库
			PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl();
			PgAcuCmd cmd = new PgAcuCmd();
			cmd.setCmd_type(setOffCmd.getCommandType());
			cmd.setDest_acu_code(acucode);
			cmd.setTm(setOffCmd.getTime().getTime());
			cmdDao.addCmdRecord(cmd);
			
			// 4阻塞,循环查找响应消息池,找到对应的响应消息
			boolean flag = false;
			int times = 0;
			CommandResponse response = null;
			while (flag == false && times < 240) {
				response = ACUClientUtil.getInstance().responsePool.getResponse(cmd.getId());
				
				if (null != response && response.equals("") == false) {
					flag = true;
				}
				
				times++;
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// 目前的处理流程为1)记录日志;2)将命令置为超时
					logger.error("在响应池中查找命令的响应消息阻塞线程被异常打断", e);
					cmdDao.updateCmdRecordTimeout(cmd.getId());
					
					jResult.put("success", false);
					jResult.put("code", "5");
					jResult.put("reason", UnicodeConvertor.string2Unicode("查找命令的响应消息时异常"));
					
					returnToFront(jResult);
					return null;
				}
			}
			
			// 5若未超时,将值存入数据库
			if (null != response) {
				// 6根据命令类型的不同将监测值存入对应的数据库
				response.afterAction();
				
				// 成功返回
				jResult.put("success", true);
				jResult.put("code", "0");
				
				// 20190515增加的逻辑,在置1打开位一秒钟后,将该位复位
				setOffCmd = LightCommandService.buildResetTurnOffCommand(sour, dest, zcbh);
				if (null != setOffCmd) {
					byte[] content = finspi.messageToBytes(setOffCmd);
					ACUClientUtil.getInstance().sendACUCommand(client, content);
				}
				
				returnToFront(jResult);
				return null;
			} else {
				// 9超时,将命令的超时标志位置1
				logger.warn("命令超时" + cmd.getId());
				cmdDao.updateCmdRecordTimeout(cmd.getId());
				
				jResult.put("success", false);
				jResult.put("code", "6");
				jResult.put("reason", UnicodeConvertor.string2Unicode("命令发送超时"));
				
				returnToFront(jResult);
				return null;
			}
		} else {
			jResult.put("success", false);
			jResult.put("code", "3");
			jResult.put("resaon", UnicodeConvertor.string2Unicode("PLC主机不在线"));
			
			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());
	}
}