Newer
Older
pgdsc / src / com / szpg / plc / message / response / read / ReadFjRtCommandResponse.java
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.PgFjDao;
import com.szpg.db.dao.PgHjsbblDao;
import com.szpg.db.dao.PgDeviceDao;
import com.szpg.db.dao.impl.PgFjDaoImpl;
import com.szpg.db.dao.impl.PgHjsbblDaoImpl;
import com.szpg.db.data.PgHjsbbl;
import com.szpg.db.dao.impl.PgDeviceDaoImpl;
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 ReadFjRtCommandResponse extends ReadMemoryCommandResponse {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3733073050500592967L;

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

	private List<Integer> fjscs; // 风机运行时长——秒数
	private List<Integer> fjsch; // 风机运行时长——小时数
	private String[] zcList; //资产列表,从配置文件中获取

	public ReadFjRtCommandResponse() {
		fjscs = new ArrayList<Integer>();
		fjsch = new ArrayList<Integer>();
	}

	public List<Integer> getFjscs() {
		return fjscs;
	}

	public void setFjscs(List<Integer> fjscs) {
		this.fjscs = fjscs;
	}

	public List<Integer> getFjsch() {
		return fjsch;
	}

	public void setFjsch(List<Integer> fjsch) {
		this.fjsch = fjsch;
	}
	
	public String[] getZcList() {
		return zcList;
	}

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

	@Override
	public void afterAction() {
		// 1将风机累计运行时长数据存入数据库
		PgFjDao fjDao = new PgFjDaoImpl();
		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) {
					fjDao.addRtRecord(fjscs.get(i), fjsch.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 + ".FJRT.WORDCOUNT")) * 2) {
			logger.error("返回的数据长度与读取的不一致!");
			this.setValid(false);
			return;
		}
		
		PgHjsbblDao blDao = new PgHjsbblDaoImpl();
		
		// 获取目标ACU风机运行时间内存区域的起始字地址
		int start = Integer.parseInt(Configure.getProperty("acubl", acucode + ".FJRT.START"));
		
		// 获取目标ACU风机相关的资产列表,即风机设备列表
		String zcListStr = Configure.getProperty("acubl", acucode + ".FJ.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 hourBlObj = blDao.findBlByBh(zcbh + ".RunH");
			if (null == hourBlObj) {
				continue;
			}
			
			int hkszdz = hourBlObj.getKszdz(); //开始字地址
			int hjszdz = hourBlObj.getJszdz(); //结束字地址
			int hn = hjszdz - (hkszdz - 1); //字数
			int hoffset = hkszdz - start; //与开始字的偏移量
			
			Bytes hourBytes = new Bytes();
			for (int j = hn; j > 0; j--) {
				hourBytes.append(new byte[] {messageData[(hoffset + j - 1) * 2], messageData[(hoffset + j - 1) * 2 + 1]});
			}
			
			int hour = ByteUtil.binToInt(hourBytes.toBytes()); //运行时长的小时数
			
			// 解析风机运行时长的秒数
			PgHjsbbl secondBlObj = blDao.findBlByBh(zcbh + ".RunS");
			if (null == secondBlObj) {
				continue;
			}
			
			int skszdz = secondBlObj.getKszdz(); //开始字地址
			int sjszdz= secondBlObj.getJszdz(); //结束字地址
			int sn = sjszdz - (skszdz - 1); //字数
			int soffset = skszdz - start; //与开始字的偏移量
			
			Bytes secondBytes = new Bytes();
			for (int k = sn; k > 0; k--) {
				secondBytes.append(new byte[] {messageData[(soffset + k - 1) * 2], messageData[(soffset + k - 1) * 2 + 1]});
			}
			
			int second = ByteUtil.binToInt(secondBytes.toBytes()); //运行时长的秒数
			
			getFjsch().add(hour);
			getFjscs().add(second);
		}
	}

}