From 409328dbe3f3d0d9b917a7c039a7d8d5ad3a4df7 Mon Sep 17 00:00:00 2001 From: dshvaika Date: Fri, 8 Aug 2025 17:28:57 +0300 Subject: [PATCH] removed no needed logic from geofencing arugment --- ...faultCalculatedFieldProcessingService.java | 46 ++++++++----------- .../service/cf/ctx/state/ArgumentEntry.java | 5 +- .../cf/ctx/state/GeofencingArgumentEntry.java | 6 +-- .../state/GeofencingCalculatedFieldState.java | 7 ++- .../server/utils/CalculatedFieldUtils.java | 16 +------ ...eofencingCalculatedFieldConfiguration.java | 2 + .../cf/configuration/GeofencingEvent.java | 29 +----------- common/proto/src/main/proto/queue.proto | 9 ---- 8 files changed, 33 insertions(+), 87 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/service/cf/DefaultCalculatedFieldProcessingService.java b/application/src/main/java/org/thingsboard/server/service/cf/DefaultCalculatedFieldProcessingService.java index dd4049241c..ade1d3eefd 100644 --- a/application/src/main/java/org/thingsboard/server/service/cf/DefaultCalculatedFieldProcessingService.java +++ b/application/src/main/java/org/thingsboard/server/service/cf/DefaultCalculatedFieldProcessingService.java @@ -36,8 +36,6 @@ import org.thingsboard.server.common.data.cf.CalculatedFieldType; import org.thingsboard.server.common.data.cf.configuration.Argument; import org.thingsboard.server.common.data.cf.configuration.ArgumentType; import org.thingsboard.server.common.data.cf.configuration.CFArgumentDynamicSourceType; -import org.thingsboard.server.common.data.cf.configuration.GeofencingCalculatedFieldConfiguration; -import org.thingsboard.server.common.data.cf.configuration.GeofencingZoneGroupConfiguration; import org.thingsboard.server.common.data.cf.configuration.OutputType; import org.thingsboard.server.common.data.cf.configuration.RelationQueryDynamicSourceConfiguration; import org.thingsboard.server.common.data.id.CalculatedFieldId; @@ -131,18 +129,18 @@ public class DefaultCalculatedFieldProcessingService implements CalculatedFieldP @Override public ListenableFuture fetchStateFromDb(CalculatedFieldCtx ctx, EntityId entityId) { - Map> argFutures = new HashMap<>(); - - if (ctx.getCalculatedField().getType().equals(CalculatedFieldType.GEOFENCING)) { - fetchGeofencingCalculatedFieldArguments(ctx, entityId, argFutures, false); - } else { - for (var entry : ctx.getArguments().entrySet()) { - var argEntityId = resolveEntityId(entityId, entry); - var argValueFuture = fetchKvEntry(ctx.getTenantId(), argEntityId, entry.getValue()); - argFutures.put(entry.getKey(), argValueFuture); + Map> argFutures = switch (ctx.getCalculatedField().getType()) { + case GEOFENCING -> fetchGeofencingCalculatedFieldArguments(ctx, entityId, false); + case SIMPLE, SCRIPT -> { + Map> futures = new HashMap<>(); + for (var entry : ctx.getArguments().entrySet()) { + var argEntityId = resolveEntityId(entityId, entry); + var argValueFuture = fetchKvEntry(ctx.getTenantId(), argEntityId, entry.getValue()); + futures.put(entry.getKey(), argValueFuture); + } + yield futures; } - } - + }; return Futures.whenAllComplete(argFutures.values()).call(() -> { var result = createStateByType(ctx); result.updateState(ctx, resolveArgumentFutures(argFutures)); @@ -156,14 +154,11 @@ public class DefaultCalculatedFieldProcessingService implements CalculatedFieldP if (!ctx.getCalculatedField().getType().equals(CalculatedFieldType.GEOFENCING)) { return Map.of(); } - Map> argFutures = new HashMap<>(); - fetchGeofencingCalculatedFieldArguments(ctx, entityId, argFutures, true); - return resolveArgumentFutures(argFutures); + return resolveArgumentFutures(fetchGeofencingCalculatedFieldArguments(ctx, entityId, true)); } - private void fetchGeofencingCalculatedFieldArguments(CalculatedFieldCtx ctx, EntityId entityId, Map> argFutures, boolean dynamicArgumentsOnly) { - var configuration = (GeofencingCalculatedFieldConfiguration) ctx.getCalculatedField().getConfiguration(); - var zoneGroupConfigs = configuration.getGeofencingZoneGroupConfigurations(); + private Map> fetchGeofencingCalculatedFieldArguments(CalculatedFieldCtx ctx, EntityId entityId, boolean dynamicArgumentsOnly) { + Map> argFutures = new HashMap<>(); Set> entries = ctx.getArguments().entrySet(); if (dynamicArgumentsOnly) { entries = entries.stream() @@ -175,13 +170,13 @@ public class DefaultCalculatedFieldProcessingService implements CalculatedFieldP case ENTITY_ID_LATITUDE_ARGUMENT_KEY, ENTITY_ID_LONGITUDE_ARGUMENT_KEY -> argFutures.put(entry.getKey(), fetchKvEntry(ctx.getTenantId(), resolveEntityId(entityId, entry), entry.getValue())); default -> { - var zoneGroupConfiguration = zoneGroupConfigs.get(entry.getKey()); var resolvedEntityIdsFuture = resolveGeofencingEntityIds(ctx.getTenantId(), entityId, entry); argFutures.put(entry.getKey(), Futures.transformAsync(resolvedEntityIdsFuture, resolvedEntityIds -> - fetchGeofencingKvEntry(ctx.getTenantId(), resolvedEntityIds, entry.getValue(), zoneGroupConfiguration), calculatedFieldCallbackExecutor)); + fetchGeofencingKvEntry(ctx.getTenantId(), resolvedEntityIds, entry.getValue()), calculatedFieldCallbackExecutor)); } } } + return argFutures; } @Override @@ -321,12 +316,11 @@ public class DefaultCalculatedFieldProcessingService implements CalculatedFieldP }; } - private ListenableFuture fetchGeofencingKvEntry(TenantId tenantId, List geofencingEntities, - Argument argument, GeofencingZoneGroupConfiguration zoneGroupConfiguration) { + private ListenableFuture fetchGeofencingKvEntry(TenantId tenantId, List geofencingEntities, Argument argument) { if (argument.getRefEntityKey().getType() != ArgumentType.ATTRIBUTE) { throw new IllegalStateException("Unsupported argument key type: " + argument.getRefEntityKey().getType()); } - List>> kvFutures = geofencingEntities.stream() + List>> kvFutures = geofencingEntities.stream() .map(entityId -> { var attributesFuture = attributesService.find( tenantId, @@ -341,10 +335,10 @@ public class DefaultCalculatedFieldProcessingService implements CalculatedFieldP ); }).collect(Collectors.toList()); - ListenableFuture>> allFutures = Futures.allAsList(kvFutures); + ListenableFuture>> allFutures = Futures.allAsList(kvFutures); return Futures.transform(allFutures, entries -> ArgumentEntry.createGeofencingValueArgument(entries.stream() - .collect(Collectors.toMap(Entry::getKey, Entry::getValue)), zoneGroupConfiguration), + .collect(Collectors.toMap(Entry::getKey, Entry::getValue))), calculatedFieldCallbackExecutor ); } diff --git a/application/src/main/java/org/thingsboard/server/service/cf/ctx/state/ArgumentEntry.java b/application/src/main/java/org/thingsboard/server/service/cf/ctx/state/ArgumentEntry.java index f76c6855a6..c7f830431b 100644 --- a/application/src/main/java/org/thingsboard/server/service/cf/ctx/state/ArgumentEntry.java +++ b/application/src/main/java/org/thingsboard/server/service/cf/ctx/state/ArgumentEntry.java @@ -19,7 +19,6 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonTypeInfo; import org.thingsboard.script.api.tbel.TbelCfArg; -import org.thingsboard.server.common.data.cf.configuration.GeofencingZoneGroupConfiguration; import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.kv.KvEntry; import org.thingsboard.server.common.data.kv.TsKvEntry; @@ -62,8 +61,8 @@ public interface ArgumentEntry { return new TsRollingArgumentEntry(kvEntries, limit, timeWindow); } - static ArgumentEntry createGeofencingValueArgument(Map entityIdkvEntryMap, GeofencingZoneGroupConfiguration zoneGroupConfiguration) { - return new GeofencingArgumentEntry(entityIdkvEntryMap, zoneGroupConfiguration); + static ArgumentEntry createGeofencingValueArgument(Map entityIdkvEntryMap) { + return new GeofencingArgumentEntry(entityIdkvEntryMap); } } diff --git a/application/src/main/java/org/thingsboard/server/service/cf/ctx/state/GeofencingArgumentEntry.java b/application/src/main/java/org/thingsboard/server/service/cf/ctx/state/GeofencingArgumentEntry.java index 2acdf0be4c..4b88419fbb 100644 --- a/application/src/main/java/org/thingsboard/server/service/cf/ctx/state/GeofencingArgumentEntry.java +++ b/application/src/main/java/org/thingsboard/server/service/cf/ctx/state/GeofencingArgumentEntry.java @@ -19,7 +19,6 @@ import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.thingsboard.script.api.tbel.TbelCfArg; import org.thingsboard.script.api.tbel.TbelCfTsGeofencingArg; -import org.thingsboard.server.common.data.cf.configuration.GeofencingZoneGroupConfiguration; import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.kv.KvEntry; @@ -32,17 +31,14 @@ import java.util.stream.Collectors; public class GeofencingArgumentEntry implements ArgumentEntry { private Map zoneStates; - private GeofencingZoneGroupConfiguration zoneGroupConfiguration; private boolean forceResetPrevious; public GeofencingArgumentEntry() { } - public GeofencingArgumentEntry(Map entityIdkvEntryMap, - GeofencingZoneGroupConfiguration zoneGroupConfiguration) { + public GeofencingArgumentEntry(Map entityIdkvEntryMap) { this.zoneStates = toZones(entityIdkvEntryMap); - this.zoneGroupConfiguration = zoneGroupConfiguration; } @Override diff --git a/application/src/main/java/org/thingsboard/server/service/cf/ctx/state/GeofencingCalculatedFieldState.java b/application/src/main/java/org/thingsboard/server/service/cf/ctx/state/GeofencingCalculatedFieldState.java index a98d6b65b1..8a209dd138 100644 --- a/application/src/main/java/org/thingsboard/server/service/cf/ctx/state/GeofencingCalculatedFieldState.java +++ b/application/src/main/java/org/thingsboard/server/service/cf/ctx/state/GeofencingCalculatedFieldState.java @@ -23,7 +23,9 @@ import lombok.Data; import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.common.util.geo.Coordinates; import org.thingsboard.server.common.data.cf.CalculatedFieldType; +import org.thingsboard.server.common.data.cf.configuration.GeofencingCalculatedFieldConfiguration; import org.thingsboard.server.common.data.cf.configuration.GeofencingEvent; +import org.thingsboard.server.common.data.cf.configuration.GeofencingZoneGroupConfiguration; import org.thingsboard.server.service.cf.CalculatedFieldResult; import org.thingsboard.server.service.cf.ctx.CalculatedFieldEntityCtxId; import org.thingsboard.server.utils.CalculatedFieldUtils; @@ -119,9 +121,12 @@ public class GeofencingCalculatedFieldState implements CalculatedFieldState { double longitude = (double) arguments.get(ENTITY_ID_LONGITUDE_ARGUMENT_KEY).getValue(); Coordinates entityCoordinates = new Coordinates(latitude, longitude); + var configuration = (GeofencingCalculatedFieldConfiguration) ctx.getCalculatedField().getConfiguration(); + Map geofencingZoneGroupConfigurations = configuration.getGeofencingZoneGroupConfigurations(); + ObjectNode resultNode = JacksonUtil.newObjectNode(); getGeofencingArguments().forEach((argumentKey, argumentEntry) -> { - var zoneGroupConfig = argumentEntry.getZoneGroupConfiguration(); + var zoneGroupConfig = geofencingZoneGroupConfigurations.get(argumentKey); Set zoneEvents = argumentEntry.getZoneStates() .values() .stream() diff --git a/application/src/main/java/org/thingsboard/server/utils/CalculatedFieldUtils.java b/application/src/main/java/org/thingsboard/server/utils/CalculatedFieldUtils.java index aaa68e1dd9..4876fa8feb 100644 --- a/application/src/main/java/org/thingsboard/server/utils/CalculatedFieldUtils.java +++ b/application/src/main/java/org/thingsboard/server/utils/CalculatedFieldUtils.java @@ -18,8 +18,6 @@ package org.thingsboard.server.utils; import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.cf.CalculatedFieldType; -import org.thingsboard.server.common.data.cf.configuration.GeofencingEvent; -import org.thingsboard.server.common.data.cf.configuration.GeofencingZoneGroupConfiguration; import org.thingsboard.server.common.data.id.CalculatedFieldId; import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityIdFactory; @@ -30,7 +28,6 @@ import org.thingsboard.server.gen.transport.TransportProtos.CalculatedFieldEntit import org.thingsboard.server.gen.transport.TransportProtos.CalculatedFieldIdProto; import org.thingsboard.server.gen.transport.TransportProtos.CalculatedFieldStateProto; import org.thingsboard.server.gen.transport.TransportProtos.GeofencingArgumentProto; -import org.thingsboard.server.gen.transport.TransportProtos.GeofencingEventProto; import org.thingsboard.server.gen.transport.TransportProtos.GeofencingZoneIdProto; import org.thingsboard.server.gen.transport.TransportProtos.GeofencingZoneProto; import org.thingsboard.server.gen.transport.TransportProtos.SingleValueArgumentProto; @@ -48,7 +45,6 @@ import org.thingsboard.server.service.cf.ctx.state.SimpleCalculatedFieldState; import org.thingsboard.server.service.cf.ctx.state.SingleValueArgumentEntry; import org.thingsboard.server.service.cf.ctx.state.TsRollingArgumentEntry; -import java.util.List; import java.util.Map; import java.util.Optional; import java.util.TreeMap; @@ -127,15 +123,11 @@ public class CalculatedFieldUtils { private static GeofencingArgumentProto toGeofencingArgumentProto(String argName, GeofencingArgumentEntry geofencingArgumentEntry) { - var zoneGroupConfiguration = geofencingArgumentEntry.getZoneGroupConfiguration(); Map zoneStates = geofencingArgumentEntry.getZoneStates(); GeofencingArgumentProto.Builder builder = GeofencingArgumentProto.newBuilder() - .setArgName(argName) - .setTelemetryPrefix(zoneGroupConfiguration.getReportTelemetryPrefix()); + .setArgName(argName); zoneStates.forEach((entityId, zoneState) -> builder.addZones(toGeofencingZoneProto(entityId, zoneState))); - zoneGroupConfiguration.getReportEvents().forEach(event -> - builder.addReportEvents(GeofencingEventProto.forNumber(event.getProtoNumber()))); return builder.build(); } @@ -213,14 +205,8 @@ public class CalculatedFieldUtils { .stream() .map(GeofencingZoneState::new) .collect(Collectors.toMap(GeofencingZoneState::getZoneId, Function.identity())); - List geofencingEvents = proto.getReportEventsList() - .stream() - .map(geofencingEventProto -> GeofencingEvent.fromProtoNumber(geofencingEventProto.getNumber())) - .toList(); - var zoneGroupConfiguration = new GeofencingZoneGroupConfiguration(proto.getTelemetryPrefix(), geofencingEvents); GeofencingArgumentEntry geofencingArgumentEntry = new GeofencingArgumentEntry(); geofencingArgumentEntry.setZoneStates(zoneStates); - geofencingArgumentEntry.setZoneGroupConfiguration(zoneGroupConfiguration); return geofencingArgumentEntry; } diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/GeofencingCalculatedFieldConfiguration.java b/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/GeofencingCalculatedFieldConfiguration.java index 38d4bf6ca2..64fdc05144 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/GeofencingCalculatedFieldConfiguration.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/GeofencingCalculatedFieldConfiguration.java @@ -40,6 +40,8 @@ public class GeofencingCalculatedFieldConfiguration extends BaseCalculatedFieldC ENTITY_ID_LONGITUDE_ARGUMENT_KEY ); + private String zoneRelationType; + private boolean trackZoneRelations; private Map geofencingZoneGroupConfigurations; @Override diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/GeofencingEvent.java b/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/GeofencingEvent.java index 9cb51ea294..e812918df7 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/GeofencingEvent.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/GeofencingEvent.java @@ -15,35 +15,8 @@ */ package org.thingsboard.server.common.data.cf.configuration; -import lombok.Getter; - -import java.util.Arrays; - -@Getter public enum GeofencingEvent { - ENTERED(0), LEFT(1), INSIDE(2), OUTSIDE(3); - - private final int protoNumber; // Corresponds to GeofencingEvent - - GeofencingEvent(int protoNumber) { - this.protoNumber = protoNumber; - } - - private static final GeofencingEvent[] BY_PROTO; - - static { - BY_PROTO = new GeofencingEvent[Arrays.stream(values()).mapToInt(GeofencingEvent::getProtoNumber).max().orElse(0) + 1]; - for (var event : values()) { - BY_PROTO[event.getProtoNumber()] = event; - } - } - - public static GeofencingEvent fromProtoNumber(int protoNumber) { - if (protoNumber < 0 || protoNumber >= BY_PROTO.length) { - throw new IllegalArgumentException("Invalid GeofencingEvent proto number " + protoNumber); - } - return BY_PROTO[protoNumber]; - } + ENTERED, LEFT, INSIDE, OUTSIDE; } diff --git a/common/proto/src/main/proto/queue.proto b/common/proto/src/main/proto/queue.proto index 90332be104..10ffb10793 100644 --- a/common/proto/src/main/proto/queue.proto +++ b/common/proto/src/main/proto/queue.proto @@ -908,17 +908,8 @@ message GeofencingZoneProto { optional bool inside = 5; } -enum GeofencingEventProto { - ENTERED = 0; - LEFT = 1; - INSIDE = 2; - OUTSIDE = 3; -} - message GeofencingArgumentProto { string argName = 1; - string telemetryPrefix = 2; - repeated GeofencingEventProto reportEvents = 3; repeated GeofencingZoneProto zones = 4; }