From 2372a4fc6fd6bc32dab386340f658afcd05ab3fb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=8D=AD=E7=90=AA=E6=9E=AB?= <374696376@qq.com>
Date: Tue, 9 May 2023 23:41:46 +0800
Subject: [PATCH] =?UTF-8?q?feat:=E6=B7=BB=E5=8A=A0=E9=82=AE=E7=AE=B1?=
=?UTF-8?q?=E6=B6=88=E6=81=AF=E9=80=9A=E7=9F=A5?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
iot-common/iot-message-notify/pom.xml | 4 ++
.../iotkit/message/config/VertxManager.java | 19 +++++++
.../DingTalkEventListener.java | 21 ++++----
.../message/listener/EmailEventListener.java | 50 +++++++++++++++++++
.../MessageEventListener.java | 5 +-
.../QyWechatEventListener.java | 18 +++----
.../cc/iotkit/message/model/EmailConfig.java | 21 ++++++++
.../cc/iotkit/message/model/EmailMessage.java | 3 ++
.../java/cc/iotkit/message/model/Message.java | 1 +
9 files changed, 117 insertions(+), 25 deletions(-)
create mode 100644 iot-common/iot-message-notify/src/main/java/cc/iotkit/message/config/VertxManager.java
rename iot-common/iot-message-notify/src/main/java/cc/iotkit/message/{notify => listener}/DingTalkEventListener.java (56%)
create mode 100644 iot-common/iot-message-notify/src/main/java/cc/iotkit/message/listener/EmailEventListener.java
rename iot-common/iot-message-notify/src/main/java/cc/iotkit/message/{notify => listener}/MessageEventListener.java (55%)
rename iot-common/iot-message-notify/src/main/java/cc/iotkit/message/{notify => listener}/QyWechatEventListener.java (63%)
create mode 100644 iot-common/iot-message-notify/src/main/java/cc/iotkit/message/model/EmailConfig.java
diff --git a/iot-common/iot-message-notify/pom.xml b/iot-common/iot-message-notify/pom.xml
index 2402aca1..e3c16f94 100644
--- a/iot-common/iot-message-notify/pom.xml
+++ b/iot-common/iot-message-notify/pom.xml
@@ -22,6 +22,10 @@
org.springframework.boot
spring-boot-starter-web
+
+ org.springframework.boot
+ spring-boot-starter-mail
+
org.projectlombok
diff --git a/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/config/VertxManager.java b/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/config/VertxManager.java
new file mode 100644
index 00000000..bd4e54d6
--- /dev/null
+++ b/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/config/VertxManager.java
@@ -0,0 +1,19 @@
+/*
+ * +----------------------------------------------------------------------
+ * | Copyright (c) 奇特物联 2021-2022 All rights reserved.
+ * +----------------------------------------------------------------------
+ * | Licensed 未经许可不能去掉「奇特物联」相关版权
+ * +----------------------------------------------------------------------
+ * | Author: xw2sy@163.com
+ * +----------------------------------------------------------------------
+ */
+package cc.iotkit.message.config;
+
+import io.vertx.core.Vertx;
+
+public enum VertxManager {
+ INSTANCE;
+ public Vertx getVertx() {
+ return Vertx.vertx();
+ }
+}
diff --git a/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/notify/DingTalkEventListener.java b/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/listener/DingTalkEventListener.java
similarity index 56%
rename from iot-common/iot-message-notify/src/main/java/cc/iotkit/message/notify/DingTalkEventListener.java
rename to iot-common/iot-message-notify/src/main/java/cc/iotkit/message/listener/DingTalkEventListener.java
index 88477371..e8c7aafe 100644
--- a/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/notify/DingTalkEventListener.java
+++ b/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/listener/DingTalkEventListener.java
@@ -1,8 +1,9 @@
-package cc.iotkit.message.notify;
+package cc.iotkit.message.listener;
+import cc.iotkit.message.config.VertxManager;
import cc.iotkit.message.model.DingTalkMessage;
import cc.iotkit.message.model.Message;
-import io.vertx.core.Vertx;
+import cc.iotkit.message.notify.EventListener;
import io.vertx.ext.web.client.WebClient;
import lombok.extern.slf4j.Slf4j;
@@ -12,23 +13,19 @@ import lombok.extern.slf4j.Slf4j;
* description:
**/
@Slf4j
-public class DingTalkEventListener implements EventListener{
+public class DingTalkEventListener implements EventListener {
private String baseUrl = "https://oapi.dingtalk.com/robot/send?access_token=%s";
+
@Override
public void doEvent(Message message) {
- WebClient client = WebClient.create(Vertx.vertx());
+ 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(message.getContent()).build())
.build();
- client.post(url).sendJson(qyWechatMessage, rs -> {
- if (rs.succeeded()) {
- log.info("发送成功.");
- }
- if (rs.failed()) {
- log.info("发送失败.");
- }
- });
+ client.post(url).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-common/iot-message-notify/src/main/java/cc/iotkit/message/listener/EmailEventListener.java b/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/listener/EmailEventListener.java
new file mode 100644
index 00000000..8394bd56
--- /dev/null
+++ b/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/listener/EmailEventListener.java
@@ -0,0 +1,50 @@
+package cc.iotkit.message.listener;
+
+import cc.iotkit.message.model.EmailConfig;
+import cc.iotkit.message.model.Message;
+import cc.iotkit.message.notify.EventListener;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.mail.javamail.JavaMailSenderImpl;
+import org.springframework.mail.javamail.MimeMessageHelper;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+import java.util.Properties;
+
+/**
+ * author: 石恒
+ * date: 2023-05-08 15:09
+ * description:
+ **/
+@Slf4j
+public class EmailEventListener implements EventListener {
+
+ @Override
+ public void doEvent(Message message) {
+ EmailConfig emailConfig = new EmailConfig();
+ JavaMailSenderImpl jms = new JavaMailSenderImpl();
+ jms.setHost(emailConfig.getHost());
+ jms.setUsername(emailConfig.getUserName());
+ jms.setPassword(emailConfig.getPassWord());
+ jms.setDefaultEncoding("utf-8");
+ Properties p = new Properties();
+ p.setProperty("mail.smtp.auth", "true");
+ jms.setJavaMailProperties(p);
+ MimeMessage mimeMessage = jms.createMimeMessage();
+ try {
+ MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
+ //收件人
+ String[] split = emailConfig.getTo().split(",");
+ messageHelper.setTo(split);
+ //标题
+ messageHelper.setSubject(emailConfig.getTitle());
+ //内容
+ messageHelper.setText(emailConfig.getContent(), true);
+ //发件人
+ messageHelper.setFrom(emailConfig.getFrom());
+ jms.send(mimeMessage);
+ } catch (MessagingException e) {
+ log.error("发送邮件失败.", e);
+ }
+ }
+}
diff --git a/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/notify/MessageEventListener.java b/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/listener/MessageEventListener.java
similarity index 55%
rename from iot-common/iot-message-notify/src/main/java/cc/iotkit/message/notify/MessageEventListener.java
rename to iot-common/iot-message-notify/src/main/java/cc/iotkit/message/listener/MessageEventListener.java
index 3aaaeea9..813d2d23 100644
--- a/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/notify/MessageEventListener.java
+++ b/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/listener/MessageEventListener.java
@@ -1,13 +1,14 @@
-package cc.iotkit.message.notify;
+package cc.iotkit.message.listener;
import cc.iotkit.message.model.Message;
+import cc.iotkit.message.notify.EventListener;
/**
* author: 石恒
* date: 2023-05-08 15:09
* description:
**/
-public class MessageEventListener implements EventListener{
+public class MessageEventListener implements EventListener {
@Override
public void doEvent(Message message) {
diff --git a/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/notify/QyWechatEventListener.java b/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/listener/QyWechatEventListener.java
similarity index 63%
rename from iot-common/iot-message-notify/src/main/java/cc/iotkit/message/notify/QyWechatEventListener.java
rename to iot-common/iot-message-notify/src/main/java/cc/iotkit/message/listener/QyWechatEventListener.java
index b80dfafc..709e4de6 100644
--- a/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/notify/QyWechatEventListener.java
+++ b/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/listener/QyWechatEventListener.java
@@ -1,8 +1,9 @@
-package cc.iotkit.message.notify;
+package cc.iotkit.message.listener;
+import cc.iotkit.message.config.VertxManager;
import cc.iotkit.message.model.Message;
import cc.iotkit.message.model.QyWechatMessage;
-import io.vertx.core.Vertx;
+import cc.iotkit.message.notify.EventListener;
import io.vertx.ext.web.client.WebClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@@ -20,19 +21,14 @@ public class QyWechatEventListener implements EventListener {
@Override
public void doEvent(Message message) {
- WebClient client = WebClient.create(Vertx.vertx());
+ 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(message.getContent()).build())
.build();
- client.post(url).sendJson(qyWechatMessage, rs -> {
- if (rs.succeeded()) {
- log.info("发送成功.");
- }
- if (rs.failed()) {
- log.info("发送失败.");
- }
- });
+ client.post(url).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-common/iot-message-notify/src/main/java/cc/iotkit/message/model/EmailConfig.java b/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/model/EmailConfig.java
new file mode 100644
index 00000000..a8cd0406
--- /dev/null
+++ b/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/model/EmailConfig.java
@@ -0,0 +1,21 @@
+package cc.iotkit.message.model;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * author: 石恒
+ * date: 2023-05-08 15:58
+ * description:
+ **/
+@Data
+public class EmailConfig implements Serializable {
+ private String host;
+ private String userName;
+ private String passWord;
+ private String from;
+ private String to;
+ private String title;
+ private String content;
+}
diff --git a/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/model/EmailMessage.java b/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/model/EmailMessage.java
index 6816cb1f..105f15f8 100644
--- a/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/model/EmailMessage.java
+++ b/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/model/EmailMessage.java
@@ -1,5 +1,7 @@
package cc.iotkit.message.model;
+import lombok.Data;
+
import java.io.Serializable;
/**
@@ -7,5 +9,6 @@ import java.io.Serializable;
* date: 2023-05-08 15:58
* description:
**/
+@Data
public class EmailMessage implements Serializable {
}
diff --git a/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/model/Message.java b/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/model/Message.java
index 9f53f96a..acb54478 100644
--- a/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/model/Message.java
+++ b/iot-common/iot-message-notify/src/main/java/cc/iotkit/message/model/Message.java
@@ -11,4 +11,5 @@ import lombok.Data;
public class Message {
private String key;
private String content;
+ private Long channelId;
}