From 3a1f0b65de96ec0a5109d6cdbe34b99248bf3c23 Mon Sep 17 00:00:00 2001 From: zhouhao Date: Mon, 28 Apr 2025 18:33:07 +0800 Subject: [PATCH 1/5] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E6=A0=87=E7=AD=BE=E6=97=A0=E6=B3=95=E6=9B=B4=E6=96=B0=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/tag/DeviceTagProperties.java | 18 ++ .../service/tag/DeviceTagSynchronizer.java | 195 ++++++++++++++++++ 2 files changed, 213 insertions(+) create mode 100644 jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/service/tag/DeviceTagProperties.java create mode 100644 jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/service/tag/DeviceTagSynchronizer.java diff --git a/jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/service/tag/DeviceTagProperties.java b/jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/service/tag/DeviceTagProperties.java new file mode 100644 index 00000000..82719f3e --- /dev/null +++ b/jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/service/tag/DeviceTagProperties.java @@ -0,0 +1,18 @@ +package org.jetlinks.community.device.service.tag; + +import org.jetlinks.community.buffer.BufferProperties; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +@Component +@ConfigurationProperties(prefix = "device.tag.synchronizer") +public class DeviceTagProperties extends BufferProperties { + + public DeviceTagProperties(){ + setFilePath("./data/device-tag-synchronizer"); + setSize(500); + setParallelism(1); + getEviction().setMaxSize(100_0000); + } + +} diff --git a/jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/service/tag/DeviceTagSynchronizer.java b/jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/service/tag/DeviceTagSynchronizer.java new file mode 100644 index 00000000..7cb935cd --- /dev/null +++ b/jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/service/tag/DeviceTagSynchronizer.java @@ -0,0 +1,195 @@ +package org.jetlinks.community.device.service.tag; + +import lombok.*; +import org.apache.commons.collections4.MapUtils; +import org.hswebframework.ezorm.rdb.mapping.ReactiveRepository; +import org.hswebframework.web.crud.events.EntityCreatedEvent; +import org.hswebframework.web.crud.events.EntitySavedEvent; +import org.jetlinks.core.device.DeviceOperator; +import org.jetlinks.core.device.DeviceRegistry; +import org.jetlinks.core.device.DeviceThingType; +import org.jetlinks.core.message.UpdateTagMessage; +import org.jetlinks.core.things.ThingsDataManager; +import org.jetlinks.core.utils.Reactors; +import org.jetlinks.community.buffer.BufferSettings; +import org.jetlinks.community.buffer.PersistenceBuffer; +import org.jetlinks.community.device.entity.DeviceTagEntity; +import org.jetlinks.community.gateway.annotation.Subscribe; +import org.jetlinks.community.things.data.ThingsDataWriter; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.context.event.EventListener; +import org.springframework.stereotype.Component; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; +import java.io.Externalizable; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.util.Date; +import java.util.List; +import java.util.Map; + +@Component +@RequiredArgsConstructor +public class DeviceTagSynchronizer implements CommandLineRunner { + + private final DeviceTagProperties properties; + + private final DeviceRegistry registry; + + private final ThingsDataWriter dataWriter; + + private final ThingsDataManager dataManager; + + private final ReactiveRepository tagRepository; + + public PersistenceBuffer buffer; + + @Subscribe(value = "/device/*/*/message/tags/update") + public Mono updateDeviceTag(UpdateTagMessage message) { + Map tags = message.getTags(); + if (MapUtils.isEmpty(tags)) { + return Mono.empty(); + } + String deviceId = message.getDeviceId(); + + return registry + .getDevice(deviceId) + .flatMap(DeviceOperator::getMetadata) + .flatMapMany(metadata -> Flux + .fromIterable(tags.entrySet()) + .filter(e -> e.getValue() != null) + .flatMap(e -> { + DeviceTagEntity tagEntity = metadata + .getTag(e.getKey()) + .map(tagMeta -> DeviceTagEntity.of(tagMeta, e.getValue())) + .orElseGet(() -> { + DeviceTagEntity entity = new DeviceTagEntity(); + entity.setKey(e.getKey()); + entity.setType("string"); + entity.setName(e.getKey()); + entity.setCreateTime(new Date()); + entity.setDescription("设备上报"); + entity.setValue(String.valueOf(e.getValue())); + return entity; + }); + tagEntity.setTimestamp(message.getTimestamp()); + tagEntity.setDeviceId(deviceId); + tagEntity.setId(DeviceTagEntity.createTagId(deviceId, tagEntity.getKey())); + return writeBuffer(tagEntity); + })) + .then(); + } + + + public Mono writeBuffer(DeviceTagEntity entity) { + return buffer.writeAsync(new DeviceTagBuffer(entity)); + } + + + private Mono convertEntity(DeviceTagBuffer buffer) { + //从最新缓存中获取最新的数据,并填入准备入库的实体中 + return dataManager + .getLastTag(DeviceThingType.device.getId(), + buffer.getTag().getDeviceId(), + buffer.getTag().getKey(), + System.currentTimeMillis()) + .map(tag -> { + //缓存中的数据比buffer中的新,则更新为buffer中的数据 + if (tag.getTimestamp() >= buffer.tag.getTimestamp()) { + buffer.getTag().setTimestamp(tag.getTimestamp()); + buffer.getTag().setValue(String.valueOf(tag.getValue())); + } + return buffer.getTag(); + }) + .defaultIfEmpty(buffer.tag); + } + + public Mono handleBuffer(Flux buffer) { + + return tagRepository + .save(buffer.flatMap(this::convertEntity)) + .contextWrite(ctx -> ctx.put(DeviceTagSynchronizer.class, this)) + .then(Reactors.ALWAYS_FALSE); + } + + @EventListener + public void handleDeviceTagEvent(EntityCreatedEvent event) { + event.async(updateTag(event.getEntity())); + } + + @EventListener + public void handleDeviceTagEvent(EntitySavedEvent event) { + event.async(updateTag(event.getEntity())); + } + + /** + * 更新标签,界面上手动修改标签? + * + * @param entityList 标签 + * @return Void + */ + private Mono updateTag(List entityList) { + return Mono.deferContextual(ctx -> { + //更新来自消息的标签,不需要再次更新 + if (ctx.hasKey(DeviceTagSynchronizer.class)) { + return Mono.empty(); + } + return Flux + .fromIterable(entityList) + .flatMap(entity -> dataWriter + .updateTag(DeviceThingType.device.getId(), + entity.getDeviceId(), + entity.getKey(), + System.currentTimeMillis(), + entity.getValue())) + .then(); + }); + } + + @PostConstruct + public void init() { + buffer = new PersistenceBuffer<>( + BufferSettings.create(properties), + DeviceTagBuffer::new, + this::handleBuffer) + .name("device-tag-synchronizer"); + buffer.init(); + } + + @PreDestroy + public void shutdown() { + buffer.stop(); + } + + @Override + public void run(String... args) throws Exception { + buffer.start(); + SpringApplication + .getShutdownHandlers() + .add(buffer::dispose); + } + + + @Getter + @Setter + @AllArgsConstructor + @NoArgsConstructor + public static class DeviceTagBuffer implements Externalizable { + private DeviceTagEntity tag; + + @Override + public void writeExternal(ObjectOutput out) { + tag.writeExternal(out); + } + + @Override + public void readExternal(ObjectInput in) { + tag = new DeviceTagEntity(); + tag.readExternal(in); + } + } +} From 80ed50211af80d485bb73ad8253358afb4ced2c3 Mon Sep 17 00:00:00 2001 From: zhouhao Date: Tue, 29 Apr 2025 18:11:00 +0800 Subject: [PATCH 2/5] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E6=A0=87?= =?UTF-8?q?=E7=AD=BE=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../device/entity/DeviceTagEntity.java | 13 +++++--- .../service/tag/DeviceTagSynchronizer.java | 33 +++++++++++++++++-- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/entity/DeviceTagEntity.java b/jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/entity/DeviceTagEntity.java index 1be4c687..802f105a 100644 --- a/jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/entity/DeviceTagEntity.java +++ b/jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/entity/DeviceTagEntity.java @@ -127,24 +127,27 @@ public class DeviceTagEntity extends GenericEntity { return tag; } + public DeviceProperty toProperty() { DeviceProperty property = new DeviceProperty(); property.setProperty(getKey()); property.setDeviceId(deviceId); property.setType(type); property.setPropertyName(name); + property.setValue(parseValue()); + return property; + } + + public Object parseValue() { DataType type = Optional .ofNullable(DataTypes.lookup(getType())) .map(Supplier::get) .orElseGet(UnknownType::new); if (type instanceof Converter) { - property.setValue(((Converter) type).convert(getValue())); + return ((Converter) type).convert(getValue()); } else { - property.setValue(getValue()); + return getValue(); } - return property; - - } //以物模型标签基础数据为准,重构数据库保存的可能已过时的标签数据 diff --git a/jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/service/tag/DeviceTagSynchronizer.java b/jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/service/tag/DeviceTagSynchronizer.java index 7cb935cd..4bb4cf17 100644 --- a/jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/service/tag/DeviceTagSynchronizer.java +++ b/jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/service/tag/DeviceTagSynchronizer.java @@ -4,6 +4,8 @@ import lombok.*; import org.apache.commons.collections4.MapUtils; import org.hswebframework.ezorm.rdb.mapping.ReactiveRepository; import org.hswebframework.web.crud.events.EntityCreatedEvent; +import org.hswebframework.web.crud.events.EntityDeletedEvent; +import org.hswebframework.web.crud.events.EntityModifyEvent; import org.hswebframework.web.crud.events.EntitySavedEvent; import org.jetlinks.core.device.DeviceOperator; import org.jetlinks.core.device.DeviceRegistry; @@ -79,7 +81,15 @@ public class DeviceTagSynchronizer implements CommandLineRunner { tagEntity.setTimestamp(message.getTimestamp()); tagEntity.setDeviceId(deviceId); tagEntity.setId(DeviceTagEntity.createTagId(deviceId, tagEntity.getKey())); - return writeBuffer(tagEntity); + + return dataWriter + .updateTag(DeviceThingType.device.getId(), + tagEntity.getDeviceId(), + tagEntity.getKey(), + System.currentTimeMillis(), + e.getValue()) + .then(writeBuffer(tagEntity)); + })) .then(); } @@ -126,6 +136,25 @@ public class DeviceTagSynchronizer implements CommandLineRunner { event.async(updateTag(event.getEntity())); } + @EventListener + public void handleDeviceTagEvent(EntityModifyEvent event) { + event.async(updateTag(event.getAfter())); + } + + @EventListener + public void handleDeviceTagEvent(EntityDeletedEvent event) { + event.async( + Flux + .fromIterable(event.getEntity()) + .flatMap(entity -> dataWriter + .removeTag(DeviceThingType.device.getId(), + entity.getDeviceId(), + entity.getKey()) + .then() + )); + } + + /** * 更新标签,界面上手动修改标签? * @@ -145,7 +174,7 @@ public class DeviceTagSynchronizer implements CommandLineRunner { entity.getDeviceId(), entity.getKey(), System.currentTimeMillis(), - entity.getValue())) + entity.parseValue())) .then(); }); } From a6100d36f902c4e7b1b58052994dce35261bb68e Mon Sep 17 00:00:00 2001 From: laokou <2413176044@qq.com> Date: Tue, 6 May 2025 09:25:11 +0800 Subject: [PATCH 3/5] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DMqttClient?= =?UTF-8?q?=E7=BD=91=E7=BB=9C=E7=BB=84=E4=BB=B6=E5=AE=9A=E4=B9=89=E7=9A=84?= =?UTF-8?q?=E5=85=83=E6=95=B0=E6=8D=AE=E5=90=8D=E7=A7=B0=E5=92=8C=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E9=94=99=E8=AF=AF=20(#632)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../network/mqtt/client/MqttClientProvider.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/jetlinks-components/network-component/mqtt-component/src/main/java/org/jetlinks/community/network/mqtt/client/MqttClientProvider.java b/jetlinks-components/network-component/mqtt-component/src/main/java/org/jetlinks/community/network/mqtt/client/MqttClientProvider.java index 7491465b..79eec6d4 100755 --- a/jetlinks-components/network-component/mqtt-component/src/main/java/org/jetlinks/community/network/mqtt/client/MqttClientProvider.java +++ b/jetlinks-components/network-component/mqtt-component/src/main/java/org/jetlinks/community/network/mqtt/client/MqttClientProvider.java @@ -110,12 +110,12 @@ public class MqttClientProvider implements NetworkProvider return new DefaultConfigMetadata() .add("id", "id", "", new StringType()) .add("remoteHost", "远程地址", "", new StringType()) - .add("remotePort", "远程地址", "", new IntType()) - .add("certId", "证书id", "", new StringType()) + .add("remotePort", "远程端口", "", new IntType()) + .add("certId", "证书ID", "", new StringType()) .add("secure", "开启TSL", "", new BooleanType()) - .add("clientId", "客户端ID", "", new BooleanType()) - .add("username", "用户名", "", new BooleanType()) - .add("password", "密码", "", new BooleanType()); + .add("clientId", "客户端ID", "", new StringType()) + .add("username", "用户名", "", new StringType()) + .add("password", "密码", "", new StringType()); } @Nonnull From 520ca24fedf326aa14cc52457bde9cf5c1acf869 Mon Sep 17 00:00:00 2001 From: zhouhao Date: Tue, 20 May 2025 15:31:49 +0800 Subject: [PATCH 4/5] =?UTF-8?q?refactor:=20=E5=A2=9E=E5=8A=A0ui=20resource?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UiResourceConfiguration.java | 24 ++++++ .../resource/ui/UiMenuResourceProvider.java | 14 ++++ .../resource/ui/UiResourceProvider.java | 83 +++++++++++++++++++ .../web/SystemResourcesController.java | 11 +++ ...ot.autoconfigure.AutoConfiguration.imports | 3 +- 5 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 jetlinks-components/common-component/src/main/java/org/jetlinks/community/configuration/UiResourceConfiguration.java create mode 100644 jetlinks-components/common-component/src/main/java/org/jetlinks/community/resource/ui/UiMenuResourceProvider.java create mode 100644 jetlinks-components/common-component/src/main/java/org/jetlinks/community/resource/ui/UiResourceProvider.java diff --git a/jetlinks-components/common-component/src/main/java/org/jetlinks/community/configuration/UiResourceConfiguration.java b/jetlinks-components/common-component/src/main/java/org/jetlinks/community/configuration/UiResourceConfiguration.java new file mode 100644 index 00000000..7d3c70a9 --- /dev/null +++ b/jetlinks-components/common-component/src/main/java/org/jetlinks/community/configuration/UiResourceConfiguration.java @@ -0,0 +1,24 @@ +package org.jetlinks.community.configuration; + +import org.jetlinks.community.resource.ui.UiMenuResourceProvider; +import org.jetlinks.community.resource.ui.UiResourceProvider; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; + +@AutoConfiguration +@ConditionalOnProperty(prefix = "jetlinks.ui", name = "enabled", havingValue = "true", matchIfMissing = true) +public class UiResourceConfiguration { + + + @Bean + public UiResourceProvider uiResourceProvider() { + return new UiResourceProvider(); + } + + @Bean + public UiMenuResourceProvider uiMenuResourceProvider() { + return new UiMenuResourceProvider(); + } + +} diff --git a/jetlinks-components/common-component/src/main/java/org/jetlinks/community/resource/ui/UiMenuResourceProvider.java b/jetlinks-components/common-component/src/main/java/org/jetlinks/community/resource/ui/UiMenuResourceProvider.java new file mode 100644 index 00000000..1ab070e7 --- /dev/null +++ b/jetlinks-components/common-component/src/main/java/org/jetlinks/community/resource/ui/UiMenuResourceProvider.java @@ -0,0 +1,14 @@ +package org.jetlinks.community.resource.ui; + +import lombok.extern.slf4j.Slf4j; +import org.jetlinks.community.resource.ClassPathJsonResourceProvider; + +@Slf4j +public class UiMenuResourceProvider extends ClassPathJsonResourceProvider { + public static final String TYPE = "ui-menus"; + + + public UiMenuResourceProvider() { + super(TYPE, "classpath*:/ui/*/baseMenu.json"); + } +} \ No newline at end of file diff --git a/jetlinks-components/common-component/src/main/java/org/jetlinks/community/resource/ui/UiResourceProvider.java b/jetlinks-components/common-component/src/main/java/org/jetlinks/community/resource/ui/UiResourceProvider.java new file mode 100644 index 00000000..174878d1 --- /dev/null +++ b/jetlinks-components/common-component/src/main/java/org/jetlinks/community/resource/ui/UiResourceProvider.java @@ -0,0 +1,83 @@ +package org.jetlinks.community.resource.ui; + +import lombok.Getter; +import lombok.Setter; +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.jetlinks.community.resource.Resource; +import org.jetlinks.community.resource.ResourceProvider; +import org.jetlinks.community.resource.SimpleResource; +import org.jetlinks.community.utils.ObjectMappers; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; +import org.springframework.core.io.support.ResourcePatternResolver; +import org.springframework.util.StreamUtils; +import reactor.core.publisher.Flux; + +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +@Slf4j +public class UiResourceProvider implements ResourceProvider { + public static final String TYPE = "ui"; + + private static final ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); + + private List cache; + + @Override + public String getType() { + return TYPE; + } + + @Override + public Flux getResources() { + return Flux.fromIterable(cache == null ? cache = read() : cache); + } + + @SneakyThrows + private List read() { + List resources = new ArrayList<>(); + try { + for (org.springframework.core.io.Resource resource : resolver.getResources("classpath*:/ui/*/package.json")) { + try (InputStream stream = resource.getInputStream()) { + String s = StreamUtils.copyToString(stream, StandardCharsets.UTF_8); + Module m = ObjectMappers.parseJson(s, Module.class); + String path = resource.getURL().getPath(); + String[] parts = path.split("/"); + if (parts.length > 2) { + m.setPath(parts[parts.length - 3] + "/" + parts[parts.length - 2]); + resources.add(m.toResource()); + } + } + } + } catch (Throwable e) { + log.warn("load ui resource error", e); + } + return resources; + } + + + @Override + public Flux getResources(Collection id) { + return Flux.empty(); + } + + + @Getter + @Setter + public static class Module { + private String id; + private String name; + private String description; + private String path; + + public SimpleResource toResource() { + id = StringUtils.isBlank(id) ? name : id; + return SimpleResource.of(id, TYPE, ObjectMappers.toJsonString(this)); + } + } +} \ No newline at end of file diff --git a/jetlinks-components/common-component/src/main/java/org/jetlinks/community/web/SystemResourcesController.java b/jetlinks-components/common-component/src/main/java/org/jetlinks/community/web/SystemResourcesController.java index ee6c9157..f33f9665 100644 --- a/jetlinks-components/common-component/src/main/java/org/jetlinks/community/web/SystemResourcesController.java +++ b/jetlinks-components/common-component/src/main/java/org/jetlinks/community/web/SystemResourcesController.java @@ -9,6 +9,7 @@ import org.hswebframework.web.authorization.exception.UnAuthorizedException; import org.jetlinks.community.resource.Resource; import org.jetlinks.community.resource.ResourceManager; import org.jetlinks.community.resource.TypeScriptDeclareResourceProvider; +import org.jetlinks.community.resource.ui.UiResourceProvider; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @@ -17,6 +18,7 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import java.util.Collections; +import java.util.HashMap; @RestController @RequestMapping("/system/resources") @@ -26,6 +28,15 @@ public class SystemResourcesController { private final ResourceManager resourceManager; + @GetMapping("/ui") + @SneakyThrows + @Authorize(merge = false) + public Flux getUIResources() { + return resourceManager + .getResources(UiResourceProvider.TYPE) + .map(resource->resource.as(HashMap.class)); + } + @GetMapping("/{type}") @SneakyThrows public Flux getResources(@PathVariable String type) { diff --git a/jetlinks-components/common-component/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/jetlinks-components/common-component/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index c761205a..b66587f1 100644 --- a/jetlinks-components/common-component/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/jetlinks-components/common-component/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1 +1,2 @@ -org.jetlinks.community.configuration.CommonConfiguration \ No newline at end of file +org.jetlinks.community.configuration.CommonConfiguration +org.jetlinks.community.configuration.UiResourceConfiguration \ No newline at end of file From fc651ecd9d44e2b773fc96b8573c3b1c86503b0b Mon Sep 17 00:00:00 2001 From: Zhang Ji <125540670@qq.com> Date: Mon, 26 May 2025 19:03:49 +0800 Subject: [PATCH 5/5] =?UTF-8?q?fix(=E5=9C=BA=E6=99=AF=E8=81=94=E5=8A=A8):?= =?UTF-8?q?=20=E4=BF=AE=E5=A4=8D=E6=89=A7=E8=A1=8C=E5=8A=A8=E4=BD=9C?= =?UTF-8?q?=E7=9A=84=E5=86=85=E7=BD=AE=E5=8F=82=E6=95=B0=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E5=A4=B1=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20(#636)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rule/engine/scene/SceneUtils.java | 29 +++++++++++++++---- .../community/rule/engine/scene/Variable.java | 2 ++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/jetlinks-manager/rule-engine-manager/src/main/java/org/jetlinks/community/rule/engine/scene/SceneUtils.java b/jetlinks-manager/rule-engine-manager/src/main/java/org/jetlinks/community/rule/engine/scene/SceneUtils.java index b3010e3f..e1b330cc 100644 --- a/jetlinks-manager/rule-engine-manager/src/main/java/org/jetlinks/community/rule/engine/scene/SceneUtils.java +++ b/jetlinks-manager/rule-engine-manager/src/main/java/org/jetlinks/community/rule/engine/scene/SceneUtils.java @@ -219,15 +219,32 @@ public class SceneUtils { } } - public static void refactorUpperKey(DeviceSelectorSpec deviceSelectorSpec) { + @SuppressWarnings("all") + public static void refactorUpperKey(Object source) { // 将变量格式改为与查询的别名一致 - if (VariableSource.Source.upper.equals(deviceSelectorSpec.getSource())) { - // scene.xx.current -> scene.scene_xx_current - if (deviceSelectorSpec.getUpperKey().startsWith("scene.")) { - String alias = SceneUtils.createColumnAlias("properties", deviceSelectorSpec.getUpperKey(), false); - deviceSelectorSpec.setUpperKey("scene." + alias); + if (source instanceof VariableSource) { + VariableSource variableSource = (VariableSource) source; + if (VariableSource.Source.upper.equals(variableSource.getSource())) { + variableSource.setUpperKey(transferSceneUpperKey(variableSource.getUpperKey())); } } + if (source instanceof Map) { + Map map = (Map) source; + VariableSource variableSource = VariableSource.of(source); + // 将变量格式改为与查询的别名一致 + if (VariableSource.Source.upper.equals(variableSource.getSource())) { + map.put("upperKey", transferSceneUpperKey(variableSource.getUpperKey())); + } + } + } + + public static String transferSceneUpperKey(String upperKey) { + // scene.xx.current -> scene.scene_xx_current + if (upperKey.startsWith("scene.")) { + String alias = SceneUtils.createColumnAlias("scene", upperKey, false); + return "scene." + alias; + } + return upperKey; } private static boolean isContainThis(String[] arr) { diff --git a/jetlinks-manager/rule-engine-manager/src/main/java/org/jetlinks/community/rule/engine/scene/Variable.java b/jetlinks-manager/rule-engine-manager/src/main/java/org/jetlinks/community/rule/engine/scene/Variable.java index dcb3fbef..61de7001 100644 --- a/jetlinks-manager/rule-engine-manager/src/main/java/org/jetlinks/community/rule/engine/scene/Variable.java +++ b/jetlinks-manager/rule-engine-manager/src/main/java/org/jetlinks/community/rule/engine/scene/Variable.java @@ -148,11 +148,13 @@ public class Variable { } public void refactorPrefix(Variable main) { + id = SceneUtils.transferSceneUpperKey(id); if (CollectionUtils.isNotEmpty(children)) { for (Variable child : children) { if (!child.getId().startsWith(main.id + ".")) { child.setId(main.id + "." + child.getId()); } + child.setId(SceneUtils.transferSceneUpperKey(child.getId())); if (StringUtils.hasText(child.getFullName()) && StringUtils.hasText(main.getFullName())) { child.setFullName(main.getFullName() + "/" + child.getFullName());