From 1ef0ac88a08dedaf1c76c4b99a9fccaf617a962b Mon Sep 17 00:00:00 2001 From: xiwa Date: Sun, 18 Jun 2023 14:34:59 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E8=A7=84=E5=88=99=E5=BC=95=E6=93=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cc/iotkit/model/rule/FilterConfig.java | 21 ++++++++ .../java/cc/iotkit/model/rule/RuleInfo.java | 20 ++++---- .../java/cc/iotkit/data/model/TbRuleInfo.java | 3 -- .../iotkit/data/service/RuleInfoDataImpl.java | 51 ++++++++++++++++--- .../controller/RuleEngineController.java | 4 +- .../manager/dto/bo/ruleinfo/RuleInfoBo.java | 39 ++++---------- .../manager/dto/vo/ruleinfo/RuleInfoVo.java | 15 +++--- .../service/impl/RuleEngineServiceImpl.java | 4 +- .../iotkit/ruleengine/rule/RuleManager.java | 21 ++++++-- iot-test-tool/iot-test-mqtt/pom.xml | 23 --------- .../java/cc/iotkit/test/mqtt/Simulator.java | 48 ----------------- .../test/mqtt/performance/ConnectionTest.java | 2 +- iot-test-tool/pom.xml | 3 +- 13 files changed, 112 insertions(+), 142 deletions(-) create mode 100644 iot-common/iot-common-dao/iot-common-model/src/main/java/cc/iotkit/model/rule/FilterConfig.java delete mode 100755 iot-test-tool/iot-test-mqtt/src/main/java/cc/iotkit/test/mqtt/Simulator.java diff --git a/iot-common/iot-common-dao/iot-common-model/src/main/java/cc/iotkit/model/rule/FilterConfig.java b/iot-common/iot-common-dao/iot-common-model/src/main/java/cc/iotkit/model/rule/FilterConfig.java new file mode 100644 index 00000000..7c3d7ee6 --- /dev/null +++ b/iot-common/iot-common-dao/iot-common-model/src/main/java/cc/iotkit/model/rule/FilterConfig.java @@ -0,0 +1,21 @@ +package cc.iotkit.model.rule; + +import lombok.Data; + +/** + * 监听器和过滤器的过滤条件配置 + * + * @author sjg + */ +@Data +public class FilterConfig { + /** + * 条件类型 + */ + private String type; + + /** + * 条件配置 + */ + protected String config; +} diff --git a/iot-common/iot-common-dao/iot-common-model/src/main/java/cc/iotkit/model/rule/RuleInfo.java b/iot-common/iot-common-dao/iot-common-model/src/main/java/cc/iotkit/model/rule/RuleInfo.java index b42c509b..b6ffafff 100644 --- a/iot-common/iot-common-dao/iot-common-model/src/main/java/cc/iotkit/model/rule/RuleInfo.java +++ b/iot-common/iot-common-dao/iot-common-model/src/main/java/cc/iotkit/model/rule/RuleInfo.java @@ -14,6 +14,7 @@ import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; +import java.util.ArrayList; import java.util.List; @Data @@ -33,9 +34,9 @@ public class RuleInfo implements Owned { private String type; - private List listeners; + private List listeners; - private List filters; + private List filters; private List actions; @@ -47,16 +48,15 @@ public class RuleInfo implements Owned { private Long createAt; - @Data - public static class Listener { - private String type; - protected String config; + public List getListeners() { + return listeners == null ? new ArrayList<>() : listeners; } - @Data - public static class Filter { - private String type; - protected String config; + public List getFilters() { + return filters == null ? new ArrayList<>() : filters; } + public List getActions() { + return actions == null ? new ArrayList<>() : actions; + } } diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/model/TbRuleInfo.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/model/TbRuleInfo.java index cbcd7641..27b97c0e 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/model/TbRuleInfo.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/model/TbRuleInfo.java @@ -44,13 +44,11 @@ public class TbRuleInfo { @ApiModelProperty(value = "监听器") @AutoMapping(ignore = true) @ReverseAutoMapping(ignore = true) - private String listeners; @Column(columnDefinition = "text") @AutoMapping(ignore = true) @ReverseAutoMapping(ignore = true) - @ApiModelProperty(value = "过滤器") private String filters; @@ -58,7 +56,6 @@ public class TbRuleInfo { @ApiModelProperty(value = "动作") @AutoMapping(ignore = true) @ReverseAutoMapping(ignore = true) - private String actions; @ApiModelProperty(value = "用户id") diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/RuleInfoDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/RuleInfoDataImpl.java index 1244bd82..b3faa10e 100644 --- a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/RuleInfoDataImpl.java +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/RuleInfoDataImpl.java @@ -9,12 +9,15 @@ */ package cc.iotkit.data.service; +import cc.iotkit.common.utils.JsonUtils; import cc.iotkit.common.utils.MapstructUtils; import cc.iotkit.data.dao.IJPACommData; import cc.iotkit.data.manager.IRuleInfoData; import cc.iotkit.data.dao.RuleInfoRepository; import cc.iotkit.data.model.TbRuleInfo; import cc.iotkit.common.api.Paging; +import cc.iotkit.model.rule.FilterConfig; +import cc.iotkit.model.rule.RuleAction; import cc.iotkit.model.rule.RuleInfo; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -24,9 +27,11 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; @Primary @Service @@ -52,15 +57,16 @@ public class RuleInfoDataImpl implements IRuleInfoData, IJPACommData findByUidAndType(String uid, String type) { - return MapstructUtils.convert(ruleInfoRepository.findByUidAndType(uid, type), RuleInfo.class); + return fromTb(ruleInfoRepository.findByUidAndType(uid, type)); } @Override public Paging findByUidAndType(String uid, String type, int page, int size) { Page paged = ruleInfoRepository.findByUidAndType(uid, type, Pageable.ofSize(size).withPage(page - 1)); + return new Paging<>(paged.getTotalElements(), - MapstructUtils.convert(paged.getContent(), RuleInfo.class)); + fromTb(paged.getContent())); } @Override @@ -68,20 +74,19 @@ public class RuleInfoDataImpl implements IRuleInfoData, IJPACommData paged = ruleInfoRepository.findByType(type, Pageable.ofSize(size).withPage(page - 1)); return new Paging<>(paged.getTotalElements(), - MapstructUtils.convert(paged.getContent(), RuleInfo.class)); + fromTb(paged.getContent())); } @Override public List findByUid(String uid) { - return MapstructUtils.convert(ruleInfoRepository.findByUid(uid), RuleInfo.class); + return fromTb(ruleInfoRepository.findByUid(uid)); } @Override public Paging findByUid(String uid, int page, int size) { Page paged = ruleInfoRepository.findByUid(uid, Pageable.ofSize(size).withPage(page - 1)); - return new Paging<>(paged.getTotalElements(), - MapstructUtils.convert(paged.getContent(), RuleInfo.class)); + return new Paging<>(paged.getTotalElements(), fromTb(paged.getContent())); } @Override @@ -92,7 +97,7 @@ public class RuleInfoDataImpl implements IRuleInfoData, IJPACommData fromTb(List list) { + if (list == null) { + return new ArrayList<>(); + } + return list.stream().map(RuleInfoDataImpl::from).collect(Collectors.toList()); + } + + private static List fromBo(List list) { + if (list == null) { + return new ArrayList<>(); + } + return list.stream().map(RuleInfoDataImpl::from).collect(Collectors.toList()); + } } diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/controller/RuleEngineController.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/controller/RuleEngineController.java index 3e801dba..62f63532 100644 --- a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/controller/RuleEngineController.java +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/controller/RuleEngineController.java @@ -49,7 +49,7 @@ public class RuleEngineController { return ruleEngineService.selectPageList(request); } - @ApiOperation("规则编辑") + @ApiOperation("保存规则") @PostMapping("/edit") public boolean saveRule(@RequestBody @Validated Request ruleInfoBo) { return ruleEngineService.saveRule(ruleInfoBo.getData()); @@ -99,7 +99,7 @@ public class RuleEngineController { return ruleEngineService.selectTaskPageList(request); } - @ApiOperation("定时任务编辑") + @ApiOperation("保存定时任务") @PostMapping("/task/save") public boolean saveTask(@Validated @RequestBody Request taskInfo) { return ruleEngineService.saveTask(taskInfo.getData()); diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/ruleinfo/RuleInfoBo.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/ruleinfo/RuleInfoBo.java index b3451ee4..73f1b7e6 100644 --- a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/ruleinfo/RuleInfoBo.java +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/ruleinfo/RuleInfoBo.java @@ -1,30 +1,18 @@ package cc.iotkit.manager.dto.bo.ruleinfo; +import cc.iotkit.common.api.BaseDto; +import cc.iotkit.model.rule.FilterConfig; +import cc.iotkit.model.rule.RuleAction; import cc.iotkit.model.rule.RuleInfo; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonFormat; -import io.github.linpeilie.annotations.AutoMapping; -import io.github.linpeilie.annotations.ReverseAutoMapping; -import org.springframework.format.annotation.DateTimeFormat; - +import io.github.linpeilie.annotations.AutoMapper; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; - import jakarta.validation.constraints.Size; - - -import java.io.Serializable; -import java.util.Date; -import java.math.BigDecimal; - -import cc.iotkit.common.api.BaseDto; -import cc.iotkit.common.validate.AddGroup; -import cc.iotkit.common.validate.EditGroup; - -import io.github.linpeilie.annotations.AutoMapper; import lombok.Data; import lombok.EqualsAndHashCode; +import java.util.List; + @ApiModel(value = "RuleInfoBo") @Data @EqualsAndHashCode(callSuper = true) @@ -37,10 +25,7 @@ public class RuleInfoBo extends BaseDto { private String id; @ApiModelProperty(value = "动作") - @Size(max = 65535, message = "动作长度不正确") - @AutoMapping(ignore = true) - @ReverseAutoMapping(ignore = true) - private String actions; + private List actions; @ApiModelProperty(value = "创建时间") private Long createAt; @@ -50,16 +35,10 @@ public class RuleInfoBo extends BaseDto { private String desc; @ApiModelProperty(value = "过滤器") - @Size(max = 65535, message = "过滤器长度不正确") - @AutoMapping(ignore = true) - @ReverseAutoMapping(ignore = true) - private String filters; + private List filters; @ApiModelProperty(value = "监听器") - @Size(max = 65535, message = "监听器长度不正确") - @AutoMapping(ignore = true) - @ReverseAutoMapping(ignore = true) - private String listeners; + private List listeners; @ApiModelProperty(value = "规则名称") @Size(max = 255, message = "规则名称长度不正确") diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/ruleinfo/RuleInfoVo.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/ruleinfo/RuleInfoVo.java index 8d706135..7e9d8e40 100644 --- a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/ruleinfo/RuleInfoVo.java +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/ruleinfo/RuleInfoVo.java @@ -1,5 +1,7 @@ package cc.iotkit.manager.dto.vo.ruleinfo; +import cc.iotkit.model.rule.FilterConfig; +import cc.iotkit.model.rule.RuleAction; import cc.iotkit.model.rule.RuleInfo; import io.github.linpeilie.annotations.AutoMapping; import io.github.linpeilie.annotations.ReverseAutoMapping; @@ -11,6 +13,7 @@ import lombok.ToString; import java.util.Date; import java.io.Serializable; import java.math.BigDecimal; +import java.util.List; import com.fasterxml.jackson.annotation.JsonFormat; import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; @@ -33,9 +36,7 @@ public class RuleInfoVo implements Serializable { @ApiModelProperty(value = "动作") @ExcelProperty(value = "动作") - @AutoMapping(ignore = true) - @ReverseAutoMapping(ignore = true) - private String actions; + private List actions; @ApiModelProperty(value = "创建时间") @ExcelProperty(value = "创建时间") @@ -47,15 +48,11 @@ public class RuleInfoVo implements Serializable { @ApiModelProperty(value = "过滤器") @ExcelProperty(value = "过滤器") - @AutoMapping(ignore = true) - @ReverseAutoMapping(ignore = true) - private String filters; + private List filters; @ApiModelProperty(value = "监听器") @ExcelProperty(value = "监听器") - @AutoMapping(ignore = true) - @ReverseAutoMapping(ignore = true) - private String listeners; + private List listeners; @ApiModelProperty(value = "规则名称") @ExcelProperty(value = "规则名称") diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/impl/RuleEngineServiceImpl.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/impl/RuleEngineServiceImpl.java index 3e2f323d..81c36a6f 100644 --- a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/impl/RuleEngineServiceImpl.java +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/impl/RuleEngineServiceImpl.java @@ -83,7 +83,7 @@ public class RuleEngineServiceImpl implements IRuleEngineService { @Override public boolean saveRule(RuleInfoBo ruleInfoBo) { - RuleInfo rule = MapstructUtils.convert(ruleInfoBo, RuleInfo.class); + RuleInfo rule = ruleInfoBo.to(RuleInfo.class); if (StringUtils.isBlank(rule.getId())) { rule.setId(UUID.randomUUID().toString()); rule.setState(RuleInfo.STATE_STOPPED); @@ -154,7 +154,7 @@ public class RuleEngineServiceImpl implements IRuleEngineService { @Override public Paging selectRuleLogPageList(PageRequest request) { - RuleLog ruleLog = request.getData().to(RuleLog.class); + RuleLog ruleLog = request.getData().to(RuleLog.class); return ruleLogData.findByRuleId(ruleLog.getRuleId(), request.getPageNum(), request.getPageSize()).to(RuleLogVo.class); } diff --git a/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/rule/RuleManager.java b/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/rule/RuleManager.java index 50488b99..2d36b3fd 100644 --- a/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/rule/RuleManager.java +++ b/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/rule/RuleManager.java @@ -11,9 +11,11 @@ package cc.iotkit.ruleengine.rule; import cc.iotkit.common.api.PageRequest; import cc.iotkit.common.utils.JsonUtils; +import cc.iotkit.common.utils.StringUtils; import cc.iotkit.data.manager.IDeviceInfoData; import cc.iotkit.data.manager.IRuleInfoData; import cc.iotkit.common.api.Paging; +import cc.iotkit.model.rule.FilterConfig; import cc.iotkit.model.rule.RuleAction; import cc.iotkit.model.rule.RuleInfo; import cc.iotkit.ruleengine.action.*; @@ -76,11 +78,11 @@ public class RuleManager { int idx = 1; while (true) { PageRequest pageRequest = new PageRequest<>(); - pageRequest.setPageNum(idx+=1); + pageRequest.setPageNum(idx += 1); pageRequest.setPageSize(100); Paging all = ruleInfoData.findAll(pageRequest); List rules = all.getRows(); - if(CollectionUtil.isEmpty(rules)){ + if (CollectionUtil.isEmpty(rules)) { return; } rules.forEach(rule -> { @@ -120,15 +122,26 @@ public class RuleManager { private Rule parseRule(RuleInfo ruleInfo) { List> listeners = new ArrayList<>(); - for (RuleInfo.Listener listener : ruleInfo.getListeners()) { + for (FilterConfig listener : ruleInfo.getListeners()) { + if (StringUtils.isBlank(listener.getConfig())) { + continue; + } listeners.add(parseListener(listener.getType(), listener.getConfig())); } + List> filters = new ArrayList<>(); - for (RuleInfo.Filter filter : ruleInfo.getFilters()) { + for (FilterConfig filter : ruleInfo.getFilters()) { + if (StringUtils.isBlank(filter.getConfig())) { + continue; + } filters.add(parseFilter(filter.getType(), filter.getConfig())); } + List> actions = new ArrayList<>(); for (RuleAction action : ruleInfo.getActions()) { + if (StringUtils.isBlank(action.getConfig())) { + continue; + } actions.add(parseAction(ruleInfo.getId(), action.getType(), action.getConfig())); } diff --git a/iot-test-tool/iot-test-mqtt/pom.xml b/iot-test-tool/iot-test-mqtt/pom.xml index 0b20fa2a..cb8ba989 100755 --- a/iot-test-tool/iot-test-mqtt/pom.xml +++ b/iot-test-tool/iot-test-mqtt/pom.xml @@ -52,31 +52,8 @@ ${java.version} ${java.version} - utf8 - - org.apache.maven.plugins - maven-assembly-plugin - - - package - - single - - - - - cc.iotkit.test.mqtt.performance.ReportTest - - - - jar-with-dependencies - - - - - diff --git a/iot-test-tool/iot-test-mqtt/src/main/java/cc/iotkit/test/mqtt/Simulator.java b/iot-test-tool/iot-test-mqtt/src/main/java/cc/iotkit/test/mqtt/Simulator.java deleted file mode 100755 index 5c3f50f7..00000000 --- a/iot-test-tool/iot-test-mqtt/src/main/java/cc/iotkit/test/mqtt/Simulator.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * +---------------------------------------------------------------------- - * | Copyright (c) 奇特物联 2021-2022 All rights reserved. - * +---------------------------------------------------------------------- - * | Licensed 未经许可不能去掉「奇特物联」相关版权 - * +---------------------------------------------------------------------- - * | Author: xw2sy@163.com - * +---------------------------------------------------------------------- - */ -package cc.iotkit.test.mqtt; - -import cc.iotkit.test.mqtt.config.Mqtt; -import cc.iotkit.test.mqtt.service.Gateway; - -import java.io.IOException; - -public class Simulator { - - public static void main(String[] args) throws IOException { - - if (args.length == 0) { - Mqtt.brokerHost = "127.0.0.1"; -// Mqtt.brokerHost = "120.76.96.206"; - } else { - Mqtt.brokerHost = args[0]; - } - - new Thread(() -> { - Gateway gateway = new Gateway("hbtgIA0SuVw9lxjB", "AA:BB:CC:DD:22"); - gateway.addSubDevice("Rf4QSjbm65X45753", "ABC12300002", "S01"); -// gateway.addSubDevice("Rf4QSjbm65X45753", "ABC12300003", "S01"); -// gateway.addSubDevice("hdX3PCMcFrCYpesJ", "ABD12300001", "F01"); -// gateway.addSubDevice("hdX3PCMcFrCYpesJ", "ABD12300002", "F01"); -// gateway.addSubDevice("hdX3PCMcFrCYpesJ", "ABD12300002", "F01"); - gateway.start(); -// -// Gateway gateway2 = new Gateway("N523nWsCiG3CAn6X", "AA:BB:CC:EE:01"); -// //插座 -// gateway2.addSubDevice("cGCrkK7Ex4FESAwe", "ABE12300001", "S1"); -// gateway2.addSubDevice("cGCrkK7Ex4FESAwe", "ABE12300002", "S1"); -// gateway2.addSubDevice("6kYp6jszrDns2yh4", "ABE12400001", "S1"); -// gateway2.addSubDevice("", "ABE12500001", "M1"); -// gateway2.start(); - }).start(); - - System.in.read(); - } -} diff --git a/iot-test-tool/iot-test-mqtt/src/main/java/cc/iotkit/test/mqtt/performance/ConnectionTest.java b/iot-test-tool/iot-test-mqtt/src/main/java/cc/iotkit/test/mqtt/performance/ConnectionTest.java index 0be221a2..1f6e93b3 100755 --- a/iot-test-tool/iot-test-mqtt/src/main/java/cc/iotkit/test/mqtt/performance/ConnectionTest.java +++ b/iot-test-tool/iot-test-mqtt/src/main/java/cc/iotkit/test/mqtt/performance/ConnectionTest.java @@ -33,7 +33,7 @@ public class ConnectionTest { Mqtt.brokerHost = args[0]; } - int total = 100; + int total = 10; if (args.length > 1) { total = Integer.parseInt(args[1]); } diff --git a/iot-test-tool/pom.xml b/iot-test-tool/pom.xml index d06a387b..a7ce6362 100755 --- a/iot-test-tool/pom.xml +++ b/iot-test-tool/pom.xml @@ -14,11 +14,10 @@ - + iot-test-mqtt iot-virtual-device iot-test-tool - \ No newline at end of file