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.*;
import com.szpg.plc.protocol.fins.FINSConstants;
import com.szpg.plc.util.ByteUtil;

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

	/**
	 * 根据资产编号构建打开井盖控制指令
	 *
	 * @param sour 源地址
	 * @param dest 目的地址
	 * @param zcbh 资产编号
	 * @return
	 */
	public static SetJgLockBitCommand buildLockCommand(String sour, String dest, String zcbh) {
		PgHjsbblDao blDao = new PgHjsbblDaoImpl();

		SetJgLockBitCommand setLockCmd = new SetJgLockBitCommand();
		setLockCmd.setUnlock(SetJgLockBitCommand.JG_LOCK);
		PgHjsbbl lockBlObj = blDao.findBlByBh(zcbh + ".Lock");
		if (null != lockBlObj) {
			setLockCmd.setMessageProducerId(sour);
			setLockCmd.setDestinationId(dest);

			// SID在new对象的时候已经生成

			// 内存区域——按位写
			setLockCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT);

			int start = lockBlObj.getKszdz();
			int end = lockBlObj.getJszdz();
			int bit = lockBlObj.getSzw();

			// 开始字地址
			setLockCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2)));

			// 位地址
			setLockCmd.setBit(bit);

			// 位数
			setLockCmd.setCount(end - start + 1);

			// 位内容
			setLockCmd.setValue(new byte[] {(byte) SetJgLockBitCommand.JG_LOCK} );

			return setLockCmd;
		} else {
			return null;
		}
	}


	/**
	 * 根据资产编号构建打开井盖控制指令
	 *
	 * @param sour 源地址
	 * @param dest 目的地址
	 * @param zcbh 资产编号
	 * @return
	 */
	public static SetJgUnlockBitCommand buildUnlockCommand(String sour, String dest, String zcbh) {
		PgHjsbblDao blDao = new PgHjsbblDaoImpl();

		SetJgUnlockBitCommand setUnlockCmd = new SetJgUnlockBitCommand();
		setUnlockCmd.setUnlock(SetJgUnlockBitCommand.JG_UNLOCK);
		PgHjsbbl unlockBlObj = blDao.findBlByBh(zcbh + ".Lock");
		if (null != unlockBlObj) {
			setUnlockCmd.setMessageProducerId(sour);
			setUnlockCmd.setDestinationId(dest);

			// SID在new对象的时候已经生成

			// 内存区域——按位写
			setUnlockCmd.setMemoryArea(FINSConstants.MEMORY_WORK_AREA_BIT);

			int start = unlockBlObj.getKszdz();
			int end = unlockBlObj.getJszdz();
			int bit = unlockBlObj.getSzw();

			// 开始字地址
			setUnlockCmd.setStartAddress(ByteUtil.binToHexString(ByteUtil.intToBins(start, 2)));

			// 位地址
			setUnlockCmd.setBit(bit);

			// 位数
			setUnlockCmd.setCount(end - start + 1);

			// 位内容
			setUnlockCmd.setValue(new byte[] {(byte) SetJgUnlockBitCommand.JG_UNLOCK} );

			return setUnlockCmd;
		} else {
			return null;
		}
	}

	/**
	 * 根据资产编号构建打开井盖控制指令
	 * 
	 * @param sour 源地址
	 * @param dest 目的地址
	 * @param zcbh 资产编号
	 * @return
	 */
	public static SetJgOnBitCommand buildTurnOnCommand(String sour, String dest, String zcbh) {
		PgHjsbblDao blDao = new PgHjsbblDaoImpl();

		SetJgOnBitCommand setOnCmd = new SetJgOnBitCommand();
		setOnCmd.setJgon(SetJgOnBitCommand.JG_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) SetJgOnBitCommand.JG_ON_ENABLE} );
			
			return setOnCmd;
		} else {
			return null;
		}
	}
	
	/**
	 * 根据资产编号构建复位打开井盖控制指令
	 * @param sour
	 * @param dest
	 * @param zcbh
	 * @return
	 */
	public static SetJgOnBitCommand buildResetTurnOnCommand(String sour, String dest, String zcbh) {
		PgHjsbblDao blDao = new PgHjsbblDaoImpl();

		SetJgOnBitCommand setOnCmd = new SetJgOnBitCommand();
		setOnCmd.setJgon(SetJgOnBitCommand.JG_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) SetJgOnBitCommand.JG_ON_DISABLE} );
			
			return setOnCmd;
		} else {
			return null;
		}
	}
	
	
	public static SetJgOffBitCommand buildTurnOffCommand(String sour, String dest, String zcbh) {
		PgHjsbblDao blDao = new PgHjsbblDaoImpl();

		SetJgOffBitCommand setOffCmd = new SetJgOffBitCommand();
		setOffCmd.setJgoff(SetJgOffBitCommand.JG_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[] {SetJgOffBitCommand.JG_OFF_ENABLE} );
			
			return setOffCmd;
		} else {
			return null;
		}
	}
	
	/**
	 * 根据资产编号构建复位关闭井盖控制指令
	 * @param sour
	 * @param dest
	 * @param zcbh
	 * @return
	 */
	public static SetJgOffBitCommand buildResetTurnOffCommand(String sour, String dest, String zcbh) {
		PgHjsbblDao blDao = new PgHjsbblDaoImpl();

		SetJgOffBitCommand setOffCmd = new SetJgOffBitCommand();
		setOffCmd.setJgoff(SetJgOffBitCommand.JG_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[] {SetJgOffBitCommand.JG_OFF_DISABLE} );
			
			return setOffCmd;
		} else {
			return null;
		}
	}
}
