Expression pollRate sdk

We have developed an expression, but I hope that this expression can control the execution rate by passing a parameter, for example, 1000 after runscript ('exr() ', 1000) to control the execution rate, and now() expression, because the expression we developed also hopes to be executed repeatedly, can you give us some hints

Thanks

Make your expression implementation a subclass of com.inductiveautomation.ignition.common.expressions.functions.AbstractPollingFunction, and call its setPollRate() method with an appropriate millisecond interval within your execute() method.

{This topic belongs in the Module Development category…}

1 Like

i see

It can be repeatedly executed, but when getting parameters, it is always getting the parameters of the last expression. When this expression is repeatedly used

    @Override
    public QualifiedValue execute(Expression[] expressions) throws ExpressionException {

//       获取测点数据
        int pollTime = 1;
        if (expressions.length == 4) {
            pollTime = TypeUtilities.toInteger(expressions[3].execute().getValue());
        }
        setPollRate(pollTime * 1000);
//       获取参数出现问题
        String pathStr = TypeUtilities.toString(expressions[0].execute().getValue());
        int timeI = TypeUtilities.toInteger(expressions[2].execute().getValue());
        logger.info(pathStr);

I recommend you execute all of the expressions passed into your function, saving the results in a local array, before working with any of the results. Also, in my usage, I call setPollRate() just before returning the final result.

I’m not sure beyond that. You’ll have to show more of your code if this doesn’t help.

public class AIChangeFast extends AbstractPollingFunction {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    GatewayContext gatewayContext = null;

    public AIChangeFast(GatewayContext gatewayContext) {
        this.gatewayContext = gatewayContext;
    }

    public AIChangeFast() {
    }

    @Override
    public Function copy() {
        return this;
    }

    @Override
    public Class<?> getType() {
        return Boolean.class;
    }

    @Override
    protected String getFunctionDisplayName() {
        // This String shows up in logs as "Function 'exampleMultiply' ..." if there is an error when executing
        // the expression
        return "AIChangeFast";
    }

    @Override
    public String getArgDocString() {
        // This String shows in in the Designer when selecting the expression.
        return "tag1,float,time";
    }

//    @Override
//    protected boolean validateNumArgs(int num) {
//        // We need to have exactly 2 args for this expression.
//        return num == 2;
//    }

    @Override
    public QualifiedValue execute(Expression[] expressions) throws ExpressionException {

//       获取测点数据
        int pollTime = 1;
        if (expressions.length == 4) {
            pollTime = TypeUtilities.toInteger(expressions[3].execute().getValue());
        }
        setPollRate(pollTime * 1000);
//       获取参数出现问题
        String pathStr = TypeUtilities.toString(expressions[0].execute().getValue());
        int timeI = TypeUtilities.toInteger(expressions[2].execute().getValue());
        logger.info(pathStr);
//参数
        BasicTagHistoryQueryParams tagHistoryQueryParams = new BasicTagHistoryQueryParams();
        tagHistoryQueryParams.setReturnSize(1);
        tagHistoryQueryParams.setAggregationMode(AggregationMode.MinMax);
        Date startTime = new Date();
        startTime.setTime(new Date().getTime() - 1000 * timeI);
        tagHistoryQueryParams.setStartDate(startTime);
        Date endDate = new Date();
        tagHistoryQueryParams.setEndDate(endDate);
        // Tall : Column format is fixed at "path, value, quality, timestamp".
        tagHistoryQueryParams.setReturnFormat(ReturnFormat.Wide);
        tagHistoryQueryParams.setPaths(Tag.pathList(pathStr));
        BasicStreamingDataset streamingDataset = new BasicStreamingDataset();
//        查询结果
        gatewayContext.getTagHistoryManager().queryHistory(tagHistoryQueryParams, streamingDataset);
        if (streamingDataset.getRowCount() < 2) {
            return new BasicQualifiedValue(false);
        }
        float startValue = Float.valueOf(String.valueOf(streamingDataset.getValueAt(0, 1)));
        float endValue = Float.valueOf(String.valueOf(streamingDataset.getValueAt(1, 1)));
        float reference = TypeUtilities.toFloat(expressions[1].execute().getValue());
        return new BasicQualifiedValue(Math.abs(endValue - startValue) > reference);
    }


}


The function is repeatedly called 4 times, and the parameters are not necessarily the same, but the log shows that the parameters are the same

I’m not sure about using tag history queries inside an expression function–I wouldn’t expect that to work properly, especially if any query can take more than a tiny fraction of a second. Aside from that, you definitely need to change your copy() method, since each instance must be unique to have its own timer. Like so:

    @Override
    public Function copy() {
        return new AIChangeFast(gatewayContext);
    }

i see