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.PgHjsbblDao;
import com.szpg.db.dao.PgWsDao;
import com.szpg.db.dao.impl.PgDeviceDaoImpl;
import com.szpg.db.dao.impl.PgHjsbblDaoImpl;
import com.szpg.db.dao.impl.PgWsDaoImpl;
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 ReadWSValueCommandResponse extends ReadMemoryCommandResponse {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2964733118081941174L;

	private final Logger logger = Logger.getLogger(this.getClass().getName());
	
	private List<Float> wd; //温度监测值
	private List<Float> sd; //湿度监测值
	private List<Float> wdbjz; //温度报警阈值
	private List<Float> sdbjz; //湿度报警阈值
	private String[] zcList; //温湿度监测的资产列表，从配置文件中获取
	
	public ReadWSValueCommandResponse() {
		wd = new ArrayList<Float>();
		sd = new ArrayList<Float>();
		wdbjz = new ArrayList<Float>();
		sdbjz = new ArrayList<Float>();
	}

	public List<Float> getWd() {
		return wd;
	}

	public void setWd(List<Float> wd) {
		this.wd = wd;
	}

	public List<Float> getSd() {
		return sd;
	}

	public void setSd(List<Float> sd) {
		this.sd = sd;
	}

	public List<Float> getWdbjz() {
		return wdbjz;
	}

	public void setWdbjz(List<Float> wdbjz) {
		this.wdbjz = wdbjz;
	}

	public List<Float> getSdbjz() {
		return sdbjz;
	}

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

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

	@Override
	public void afterAction() {
		// 1将温湿度监测值数据存入数据库
		PgWsDao wsDao = new PgWsDaoImpl();
		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) {
					wsDao.addWsdzRecord(wd.get(i), sd.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 + ".WS.WORDCOUNT")) * 2) {
			logger.error("返回的数据长度与读取的不一致！");
			this.setValid(false);
			return;
		}
		
		PgHjsbblDao blDao = new PgHjsbblDaoImpl();
		
		// 获取目标ACU温湿度监测值内存区域的起始字地址
		int start = Integer.parseInt(Configure.getProperty("acubl", acucode + ".WS.START"));
		
		// 获取目标ACU温湿度相关的资产列表，即温湿度设备列表
		String zcListStr = Configure.getProperty("acubl", acucode + ".WS.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];
			
			// 1解析温度监测值
			PgHjsbbl valueTemBlObj = blDao.findBlByBh(zcbh + ".TemValue");
			if (null == valueTemBlObj) {
				continue;
			}
			
			int vwkszdz = valueTemBlObj.getKszdz(); //开始字地址
			int vwjszdz = valueTemBlObj.getJszdz(); //结束字地址
			int vwn = vwjszdz - (vwkszdz - 1); //字数
			int vwoffset = vwkszdz - start; //与开始字的偏移量
			
			Bytes valueTemBytes = new Bytes();
			for (int j = vwn; j > 0; j--) {
				valueTemBytes.append(new byte[] {messageData[(vwoffset + j - 1) * 2], messageData[(vwoffset + j - 1) * 2 + 1]});
			}
			
			float valueTem = Float.intBitsToFloat(Integer.parseInt(ByteUtil.binToHexString(valueTemBytes.toBytes()), 16)); //温度监测值
			
			
			// 2解析湿度监测值
			PgHjsbbl valueWetBlObj = blDao.findBlByBh(zcbh + ".WetValue");
			if (null == valueWetBlObj) {
				continue;
			}
			
			int vskszdz = valueWetBlObj.getKszdz(); //开始字地址
			int vsjszdz = valueWetBlObj.getJszdz(); //结束字地址
			int vsn = vsjszdz - (vskszdz - 1); //字数
			int vsoffset = vskszdz - start; //与开始字的偏移量
			
			Bytes valueWetBytes = new Bytes();
			for (int j = vsn; j > 0; j--) {
				valueWetBytes.append(new byte[] {messageData[(vsoffset + j - 1) * 2], messageData[(vsoffset + j - 1) * 2 + 1]});
			}
			
			float valueWet = Float.intBitsToFloat(Integer.parseInt(ByteUtil.binToHexString(valueWetBytes.toBytes()), 16)); //湿度监测值
			
			// 3解析温度报警阈值
			PgHjsbbl thresholdTemBlObj = blDao.findBlByBh(zcbh + ".TemSet");
			if (null == thresholdTemBlObj) {
				continue;
			}
			
			int twkszdz = thresholdTemBlObj.getKszdz(); //开始字地址
			int twjszdz= thresholdTemBlObj.getJszdz(); //结束字地址
			int twn = twjszdz - (twkszdz - 1); //字数
			int twoffset = twkszdz - start; //与开始字的偏移量
			
			Bytes thresholdTemBytes = new Bytes();
			for (int k = twn; k > 0; k--) {
				thresholdTemBytes.append(new byte[] {messageData[(twoffset + k - 1) * 2], messageData[(twoffset + k - 1) * 2 + 1]});
			}
			
			float thresholdTem = Float.intBitsToFloat(Integer.parseInt(ByteUtil.binToHexString(thresholdTemBytes.toBytes()), 16)); //温度报警阈值
			
			
			// 4解析湿度报警阈值
			PgHjsbbl thresholdWetBlObj = blDao.findBlByBh(zcbh + ".WetSet");
			if (null == thresholdWetBlObj) {
				continue;
			}
			
			int tskszdz = thresholdWetBlObj.getKszdz(); //开始字地址
			int tsjszdz= thresholdWetBlObj.getJszdz(); //结束字地址
			int tsn = tsjszdz - (tskszdz - 1); //字数
			int tsoffset = tskszdz - start; //与开始字的偏移量
			
			Bytes thresholdWetBytes = new Bytes();
			for (int k = tsn; k > 0; k--) {
				thresholdWetBytes.append(new byte[] {messageData[(tsoffset + k - 1) * 2], messageData[(tsoffset + k - 1) * 2 + 1]});
			}
			
			float thresholdWet = Float.intBitsToFloat(Integer.parseInt(ByteUtil.binToHexString(thresholdWetBytes.toBytes()), 16)); //湿度报警阈值
			
			getWd().add(valueTem);
			getSd().add(valueWet);
			getWdbjz().add(thresholdTem);
			getSdbjz().add(thresholdWet);
		}
	}

}
