From aec5a23b3fe7ebd82553f1b6dd7589618f1628fe Mon Sep 17 00:00:00 2001 From: xiwa Date: Sun, 24 Dec 2023 19:46:17 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=E8=A7=84=E5=88=99=E5=BC=95=E6=93=8E?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=85=B3=E8=81=94=E5=91=8A=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/init/channelConfig.json | 3 +- .../cc/iotkit/model/alert/AlertConfig.java | 2 +- .../cc/iotkit/model/notify/ChannelConfig.java | 20 +------ .../data/dao/AlertRecordRepository.java | 3 +- .../data/model/ChannelConfigMapper.java | 44 -------------- .../cc/iotkit/data/model/TbAlertRecord.java | 5 +- .../data/service/AlertRecordDataImpl.java | 27 +++++++-- .../data/service/ChannelConfigDataImpl.java | 9 +-- .../dto/bo/channel/ChannelConfigBo.java | 4 +- .../dto/vo/channel/ChannelConfigVo.java | 4 -- .../service/AlertMessageEventListener.java | 33 +++++++++++ .../iotkit/manager/service/AlertService.java | 12 +++- iot-module/iot-message-notify/pom.xml | 5 ++ .../listener/DingTalkEventListener.java | 17 ++++-- .../message/listener/EmailEventListener.java | 18 +++--- .../listener/MessageEventListener.java | 14 +---- .../listener/PhoneMessageEventListener.java | 16 ----- .../listener/QyWechatEventListener.java | 18 +++--- .../message/listener/VoiceEventListener.java | 16 ----- .../iotkit/message/model/DingTalkConfig.java | 13 ++++ .../java/cc/iotkit/message/model/Message.java | 20 ++++++- .../iotkit/message/model/QyWechatConfig.java | 13 ++++ .../iotkit/message/notify/EventManager.java | 36 ----------- .../cc/iotkit/message/notify/EventType.java | 10 ---- .../message/service/MessageService.java | 5 +- .../plugin/main/script/ScriptVerticle.java | 1 - iot-module/iot-rule-engine/pom.xml | 5 ++ .../ruleengine/action/ScriptService.java | 1 + .../ruleengine/action/alert/AlertAction.java | 11 ++-- .../ruleengine/action/alert/AlertService.java | 17 ++++-- .../handler/RuleDeviceConsumer.java | 2 +- .../iotkit/ruleengine/rule/RuleManager.java | 59 ++++++++++++++++++- .../iotkit/virtualdevice/VirtualManager.java | 4 +- 33 files changed, 248 insertions(+), 219 deletions(-) delete mode 100644 iot-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/model/ChannelConfigMapper.java create mode 100644 iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/AlertMessageEventListener.java delete mode 100644 iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/PhoneMessageEventListener.java delete mode 100644 iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/VoiceEventListener.java create mode 100644 iot-module/iot-message-notify/src/main/java/cc/iotkit/message/model/DingTalkConfig.java create mode 100644 iot-module/iot-message-notify/src/main/java/cc/iotkit/message/model/QyWechatConfig.java delete mode 100644 iot-module/iot-message-notify/src/main/java/cc/iotkit/message/notify/EventManager.java delete mode 100644 iot-module/iot-message-notify/src/main/java/cc/iotkit/message/notify/EventType.java diff --git a/data/init/channelConfig.json b/data/init/channelConfig.json index 0d1bfd78..17a4b165 100644 --- a/data/init/channelConfig.json +++ b/data/init/channelConfig.json @@ -19,8 +19,7 @@ "channelId": 1, "title": "告警钉钉配置", "param": { - "dingTalkWebhook":"xxxxxxxxxxxxxxxx", - "dingTalkSecret":"xxxx" + "dingTalkWebhook":"xxxxxxxxxxxxxxxx" }, "createAt": 1683816661690 }, diff --git a/iot-dao/iot-data-model/src/main/java/cc/iotkit/model/alert/AlertConfig.java b/iot-dao/iot-data-model/src/main/java/cc/iotkit/model/alert/AlertConfig.java index 39c21715..934a5761 100644 --- a/iot-dao/iot-data-model/src/main/java/cc/iotkit/model/alert/AlertConfig.java +++ b/iot-dao/iot-data-model/src/main/java/cc/iotkit/model/alert/AlertConfig.java @@ -49,7 +49,7 @@ public class AlertConfig implements Owned { /** * 关联消息转发模板ID */ - private String messageTemplateId; + private Long messageTemplateId; /** * 描述 diff --git a/iot-dao/iot-data-model/src/main/java/cc/iotkit/model/notify/ChannelConfig.java b/iot-dao/iot-data-model/src/main/java/cc/iotkit/model/notify/ChannelConfig.java index 10208d9e..110d5ff8 100644 --- a/iot-dao/iot-data-model/src/main/java/cc/iotkit/model/notify/ChannelConfig.java +++ b/iot-dao/iot-data-model/src/main/java/cc/iotkit/model/notify/ChannelConfig.java @@ -1,14 +1,11 @@ package cc.iotkit.model.notify; import cc.iotkit.model.Id; -import com.fasterxml.jackson.annotation.JsonInclude; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; -import java.io.Serializable; - /** * author: 石恒 * date: 2023-05-11 16:30 @@ -26,23 +23,8 @@ public class ChannelConfig implements Id { private String title; - private ChannelParam param; + private String param; private Long createAt; - @Data - @JsonInclude(JsonInclude.Include.NON_NULL) - public static class ChannelParam implements Serializable { - private String userName; - private String passWord; - private String host; - private Integer port; - private Boolean mailSmtpAuth; - private String from; - private String to; - private String dingTalkWebhook; - private String dingTalkSecret; - private String qyWechatWebhook; - - } } diff --git a/iot-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/dao/AlertRecordRepository.java b/iot-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/dao/AlertRecordRepository.java index 357d5ccf..1fe17677 100644 --- a/iot-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/dao/AlertRecordRepository.java +++ b/iot-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/dao/AlertRecordRepository.java @@ -11,7 +11,8 @@ package cc.iotkit.data.dao; import cc.iotkit.data.model.TbAlertRecord; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.querydsl.QuerydslPredicateExecutor; -public interface AlertRecordRepository extends JpaRepository { +public interface AlertRecordRepository extends JpaRepository, QuerydslPredicateExecutor { } diff --git a/iot-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/model/ChannelConfigMapper.java b/iot-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/model/ChannelConfigMapper.java deleted file mode 100644 index 86a76ca8..00000000 --- a/iot-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/model/ChannelConfigMapper.java +++ /dev/null @@ -1,44 +0,0 @@ -package cc.iotkit.data.model; - -import cc.iotkit.common.utils.JsonUtils; -import cc.iotkit.model.notify.ChannelConfig; -import org.mapstruct.Mapper; -import org.mapstruct.Mapping; -import org.mapstruct.Mappings; - -import java.util.Objects; - -/** - * @Author: 石恒 - * @Date: 2023/5/11 21:02 - * @Description: - */ -@Mapper(componentModel = "spring") -public interface ChannelConfigMapper { - - @Mappings({ - @Mapping(target = "param", expression = "java(stingToParam(vo.getParam()))") - }) - ChannelConfig toDto(TbChannelConfig vo); - - @Mappings({ - @Mapping(target = "param", expression = "java(paramToSting(dto.getParam()))") - }) - TbChannelConfig toVo(ChannelConfig dto); - - default String paramToSting(ChannelConfig.ChannelParam param) { - if (Objects.isNull(param)) { - return null; - } - return JsonUtils.toJsonString(param); - - } - - default ChannelConfig.ChannelParam stingToParam(String param) { - if (Objects.isNull(param)) { - return null; - } - return JsonUtils.parse(param, ChannelConfig.ChannelParam.class); - - } -} diff --git a/iot-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/model/TbAlertRecord.java b/iot-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/model/TbAlertRecord.java index bf1a6f18..80226822 100644 --- a/iot-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/model/TbAlertRecord.java +++ b/iot-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/model/TbAlertRecord.java @@ -1,6 +1,6 @@ package cc.iotkit.data.model; -import cc.iotkit.model.alert.AlertConfig; +import cc.iotkit.model.alert.AlertRecord; import io.github.linpeilie.annotations.AutoMapper; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @@ -13,9 +13,8 @@ import javax.persistence.Table; @Data @Entity -//@ApiModel(value = "告警记录") @Table(name = "alert_record") -@AutoMapper(target= AlertConfig.class) +@AutoMapper(target = AlertRecord.class) public class TbAlertRecord { @Id diff --git a/iot-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/AlertRecordDataImpl.java b/iot-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/AlertRecordDataImpl.java index 24b23ade..09a49e42 100644 --- a/iot-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/AlertRecordDataImpl.java +++ b/iot-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/AlertRecordDataImpl.java @@ -3,11 +3,19 @@ package cc.iotkit.data.service; import cc.iotkit.common.api.PageRequest; import cc.iotkit.common.api.Paging; import cc.iotkit.common.utils.MapstructUtils; +import cc.iotkit.common.utils.StringUtils; import cc.iotkit.data.dao.AlertRecordRepository; import cc.iotkit.data.dao.IJPACommData; import cc.iotkit.data.manager.IAlertRecordData; import cc.iotkit.data.model.TbAlertRecord; +import cc.iotkit.data.util.PageBuilder; +import cc.iotkit.data.util.PredicateBuilder; import cc.iotkit.model.alert.AlertRecord; +import cc.iotkit.model.system.SysLoginInfo; +import com.querydsl.core.QueryResults; +import com.querydsl.core.types.Predicate; +import com.querydsl.jpa.impl.JPAQuery; +import com.querydsl.jpa.impl.JPAQueryFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Primary; import org.springframework.data.domain.Page; @@ -15,6 +23,8 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Service; +import static cc.iotkit.data.model.QTbAlertRecord.tbAlertRecord; + @Primary @Service public class AlertRecordDataImpl implements IAlertRecordData, IJPACommData { @@ -22,6 +32,8 @@ public class AlertRecordDataImpl implements IAlertRecordData, IJPACommData tbAlertRecord.name.like(data.getName())) + .and(StringUtils.isNotBlank(data.getLevel()), () -> tbAlertRecord.level.eq(data.getLevel())) + .build(); + } @Override public Paging selectAlertConfigPage(PageRequest request) { - Page alertRecordPage = alertRecordRepository.findAll(Pageable.ofSize(request.getPageSize()) - .withPage(request.getPageNum() - 1)); - return new Paging<>(alertRecordPage.getTotalElements(), - MapstructUtils.convert(alertRecordPage.getContent(), AlertRecord.class)); + QueryResults results = jpaQueryFactory.selectFrom(tbAlertRecord).where(genPredicate(request.getData())) + .orderBy(tbAlertRecord.id.desc()) + .limit(request.getPageSize()) + .offset(request.getOffset()).fetchResults(); + return new Paging<>(results.getTotal(), results.getResults()).to(AlertRecord.class); } } diff --git a/iot-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ChannelConfigDataImpl.java b/iot-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ChannelConfigDataImpl.java index 1ed88870..bf8b1274 100644 --- a/iot-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ChannelConfigDataImpl.java +++ b/iot-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ChannelConfigDataImpl.java @@ -5,7 +5,6 @@ import cc.iotkit.common.api.Paging; import cc.iotkit.data.dao.ChannelConfigRepository; import cc.iotkit.data.dao.IJPACommData; import cc.iotkit.data.manager.IChannelConfigData; -import cc.iotkit.data.model.ChannelConfigMapper; import cc.iotkit.data.model.TbChannelConfig; import cc.iotkit.model.notify.ChannelConfig; import org.springframework.context.annotation.Primary; @@ -15,7 +14,6 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Service; import javax.annotation.Resource; -import java.util.stream.Collectors; /** * author: 石恒 @@ -29,9 +27,6 @@ public class ChannelConfigDataImpl implements IChannelConfigData, IJPACommData( tbChannelConfigs.getTotalElements(), tbChannelConfigs.getContent() - .stream().map(channelConfigMapper::toDto) - .collect(Collectors.toList()) - ); + ).to(ChannelConfig.class); } } diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/channel/ChannelConfigBo.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/channel/ChannelConfigBo.java index 7daeebde..c11acece 100644 --- a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/channel/ChannelConfigBo.java +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/channel/ChannelConfigBo.java @@ -3,7 +3,6 @@ package cc.iotkit.manager.dto.bo.channel; import cc.iotkit.common.api.BaseDto; import cc.iotkit.model.notify.ChannelConfig; import io.github.linpeilie.annotations.AutoMapper; -import io.github.linpeilie.annotations.AutoMapping; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @@ -13,7 +12,7 @@ import lombok.EqualsAndHashCode; @ApiModel(value = "ChannelConfigBo") @Data @EqualsAndHashCode(callSuper = true) -@AutoMapper(target = ChannelConfig.class, reverseConvertGenerate = false) +@AutoMapper(target = ChannelConfig.class) public class ChannelConfigBo extends BaseDto { private static final long serialVersionUID = -1L; @@ -28,7 +27,6 @@ public class ChannelConfigBo extends BaseDto { private String title; @ApiModelProperty(value="通道配置参数") - @AutoMapping(target = "param", expression = "java(cc.iotkit.common.utils.JsonUtils.parse(source.getParam(), ChannelConfig.ChannelParam.class))") private String param; @ApiModelProperty(value="创建时间") diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/channel/ChannelConfigVo.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/channel/ChannelConfigVo.java index 610886fd..962d189a 100644 --- a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/channel/ChannelConfigVo.java +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/channel/ChannelConfigVo.java @@ -4,8 +4,6 @@ import cc.iotkit.model.notify.ChannelConfig; import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; import com.alibaba.excel.annotation.ExcelProperty; import io.github.linpeilie.annotations.AutoMapper; -import io.github.linpeilie.annotations.AutoMapping; -import io.github.linpeilie.annotations.ReverseAutoMapping; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @@ -34,8 +32,6 @@ public class ChannelConfigVo implements Serializable { @ApiModelProperty(value="通道配置参数") @ExcelProperty(value = "通道配置参数") - @ReverseAutoMapping(target = "param", expression = "java(cc.iotkit.common.utils.JsonUtils.toJsonString(source.getParam()))") - @AutoMapping(target = "param", expression = "java(cc.iotkit.common.utils.JsonUtils.parse(source.getParam(), ChannelConfig.ChannelParam.class))") private String param; @ApiModelProperty(value="创建时间") diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/AlertMessageEventListener.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/AlertMessageEventListener.java new file mode 100644 index 00000000..6ccae2ae --- /dev/null +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/AlertMessageEventListener.java @@ -0,0 +1,33 @@ +package cc.iotkit.manager.service; + +import cc.iotkit.data.manager.IAlertConfigData; +import cc.iotkit.message.event.MessageEvent; +import cc.iotkit.message.listener.MessageEventListener; +import cc.iotkit.message.model.Message; +import cc.iotkit.model.alert.AlertConfig; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.event.EventListener; +import org.springframework.stereotype.Component; + +/** + * @author sjg + */ +@Slf4j +@Component +public class AlertMessageEventListener implements MessageEventListener { + + @Autowired + private IAlertConfigData alertConfigData; + + @Autowired + private AlertService alertService; + + @Override + @EventListener(classes = MessageEvent.class) + public void doEvent(MessageEvent event) { + Message message = event.getMessage(); + AlertConfig alertConfig = alertConfigData.findById(message.getAlertConfigId()); + alertService.addAlert(alertConfig, message.getFormatContent()); + } +} diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/AlertService.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/AlertService.java index 48db20a8..183c143e 100644 --- a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/AlertService.java +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/AlertService.java @@ -38,7 +38,7 @@ public class AlertService { } public void deleteAlertConfigById(Long id) { - alertConfigData.deleteById(id); + alertConfigData.deleteById(id); } public Paging selectAlertConfigPage(PageRequest request) { @@ -48,4 +48,14 @@ public class AlertService { public Paging selectAlertRecordPage(PageRequest request) { return alertRecordData.selectAlertConfigPage(request); } + + public void addAlert(AlertConfig config, String content) { + alertRecordData.save(AlertRecord.builder() + .level(config.getLevel()) + .name(config.getName()) + .readFlg(false) + .alertTime(System.currentTimeMillis()) + .details(content) + .build()); + } } diff --git a/iot-module/iot-message-notify/pom.xml b/iot-module/iot-message-notify/pom.xml index 2edd5f55..7a09ee13 100644 --- a/iot-module/iot-message-notify/pom.xml +++ b/iot-module/iot-message-notify/pom.xml @@ -13,6 +13,11 @@ + + cc.iotkit + iot-common-core + + io.vertx vertx-web-client diff --git a/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/DingTalkEventListener.java b/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/DingTalkEventListener.java index 95733173..1f42412d 100644 --- a/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/DingTalkEventListener.java +++ b/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/DingTalkEventListener.java @@ -1,6 +1,9 @@ package cc.iotkit.message.listener; +import cc.iotkit.common.utils.JsonUtils; import cc.iotkit.message.config.VertxManager; +import cc.iotkit.message.event.MessageEvent; +import cc.iotkit.message.model.DingTalkConfig; import cc.iotkit.message.model.DingTalkMessage; import cc.iotkit.message.model.Message; import io.vertx.ext.web.client.WebClient; @@ -16,18 +19,20 @@ import org.springframework.stereotype.Component; @Slf4j @Component public class DingTalkEventListener implements MessageEventListener { - private String baseUrl = "https://oapi.dingtalk.com/robot/send?access_token=%s"; @Override - @EventListener(condition = "message.channel()='DingTalk'") - public void doEvent(Message message) { + @EventListener(classes = MessageEvent.class, condition = "#event.message.channel=='DingTalk'") + public void doEvent(MessageEvent event) { + Message message = event.getMessage(); + String channelConfig = message.getChannelConfig(); + DingTalkConfig dingTalkConfig = JsonUtils.parse(channelConfig, DingTalkConfig.class); + WebClient client = WebClient.create(VertxManager.INSTANCE.getVertx()); - String url = String.format(baseUrl, message.getKey()); DingTalkMessage qyWechatMessage = DingTalkMessage.builder() .msgtype("text") - .text(DingTalkMessage.MessageContent.builder().content(getContent(message)).build()) + .text(DingTalkMessage.MessageContent.builder().content(message.getFormatContent()).build()) .build(); - client.post(url).sendJson(qyWechatMessage) + client.post(dingTalkConfig.getDingTalkWebhook()).sendJson(qyWechatMessage) .onSuccess(response -> log.info("Received response with status code" + response.statusCode())) .onFailure(err -> log.error("Something went wrong " + err.getMessage())); } diff --git a/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/EmailEventListener.java b/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/EmailEventListener.java index 0ade8115..781865dd 100644 --- a/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/EmailEventListener.java +++ b/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/EmailEventListener.java @@ -1,5 +1,7 @@ package cc.iotkit.message.listener; +import cc.iotkit.common.utils.JsonUtils; +import cc.iotkit.message.event.MessageEvent; import cc.iotkit.message.model.EmailConfig; import cc.iotkit.message.model.Message; import lombok.extern.slf4j.Slf4j; @@ -8,7 +10,6 @@ import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Component; -import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.util.Properties; @@ -22,9 +23,12 @@ import java.util.Properties; public class EmailEventListener implements MessageEventListener { @Override - @EventListener(condition = "message.channel()='Email'") - public void doEvent(Message message) { - EmailConfig emailConfig = new EmailConfig(); + @EventListener(classes = MessageEvent.class, condition = "#event.message.channel=='Email'") + public void doEvent(MessageEvent event) { + Message message = event.getMessage(); + String channelConfig = message.getChannelConfig(); + EmailConfig emailConfig = JsonUtils.parse(channelConfig, EmailConfig.class); + JavaMailSenderImpl jms = new JavaMailSenderImpl(); jms.setHost(emailConfig.getHost()); jms.setUsername(emailConfig.getUserName()); @@ -35,19 +39,19 @@ public class EmailEventListener implements MessageEventListener { jms.setJavaMailProperties(p); MimeMessage mimeMessage = jms.createMimeMessage(); try { - String content = getContent(message); + String content = message.getFormatContent(); MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true); //收件人 String[] split = emailConfig.getTo().split(","); messageHelper.setTo(split); //标题 - messageHelper.setSubject(emailConfig.getTitle()); + messageHelper.setSubject(content.substring(0, Math.min(20, content.length()))); //内容 messageHelper.setText(content, true); //发件人 messageHelper.setFrom(emailConfig.getFrom()); jms.send(mimeMessage); - } catch (MessagingException e) { + } catch (Exception e) { log.error("发送邮件失败.", e); } } diff --git a/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/MessageEventListener.java b/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/MessageEventListener.java index 09a6e3d6..4f06d060 100644 --- a/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/MessageEventListener.java +++ b/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/MessageEventListener.java @@ -1,8 +1,6 @@ package cc.iotkit.message.listener; -import cc.iotkit.message.model.Message; - -import java.util.Map; +import cc.iotkit.message.event.MessageEvent; /** * author: 石恒 @@ -11,14 +9,6 @@ import java.util.Map; **/ public interface MessageEventListener { - void doEvent(Message message); + void doEvent(MessageEvent event); - default String getContent(Message message) { - String content = message.getContent(); - Map param = message.getParam(); - for (String key : param.keySet()) { - content = content.replaceAll("${" + key + "}", param.get(key)); - } - return content; - } } diff --git a/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/PhoneMessageEventListener.java b/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/PhoneMessageEventListener.java deleted file mode 100644 index 922b61c0..00000000 --- a/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/PhoneMessageEventListener.java +++ /dev/null @@ -1,16 +0,0 @@ -package cc.iotkit.message.listener; - -import cc.iotkit.message.model.Message; - -/** - * author: 石恒 - * date: 2023-05-08 15:09 - * description: - **/ -public class PhoneMessageEventListener implements MessageEventListener { - - @Override - public void doEvent(Message message) { - - } -} diff --git a/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/QyWechatEventListener.java b/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/QyWechatEventListener.java index 26253f0e..83d3da0e 100644 --- a/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/QyWechatEventListener.java +++ b/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/QyWechatEventListener.java @@ -1,7 +1,10 @@ package cc.iotkit.message.listener; +import cc.iotkit.common.utils.JsonUtils; import cc.iotkit.message.config.VertxManager; +import cc.iotkit.message.event.MessageEvent; import cc.iotkit.message.model.Message; +import cc.iotkit.message.model.QyWechatConfig; import cc.iotkit.message.model.QyWechatMessage; import io.vertx.ext.web.client.WebClient; import lombok.extern.slf4j.Slf4j; @@ -17,18 +20,19 @@ import org.springframework.stereotype.Component; @Component public class QyWechatEventListener implements MessageEventListener { - private String baseUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=%s"; - @Override - @EventListener(condition = "message.channel()='QyWechat'") - public void doEvent(Message message) { + @EventListener(classes = MessageEvent.class, condition = "#event.message.channel=='QyWechat'") + public void doEvent(MessageEvent event) { + Message message = event.getMessage(); + String channelConfig = message.getChannelConfig(); + QyWechatConfig qyWechatConfig = JsonUtils.parse(channelConfig, QyWechatConfig.class); + WebClient client = WebClient.create(VertxManager.INSTANCE.getVertx()); - String url = String.format(baseUrl, message.getKey()); QyWechatMessage qyWechatMessage = QyWechatMessage.builder() .msgtype("text") - .text(QyWechatMessage.MessageContent.builder().content(getContent(message)).build()) + .text(QyWechatMessage.MessageContent.builder().content(message.getFormatContent()).build()) .build(); - client.post(url).sendJson(qyWechatMessage) + client.post(qyWechatConfig.getQyWechatWebhook()).sendJson(qyWechatMessage) .onSuccess(response -> log.info("Received response with status code" + response.statusCode())) .onFailure(err -> log.error("Something went wrong " + err.getMessage())); } diff --git a/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/VoiceEventListener.java b/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/VoiceEventListener.java deleted file mode 100644 index e9f8d2fa..00000000 --- a/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/listener/VoiceEventListener.java +++ /dev/null @@ -1,16 +0,0 @@ -package cc.iotkit.message.listener; - -import cc.iotkit.message.model.Message; - -/** - * author: 石恒 - * date: 2023-05-08 15:09 - * description: - **/ -public class VoiceEventListener implements MessageEventListener { - - @Override - public void doEvent(Message message) { - - } -} diff --git a/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/model/DingTalkConfig.java b/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/model/DingTalkConfig.java new file mode 100644 index 00000000..9ee3eb7f --- /dev/null +++ b/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/model/DingTalkConfig.java @@ -0,0 +1,13 @@ +package cc.iotkit.message.model; + +import lombok.Data; + +import java.io.Serializable; + +/** + * @author sjg + */ +@Data +public class DingTalkConfig implements Serializable { + private String dingTalkWebhook; +} diff --git a/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/model/Message.java b/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/model/Message.java index 526a388f..eb5e8723 100644 --- a/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/model/Message.java +++ b/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/model/Message.java @@ -1,6 +1,9 @@ package cc.iotkit.message.model; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; +import lombok.NoArgsConstructor; import java.util.Map; @@ -10,10 +13,23 @@ import java.util.Map; * description: **/ @Data +@NoArgsConstructor +@AllArgsConstructor +@Builder public class Message { - private String key; private String content; - private Map param; + private Map param; private Long channelId; private String channel; + private String channelConfig; + private Long alertConfigId; + + public String getFormatContent() { + String fmt = content; + for (String key : param.keySet()) { + Object val = param.get(key); + fmt = fmt.replace("${" + key + "}", val == null ? "" : val.toString()); + } + return fmt; + } } diff --git a/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/model/QyWechatConfig.java b/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/model/QyWechatConfig.java new file mode 100644 index 00000000..911c193a --- /dev/null +++ b/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/model/QyWechatConfig.java @@ -0,0 +1,13 @@ +package cc.iotkit.message.model; + +import lombok.Data; + +import java.io.Serializable; + +/** + * @author sjg + */ +@Data +public class QyWechatConfig implements Serializable { + private String qyWechatWebhook; +} diff --git a/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/notify/EventManager.java b/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/notify/EventManager.java deleted file mode 100644 index b27bc715..00000000 --- a/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/notify/EventManager.java +++ /dev/null @@ -1,36 +0,0 @@ -package cc.iotkit.message.notify; - -import cc.iotkit.message.listener.MessageEventListener; -import cc.iotkit.message.model.Message; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * author: 石恒 - * date: 2023-05-08 15:17 - * description: - **/ -public class EventManager { - - Map, List> listeners = new HashMap<>(); - - public void subscribe(Enum eventType, MessageEventListener listener) { - List users = listeners.get(eventType); - users.add(listener); - } - - public void unsubscribe(Enum eventType, MessageEventListener listener) { - List users = listeners.get(eventType); - users.remove(listener); - } - - public void notify(Enum eventType, Message result) { - List users = listeners.get(eventType); - for (MessageEventListener listener : users) { - listener.doEvent(result); - } - } - -} diff --git a/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/notify/EventType.java b/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/notify/EventType.java deleted file mode 100644 index 2cdfa823..00000000 --- a/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/notify/EventType.java +++ /dev/null @@ -1,10 +0,0 @@ -package cc.iotkit.message.notify; - -/** - * author: 石恒 - * date: 2023-05-08 15:21 - * description: - **/ -public enum EventType { - MQ, Message -} diff --git a/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/service/MessageService.java b/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/service/MessageService.java index 562de447..9c6e11a6 100644 --- a/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/service/MessageService.java +++ b/iot-module/iot-message-notify/src/main/java/cc/iotkit/message/service/MessageService.java @@ -3,6 +3,7 @@ package cc.iotkit.message.service; import cc.iotkit.message.event.MessageEvent; import cc.iotkit.message.model.Message; import org.springframework.context.ApplicationEventPublisher; +import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -11,12 +12,12 @@ import javax.annotation.Resource; * date: 2023-05-08 16:02 * description: **/ +@Service public class MessageService { @Resource private ApplicationEventPublisher applicationEventPublisher; - public void sendMessage() { - Message message = new Message(); + public void sendMessage(Message message) { applicationEventPublisher.publishEvent(new MessageEvent(message)); } diff --git a/iot-module/iot-plugin/iot-plugin-main/src/main/java/cc/iotkit/plugin/main/script/ScriptVerticle.java b/iot-module/iot-plugin/iot-plugin-main/src/main/java/cc/iotkit/plugin/main/script/ScriptVerticle.java index 4c7f2a72..307e23c3 100644 --- a/iot-module/iot-plugin/iot-plugin-main/src/main/java/cc/iotkit/plugin/main/script/ScriptVerticle.java +++ b/iot-module/iot-plugin/iot-plugin-main/src/main/java/cc/iotkit/plugin/main/script/ScriptVerticle.java @@ -90,7 +90,6 @@ public class ScriptVerticle extends AbstractVerticle { @Override public void start() { initTcpServer(); - log.info("init tcp server failed"); } @Override diff --git a/iot-module/iot-rule-engine/pom.xml b/iot-module/iot-rule-engine/pom.xml index 6eecf69e..176967ef 100644 --- a/iot-module/iot-rule-engine/pom.xml +++ b/iot-module/iot-rule-engine/pom.xml @@ -46,6 +46,11 @@ iot-script-engine + + cc.iotkit + iot-message-notify + + diff --git a/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/action/ScriptService.java b/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/action/ScriptService.java index 26166fa3..08faea55 100644 --- a/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/action/ScriptService.java +++ b/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/action/ScriptService.java @@ -30,6 +30,7 @@ public class ScriptService { private IDeviceInfoData deviceInfoData; public void setScript(String script) { + this.script=script; scriptEngine.setScript(script); } diff --git a/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/action/alert/AlertAction.java b/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/action/alert/AlertAction.java index 6618489a..b9fef9b3 100644 --- a/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/action/alert/AlertAction.java +++ b/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/action/alert/AlertAction.java @@ -18,16 +18,19 @@ import lombok.NoArgsConstructor; import java.util.ArrayList; import java.util.List; +/** + * @author sjg + */ @NoArgsConstructor @AllArgsConstructor @Data -public class AlertAction implements Action> { +public class AlertAction implements Action { - public static final String TYPE = "alarm"; + public static final String TYPE = "alert"; private String type; - private List> services; + private List services; @Override public String getType() { @@ -37,7 +40,7 @@ public class AlertAction implements Action> { @Override public List execute(ThingModelMessage msg) { List results = new ArrayList<>(); - for (AlertService service : services) { + for (AlertService service : services) { results.add(service.execute(msg)); } return results; diff --git a/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/action/alert/AlertService.java b/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/action/alert/AlertService.java index 2c7f9b76..7221cd4b 100644 --- a/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/action/alert/AlertService.java +++ b/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/action/alert/AlertService.java @@ -10,8 +10,9 @@ package cc.iotkit.ruleengine.action.alert; import cc.iotkit.common.thing.ThingModelMessage; +import cc.iotkit.message.model.Message; +import cc.iotkit.message.service.MessageService; import cc.iotkit.ruleengine.action.ScriptService; -import cc.iotkit.ruleengine.alert.Alerter; import com.fasterxml.jackson.core.type.TypeReference; import lombok.Data; import lombok.EqualsAndHashCode; @@ -20,13 +21,17 @@ import lombok.extern.slf4j.Slf4j; import java.util.Map; +/** + * @author sjg + */ @EqualsAndHashCode(callSuper = true) @Slf4j @Data -public class AlertService extends ScriptService { - private String configId; +public class AlertService extends ScriptService { - private T alert; + private Message message; + + private MessageService messageService; @SneakyThrows public String execute(ThingModelMessage msg) { @@ -37,6 +42,8 @@ public class AlertService extends ScriptService { log.warn("execScript result is null"); return "execScript result is null"; } - return alert.send(result); + message.setParam(result); + messageService.sendMessage(message); + return "ok"; } } diff --git a/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/handler/RuleDeviceConsumer.java b/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/handler/RuleDeviceConsumer.java index a7af1e8d..67cd4e80 100644 --- a/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/handler/RuleDeviceConsumer.java +++ b/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/handler/RuleDeviceConsumer.java @@ -48,7 +48,7 @@ public class RuleDeviceConsumer implements ConsumerHandler, A @SneakyThrows @Override public void handler(ThingModelMessage msg) { - log.info("received thing model message:{}", JsonUtils.toJsonString(msg)); + log.info("received thing model message:{}", msg); try { for (DeviceMessageHandler handler : this.handlers) { messageHandlerPool.submit(() -> { 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 24422791..e0a24eb3 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 @@ -13,12 +13,19 @@ import cc.iotkit.common.api.PageRequest; import cc.iotkit.common.api.Paging; 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.data.manager.*; +import cc.iotkit.message.model.Message; +import cc.iotkit.message.service.MessageService; +import cc.iotkit.model.alert.AlertConfig; +import cc.iotkit.model.notify.Channel; +import cc.iotkit.model.notify.ChannelConfig; +import cc.iotkit.model.notify.ChannelTemplate; import cc.iotkit.model.rule.FilterConfig; import cc.iotkit.model.rule.RuleAction; import cc.iotkit.model.rule.RuleInfo; import cc.iotkit.ruleengine.action.Action; +import cc.iotkit.ruleengine.action.alert.AlertAction; +import cc.iotkit.ruleengine.action.alert.AlertService; import cc.iotkit.ruleengine.action.device.DeviceAction; import cc.iotkit.ruleengine.action.device.DeviceActionService; import cc.iotkit.ruleengine.action.http.HttpAction; @@ -36,6 +43,8 @@ import cc.iotkit.ruleengine.link.LinkFactory; import cc.iotkit.ruleengine.listener.DeviceListener; import cc.iotkit.ruleengine.listener.Listener; import cn.hutool.core.collection.CollectionUtil; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -44,6 +53,7 @@ import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -68,6 +78,21 @@ public class RuleManager { @Autowired private DeviceActionService deviceActionService; + @Autowired + private IAlertConfigData alertConfigData; + + @Autowired + private IChannelTemplateData channelTemplateData; + + @Autowired + private IChannelConfigData channelConfigData; + + @Autowired + private IChannelData channelData; + + @Autowired + private MessageService messageService; + public RuleManager() { ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1); executorService.schedule(this::initRules, 1, TimeUnit.SECONDS); @@ -196,6 +221,36 @@ public class RuleManager { service.initLink(ruleId); } return tcpAction; + } else if (AlertAction.TYPE.equals(type)) { + List alertConfigs = alertConfigData.findAllByCondition(AlertConfig.builder() + .ruleInfoId(ruleId) + .build()); + + AlertAction alertAction = parse(config, AlertAction.class); + String script = alertAction.getServices().get(0).getScript(); + + List alertServices = new ArrayList<>(); + for (AlertConfig alertConfig : alertConfigs) { + AlertService service = new AlertService(); + service.setScript(script); + service.setDeviceInfoData(deviceInfoData); + service.setMessageService(messageService); + + ChannelTemplate channelTemplate = channelTemplateData.findById(alertConfig.getMessageTemplateId()); + ChannelConfig channelConfig = channelConfigData.findById(channelTemplate.getChannelConfigId()); + Channel channel = channelData.findById(channelConfig.getChannelId()); + + service.setMessage(Message.builder() + .channel(channel.getCode()) + .channelId(channel.getId()) + .channelConfig(channelConfig.getParam()) + .content(channelTemplate.getContent()) + .alertConfigId(alertConfig.getId()) + .build()); + alertServices.add(service); + } + alertAction.setServices(alertServices); + return alertAction; } return null; } diff --git a/iot-test-tool/iot-virtual-device/src/main/java/cc/iotkit/virtualdevice/VirtualManager.java b/iot-test-tool/iot-virtual-device/src/main/java/cc/iotkit/virtualdevice/VirtualManager.java index 2d3ea1e1..645157d4 100644 --- a/iot-test-tool/iot-virtual-device/src/main/java/cc/iotkit/virtualdevice/VirtualManager.java +++ b/iot-test-tool/iot-virtual-device/src/main/java/cc/iotkit/virtualdevice/VirtualManager.java @@ -59,8 +59,8 @@ public class VirtualManager { private MqProducer producer; public VirtualManager(){ - ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1); - executorService.schedule(this::init, 8, TimeUnit.SECONDS); +// ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1); +// executorService.schedule(this::init, 8, TimeUnit.SECONDS); } public void init() { From 7a4c5e73902712c9334dd2b379107be34323caa8 Mon Sep 17 00:00:00 2001 From: xiwa Date: Wed, 27 Dec 2023 06:05:55 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat:=E6=94=AF=E6=8C=81=E6=97=A0=E9=80=9A?= =?UTF-8?q?=E9=81=93=E5=91=8A=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iotkit/ruleengine/rule/RuleManager.java | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) 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 e0a24eb3..00301ea0 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 @@ -231,22 +231,32 @@ public class RuleManager { List alertServices = new ArrayList<>(); for (AlertConfig alertConfig : alertConfigs) { + if(alertConfig.getEnable()!=null && !alertConfig.getEnable()){ + continue; + } + AlertService service = new AlertService(); service.setScript(script); service.setDeviceInfoData(deviceInfoData); service.setMessageService(messageService); ChannelTemplate channelTemplate = channelTemplateData.findById(alertConfig.getMessageTemplateId()); - ChannelConfig channelConfig = channelConfigData.findById(channelTemplate.getChannelConfigId()); - Channel channel = channelData.findById(channelConfig.getChannelId()); + Long channelConfigId = channelTemplate.getChannelConfigId(); - service.setMessage(Message.builder() - .channel(channel.getCode()) - .channelId(channel.getId()) - .channelConfig(channelConfig.getParam()) + Message message = Message.builder() .content(channelTemplate.getContent()) .alertConfigId(alertConfig.getId()) - .build()); + .build(); + + if(channelConfigId!=null) { + ChannelConfig channelConfig = channelConfigData.findById(channelTemplate.getChannelConfigId()); + Channel channel = channelData.findById(channelConfig.getChannelId()); + message.setChannel(channel.getCode()); + message.setChannelId(channel.getId()); + message.setChannelConfig(channelConfig.getParam()); + } + + service.setMessage(message); alertServices.add(service); } alertAction.setServices(alertServices); From 8bd516de55e28fa6623e8f7362b61ed54e12a164 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=81=93=E7=94=AB?= Date: Wed, 27 Dec 2023 17:59:00 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix=EF=BC=9A=E4=BF=AE=E5=A4=8D=E6=A8=A1?= =?UTF-8?q?=E6=8B=9F=E8=AE=BE=E5=A4=87=E4=B8=8B=E8=A1=8C=E5=B1=9E=E6=80=A7?= =?UTF-8?q?=E8=B0=83=E8=AF=95=E6=8A=A5=E9=94=99=EF=BC=8C=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=A8=A1=E6=8B=9F=E8=AE=BE=E5=A4=87=E4=B8=8B=E8=A1=8C=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cc/iotkit/plugin/main/PluginMainImpl.java | 15 +++++++++------ .../ruleengine/handler/RuleDeviceConsumer.java | 4 ++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/iot-module/iot-plugin/iot-plugin-main/src/main/java/cc/iotkit/plugin/main/PluginMainImpl.java b/iot-module/iot-plugin/iot-plugin-main/src/main/java/cc/iotkit/plugin/main/PluginMainImpl.java index 014aeb03..22db6d7e 100644 --- a/iot-module/iot-plugin/iot-plugin-main/src/main/java/cc/iotkit/plugin/main/PluginMainImpl.java +++ b/iot-module/iot-plugin/iot-plugin-main/src/main/java/cc/iotkit/plugin/main/PluginMainImpl.java @@ -104,17 +104,16 @@ public class PluginMainImpl implements IPluginMain, DeviceService { throw new BizException(ErrCode.PLUGIN_SERVICE_NOT_FOUND); } - if (!(service.getParams() instanceof Map)) { - throw new BizException(ErrCode.PARAMS_EXCEPTION); - } - Map params = (Map) service.getParams(); - IDevice deviceService = deviceServices.get(0); String type = service.getType(); String identifier = service.getIdentifier(); ActionResult result = null; if (ThingService.TYPE_SERVICE.equals(type)) { + if (!(service.getParams() instanceof Map)) { + throw new BizException(ErrCode.PARAMS_EXCEPTION); + } + Map params = (Map) service.getParams(); //服务调用 ServiceInvoke action = ServiceInvoke.builder() .id(service.getMid()) @@ -128,6 +127,10 @@ public class PluginMainImpl implements IPluginMain, DeviceService { publish(service, result.getCode()); } else if (ThingService.TYPE_PROPERTY.equals(type)) { if ("set".equals(identifier)) { + if (!(service.getParams() instanceof Map)) { + throw new BizException(ErrCode.PARAMS_EXCEPTION); + } + Map params = (Map) service.getParams(); //属性设置 PropertySet action = PropertySet.builder() .id(service.getMid()) @@ -144,7 +147,7 @@ public class PluginMainImpl implements IPluginMain, DeviceService { .id(service.getMid()) .productKey(linkPk) .deviceName(linkDn) - .keys(new ArrayList<>(params.keySet())) + .keys((List) service.getParams()) .build(); //调用插件设备服务接口 result = deviceService.propertyGet(action); diff --git a/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/handler/RuleDeviceConsumer.java b/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/handler/RuleDeviceConsumer.java index 67cd4e80..1b3539eb 100644 --- a/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/handler/RuleDeviceConsumer.java +++ b/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/handler/RuleDeviceConsumer.java @@ -23,6 +23,7 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ScheduledThreadPoolExecutor; @@ -53,6 +54,9 @@ public class RuleDeviceConsumer implements ConsumerHandler, A for (DeviceMessageHandler handler : this.handlers) { messageHandlerPool.submit(() -> { try { + if (!(msg.getData() instanceof Map)) { + msg.setData(new HashMap<>()); + } handler.handle(msg); } catch (Throwable e) { log.error("handler message error", e);