Newer
Older
pgdsc / src / com / szpg / plc / message / response / read / ReadSbRtCommandResponse.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.PgDeviceDao;
import com.szpg.db.dao.PgSbDao;
import com.szpg.db.dao.PgHjsbblDao;
import com.szpg.db.dao.impl.PgDeviceDaoImpl;
import com.szpg.db.dao.impl.PgSbDaoImpl;
import com.szpg.db.dao.impl.PgHjsbblDaoImpl;
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 ReadSbRtCommandResponse extends ReadMemoryCommandResponse {

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

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

	private List<Integer> sb1scs; // 水泵1运行时长——秒数
	private List<Integer> sb2scs; // 水泵2运行时长——秒数
	private List<Integer> sb1sch; // 水泵1运行时长——小时数
	private List<Integer> sb2sch; // 水泵2运行时长——小时数
	private String[] zcList; //资产列表,从配置文件中获取

	public ReadSbRtCommandResponse() {
		sb1scs = new ArrayList<Integer>();
		sb2scs = new ArrayList<Integer>();
		sb1sch = new ArrayList<Integer>();
		sb2sch = new ArrayList<Integer>();
	}
	
	public List<Integer> getSb1scs() {
		return sb1scs;
	}

	public void setSb1scs(List<Integer> sb1scs) {
		this.sb1scs = sb1scs;
	}

	public List<Integer> getSb2scs() {
		return sb2scs;
	}

	public void setSb2scs(List<Integer> sb2scs) {
		this.sb2scs = sb2scs;
	}

	public List<Integer> getSb1sch() {
		return sb1sch;
	}

	public void setSb1sch(List<Integer> sb1sch) {
		this.sb1sch = sb1sch;
	}

	public List<Integer> getSb2sch() {
		return sb2sch;
	}

	public void setSb2sch(List<Integer> sb2sch) {
		this.sb2sch = sb2sch;
	}

	public String[] getZcList() {
		return zcList;
	}

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

	@Override
	public void afterAction() {
		// 1将水泵累计运行时长数据存入数据库
		PgSbDao sbDao = new PgSbDaoImpl();
		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) {
					// 根据是否有数据来判断单泵还是双泵
					if (null == sb2sch.get(i)) {
						sbDao.addRtRecord(sb1scs.get(i), sb1sch.get(i), TimeFormat.formatTimestamp(this.getTime().getTime()), id);
					} else {
						sbDao.addRtRecord(sb1scs.get(i), sb1sch.get(i), sb2scs.get(i), sb2sch.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 + ".SBRT.WORDCOUNT")) * 2) {
			logger.error("返回的数据长度与读取的不一致!");
			this.setValid(false);
			return;
		}
		
		PgHjsbblDao blDao = new PgHjsbblDaoImpl();
		
		// 获取目标ACU水泵运行时间内存区域的起始字地址
		int start = Integer.parseInt(Configure.getProperty("acubl", acucode + ".SBRT.START"));
		
		// 获取目标ACU水泵相关的资产列表,即水泵设备列表
		String zcListStr = Configure.getProperty("acubl", acucode + ".SB.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 hour1BlObj = blDao.findBlByBh(zcbh + ".RunH1");
			if (null == hour1BlObj) {
				continue;
			}
			
			int h1kszdz = hour1BlObj.getKszdz(); //开始字地址
			int h1jszdz = hour1BlObj.getJszdz(); //结束字地址
			int h1n = h1jszdz - (h1kszdz - 1); //字数
			int h1offset = h1kszdz - start; //与开始字的偏移量
			
			Bytes hour1Bytes = new Bytes();
			for (int j = h1n; j > 0; j--) {
				hour1Bytes.append(new byte[] {messageData[(h1offset + j - 1) * 2], messageData[(h1offset + j - 1) * 2 + 1]});
			}
			
			int hour1 = ByteUtil.binToInt(hour1Bytes.toBytes()); //运行时长的小时数
			
			// 解析水泵2运行时长的小时数
			PgHjsbbl hour2BlObj = blDao.findBlByBh(zcbh + ".RunH2");
			if (null == hour2BlObj) {
				sb2sch.add(null);
			} else {
				int h2kszdz = hour2BlObj.getKszdz(); //开始字地址
				int h2jszdz = hour2BlObj.getJszdz(); //结束字地址
				int h2n = h2jszdz - (h2kszdz - 1); //字数
				int h2offset = h2kszdz - start; //与开始字的偏移量
				
				Bytes hour2Bytes = new Bytes();
				for (int j = h2n; j > 0; j--) {
					hour2Bytes.append(new byte[] {messageData[(h2offset + j - 1) * 2], messageData[(h2offset + j - 1) * 2 + 1]});
				}
				
				int hour2 = ByteUtil.binToInt(hour2Bytes.toBytes()); //运行时长的小时数
				sb2sch.add(hour2);
			}
			
			// 解析水泵1运行时长的秒数
			PgHjsbbl second1BlObj = blDao.findBlByBh(zcbh + ".RunS1");
			if (null == second1BlObj) {
				continue;
			}
			
			int s1kszdz = second1BlObj.getKszdz(); //开始字地址
			int s1jszdz= second1BlObj.getJszdz(); //结束字地址
			int s1n = s1jszdz - (s1kszdz - 1); //字数
			int s1offset = s1kszdz - start; //与开始字的偏移量
			
			Bytes second1Bytes = new Bytes();
			for (int k = s1n; k > 0; k--) {
				second1Bytes.append(new byte[] {messageData[(s1offset + k - 1) * 2], messageData[(s1offset + k - 1) * 2 + 1]});
			}
			
			int second1 = ByteUtil.binToInt(second1Bytes.toBytes()); //运行时长的秒数
			
			// 解析水泵2运行时长的秒数
			PgHjsbbl second2BlObj = blDao.findBlByBh(zcbh + ".RunS2");
			if (null == second2BlObj) {
				sb2scs.add(null);
			} else {
				int s2kszdz = second2BlObj.getKszdz(); //开始字地址
				int s2jszdz= second2BlObj.getJszdz(); //结束字地址
				int s2n = s2jszdz - (s2kszdz - 1); //字数
				int s2offset = s2kszdz - start; //与开始字的偏移量
				
				Bytes second2Bytes = new Bytes();
				for (int k = s2n; k > 0; k--) {
					second2Bytes.append(new byte[] {messageData[(s2offset + k - 1) * 2], messageData[(s2offset + k - 1) * 2 + 1]});
				}
				
				int second2 = ByteUtil.binToInt(second2Bytes.toBytes()); //运行时长的秒数
				sb2scs.add(second2);
			}
			
			getSb1sch().add(hour1);
			getSb1scs().add(second1);
		}
	}

}