package com.szpg.task;

import java.util.Map;

import com.szpg.plc.message.AppMessageConstants;
import com.szpg.plc.message.command.ReadMemoryCommand;
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.util.Configure;

public class ReadSbStatTask implements Runnable {

	private ReadControllerStatusService service;

	public ReadSbStatTask() {
		service = new ReadControllerStatusService();
	}

	@Override
	public void run() {
		// 查找所有的client
		Map<String, ACUClient> clients = ACUClientUtil.getInstance().getClients();
		
		for (String key : clients.keySet()) {
			ACUClient client = clients.get(key);
			
			ReadMemoryCommand command = ReadMemoryCommand.getInstance(AppMessageConstants.CMD_TYPE_READSBSTAT);
			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);
			
			command.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.START")), 2)) + "00");
			command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SBSTAT.WORDCOUNT")));
			command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".SB.COUNT")));
			
			service.executeService(client, command);
		}
	}

}
