package com.szpg.service.command;

import com.szpg.db.dao.PgHjsbblDao;
import com.szpg.db.dao.impl.PgHjsbblDaoImpl;
import com.szpg.db.data.PgHjsbbl;
import com.szpg.plc.message.command.write.SetZmOffBitCommand;
import com.szpg.plc.message.command.write.SetZmOnBitCommand;
import com.szpg.plc.protocol.fins.FINSConstants;
import com.szpg.plc.util.ByteUtil;

/**
 * 照明控制指令辅助类
 * @author admin
 *
 */
public class LightCommandService {

	/**
	 * 根据资产编号构建打开风机控制指令
	 * 
	 * @param sour 源地址
	 * @param dest 目的地址
	 * @param zcbh 资产编号
	 * @return
	 */
	public static SetZmOnBitCommand buildTurnOnCommand(String sour, String dest, String zcbh) {
		PgHjsbblDao blDao = new PgHjsbblDaoImpl();
		
		SetZmOnBitCommand setOnCmd = new SetZmOnBitCommand();
		setOnCmd.setZmon(SetZmOnBitCommand.ZM_ON_ENABLE);
		PgHjsbbl onBlObj = blDao.findBlByBh(zcbh + ".ON");
		if (null != onBlObj) {
			setOnCmd.setMessageProducerId(sour);
			setOnCmd.setDestinationId(dest);
			
			// SID在new对象的时候已经生成
			
			// 内存区域——按位写
			setOnCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT);
			
			int start = onBlObj.getKszdz();
			int end = onBlObj.getJszdz();
			int bit = onBlObj.getSzw();
			
			// 开始字地址
			setOnCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2)));
			
			// 位地址
			setOnCmd.setBit(bit);
			
			// 位数
			setOnCmd.setCount(end - start + 1);
			
			// 位内容
			setOnCmd.setValue(new byte[] {(byte) SetZmOnBitCommand.ZM_ON_ENABLE} );
			
			return setOnCmd;
		} else {
			return null;
		}
	}
	
	/**
	 * 根据资产编号构建复位打开照明控制指令
	 * @param sour
	 * @param dest
	 * @param zcbh
	 * @return
	 */
	public static SetZmOnBitCommand buildResetTurnOnCommand(String sour, String dest, String zcbh) {
		PgHjsbblDao blDao = new PgHjsbblDaoImpl();
		
		SetZmOnBitCommand setOnCmd = new SetZmOnBitCommand();
		setOnCmd.setZmon(SetZmOnBitCommand.ZM_ON_DISABLE);
		PgHjsbbl onBlObj = blDao.findBlByBh(zcbh + ".ON");
		if (null != onBlObj) {
			setOnCmd.setMessageProducerId(sour);
			setOnCmd.setDestinationId(dest);
			
			// SID在new对象的时候已经生成
			
			// 内存区域——按位写
			setOnCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT);
			
			int start = onBlObj.getKszdz();
			int end = onBlObj.getJszdz();
			int bit = onBlObj.getSzw();
			
			// 开始字地址
			setOnCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2)));
			
			// 位地址
			setOnCmd.setBit(bit);
			
			// 位数
			setOnCmd.setCount(end - start + 1);
			
			// 位内容
			setOnCmd.setValue(new byte[] {(byte) SetZmOnBitCommand.ZM_ON_DISABLE} );
			
			return setOnCmd;
		} else {
			return null;
		}
	}
	
	
	public static SetZmOffBitCommand buildTurnOffCommand(String sour, String dest, String zcbh) {
		PgHjsbblDao blDao = new PgHjsbblDaoImpl();
		
		SetZmOffBitCommand setOffCmd = new SetZmOffBitCommand();
		setOffCmd.setZmoff(SetZmOffBitCommand.ZM_OFF_ENABLE);
		PgHjsbbl offBlObj = blDao.findBlByBh(zcbh + ".OFF");
		if (null != offBlObj) {
			setOffCmd.setMessageProducerId(sour);
			setOffCmd.setDestinationId(dest);
			
			// SID在new对象的时候已经生成
			
			// 内存区域——按位写
			setOffCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT);
			
			int start = offBlObj.getKszdz();
			int end = offBlObj.getJszdz();
			int bit = offBlObj.getSzw();
			
			// 开始字地址
			setOffCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2)));
			
			// 位地址
			setOffCmd.setBit(bit);
			
			// 位数
			setOffCmd.setCount(end - start + 1);
			
			// 位内容
			setOffCmd.setValue(new byte[] {SetZmOffBitCommand.ZM_OFF_ENABLE} );
			
			return setOffCmd;
		} else {
			return null;
		}
	}
	
	/**
	 * 根据资产编号构建复位关闭照明控制指令
	 * @param sour
	 * @param dest
	 * @param zcbh
	 * @return
	 */
	public static SetZmOffBitCommand buildResetTurnOffCommand(String sour, String dest, String zcbh) {
		PgHjsbblDao blDao = new PgHjsbblDaoImpl();
		
		SetZmOffBitCommand setOffCmd = new SetZmOffBitCommand();
		setOffCmd.setZmoff(SetZmOffBitCommand.ZM_OFF_DISABLE);
		PgHjsbbl offBlObj = blDao.findBlByBh(zcbh + ".OFF");
		if (null != offBlObj) {
			setOffCmd.setMessageProducerId(sour);
			setOffCmd.setDestinationId(dest);
			
			// SID在new对象的时候已经生成
			
			// 内存区域——按位写
			setOffCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT);
			
			int start = offBlObj.getKszdz();
			int end = offBlObj.getJszdz();
			int bit = offBlObj.getSzw();
			
			// 开始字地址
			setOffCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2)));
			
			// 位地址
			setOffCmd.setBit(bit);
			
			// 位数
			setOffCmd.setCount(end - start + 1);
			
			// 位内容
			setOffCmd.setValue(new byte[] {SetZmOffBitCommand.ZM_OFF_DISABLE} );
			
			return setOffCmd;
		} else {
			return null;
		}
	}
}
