package com.szpg.plc.message.response.read;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.szpg.db.dao.PgYwDao;
import com.szpg.db.dao.PgDeviceDao;
import com.szpg.db.dao.PgHjsbblDao;
import com.szpg.db.dao.impl.PgYwDaoImpl;
import com.szpg.db.data.PgHjsbbl;
import com.szpg.db.dao.impl.PgDeviceDaoImpl;
import com.szpg.db.dao.impl.PgHjsbblDaoImpl;
import com.szpg.plc.message.response.ReadMemoryCommandResponse;
import com.szpg.plc.util.ByteUtil;
import com.szpg.util.Configure;
import com.szpg.util.TimeFormat;

public class ReadYWStatusCommandResponse extends ReadMemoryCommandResponse {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2363774800273830916L;

	private final Logger logger = Logger.getLogger(this.getClass().getName());
	
	private List<Boolean> ywbj; // 爆管液位报警状态
	private String[] zcList; //爆管液位监测的资产列表，从配置文件中获取
	
	public ReadYWStatusCommandResponse() {
		ywbj = new ArrayList<Boolean>();
	}

	public List<Boolean> getYwbj() {
		return ywbj;
	}

	public void setYwbj(List<Boolean> ywbj) {
		this.ywbj = ywbj;
	}
	
	public String[] getZcList() {
		return zcList;
	}

	public void setZcList(String[] zcList) {
		this.zcList = zcList;
	}

	@Override
	public void afterAction() {
		// 1将爆管液位浓度数据存入数据库
		PgYwDao ywDao = new PgYwDaoImpl();
		PgDeviceDao deviceDao = new PgDeviceDaoImpl();
		
		// 遍历资产列表，将爆管液位报警状态存入数据库
		for (int i = 0; i < zcList.length; i++) {
			String zcbh = zcList[i];
			
			int id = deviceDao.findDeviceIdByCode(zcbh);
			if (id > 0) {
				ywDao.addSbjzRecord(ywbj.get(i), TimeFormat.formatTimestamp(this.getTime().getTime()), id);
			}
		}
	}

	@Override
	public void parseData(byte[] messageData) {
		// 获取目标ACU的代码
		String acucode = this.getAcucode();
		
		// 判断数据的长度是否满足要求
		if (messageData.length != Integer.parseInt(Configure.getProperty("acubl", acucode + ".YWALM.WORDCOUNT")) * 2) {
			logger.error("返回的数据长度与读取的不一致！");
			this.setValid(false);
			return;
		}
		
		// 将数据转换为字符串
		// 按照高字在前，低字在后；高字节在前，低字节在后的顺序
		String dataStr = "";
		for (int c = 0; c < messageData.length; c = c + 2) {
			dataStr = ByteUtil.binToBinString(new byte[] {messageData[c], messageData[c + 1]}) + dataStr;
		}
		
		PgHjsbblDao blDao = new PgHjsbblDaoImpl();
		
		// 获取目标ACU爆管液位报警状态内存区域的起始字地址
		int start = Integer.parseInt(Configure.getProperty("acubl", acucode + ".YWALM.START"));
		
		// 获取目标ACU爆管液位相关的资产列表，即爆管液位设备列表
		String zcListStr = Configure.getProperty("acubl", acucode + ".YW.ZC.LIST");
		if (StringUtils.isEmpty(zcListStr) == true) {
			this.setValid(false);
			return;
		}
		zcList = zcListStr.split(";");
		
		// 解析爆管液位报警状态
		for (int i = 0; i < zcList.length; i++) {
			String zcbh = zcList[i];
			
			// 解析爆管液位报警状态
			PgHjsbbl almBlObj = blDao.findBlByBh(zcbh + ".AH");
			if (null == almBlObj) {
				continue;
			}
			
			int akszdz = almBlObj.getKszdz(); //开始字地址
			int aoffset = akszdz - start; //与开始字的偏移量
			int abit = almBlObj.getSzw(); //所在位
			
			char bjBt = dataStr.charAt(dataStr.length() - 1 - aoffset * 16 - abit); // 爆管液位报警状态标志位
			
			getYwbj().add(bjBt == '1' ? true : false);
		}
	}

}
