package com.szpg.task;

import java.util.Calendar;
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.ReadSensorStatusService;
import com.szpg.util.Configure;

public class ReadCOStatusTask implements Runnable {

	private ReadSensorStatusService service;

	public ReadCOStatusTask() {
		service = new ReadSensorStatusService();
	}

	@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_READCOSTATUS);
			String sour = Configure.getProperty("sys", "LOCALHOST.NET") + 
						  Configure.getProperty("sys", "LOCALHOST.NODE") + 
						  Configure.getProperty("sys", "LOCALHOST.UNIT");
			command.setMessageProducerId(sour);
			command.setTime(Calendar.getInstance());
			
			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() + ".COALM.START")), 2)) + "00");
			command.setCountWord(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".COALM.WORDCOUNT")));
			command.setCountSensor(Integer.parseInt(Configure.getProperty("acubl", client.getAcucode() + ".CO.COUNT")));
			
			service.executeService(client, (ReadMemoryCommand) command);
		}
	}

}
