|
|
@ -20,8 +20,10 @@ import com.fasterxml.jackson.databind.node.ObjectNode; |
|
|
import com.google.common.util.concurrent.Futures; |
|
|
import com.google.common.util.concurrent.Futures; |
|
|
import com.google.common.util.concurrent.ListenableFuture; |
|
|
import com.google.common.util.concurrent.ListenableFuture; |
|
|
import lombok.EqualsAndHashCode; |
|
|
import lombok.EqualsAndHashCode; |
|
|
|
|
|
import net.objecthunter.exp4j.Expression; |
|
|
import org.thingsboard.common.util.JacksonUtil; |
|
|
import org.thingsboard.common.util.JacksonUtil; |
|
|
import org.thingsboard.script.api.tbel.TbUtils; |
|
|
import org.thingsboard.script.api.tbel.TbUtils; |
|
|
|
|
|
import org.thingsboard.server.actors.TbActorRef; |
|
|
import org.thingsboard.server.common.data.cf.CalculatedFieldType; |
|
|
import org.thingsboard.server.common.data.cf.CalculatedFieldType; |
|
|
import org.thingsboard.server.common.data.cf.configuration.Output; |
|
|
import org.thingsboard.server.common.data.cf.configuration.Output; |
|
|
import org.thingsboard.server.common.data.id.EntityId; |
|
|
import org.thingsboard.server.common.data.id.EntityId; |
|
|
@ -33,26 +35,21 @@ import java.util.Map; |
|
|
@EqualsAndHashCode(callSuper = true) |
|
|
@EqualsAndHashCode(callSuper = true) |
|
|
public class SimpleCalculatedFieldState extends BaseCalculatedFieldState { |
|
|
public class SimpleCalculatedFieldState extends BaseCalculatedFieldState { |
|
|
|
|
|
|
|
|
|
|
|
private ThreadLocal<Expression> expression; |
|
|
|
|
|
|
|
|
public SimpleCalculatedFieldState(EntityId entityId) { |
|
|
public SimpleCalculatedFieldState(EntityId entityId) { |
|
|
super(entityId); |
|
|
super(entityId); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public CalculatedFieldType getType() { |
|
|
public void setCtx(CalculatedFieldCtx ctx, TbActorRef actorCtx) { |
|
|
return CalculatedFieldType.SIMPLE; |
|
|
super.setCtx(ctx, actorCtx); |
|
|
} |
|
|
this.expression = ctx.getSimpleExpressions().get(ctx.getExpression()); |
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
protected void validateNewEntry(String key, ArgumentEntry newEntry) { |
|
|
|
|
|
if (newEntry instanceof TsRollingArgumentEntry) { |
|
|
|
|
|
throw new IllegalArgumentException("Unsupported argument type detected for argument: " + key + ". " + |
|
|
|
|
|
"Rolling argument entry is not supported for simple calculated fields."); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public ListenableFuture<CalculatedFieldResult> performCalculation(Map<String, ArgumentEntry> updatedArgs, CalculatedFieldCtx ctx) { |
|
|
public ListenableFuture<CalculatedFieldResult> performCalculation(Map<String, ArgumentEntry> updatedArgs, CalculatedFieldCtx ctx) { |
|
|
double expressionResult = ctx.evaluateSimpleExpression(ctx.getExpression(), this); |
|
|
double expressionResult = ctx.evaluateSimpleExpression(expression.get(), this); |
|
|
|
|
|
|
|
|
Output output = ctx.getOutput(); |
|
|
Output output = ctx.getOutput(); |
|
|
Object result = formatResult(expressionResult, output.getDecimalsByDefault()); |
|
|
Object result = formatResult(expressionResult, output.getDecimalsByDefault()); |
|
|
@ -96,4 +93,17 @@ public class SimpleCalculatedFieldState extends BaseCalculatedFieldState { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
protected void validateNewEntry(String key, ArgumentEntry newEntry) { |
|
|
|
|
|
if (newEntry instanceof TsRollingArgumentEntry) { |
|
|
|
|
|
throw new IllegalArgumentException("Unsupported argument type detected for argument: " + key + ". " + |
|
|
|
|
|
"Rolling argument entry is not supported for simple calculated fields."); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public CalculatedFieldType getType() { |
|
|
|
|
|
return CalculatedFieldType.SIMPLE; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|