package com.jiayue.ipfcst.dataexchange.dataprocesser.datagainerhandler.iml;

import com.jiayue.ipfcst.dataexchange.container.ProtocolDataContainer;
import com.jiayue.ipfcst.dataexchange.dataprocesser.datagainerhandler.BaseDataGainer;
import com.jiayue.ipfcst.dataexchange.dataprocesser.datagainerhandler.NumOrBool;
import com.jiayue.ipfcst.dataexchange.exception.DataExchangeException;
import lombok.Getter;

import java.math.BigDecimal;

/**
 * 从池子里采集数据
 *
 * @author: xiuwei
 * @version: 3.0
 */
public class GetDataFromContainer<V> extends BaseDataGainer<V> {

  @Getter
  /**
   * 对应的数据池的点位
   */
    Integer sourcePoint;

  /**
   * 构造方法
   *
   * @param expectedType  数据类型
   * @param sourcePoint   对应的数据池的点位
   * @param magnification 倍率
   */
  public GetDataFromContainer(NumOrBool expectedType, Integer sourcePoint, Double magnification) {
    super(expectedType, magnification);
    this.sourcePoint = sourcePoint;
  }

  @Override
  public V getData() throws DataExchangeException {
    Object object = ProtocolDataContainer.getInstance().getData(sourcePoint);
    if (NumOrBool.BOOL.equals(this.getExpectedType())) {
      //需要遥信
      if (object instanceof BigDecimal) {
        object = ((BigDecimal) object).intValue() > 0;
      }
    } else {
      if (object instanceof Boolean) {
        object = ((Boolean) object) ? BigDecimal.valueOf(1) : BigDecimal.valueOf(0);
      } else {
        object = (((BigDecimal) object)).multiply(BigDecimal.valueOf(getMagnification())).setScale(2, BigDecimal.ROUND_HALF_UP);
      }
    }
    return (V) object;
  }
}
