From 25424c594135e8c5f4c684a0b7cce8fc7b4fcca6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=A7=8B=E9=A3=8E?= <15088601208@163.com>
Date: Thu, 24 Oct 2024 13:56:46 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B6=88=E6=81=AF=E8=AE=A2=E9=98=85?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../open/messaging/MessageFactory.java | 65 ++++++++++++++
...uyaSpringBootStarterSampleApplication.java | 3 +-
.../sample/ability/api/ThingConnector.java | 46 ++++++++++
.../messaging/TuyaMessageListener.java | 23 ++++-
.../messaging/msg/DeviceNameUpdate.java | 12 +--
.../messaging/msg/DeviceOfflineMessage.java | 29 +++++++
.../messaging/msg/DeviceOnlineMessage.java | 29 +++++++
.../messaging/msg/DevicePropertyMessage.java | 60 +++++++++++++
.../sample/ability/model/DeviceDetail.java | 40 +++++++++
.../ability/model/DeviceProperties.java | 39 +++++++++
.../ability/model/DeviceSpecification.java | 33 +++++++
.../boot/sample/ability/model/Firmware.java | 17 ++++
.../boot/sample/ability/model/IssueParam.java | 15 ++++
.../spring/boot/sample/service/SIService.java | 74 ++++++++++++++++
.../spring/boot/sample/web/SIController.java | 58 +++++++++++++
.../boot/sample/service/SIServiceTest.java | 85 +++++++++++++++++++
16 files changed, 618 insertions(+), 10 deletions(-)
create mode 100644 tuya-messaging/src/main/java/com/tuya/connector/open/messaging/MessageFactory.java
create mode 100644 tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/api/ThingConnector.java
create mode 100644 tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/msg/DeviceOfflineMessage.java
create mode 100644 tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/msg/DeviceOnlineMessage.java
create mode 100644 tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/msg/DevicePropertyMessage.java
create mode 100644 tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/model/DeviceDetail.java
create mode 100644 tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/model/DeviceProperties.java
create mode 100644 tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/model/DeviceSpecification.java
create mode 100644 tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/model/Firmware.java
create mode 100644 tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/model/IssueParam.java
create mode 100644 tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/service/SIService.java
create mode 100644 tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/web/SIController.java
create mode 100644 tuya-spring-boot-starter-sample/src/test/java/com/tuya/open/spring/boot/sample/service/SIServiceTest.java
diff --git a/tuya-messaging/src/main/java/com/tuya/connector/open/messaging/MessageFactory.java b/tuya-messaging/src/main/java/com/tuya/connector/open/messaging/MessageFactory.java
new file mode 100644
index 0000000..2571e39
--- /dev/null
+++ b/tuya-messaging/src/main/java/com/tuya/connector/open/messaging/MessageFactory.java
@@ -0,0 +1,65 @@
+package com.tuya.connector.open.messaging;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.tuya.connector.open.messaging.event.BaseTuyaMessage;
+import com.tuya.connector.open.messaging.event.UnknownMessage;
+import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
+import org.reflections.Reflections;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ *
TODO
+ *
+ * @author 丘枫(余秋风 qiufeng.yu@tuya.com)
+ * @since 2021/3/24 3:56 下午
+ */
+@Slf4j
+public class MessageFactory {
+
+ private static Map> messageHandler = new HashMap<>();
+
+ static {
+ Reflections reflections = new Reflections("com.tuya.connector.open.messaging.event");
+ Set> classSet = reflections.getSubTypesOf(BaseTuyaMessage.class);
+ classSet.forEach(handler -> {
+ try {
+ BaseTuyaMessage baseTuyaMessage = handler.newInstance();
+ messageHandler.put(baseTuyaMessage.type(), handler);
+ } catch (Exception ignore) {
+ log.error("ignore {} handler.", handler.getSimpleName());
+ }
+ });
+ }
+
+ @SneakyThrows
+ public static BaseTuyaMessage extract(SourceMessage sourceMessage, String sk) {
+ String data;
+ if (sourceMessage.getEncryptPayload() != null) {
+ // problem left over by history
+ data = sourceMessage.getEncryptPayload();
+ } else {
+ data = sourceMessage.getData();
+ }
+ String decryptData = AESBase64Utils.decrypt(data, sk.substring(8, 24));
+ JSONObject messageBody = JSON.parseObject(decryptData);
+ String bizCode = null;
+ if (Objects.nonNull(messageBody) && messageBody.size() > 0) {
+ bizCode = messageBody.getString("bizCode");
+ }
+ return generate(bizCode, sourceMessage, messageBody);
+ }
+
+ @SneakyThrows
+ public static BaseTuyaMessage generate(String bizCode, SourceMessage sourceMessage, JSONObject messageBody) {
+ Class extends BaseTuyaMessage> msgHandler = messageHandler.getOrDefault(bizCode, UnknownMessage.class);
+ BaseTuyaMessage tuyaMessage = msgHandler.newInstance();
+ tuyaMessage.defaultBuild(sourceMessage, messageBody);
+ return tuyaMessage;
+ }
+}
diff --git a/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/TuyaSpringBootStarterSampleApplication.java b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/TuyaSpringBootStarterSampleApplication.java
index ef67ff7..0d2a2f8 100644
--- a/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/TuyaSpringBootStarterSampleApplication.java
+++ b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/TuyaSpringBootStarterSampleApplication.java
@@ -1,12 +1,11 @@
package com.tuya.open.spring.boot.sample;
-import com.tuya.connector.open.messaging.autoconfig.EnableMessaging;
import com.tuya.connector.spring.annotations.ConnectorScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@ConnectorScan(basePackages = "com.tuya.open.spring.boot.sample.ability.api")
-@EnableMessaging(msgPaths = {"com.tuya.open.spring.boot.sample.ability.messaging.msg"})
+//@EnableMessaging(msgPaths = {"com.tuya.open.spring.boot.sample.ability.messaging.msg"})
@SpringBootApplication
public class TuyaSpringBootStarterSampleApplication {
diff --git a/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/api/ThingConnector.java b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/api/ThingConnector.java
new file mode 100644
index 0000000..509397d
--- /dev/null
+++ b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/api/ThingConnector.java
@@ -0,0 +1,46 @@
+package com.tuya.open.spring.boot.sample.ability.api;
+
+import com.tuya.connector.api.annotations.Body;
+import com.tuya.connector.api.annotations.GET;
+import com.tuya.connector.api.annotations.POST;
+import com.tuya.connector.api.annotations.Path;
+import com.tuya.connector.api.model.Result;
+import com.tuya.open.spring.boot.sample.ability.model.DeviceDetail;
+import com.tuya.open.spring.boot.sample.ability.model.DeviceProperties;
+import com.tuya.open.spring.boot.sample.ability.model.DeviceSpecification;
+import com.tuya.open.spring.boot.sample.ability.model.Firmware;
+
+import java.util.List;
+import java.util.Map;
+
+public interface ThingConnector {
+ @GET("/v2.0/cloud/thing/{device_id}")
+ Result getDeviceResult(@Path("device_id") String deviceId);
+
+ @GET("/v2.0/cloud/thing/{device_id}")
+ DeviceDetail getDevice(@Path("device_id") String deviceId);
+
+ @GET("/v1.1/iot-03/devices/{device_id}")
+ DeviceDetail getIndustryDevice(@Path("device_id") String deviceId);
+
+ @GET("/v2.0/cloud/thing/{device_id}/firmware")
+ List getFirmware(@Path("device_id") String deviceId);
+
+ @GET("/v1.0/iot-03/devices/{device_id}/properties")
+ List