package com.gyee.alarm.rule;


import com.gyee.alarm.model.vo.DeviceTypeValue;
import com.gyee.alarm.rule.expression.AlarmExpression;
import com.gyee.common.model.PointData;

import java.util.ArrayList;
import java.util.List;

/**
 * @descrition:获取平均值,同一个风机传入多个测点(统一编码)
 * @author:Wanghs
 * @date:2020-04-07
 */
public class AlarmFunctionAVG  extends AlarmFunction{
    public AlarmFunctionAVG(AlarmExpression alarmExpression, DeviceTypeValue tType, String tId) {
        super(alarmExpression, tType, tId);
    }

    @Override
    public Object explain() {
        if (this.getAlarmExpression().getVarList() == null || this.getAlarmExpression().getVarList().size() < 1)
            return 0;

        List<Double> valueList=new ArrayList<>();

        for (int i = 0; i < this.getAlarmExpression().getVarList().size(); i++) {
            PointData tagInfo = this.getTagInfo(this.getAlarmExpression().getVarList().get(i));
           if(tagInfo!=null){
               valueList.add(tagInfo.getPointValueInDouble());
           }
        }

        return valueList.stream().mapToDouble(Double::doubleValue).average().getAsDouble();
    }

}
