package com.jiayue.ipfcst.dataexchange.tunnelworker.workassist.iec104tcp;


import com.jiayue.ipfcst.common.data.entity.ProtocolGatherDataPoint;
import com.jiayue.ipfcst.dataexchange.container.ProtocolDataContainer;
import org.joda.time.DateTime;
import wei.yigulu.iec104.annotation.AsduType;
import wei.yigulu.iec104.apdumodel.Apdu;
import wei.yigulu.iec104.asdudataframe.CumulativeIntegerType;
import wei.yigulu.iec104.asdudataframe.ShortFloatType;
import wei.yigulu.iec104.asdudataframe.qualitydescription.IeCounterReadingQuality;
import wei.yigulu.iec104.asdudataframe.qualitydescription.IeMeasuredQuality;
import wei.yigulu.iec104.asdudataframe.typemodel.IeFourByteInteger;
import wei.yigulu.iec104.asdudataframe.typemodel.InformationBodyAddress;
import wei.yigulu.iec104.nettyconfig.Iec104HSMasterBuilder;
import wei.yigulu.netty.BaseProtocolBuilder;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 处理短浮点
 *
 * @author 修唯xiuwei
 * @version 3.0
 */
@AsduType(typeId = 15)
public class HandleCumulativeValue extends CumulativeIntegerType {

  private ProtocolDataContainer protocolDataContainer = ProtocolDataContainer.getInstance();

  /**
   * 处理短浮点数据
   */
  @Override
  public byte[][] handleAndAnswer(Apdu apdu) {
    apdu.getLog().trace("----------处理累计值数据---------");
    BaseProtocolBuilder builder = apdu.getIec104Builder();
    if (builder != null && builder instanceof Iec104HSMasterBuilder) {
      Map<Integer, ProtocolGatherDataPoint> dataPoint = (Map<Integer, ProtocolGatherDataPoint>) builder.getConfigInfoMap().get("104DataPoint");
      builder.getConfigInfoMap().put("lastReceiveTime", DateTime.now());
      HandleCumulativeValue cumulativeValue = (HandleCumulativeValue) apdu.getAsdu().getDataFrame();
      List<InformationBodyAddress> address = cumulativeValue.getAddresses();
      Map<IeCounterReadingQuality, IeFourByteInteger> dates = cumulativeValue.getDatas();
      int i = 0;
      //存入数据池
      if (apdu.getAsdu().getVsq().getSq() == 0) {
        apdu.getLog().info("------处理累计值单一寻址-----");
        for (Map.Entry<IeCounterReadingQuality, IeFourByteInteger> e : dates.entrySet()) {
          if (dataPoint.containsKey(address.get(i).getAddress())&& e.getKey().isGoodValue()) {
            protocolDataContainer.putNumber(dataPoint.get(address.get(i).getAddress()).getId(), BigDecimal.valueOf(e.getValue().getValue()).multiply(BigDecimal.valueOf(dataPoint.get(address.get(i).getAddress()).getMagnification())));
          }
          i++;
        }
      } else if (apdu.getAsdu().getVsq().getSq() == 1) {
        apdu.getLog().info("------处理累计值连续寻址-----");
        i = address.get(0).getAddress();
        for (Map.Entry<IeCounterReadingQuality, IeFourByteInteger> e : dates.entrySet()) {
          if (dataPoint.containsKey(i) && e.getKey().isGoodValue()) {
            protocolDataContainer.putNumber(dataPoint.get(i).getId(), BigDecimal.valueOf(e.getValue().getValue()).multiply(BigDecimal.valueOf(dataPoint.get(i).getMagnification())));
          }
          i++;
        }
      }
    }
    return null;
  }

}
