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.PgHSDao;
import com.szpg.db.dao.PgHjsbblDao;
import com.szpg.db.dao.impl.PgDeviceDaoImpl;
import com.szpg.db.dao.impl.PgHSDaoImpl;
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.plc.util.Bytes;
import com.szpg.util.Configure;
import com.szpg.util.TimeFormat;

public class ReadHSValueCommandResponse extends ReadMemoryCommandResponse {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3864512113984510244L;

	private final Logger logger = Logger.getLogger(this.getClass().getName());
	
	private List<Float> hsnd; //硫化氢浓度值
	private List<Float> hsldbjz; //硫化氢联动报警值
	private String[] zcList; // 监测设备资产列表，从配置文件中获取
	
	public ReadHSValueCommandResponse() {
		hsnd = new ArrayList<Float>();
		hsldbjz = new ArrayList<Float>();
	}

	public List<Float> getHsnd() {
		return hsnd;
	}

	public void setHsnd(List<Float> hsnd) {
		this.hsnd = hsnd;
	}

	public List<Float> getHsldbjz() {
		return hsldbjz;
	}

	public void setHsldbjz(List<Float> hsldbjz) {
		this.hsldbjz = hsldbjz;
	}
	
	public String[] getZcList() {
		return zcList;
	}

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

	@Override
	public void afterAction() {
		// 1将硫化氢浓度数据存入数据库
		PgHSDao hsDao = new PgHSDaoImpl();
		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) {
					hsDao.addPblzRecord(hsnd.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 + ".HS.WORDCOUNT")) * 2) {
			logger.error("返回的数据长度与读取的不一致！");
			this.setValid(false);
			return;
		}
		
		PgHjsbblDao blDao = new PgHjsbblDaoImpl();
		
		// 获取目标ACU硫化氢监测值内存区域的起始字地址
		int start = Integer.parseInt(Configure.getProperty("acubl", acucode + ".HS.START"));
		
		// 获取目标ACU硫化氢相关的资产列表，即硫化氢设备列表
		String zcListStr = Configure.getProperty("acubl", acucode + ".HS.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 valueBlObj = blDao.findBlByBh(zcbh + ".Value");
			if (null == valueBlObj) {
				continue;
			}
			
			int vkszdz = valueBlObj.getKszdz(); //开始字地址
			int vjszdz = valueBlObj.getJszdz(); //结束字地址
			int vn = vjszdz - (vkszdz - 1); //字数
			int voffset = vkszdz - start; //与开始字的偏移量
			
			Bytes valueBytes = new Bytes();
			for (int j = vn; j > 0; j--) {
				valueBytes.append(new byte[] {messageData[(voffset + j - 1) * 2], messageData[(voffset + j - 1) * 2 + 1]});
			}
			
			float value = Float.intBitsToFloat(Integer.parseInt(ByteUtil.binToHexString(valueBytes.toBytes()), 16)); //硫化氢浓度值
			
			// 解析硫化氢浓度报警阈值
			PgHjsbbl thresholdBlObj = blDao.findBlByBh(zcbh + ".Set");
			if (null == thresholdBlObj) {
				continue;
			}
			
			int tkszdz = thresholdBlObj.getKszdz(); //开始字地址
			int tjszdz= thresholdBlObj.getJszdz(); //结束字地址
			int tn = tjszdz - (tkszdz - 1); //字数
			int toffset = tkszdz - start; //与开始字的偏移量
			
			Bytes thresholdBytes = new Bytes();
			for (int k = tn; k > 0; k--) {
				thresholdBytes.append(new byte[] {messageData[(toffset + k - 1) * 2], messageData[(toffset + k - 1) * 2 + 1]});
			}
			
			float threshold = Float.intBitsToFloat(Integer.parseInt(ByteUtil.binToHexString(thresholdBytes.toBytes()), 16)); //硫化氢报警阈值
			
			getHsnd().add(value);
			getHsldbjz().add(threshold);
		}
	}

}
