7 changed files with 112 additions and 66 deletions
@ -0,0 +1,75 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2025 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.utils; |
||||
|
|
||||
|
import lombok.NonNull; |
||||
|
import org.apache.commons.lang3.math.NumberUtils; |
||||
|
import org.thingsboard.server.common.data.StringUtils; |
||||
|
import org.thingsboard.server.common.data.cf.configuration.Argument; |
||||
|
import org.thingsboard.server.common.data.kv.AttributeKvEntry; |
||||
|
import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry; |
||||
|
import org.thingsboard.server.common.data.kv.BasicTsKvEntry; |
||||
|
import org.thingsboard.server.common.data.kv.BooleanDataEntry; |
||||
|
import org.thingsboard.server.common.data.kv.DoubleDataEntry; |
||||
|
import org.thingsboard.server.common.data.kv.KvEntry; |
||||
|
import org.thingsboard.server.common.data.kv.StringDataEntry; |
||||
|
import org.thingsboard.server.common.data.kv.TsKvEntry; |
||||
|
import org.thingsboard.server.service.cf.ctx.state.ArgumentEntry; |
||||
|
import org.thingsboard.server.service.cf.ctx.state.CalculatedFieldCtx; |
||||
|
import org.thingsboard.server.service.cf.ctx.state.CalculatedFieldState; |
||||
|
import org.thingsboard.server.service.cf.ctx.state.ScriptCalculatedFieldState; |
||||
|
import org.thingsboard.server.service.cf.ctx.state.SimpleCalculatedFieldState; |
||||
|
import org.thingsboard.server.service.cf.ctx.state.SingleValueArgumentEntry; |
||||
|
|
||||
|
import static org.thingsboard.server.service.cf.ctx.state.SingleValueArgumentEntry.DEFAULT_VERSION; |
||||
|
|
||||
|
public class CalculatedFieldArgumentUtils { |
||||
|
|
||||
|
public static ArgumentEntry transformSingleValueArgument(@NonNull KvEntry kvEntry) { |
||||
|
return kvEntry.getValue() != null ? ArgumentEntry.createSingleValueArgument(kvEntry) : new SingleValueArgumentEntry(); |
||||
|
} |
||||
|
|
||||
|
public static TsKvEntry createDefaultTsKvEntry(Argument argument, long ts) { |
||||
|
return new BasicTsKvEntry(ts, createDefaultKvEntry(argument), DEFAULT_VERSION); |
||||
|
} |
||||
|
|
||||
|
public static AttributeKvEntry createDefaultAttributeEntry(Argument argument, long ts) { |
||||
|
return new BaseAttributeKvEntry(createDefaultKvEntry(argument), ts, DEFAULT_VERSION); |
||||
|
} |
||||
|
|
||||
|
private static KvEntry createDefaultKvEntry(Argument argument) { |
||||
|
String key = argument.getRefEntityKey().getKey(); |
||||
|
String defaultValue = argument.getDefaultValue(); |
||||
|
if (StringUtils.isBlank(defaultValue)) { |
||||
|
return new StringDataEntry(key, null); |
||||
|
} |
||||
|
if (NumberUtils.isParsable(defaultValue)) { |
||||
|
return new DoubleDataEntry(key, Double.parseDouble(defaultValue)); |
||||
|
} |
||||
|
if ("true".equalsIgnoreCase(defaultValue) || "false".equalsIgnoreCase(defaultValue)) { |
||||
|
return new BooleanDataEntry(key, Boolean.parseBoolean(defaultValue)); |
||||
|
} |
||||
|
return new StringDataEntry(key, defaultValue); |
||||
|
} |
||||
|
|
||||
|
public static CalculatedFieldState createStateByType(CalculatedFieldCtx ctx) { |
||||
|
return switch (ctx.getCfType()) { |
||||
|
case SIMPLE -> new SimpleCalculatedFieldState(ctx.getArgNames()); |
||||
|
case SCRIPT -> new ScriptCalculatedFieldState(ctx.getArgNames()); |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue