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

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

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.szpg.db.dao.PgAlarmDao;
import com.szpg.db.dao.PgAlarmRuleDao;
import com.szpg.db.dao.PgDeviceDao;
import com.szpg.db.dao.PgWsDao;
import com.szpg.db.dao.impl.PgAlarmDaoImpl;
import com.szpg.db.dao.impl.PgAlarmRuleDaoImpl;
import com.szpg.db.dao.impl.PgDeviceDaoImpl;
import com.szpg.db.dao.impl.PgWsDaoImpl;
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.Configure;
import com.szpg.util.DataPushInterface;
import com.szpg.util.HttpRequest;
import com.szpg.util.NumberFormat;
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();
		PgAlarmRuleDao ruleDao = new PgAlarmRuleDaoImpl();
		PgAlarmDao alarmDao = new PgAlarmDaoImpl();
		
		if (null != zcList && zcList.length > 0) {
			// 遍历温湿度设备列表，将温湿度浓度监测值存入数据库
			for (int i = 0; i < zcList.length; i++) {
				String zcbh = zcList[i];
				
				float wdValue = wd.get(i);
				float sdValue = sd.get(i);
				
				int deviceId = deviceDao.findDeviceIdByCode(zcbh);
				if (deviceId > 0) {
					try {
						wsDao.addWsdzRecord(wdValue, sdValue, TimeFormat.formatTimestamp(this.getTime().getTime()), deviceId);
					} catch (Exception ex) {
						logger.error("添加温湿度监测记录异常");
					}
					
					// (一)如果有报警则立即推送
					// 判断是否超过报警阈值
					PgAlarmRule rule = ruleDao.findRuleByDevice(deviceId);
					if (null != rule) {
						double wdHigh = 0.0;
						double sdHigh = 0.0;
						try {
							if (null != rule) {
								String highStr = rule.getHighvalue();
								if (null != highStr && highStr.isEmpty() == false && highStr.indexOf(";") > 0) {
									wdHigh = NumberFormat.parseDouble(highStr.split(";")[0], "0.00");
									sdHigh = NumberFormat.parseDouble(highStr.split(";")[1], "0.00");
								}
							}
						} catch (Exception ex) {
							logger.error(ex);
							ex.printStackTrace();
						}
						
						// 温度超过高限阈值
						if (wdHigh > 0.0001 && wdValue > wdHigh) {
							PgAlarm wdAlarm = new PgAlarm();
							wdAlarm.setActive(1);
							wdAlarm.setAlarm_date(this.getTime().getTime());
							wdAlarm.setAlarm_value(NumberFormat.format(wdValue, "0.00"));
							wdAlarm.setAlarmdevid(deviceId);
							wdAlarm.setAlarmtypeid(PgAlarm.ALARM_TYPE_PGRUN);
							wdAlarm.setDescription("温度超过高限阈值");
							wdAlarm.setUserid(null);
							
							alarmDao.insertAlarmRecord(wdAlarm);
							deviceDao.updateDeviceStatus(deviceId, "温度超高");
							
							// 将报警日志推送至市级平台
							String date = TimeFormat.formatDate(getTime().getTime());
							int count = alarmDao.findAlarmCountByDate(date);
							String bjbh = "THXC." + date + "." + NumberFormat.format(count+1, "0000");
							String bjms = wdAlarm.getDescription() + "，温度值为" + wdValue;
							String bjsj = date.replace("-", "");
							String wdbjResp = DataPushInterface.addTempAlarmData(bjbh, zcbh, bjms, bjsj);
							logger.info("同步温度报警记录日志结果：" + wdbjResp);
							
							// 将报警值推送至市级平台
							String wdResp = DataPushInterface.addTempData(zcbh, NumberFormat.format(wdValue, "0.00"), TimeFormat.format(this.getTime().getTime(), "yyyyMMddHHmmss"));
							logger.info("同步温度值结果：" + wdResp);
							
							// 自动打开当前舱段的排风机，手动关闭风机
							// 首先找到当前舱段的排风机资产编号
							if (Boolean.valueOf(Configure.getProperty("sys", "WD_ON_FJ", "false")) == true) {
								String acucode = zcbh.substring(0, zcbh.indexOf(".", 6));
								String cabin = zcbh.substring(zcbh.lastIndexOf(".") + 1, zcbh.lastIndexOf(".") + 2);
								List<PgDevice> fjs = deviceDao.findDeviceByPositionAndType(acucode, "F", cabin);
								if (null != fjs && fjs.isEmpty() == false) {
									String baseURL = Configure.getProperty("sys", "PGDSC_API_URL");
									for (PgDevice fj : fjs) {
										String fjzcbh = fj.getAssetcode();

										// 调用远程接口启动风机
										HttpRequest.sendGet(baseURL + "remote/turnOnFj.action", "zcbh=" + fjzcbh);
									}
								}
							}
						}
					
					
						// 湿度超过高限阈值
						if (sdHigh > 0.0001 && sdValue > sdHigh) {
							PgAlarm sdAlarm = new PgAlarm();
							sdAlarm.setActive(1);
							sdAlarm.setAlarm_date(this.getTime().getTime());
							sdAlarm.setAlarm_value(NumberFormat.format(sdValue, "0.00"));
							sdAlarm.setAlarmdevid(deviceId);
							sdAlarm.setAlarmtypeid(PgAlarm.ALARM_TYPE_PGRUN);
							sdAlarm.setDescription("湿度超过高限阈值");
							sdAlarm.setUserid(null);
							
							alarmDao.insertAlarmRecord(sdAlarm);
							deviceDao.updateDeviceStatus(deviceId, "湿度超高");
							
							// 将报警日志推送至市级平台
							String date = TimeFormat.formatDate(getTime().getTime());
							int count = alarmDao.findAlarmCountByDate(date);
							String bjbh = "THXC." + date + "." + NumberFormat.format(count+1, "0000");
							String bjms = sdAlarm.getDescription() + "，湿度值为" + sdValue;
							String bjsj = date.replace("-", "");
							String sdbjResp = DataPushInterface.addHumAlarmData(bjbh, zcbh, bjms, bjsj);
							logger.info("同步湿度报警记录日志结果：" + sdbjResp);
							
							// 将报警值推送至市级平台
							String sdResp = DataPushInterface.addHumData(zcbh, NumberFormat.format(sdValue, "0.00"), TimeFormat.format(this.getTime().getTime(), "yyyyMMddHHmmss"));
							logger.info("同步湿度值结果：" + sdResp);
							
							// 湿度不需要联动打开风机
						}
					}
					
					
					// (二)如果监测值有变化则推送
					// 判断温度值是否有变化
					double lastTemp = wsDao.findLastTempByDevice(deviceId);
					if (Math.abs(wdValue - lastTemp) > 0.01) {
						// 将监测值推送至市级平台
						String wdResp = DataPushInterface.addTempData(zcbh, NumberFormat.format(wdValue, "0.00"), TimeFormat.format(this.getTime().getTime(), "yyyyMMddHHmmss"));
						logger.info("同步温度值结果：" + wdResp);
					}
					
					// 判断湿度值是否有变化
					double lastHum = wsDao.findLastHumByDevice(deviceId);
					if (Math.abs(sdValue - lastHum) > 0.01) {
						// 将监测值推送至市级平台
						String sdResp = DataPushInterface.addHumData(zcbh, NumberFormat.format(sdValue, "0.00"), TimeFormat.format(this.getTime().getTime(), "yyyyMMddHHmmss"));
						logger.info("同步湿度值结果：" + sdResp);
					}
					
					// (三)如果监测值有变化则推送
					// 每天小时各推送一个数据
					int minute = Calendar.getInstance().get(Calendar.MINUTE);
					if (minute < 10) {
						// 将监测值推送至市级平台
						String wdResp = DataPushInterface.addTempData(zcbh, NumberFormat.format(wdValue, "0.00"), TimeFormat.format(this.getTime().getTime(), "yyyyMMddHHmmss"));
						logger.info("同步温度值结果：" + wdResp);
						
						String sdResp = DataPushInterface.addHumData(zcbh, NumberFormat.format(sdValue, "0.00"), TimeFormat.format(this.getTime().getTime(), "yyyyMMddHHmmss"));
						logger.info("同步湿度值结果：" + sdResp);
					}
				} 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 + ".WS.WORDCOUNT")) * 2) {
			logger.error("返回的数据长度与读取的不一致！");
			this.setValid(false);
			return;
		}
				
		// 获取目标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解析温度监测值
			float valueTem = getValueFromData(zcbh, start, messageData, zcbh + ".TemValue");
			
			// 2解析湿度监测值
			float valueWet = getValueFromData(zcbh, start, messageData, zcbh + ".WetValue");
			
			// 3解析温度报警阈值
			float threshTem = getValueFromData(zcbh, start, messageData, zcbh + ".TemSet");
			
			// 4解析湿度报警阈值
			float threshWet = getValueFromData(zcbh, start, messageData, zcbh + ".WetSet");
			
			getWd().add(valueTem);
			getSd().add(valueWet);
			getWdbjz().add(threshTem);
			getSdbjz().add(threshWet);
			
			logger.info("[" + zcbh + "][温度值" + valueTem + "][湿度值" + valueWet + "][温度报警阈值" + threshTem + "][湿度报警阈值" + threshWet + "]");
		}
		
		logger.info("解析[读取温湿度值]响应指令结束");
	}
 
}
