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.PgO2Dao;
import com.szpg.db.dao.PgDeviceDao;
import com.szpg.db.dao.PgHjsbblDao;
import com.szpg.db.dao.impl.PgO2DaoImpl;
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.plc.util.Bytes;
import com.szpg.util.Configure;
import com.szpg.util.TimeFormat;

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

	private final Logger logger = Logger.getLogger(this.getClass().getName());
	
	private List<Float> o2nd; // 氧气浓度值
	private List<Float> o2ldbjz; // 氧气联动报警值
	private String[] zcList; // 氧气监测设备资产列表，从配置文件中获取

	public ReadO2ValueCommandResponse() {
		o2nd = new ArrayList<Float>();
		o2ldbjz = new ArrayList<Float>();
	}

	public List<Float> getO2nd() {
		return o2nd;
	}

	public void setO2nd(List<Float> o2nd) {
		this.o2nd = o2nd;
	}

	public List<Float> getO2ldbjz() {
		return o2ldbjz;
	}

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

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

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

}
