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 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> getDeviceExtProperties(@Path("device_id") String deviceId); + + @GET("/v1.0/iot-03/devices/{device_id}/specification") + DeviceSpecification getDeviceSpecification(@Path("device_id") String deviceId); + + @GET("/v2.0/cloud/thing/{device_id}/model") + Map getDeviceModel(@Path("device_id") String deviceId); + + @GET("/v2.0/cloud/thing/{device_id}/shadow/properties") + DeviceProperties getDeviceProperties(@Path("device_id") String deviceId); + + @GET("/v2.0/cloud/thing/{device_id}/state") + Map getDeviceState(@Path("device_id") String deviceId); + + @POST("/v2.0/cloud/thing/{device_id}/shadow/properties/issue") + Result issueDeviceProperties(@Path("device_id") String deviceId, @Body Map param); +} diff --git a/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/TuyaMessageListener.java b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/TuyaMessageListener.java index 5e19c6e..55dd4b7 100644 --- a/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/TuyaMessageListener.java +++ b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/TuyaMessageListener.java @@ -3,6 +3,9 @@ import com.tuya.connector.open.messaging.event.NameUpdateMessage; import com.tuya.connector.open.messaging.event.StatusReportMessage; import com.tuya.open.spring.boot.sample.ability.messaging.msg.DeviceNameUpdate; +import com.tuya.open.spring.boot.sample.ability.messaging.msg.DeviceOfflineMessage; +import com.tuya.open.spring.boot.sample.ability.messaging.msg.DeviceOnlineMessage; +import com.tuya.open.spring.boot.sample.ability.messaging.msg.DevicePropertyMessage; import lombok.extern.slf4j.Slf4j; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; @@ -17,12 +20,12 @@ @Component public class TuyaMessageListener { - @EventListener +// @EventListener public void updateStatusEvent(StatusReportMessage message) { log.info("StatusReport event happened: {}", message); } - @EventListener +// @EventListener public void nameUpdateMessage(NameUpdateMessage message) { log.info("NameUpdate event happened: {}", message); } @@ -32,4 +35,20 @@ public void deviceNameUpdateMsg(DeviceNameUpdate message) { log.info("deviceNameUpdateMsg event happened: {}", message); } + @EventListener + public void deviceOfflineMsg(DeviceOfflineMessage msg) { + log.warn("pulsar msg, DeviceOfflineMessage:{}", msg); + } + + @EventListener + public void deviceOnlineMsg(DeviceOnlineMessage msg) { + log.warn("pulsar msg, DeviceOnlineMessage:{}", msg); + } + + @EventListener + public void devicePropertyMsg(DevicePropertyMessage msg) { + log.warn("pulsar msg, DevicePropertyMessage:{}", msg); + } + + } diff --git a/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/msg/DeviceNameUpdate.java b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/msg/DeviceNameUpdate.java index 030de21..a77a639 100644 --- a/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/msg/DeviceNameUpdate.java +++ b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/msg/DeviceNameUpdate.java @@ -9,7 +9,7 @@ public class DeviceNameUpdate extends BaseTuyaMessage { private String devId; - private String produceId; + private String productId; private String uid; private String name; private String uuid; @@ -17,11 +17,11 @@ public class DeviceNameUpdate extends BaseTuyaMessage { @Override public void defaultBuild(SourceMessage sourceMessage, JSONObject messageBody) { super.defaultBuild(sourceMessage, messageBody); - this.devId = messageBody.getString("devId"); - this.produceId = messageBody.getString("produceId"); - this.uid = messageBody.getString("uid"); - this.name = messageBody.getString("name"); - this.uuid = messageBody.getString("uuid"); + this.devId = messageBody.getJSONObject("bizData").getString("devId"); + this.productId = messageBody.getJSONObject("bizData").getString("productId"); + this.uid = messageBody.getJSONObject("bizData").getString("uid"); + this.name = messageBody.getJSONObject("bizData").getString("name"); + this.uuid = messageBody.getJSONObject("bizData").getString("uuid"); } @Override diff --git a/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/msg/DeviceOfflineMessage.java b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/msg/DeviceOfflineMessage.java new file mode 100644 index 0000000..13fd4a7 --- /dev/null +++ b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/msg/DeviceOfflineMessage.java @@ -0,0 +1,29 @@ +package com.tuya.open.spring.boot.sample.ability.messaging.msg; + +import com.alibaba.fastjson.JSONObject; +import com.tuya.connector.open.messaging.SourceMessage; +import com.tuya.connector.open.messaging.event.BaseTuyaMessage; +import lombok.Getter; +import lombok.Setter; + +@Getter @Setter +public class DeviceOfflineMessage extends BaseTuyaMessage { + private String devId; + private String productId; + private String uid; + private Long time; + + @Override + public void defaultBuild(SourceMessage sourceMessage, JSONObject messageBody) { + super.defaultBuild(sourceMessage, messageBody); + this.devId = messageBody.getJSONObject("bizData").getString("devId"); + this.productId = messageBody.getJSONObject("bizData").getString("productId"); + this.uid = messageBody.getJSONObject("bizData").getString("uid"); + this.time = messageBody.getJSONObject("bizData").getLong("time"); + } + + @Override + public String type() { + return "deviceOffline"; + } +} diff --git a/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/msg/DeviceOnlineMessage.java b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/msg/DeviceOnlineMessage.java new file mode 100644 index 0000000..bfcb4f5 --- /dev/null +++ b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/msg/DeviceOnlineMessage.java @@ -0,0 +1,29 @@ +package com.tuya.open.spring.boot.sample.ability.messaging.msg; + +import com.alibaba.fastjson.JSONObject; +import com.tuya.connector.open.messaging.SourceMessage; +import com.tuya.connector.open.messaging.event.BaseTuyaMessage; +import lombok.Getter; +import lombok.Setter; + +@Getter @Setter +public class DeviceOnlineMessage extends BaseTuyaMessage { + private String devId; + private String productId; + private String uid; + private Long time; + + @Override + public void defaultBuild(SourceMessage sourceMessage, JSONObject messageBody) { + super.defaultBuild(sourceMessage, messageBody); + this.devId = messageBody.getJSONObject("bizData").getString("devId"); + this.productId = messageBody.getJSONObject("bizData").getString("productId"); + this.uid = messageBody.getJSONObject("bizData").getString("uid"); + this.time = messageBody.getJSONObject("bizData").getLong("time"); + } + + @Override + public String type() { + return "deviceOnline"; + } +} diff --git a/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/msg/DevicePropertyMessage.java b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/msg/DevicePropertyMessage.java new file mode 100644 index 0000000..db2da2e --- /dev/null +++ b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/msg/DevicePropertyMessage.java @@ -0,0 +1,60 @@ +package com.tuya.open.spring.boot.sample.ability.messaging.msg; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.tuya.connector.open.messaging.SourceMessage; +import com.tuya.connector.open.messaging.event.BaseTuyaMessage; +import lombok.Getter; +import lombok.Setter; + +import java.io.Serial; +import java.io.Serializable; +import java.util.List; + +@Getter @Setter +public class DevicePropertyMessage extends BaseTuyaMessage { + + private String dataId; + private String devId; + private String productId; + private List properties; + + @Override + public void defaultBuild(SourceMessage sourceMessage, JSONObject messageBody) { + super.defaultBuild(sourceMessage, messageBody); + this.dataId = messageBody.getJSONObject("bizData").getString("dataId"); + this.devId = messageBody.getJSONObject("bizData").getString("devId"); + this.productId = messageBody.getJSONObject("bizData").getString("productId"); + JSONArray jsonArray = messageBody.getJSONObject("bizData").getJSONArray("properties"); + if (jsonArray != null) { + properties = jsonArray.stream().map( + item -> { + JSONObject jsonObject = (JSONObject) item; + PropertyItem propertyItem = new PropertyItem(); + propertyItem.setCode(jsonObject.getString("code")); + propertyItem.setValue(jsonObject.get("value")); + propertyItem.setDpId(jsonObject.getString("dpId")); + propertyItem.setTime(jsonObject.getLong("time")); + return propertyItem; + } + ).toList(); + } + } + + @Override + public String type() { + return "devicePropertyMessage"; + } + + @Getter @Setter + class PropertyItem implements Serializable { + + @Serial + private static final long serialVersionUID = -5316969945618066530L; + + private String code; + private Object value; + private String dpId; + private Long time; + } +} diff --git a/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/model/DeviceDetail.java b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/model/DeviceDetail.java new file mode 100644 index 0000000..e020830 --- /dev/null +++ b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/model/DeviceDetail.java @@ -0,0 +1,40 @@ +package com.tuya.open.spring.boot.sample.ability.model; + +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; + +@Data +public class DeviceDetail implements Serializable { + + @Serial + private static final long serialVersionUID = -1389103173186462562L; + + private String id; // 设备 ID + private Long activeTime; // 设备的激活时间(时间戳秒数) + private String category; // 设备的产品品类 + private Long createTime; // 设备的初次配网时间(时间戳秒数) + private Long updateTime; // 设备的更新时间(时间戳秒数) + private String customName; // 设备的自定义名称 + private String icon; // 设备的图标 + private String ip; // 设备的 IP 地址 + private Boolean isOnline; // 设备的在线状态 + private Boolean online; // 设备的在线状态 + private String lat; // 设备的纬度 + private String localKey; // 设备的局域网加密后的唯一密钥 + private String lon; // 设备的经度 + private String name; // 设备的名称 + private String productId; // 设备的产品 ID + private String productName; // 设备的产品名称 + private Boolean sub; // 是否为子设备 + private String timeZone; // 设备的时区 + private String uuid; // 设备的 UUID + private String bindSpaceId; // 空间 Id + private String gatewayId; // 网关 ID + private String nodeId; // 设备的节点 ID + private String assetId; // 设备的资产 ID + private String model; // 设备的型号 + private String sn; // 设备的序列号 + +} diff --git a/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/model/DeviceProperties.java b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/model/DeviceProperties.java new file mode 100644 index 0000000..98fa458 --- /dev/null +++ b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/model/DeviceProperties.java @@ -0,0 +1,39 @@ +package com.tuya.open.spring.boot.sample.ability.model; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serial; +import java.io.Serializable; +import java.util.List; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class DeviceProperties implements Serializable { + @Serial + private static final long serialVersionUID = -8949241840263891680L; + + private List properties; + + + @Data + @AllArgsConstructor + @NoArgsConstructor + public static class Item implements Serializable { + + @Serial + private static final long serialVersionUID = 4342920416129210078L; + + private String code; + private String customName; + private Integer dpId; + private Long time; + private String type; + private Object value; + } + +} diff --git a/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/model/DeviceSpecification.java b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/model/DeviceSpecification.java new file mode 100644 index 0000000..9205959 --- /dev/null +++ b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/model/DeviceSpecification.java @@ -0,0 +1,33 @@ +package com.tuya.open.spring.boot.sample.ability.model; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serial; +import java.io.Serializable; +import java.util.List; + +@Data +public class DeviceSpecification implements Serializable { + @Serial + private static final long serialVersionUID = -8870376569649350814L; + + private String category; + private List functions; + private List status; + + + @Data + @AllArgsConstructor + @NoArgsConstructor + public static class Item implements Serializable{ + @Serial + private static final long serialVersionUID = -7907633673276744529L; + private String code; + private String desc; + private String name; + private String type; + private String values; + } +} diff --git a/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/model/Firmware.java b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/model/Firmware.java new file mode 100644 index 0000000..5ceaf2b --- /dev/null +++ b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/model/Firmware.java @@ -0,0 +1,17 @@ +package com.tuya.open.spring.boot.sample.ability.model; + +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; + +@Data +public class Firmware implements Serializable { + @Serial + private static final long serialVersionUID = 6845359510017172104L; + + private Integer type; + private String typeDesc; + private String currentVersion; + private Long lastUpgradeTime; +} diff --git a/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/model/IssueParam.java b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/model/IssueParam.java new file mode 100644 index 0000000..052c13b --- /dev/null +++ b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/model/IssueParam.java @@ -0,0 +1,15 @@ +package com.tuya.open.spring.boot.sample.ability.model; + +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; +import java.util.Map; + +@Data +public class IssueParam implements Serializable { + @Serial + private static final long serialVersionUID = -514297250121261947L; + + private Map properties; +} diff --git a/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/service/SIService.java b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/service/SIService.java new file mode 100644 index 0000000..ba986bf --- /dev/null +++ b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/service/SIService.java @@ -0,0 +1,74 @@ +package com.tuya.open.spring.boot.sample.service; + +import com.alibaba.fastjson.JSON; +import com.tuya.connector.api.model.Result; +import com.tuya.open.spring.boot.sample.ability.api.ThingConnector; +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 org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Service +public class SIService { + @Autowired + ThingConnector thingConnector; + + String DEVICE_ID = "6c8561a54bf607698f2sgw"; + + public void isDeviceExist(String deviceId) { + Result result = thingConnector.getDeviceResult(DEVICE_ID); + System.out.println(JSON.toJSONString(result)); + } + + public void getDevice(String deviceId) { + DeviceDetail device = thingConnector.getDevice(DEVICE_ID); + System.out.println(JSON.toJSONString(device)); + } + + public void getFirmware(String deviceId) { + Object ret = thingConnector.getFirmware(DEVICE_ID); + System.out.println(JSON.toJSONString(ret)); + } + + public void getExtProperties(String deviceId) { + List> ret = thingConnector.getDeviceExtProperties(DEVICE_ID); + System.out.println(JSON.toJSONString(ret)); + } + + public void getDeviceSpecification(String deviceId) { + DeviceSpecification ret = thingConnector.getDeviceSpecification(DEVICE_ID); + System.out.println(JSON.toJSONString(ret)); + } + + public void getDeviceModel(String deviceId) { + System.out.println(thingConnector.getDeviceModel(DEVICE_ID)); + } + + public void getDeviceProperties(String deviceId) { + DeviceProperties ret = thingConnector.getDeviceProperties(DEVICE_ID); + System.out.println(JSON.toJSONString(ret)); + } + + public void getDeviceState(String deviceId) { + Map ret = thingConnector.getDeviceState(DEVICE_ID); + System.out.println(JSON.toJSONString(ret)); + } + + public void issueDeviceProperties(String deviceId, DeviceProperties properties) { + Map param = new HashMap<>(); + Map kv = new HashMap<>(); + kv.put("switch_led", false); + param.put("properties", kv); + System.out.println(thingConnector.issueDeviceProperties(DEVICE_ID, param)); + } + + public void getIndustryDevice(String deviceId) { + System.out.println(JSON.toJSONString(thingConnector.getIndustryDevice(DEVICE_ID))); + } + +} diff --git a/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/web/SIController.java b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/web/SIController.java new file mode 100644 index 0000000..bce0342 --- /dev/null +++ b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/web/SIController.java @@ -0,0 +1,58 @@ +package com.tuya.open.spring.boot.sample.web; + +import com.tuya.open.spring.boot.sample.service.SIService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class SIController { + + @Autowired + SIService sIService; + + @RequestMapping("/a") + public void isDeviceExist(String deviceId) { + sIService.isDeviceExist(deviceId); + } + + @RequestMapping("/b") + public void getDevice(String deviceId) { + sIService.getDevice(deviceId); + } + + @RequestMapping("/c") + public void getFirmware(String deviceId) { + sIService.getFirmware(deviceId); + } + + @RequestMapping("/d") + public void getExtProperties(String deviceId) { + sIService.getExtProperties(deviceId); + } + + @RequestMapping("/e") + public void getDeviceSpecification(String deviceId) { + sIService.getDeviceSpecification(deviceId); + } + + @RequestMapping("/f") + public void getDeviceModel(String deviceId) { + sIService.getDeviceModel(deviceId); + } + + @RequestMapping("/g") + public void getDeviceProperties(String deviceId) { + sIService.getDeviceProperties(deviceId); + } + + @RequestMapping("/h") + public void getDeviceState(String deviceId) { + sIService.getDeviceState(deviceId); + } + + @RequestMapping("/i") + public void issue(String deviceId) { + sIService.issueDeviceProperties(null, null); + } +} diff --git a/tuya-spring-boot-starter-sample/src/test/java/com/tuya/open/spring/boot/sample/service/SIServiceTest.java b/tuya-spring-boot-starter-sample/src/test/java/com/tuya/open/spring/boot/sample/service/SIServiceTest.java new file mode 100644 index 0000000..fb35431 --- /dev/null +++ b/tuya-spring-boot-starter-sample/src/test/java/com/tuya/open/spring/boot/sample/service/SIServiceTest.java @@ -0,0 +1,85 @@ +package com.tuya.open.spring.boot.sample.service; + +import com.alibaba.fastjson.JSON; +import com.tuya.open.spring.boot.sample.ability.model.DeviceProperties; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.MockitoAnnotations; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.HashMap; +import java.util.Map; + +@SpringBootTest +class SIServiceTest { + + @InjectMocks + SIService siService; + + String deviceId = "6c8561a54bf607698f2sgw"; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void fastjson_test() { + Map m =new HashMap<>(); + Long[] longs = {17201088000000L}; + m.put("fieldValues", longs); + System.out.println(JSON.toJSONString(m)); + } + + @Test + void isDeviceExist_validDeviceId_returnsResult() { + siService.isDeviceExist(deviceId); + } + + @Test + void getDevice_validDeviceId_returnsDeviceDetail() { + siService.getDevice(deviceId); + } + + @Test + void getFirmware_validDeviceId_returnsFirmware() { + siService.getFirmware(deviceId); + } + + @Test + void getExtProperties_validDeviceId_returnsProperties() { + siService.getExtProperties(deviceId); + } + + @Test + void getDeviceSpecification_validDeviceId_returnsSpecification() { + siService.getDeviceSpecification(deviceId); + } + + @Test + void getDeviceModel_validDeviceId_returnsModel() { + siService.getDeviceModel(deviceId); + } + + @Test + void getDeviceProperties_validDeviceId_returnsProperties() { + siService.getDeviceProperties(deviceId); + } + + @Test + void getDeviceState_validDeviceId_returnsState() { + siService.getDeviceState(deviceId); + } + + @Test + void issueDeviceProperties_validDeviceIdAndProperties_issuesProperties() { + DeviceProperties properties = new DeviceProperties(); + siService.issueDeviceProperties(deviceId, properties); + } + + @Test + void getIndustryDevice_validDeviceId_returnsIndustryDevice() { + siService.getIndustryDevice(deviceId); + } +} \ No newline at end of file