package com.szpg.plc.message.response.read;

import com.szpg.db.dao.*;
import com.szpg.db.dao.impl.*;
import com.szpg.db.data.PgAlarm;
import com.szpg.db.data.PgAlarmRule;
import com.szpg.db.data.PgDevice;
import com.szpg.plc.message.response.ReadMemoryCommandResponse;
import com.szpg.util.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class ReadPowerValueCommandResponse extends ReadMemoryCommandResponse {

	/**
	 *
	 */
	private static final long serialVersionUID = 2964733118081941174L;

	private final Logger logger = Logger.getLogger(this.getClass().getName());

	private List<Float> au; //A相电压
	private List<Float> bu; //B相电压
	private List<Float> cu; //C相电压
	private List<Float> ai; //A相电流
	private List<Float> bi; //B相电流
	private List<Float> ci; //C相电流
	private String[] zcList; //电力监测的资产列表，从配置文件中获取

	public ReadPowerValueCommandResponse() {
		au = new ArrayList<Float>();
		bu = new ArrayList<Float>();
		cu = new ArrayList<Float>();
		ai = new ArrayList<Float>();
		bi = new ArrayList<Float>();
		ci = new ArrayList<Float>();
	}

	public List<Float> getAu() {
		return au;
	}

	public void setAu(List<Float> au) {
		this.au = au;
	}

	public List<Float> getBu() {
		return bu;
	}

	public void setBu(List<Float> bu) {
		this.bu = bu;
	}

	public List<Float> getCu() {
		return cu;
	}

	public void setCu(List<Float> cu) {
		this.cu = cu;
	}

	public List<Float> getAi() {
		return ai;
	}

	public void setAi(List<Float> ai) {
		this.ai = ai;
	}

	public List<Float> getBi() {
		return bi;
	}

	public void setBi(List<Float> bi) {
		this.bi = bi;
	}

	public List<Float> getCi() {
		return ci;
	}

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

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

	@Override
	public void afterAction() {
		// 1将电力监测值数据存入数据库
		PgPowerDao powerDao = new PgPowerDaoImpl();
		PgDeviceDao deviceDao = new PgDeviceDaoImpl();
		
		if (null != zcList && zcList.length > 0) {
			// 遍历电力设备列表，将电力浓度监测值存入数据库
			for (int i = 0; i < zcList.length; i++) {
				String zcbh = zcList[i];
				
				float auValue = au.get(i);
				float buValue = bu.get(i);
				float cuValue = cu.get(i);
				float aiValue = ai.get(i);
				float biValue = bi.get(i);
				float ciValue = ci.get(i);
				
				int deviceId = deviceDao.findDeviceIdByCode(zcbh);
				if (deviceId > 0) {
					try {
						powerDao.addPowerRecord(auValue, buValue, cuValue, aiValue, biValue, ciValue, TimeFormat.formatTimestamp(this.getTime().getTime()), deviceId);
					} catch (Exception ex) {
						logger.error("添加电力监测记录异常");
					}
				} else {
					logger.warn("未找到资产[" + zcbh + "]");
				}
			}
		}
	}

	@Override
	public void parseData() {
		// 获取目标ACU的代码
		String acucode = this.getAcucode();
		byte[] messageData = this.getResponseData();
		
		logger.info("开始解析[读取电力值][" + acucode + "]指令响应");
		
		// 判断数据的长度是否满足要求
		if (messageData.length != Integer.parseInt(Configure.getProperty("acubl", acucode + ".DL.WORDCOUNT")) * 2) {
			logger.error("返回的数据长度与读取的不一致！");
			this.setValid(false);
			return;
		}
				
		// 获取目标ACU电力监测值内存区域的起始字地址
		int start = Integer.parseInt(Configure.getProperty("acubl", acucode + ".DL.START"));
		
		// 获取目标ACU电力相关的资产列表，即电力设备列表
		String zcListStr = Configure.getProperty("acubl", acucode + ".DL.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解析A相电压监测值
			float valueAu = getValueFromData(zcbh, start, messageData, zcbh + ".AU");
			
			// 2解析B相电压监测值
			float valueBu = getValueFromData(zcbh, start, messageData, zcbh + ".BU");
			
			// 3解析C相电压监测值
			float valueCu = getValueFromData(zcbh, start, messageData, zcbh + ".CU");
			
			// 4解析A相电流监测值
			float valueAi = getValueFromData(zcbh, start, messageData, zcbh + ".AI");

			// 5解析B相电流监测值
			float valueBi = getValueFromData(zcbh, start, messageData, zcbh + ".BI");

			// 6解析C相电流监测值
			float valueCi = getValueFromData(zcbh, start, messageData, zcbh + ".CI");
			
			getAu().add(valueAu);
			getBu().add(valueBu);
			getCu().add(valueCu);
			getAi().add(valueAi);
			getBi().add(valueBi);
			getCi().add(valueCi);
			
			logger.info("[" + zcbh + "][A相" + valueAu + ", " + valueAi + "][B相" + valueBu + ", " + valueBi + "][C相" + valueCu + ", " + valueCi + "]");
		}
		
		logger.info("解析[读取电力值]响应指令结束");
	}
 
}
