package com.bocloud.cmp.model.installation;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;

/**
 * rackhd poller获取的传感器数据
 *
 * @author 007
 */
public class SensorData {
	/**
	 * 传感器Id，一般为传感器名称
	 */
	private String sensorId;
	/**
	 * 传感器类型，
	 */
	private String sensorType;
	/**
	 * 传感器状态
	 * <p>
	 * 只有状态ok的数据才认为是合法数据
	 */
	private String status;
	/**
	 * 传感器读数
	 * <p>
	 * 过滤掉"No Reading"的读数
	 */
	private String sensorReading;
	/**
	 * 传感器读数单位
	 */
	private String sensorReadingUnits;
	/**
	 * 传感器读数上限
	 */
	private String upperCritical;
	/**
	 * 传感器读数下限
	 */
	private String lowerCritical;

	public SensorData() {
		super();
	}

	public SensorData(String sensorId, String sensorReading, String sensorReadingUnits, String upperCritical,
			String lowerCritical) {
		super();
		this.sensorId = modify(sensorId);
		this.parse(sensorReading, sensorReadingUnits);
		this.upperCritical = upperCritical;
		this.lowerCritical = lowerCritical;
	}

	public String modify(String info) {
		if (info == null) {
			return null;
		}
		if (info.contains(LEFT) && info.contains(RIGHT)) {
			String num = info.substring(info.indexOf(LEFT) + 2, info.indexOf(RIGHT));
			return info.substring(0, info.indexOf(LEFT)) + " (" + parse(num) + ")";
		}
		return info;
	}

	private static final String LEFT = " (";
	private static final String RIGHT = ")";
	private static final String PREFIX1 = "0x";
	private static final String PREFIX2 = "0X";

	public String parse(String str) {
		try {
			if (str.startsWith(PREFIX1)) {
				String num = str.substring(str.indexOf(PREFIX1) + 2, str.length());
				return String.valueOf(Long.parseLong(num, 16));
			} else if (str.startsWith(PREFIX2)) {
				String num = str.substring(str.indexOf(PREFIX2) + 2, str.length());
				return String.valueOf(Long.parseLong(num, 16));
			} else {
				return str;
			}
		} catch (Exception e) {
			return str;
		}
	}

	private void parse(String sensorReading, String sensorReadingUnits) {
		try {
			String reg = "([a-z]|[A-Z])+";
			this.sensorReading = sensorReading.split(reg)[0].trim();
			if (StringUtils.isEmpty(sensorReadingUnits)) {
				reg = "^(\\-|\\+)?\\d+(\\.\\d+)?";
				this.sensorReadingUnits = sensorReading.split(reg)[1].trim();
			} else {
				this.sensorReadingUnits = sensorReadingUnits;
			}
		} catch (Exception e) {
			this.sensorReading = sensorReading;
			this.sensorReadingUnits = sensorReadingUnits;
		}
	}

	/**
	 * @return the sensorId
	 */
	public String getSensorId() {
		return sensorId;
	}

	/**
	 * @param sensorId the sensorId to set
	 */
	public void setSensorId(String sensorId) {
		this.sensorId = sensorId;
	}

	/**
	 * @return the sensorType
	 */
	public String getSensorType() {
		return sensorType;
	}

	/**
	 * @param sensorType the sensorType to set
	 */
	public void setSensorType(String sensorType) {
		this.sensorType = sensorType;
	}

	/**
	 * @return the status
	 */
	public String getStatus() {
		return status;
	}

	/**
	 * @param status the status to set
	 */
	public void setStatus(String status) {
		this.status = status;
	}

	/**
	 * @return the sensorReading
	 */
	public String getSensorReading() {
		return sensorReading;
	}

	/**
	 * @param sensorReading the sensorReading to set
	 */
	public void setSensorReading(String sensorReading) {
		this.sensorReading = sensorReading;
	}

	/**
	 * @return the sensorReadingUnits
	 */
	public String getSensorReadingUnits() {
		return sensorReadingUnits;
	}

	/**
	 * @param sensorReadingUnits the sensorReadingUnits to set
	 */
	public void setSensorReadingUnits(String sensorReadingUnits) {
		this.sensorReadingUnits = sensorReadingUnits;
	}

	/**
	 * @return the upperCritical
	 */
	public String getUpperCritical() {
		return upperCritical;
	}

	/**
	 * @param upperCritical the upperCritical to set
	 */
	public void setUpperCritical(String upperCritical) {
		this.upperCritical = upperCritical;
	}

	/**
	 * @return the lowerCritical
	 */
	public String getLowerCritical() {
		return lowerCritical;
	}

	/**
	 * @param lowerCritical the lowerCritical to set
	 */
	public void setLowerCritical(String lowerCritical) {
		this.lowerCritical = lowerCritical;
	}

	@Override
	public String toString() {
		return JSONObject.toJSONString(this);
	}
}
