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.PgDeviceDao;
import com.szpg.db.dao.PgFjDao;
import com.szpg.db.dao.PgHjsbblDao;
import com.szpg.db.dao.impl.PgDeviceDaoImpl;
import com.szpg.db.dao.impl.PgFjDaoImpl;
import com.szpg.db.dao.impl.PgHjsbblDaoImpl;
import com.szpg.db.data.PgHjsbbl;
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 ReadFjStatCommandResponse extends ReadMemoryCommandResponse {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5849842153973146848L;

	private final Logger logger = Logger.getLogger(this.getClass().getName());
	
	private List<Boolean> fjFlt;
	private List<Boolean> fjRun;
	private String[] zcList; //资产列表，从配置文件中获取

	public ReadFjStatCommandResponse() {
		fjFlt = new ArrayList<Boolean>();
		fjRun = new ArrayList<Boolean>();
	}
	
	public List<Boolean> getFjFlt() {
		return fjFlt;
	}

	public void setFjFlt(List<Boolean> fjFlt) {
		this.fjFlt = fjFlt;
	}

	public List<Boolean> getFjRun() {
		return fjRun;
	}

	public void setFjRun(List<Boolean> fjRun) {
		this.fjRun = fjRun;
	}

	public String[] getZcList() {
		return zcList;
	}

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

	@Override
	public void afterAction() {
		// 1将风机运行状态数据存入数据库
		PgFjDao fjDao = new PgFjDaoImpl();
		PgDeviceDao deviceDao = new PgDeviceDaoImpl();
		
		if (null != zcList && zcList.length > 0) {
			// 遍历设备列表，将风机运行状态存入数据库
			for (int i = 0; i < zcList.length; i++) {
				String zcbh = zcList[i];
				
				int id = deviceDao.findDeviceIdByCode(zcbh);
				if (id > 0) {
					fjDao.addStatRecord(fjRun.get(i), fjFlt.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 + ".FJSTAT.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 + ".FJSTAT.START"));
		
		// 获取目标ACU风机相关的资产列表，即风机列表
		String zcListStr = Configure.getProperty("acubl", acucode + ".FJ.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 runBlObj = blDao.findBlByBh(zcbh + ".Run");
			if (null == runBlObj) {
				continue;
			}
			
			int rkszdz = runBlObj.getKszdz(); //开始字地址
			int roffset = rkszdz - start; //与开始字的偏移量
			int rbit = runBlObj.getSzw(); //所在位
			
			char runBt = dataStr.charAt(dataStr.length() - 1 - roffset * 16 - rbit); // 风机运行状态标志位
			
			// 解析风机故障报警状态
			PgHjsbbl fltBlObj = blDao.findBlByBh(zcbh + ".Flt");
			if (null == fltBlObj) {
				continue;
			}
			
			int fkszdz = fltBlObj.getKszdz(); //开始字地址
			int foffset = fkszdz - start; //与开始字的偏移量
			int fbit = fltBlObj.getSzw(); //所在位
			
			char fltBt = dataStr.charAt(dataStr.length() - 1 - foffset * 16 - fbit); // 风机故障报警标志位
			
			getFjFlt().add(fltBt == '1' ? true : false);
			getFjRun().add(runBt == '1' ? true : false);
		}
	}

}
