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.PgCh4Dao;
import com.szpg.db.dao.PgDeviceDao;
import com.szpg.db.dao.PgHjsbblDao;
import com.szpg.db.dao.impl.PgCh4DaoImpl;
import com.szpg.db.dao.impl.PgDeviceDaoImpl;
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 ReadCH4ValueCommandResponse extends ReadMemoryCommandResponse {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8081222348890587881L;

	private final Logger logger = Logger.getLogger(this.getClass().getName());
	
	private List<Float> jwnd; //甲烷浓度值
	private List<Float> jwldbjz; //甲烷联动报警值
	private String[] zcList; //甲烷监测的资产列表，从配置文件中获取
	
	public ReadCH4ValueCommandResponse() {
		jwnd = new ArrayList<Float>();
		jwldbjz = new ArrayList<Float>();
	}

	public List<Float> getJwnd() {
		return jwnd;
	}

	public void setJwnd(List<Float> jwnd) {
		this.jwnd = jwnd;
	}

	public List<Float> getJwldbjz() {
		return jwldbjz;
	}

	public void setJwldbjz(List<Float> jwldbjz) {
		this.jwldbjz = jwldbjz;
	}

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

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

	@Override
	public void afterAction() {
		// 1将甲烷浓度数据存入数据库
		PgCh4Dao ch4Dao = new PgCh4DaoImpl();
		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) {
					ch4Dao.addPblzRecord(jwnd.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 + ".CH.WORDCOUNT")) * 2) {
			logger.error("返回的数据长度与读取的不一致！");
			this.setValid(false);
			return;
		}
		
		PgHjsbblDao blDao = new PgHjsbblDaoImpl();
		
		// 获取目标ACU甲烷监测值内存区域的起始字地址
		int start = Integer.parseInt(Configure.getProperty("acubl", acucode + ".CH.START"));
		
		// 获取目标ACU甲烷相关的资产列表，即甲烷设备列表
		String zcListStr = Configure.getProperty("acubl", acucode + ".CH.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)); //甲烷报警阈值
			
			getJwnd().add(value);
			getJwldbjz().add(threshold);
		}
	}

}
