committed by
GitHub
8 changed files with 198 additions and 11 deletions
@ -0,0 +1,26 @@ |
|||
/** |
|||
* 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.rule.engine.api; |
|||
|
|||
import com.google.common.util.concurrent.FutureCallback; |
|||
|
|||
public interface RuleEngineCalculatedFieldQueueService { |
|||
|
|||
void pushRequestToQueue(TimeseriesSaveRequest request, FutureCallback<Void> callback); |
|||
|
|||
void pushRequestToQueue(AttributesSaveRequest request, FutureCallback<Void> callback); |
|||
|
|||
} |
|||
@ -0,0 +1,125 @@ |
|||
/** |
|||
* 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.rule.engine.telemetry; |
|||
|
|||
import com.google.gson.JsonParser; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.thingsboard.rule.engine.api.AttributesSaveRequest; |
|||
import org.thingsboard.rule.engine.api.EmptyNodeConfiguration; |
|||
import org.thingsboard.rule.engine.api.RuleNode; |
|||
import org.thingsboard.rule.engine.api.TbContext; |
|||
import org.thingsboard.rule.engine.api.TbNode; |
|||
import org.thingsboard.rule.engine.api.TbNodeConfiguration; |
|||
import org.thingsboard.rule.engine.api.TbNodeException; |
|||
import org.thingsboard.rule.engine.api.TimeseriesSaveRequest; |
|||
import org.thingsboard.rule.engine.api.util.TbNodeUtils; |
|||
import org.thingsboard.server.common.adaptor.JsonConverter; |
|||
import org.thingsboard.server.common.data.AttributeScope; |
|||
import org.thingsboard.server.common.data.kv.AttributeKvEntry; |
|||
import org.thingsboard.server.common.data.kv.BasicTsKvEntry; |
|||
import org.thingsboard.server.common.data.kv.KvEntry; |
|||
import org.thingsboard.server.common.data.kv.TsKvEntry; |
|||
import org.thingsboard.server.common.data.plugin.ComponentType; |
|||
import org.thingsboard.server.common.msg.TbMsg; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
import static org.thingsboard.server.common.data.DataConstants.SCOPE; |
|||
|
|||
@Slf4j |
|||
@RuleNode( |
|||
type = ComponentType.ACTION, |
|||
name = "calculated fields", |
|||
configClazz = EmptyNodeConfiguration.class, |
|||
nodeDescription = "Pushes incoming messages to calculated fields service", |
|||
nodeDetails = "Node enables the processing of calculated fields without persisting incoming messages to the database. " + |
|||
"By default, the processing of calculated fields is triggered by the <b>save attributes</b> and <b>save time series</b> nodes. " + |
|||
"This rule node accepts the same messages as these nodes but allows you to trigger the processing of calculated " + |
|||
"fields independently, ensuring that derived data can be computed and utilized in real time without storing the original message in the database.", |
|||
configDirective = "tbNodeEmptyConfig", |
|||
icon = "published_with_changes" |
|||
) |
|||
public class TbCalculatedFieldsNode implements TbNode { |
|||
|
|||
private EmptyNodeConfiguration config; |
|||
|
|||
@Override |
|||
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException { |
|||
this.config = TbNodeUtils.convert(configuration, EmptyNodeConfiguration.class); |
|||
} |
|||
|
|||
@Override |
|||
public void onMsg(TbContext ctx, TbMsg msg) { |
|||
switch (msg.getInternalType()) { |
|||
case POST_TELEMETRY_REQUEST -> processPostTelemetryRequest(ctx, msg); |
|||
case POST_ATTRIBUTES_REQUEST -> processPostAttributesRequest(ctx, msg); |
|||
default -> ctx.tellFailure(msg, new IllegalArgumentException("Unsupported msg type: " + msg.getType())); |
|||
} |
|||
} |
|||
|
|||
private void processPostTelemetryRequest(TbContext ctx, TbMsg msg) { |
|||
Map<Long, List<KvEntry>> tsKvMap = JsonConverter.convertToTelemetry(JsonParser.parseString(msg.getData()), System.currentTimeMillis()); |
|||
|
|||
if (tsKvMap.isEmpty()) { |
|||
ctx.tellSuccess(msg); |
|||
return; |
|||
} |
|||
|
|||
List<TsKvEntry> tsKvEntryList = new ArrayList<>(); |
|||
for (Map.Entry<Long, List<KvEntry>> tsKvEntry : tsKvMap.entrySet()) { |
|||
for (KvEntry kvEntry : tsKvEntry.getValue()) { |
|||
tsKvEntryList.add(new BasicTsKvEntry(tsKvEntry.getKey(), kvEntry)); |
|||
} |
|||
} |
|||
|
|||
TimeseriesSaveRequest timeseriesSaveRequest = TimeseriesSaveRequest.builder() |
|||
.tenantId(ctx.getTenantId()) |
|||
.customerId(msg.getCustomerId()) |
|||
.entityId(msg.getOriginator()) |
|||
.entries(tsKvEntryList) |
|||
.previousCalculatedFieldIds(msg.getPreviousCalculatedFieldIds()) |
|||
.tbMsgId(msg.getId()) |
|||
.tbMsgType(msg.getInternalType()) |
|||
.callback(new TelemetryNodeCallback(ctx, msg)) |
|||
.build(); |
|||
|
|||
ctx.getCalculatedFieldQueueService().pushRequestToQueue(timeseriesSaveRequest, timeseriesSaveRequest.getCallback()); |
|||
} |
|||
|
|||
private void processPostAttributesRequest(TbContext ctx, TbMsg msg) { |
|||
List<AttributeKvEntry> newAttributes = new ArrayList<>(JsonConverter.convertToAttributes(JsonParser.parseString(msg.getData()))); |
|||
|
|||
if (newAttributes.isEmpty()) { |
|||
ctx.tellSuccess(msg); |
|||
return; |
|||
} |
|||
|
|||
AttributesSaveRequest attributesSaveRequest = AttributesSaveRequest.builder() |
|||
.tenantId(ctx.getTenantId()) |
|||
.entityId(msg.getOriginator()) |
|||
.scope(AttributeScope.valueOf(msg.getMetaData().getValue(SCOPE))) |
|||
.entries(newAttributes) |
|||
.previousCalculatedFieldIds(msg.getPreviousCalculatedFieldIds()) |
|||
.tbMsgId(msg.getId()) |
|||
.tbMsgType(msg.getInternalType()) |
|||
.callback(new TelemetryNodeCallback(ctx, msg)) |
|||
.build(); |
|||
ctx.getCalculatedFieldQueueService().pushRequestToQueue(attributesSaveRequest, attributesSaveRequest.getCallback()); |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue