package com.szpg.task;

import com.szpg.db.dao.impl.PgAcuRdcmdDaoImpl;
import com.szpg.db.data.PgAcuRdcmd;
import com.szpg.plc.message.AppCommand;
import com.szpg.plc.message.command.read.ReadFjRtCommand;
import com.szpg.plc.protocol.DTProtocolInterface;
import com.szpg.plc.protocol.ProtocolFactory;
import com.szpg.plc.server.ACUClient;
import com.szpg.plc.server.ACUClientUtil;
import com.szpg.plc.util.ByteUtil;

public class ReadFjRtTask implements Runnable {

	private ACUClient client;
	private AppCommand appCommand;

	public ReadFjRtTask(ACUClient client, AppCommand command) {
		this.client = client;
		this.appCommand = command;
	}

	@Override
	public void run() {
		DTProtocolInterface finspi = ProtocolFactory.getDefaultDTProtocol();
		byte[] content = finspi.messageToBytes(appCommand);
		
		// 发送读取风机运行时长内存命令
		ACUClientUtil.getInstance().sendACUCommand(client, content);
		
		ReadFjRtCommand fjRtCmd = (ReadFjRtCommand) appCommand;

		// 发送完成之后将命令保存在数据库中
		PgAcuRdcmd cmd = new PgAcuRdcmd();
		cmd.setCmd_type(fjRtCmd.getCommandType());
		cmd.setDest(fjRtCmd.getDestinationId());
		cmd.setMem_area_cd(ByteUtil.binToHexString(new byte[] { fjRtCmd.getMemoryArea() }));
		cmd.setStart_mem_word(fjRtCmd.getStartAddress().substring(0, 4));
		cmd.setStart_mem_bit(fjRtCmd.getStartAddress().substring(4));
		cmd.setCount_word(fjRtCmd.getCountWord());
		cmd.setCount_bit(fjRtCmd.getCountBit());
		cmd.setCount_sensor(fjRtCmd.getCountSensor());
		cmd.setOffset(fjRtCmd.getOffset());
		
		new PgAcuRdcmdDaoImpl().addCmdRecord(cmd);
	}

}
