package com.szpg.task;

import org.apache.log4j.Logger;

import com.szpg.db.dao.PgAcuCmdDao;
import com.szpg.db.dao.PgAcuDao;
import com.szpg.db.dao.PgDeviceDao;
import com.szpg.db.dao.PgHjsbblDao;
import com.szpg.db.dao.impl.PgAcuCmdDaoImpl;
import com.szpg.db.dao.impl.PgAcuDaoImpl;
import com.szpg.db.dao.impl.PgDeviceDaoImpl;
import com.szpg.db.dao.impl.PgHjsbblDaoImpl;
import com.szpg.db.data.PgAcu;
import com.szpg.db.data.PgAcuCmd;
import com.szpg.db.data.PgHjsbbl;
import com.szpg.plc.message.CommandResponse;
import com.szpg.plc.message.command.write.SetJgUnlockBitCommand;
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.util.Configure;

/**
 * 设置井盖解锁
 * 0=锁定/1=解锁
 * 
 * @author admin
 *
 */
public class SetJgUnlockTask implements Runnable {
	
	private Logger logger = Logger.getLogger(this.getClass().getName());
	
	private String deviceCode;
	private int unlock;
	private PgHjsbblDao blDao = new PgHjsbblDaoImpl();
	
	public SetJgUnlockTask(String deviceCode, int unlock) {
		this.deviceCode = deviceCode;
		this.unlock = unlock;
	}

	@Override
	public void run() {
		// 查找ACU的信息
		PgAcuDao acuDao = new PgAcuDaoImpl();
		PgDeviceDao deviceDao = new PgDeviceDaoImpl();
		String acucode = deviceDao.findAcuCodeByCode(deviceCode);
		PgAcu acu = acuDao.findACUByCode(acucode);
		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();
			
			// 发送设置解锁位的命令
			SetJgUnlockBitCommand setUnlockCmd = new SetJgUnlockBitCommand();
			setUnlockCmd.setUnlock(unlock);
			PgHjsbbl onBlObj = blDao.findBlByBh(deviceCode + ".Lock");
			if (null != onBlObj) {
				setUnlockCmd.setMessageProducerId(sour);
				setUnlockCmd.setMessageProducerHost(client.getHost());
				setUnlockCmd.setDestinationId(dest);
				
				// SID在new对象的时候已经生成
				
				// 内存区域——按位写
				setUnlockCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT);
				
				int start = onBlObj.getKszdz();
				int end = onBlObj.getJszdz();
				int bit = onBlObj.getSzw();
				
				// 开始字地址
				setUnlockCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2)));
				
				// 位地址
				setUnlockCmd.setBit(bit);
				
				// 位数
				setUnlockCmd.setCount(end - start + 1);
				
				// 位内容
				setUnlockCmd.setValue(new byte[] {(byte) unlock} );
				
				// 解析命令对象为字节数组
				byte[] content = finspi.messageToBytes(setUnlockCmd);
				
				// 通过socket接口发送出去
				ACUClientUtil.getInstance().sendACUCommand(client, content);
			}
			
			// 3将命令存入数据库
			PgAcuCmdDao cmdDao = new PgAcuCmdDaoImpl();
			PgAcuCmd cmd = new PgAcuCmd();
			cmd.setCmd_type(setUnlockCmd.getCommandType());
			cmd.setDest_acu_code(acucode);
			cmd.setTm(setUnlockCmd.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) {
					// TODO 阻塞线程被打断，需要处理异常
					// 目前的处理流程为1）记录日志；2）将命令置为超时
					logger.error("在响应池中查找命令的响应消息阻塞线程被异常打断", e);
					cmdDao.updateCmdRecordTimeout(cmd.getId());
					
					return;
				}
			}
			
			// 5若未超时，将值存入数据库
			if (null != response) {
				// 6根据命令类型的不同将监测值存入对应的数据库
				response.afterAction();
			} else {
				// 9超时，将命令的超时标志位置1
				logger.warn("命令超时" + cmd.getId());
				cmdDao.updateCmdRecordTimeout(cmd.getId());
			}
		}
	}

}
