Skip to content

Commit 25424c5

Browse files
committed
feat: 消息订阅
1 parent 0089178 commit 25424c5

File tree

16 files changed

+618
-10
lines changed

16 files changed

+618
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.tuya.connector.open.messaging;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import com.alibaba.fastjson.JSONObject;
5+
import com.tuya.connector.open.messaging.event.BaseTuyaMessage;
6+
import com.tuya.connector.open.messaging.event.UnknownMessage;
7+
import lombok.SneakyThrows;
8+
import lombok.extern.slf4j.Slf4j;
9+
import org.reflections.Reflections;
10+
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
import java.util.Objects;
14+
import java.util.Set;
15+
16+
/**
17+
* <p> TODO
18+
*
19+
* @author 丘枫(余秋风 qiufeng.yu@tuya.com)
20+
* @since 2021/3/24 3:56 下午
21+
*/
22+
@Slf4j
23+
public class MessageFactory {
24+
25+
private static Map<String, Class<? extends BaseTuyaMessage>> messageHandler = new HashMap<>();
26+
27+
static {
28+
Reflections reflections = new Reflections("com.tuya.connector.open.messaging.event");
29+
Set<Class<? extends BaseTuyaMessage>> classSet = reflections.getSubTypesOf(BaseTuyaMessage.class);
30+
classSet.forEach(handler -> {
31+
try {
32+
BaseTuyaMessage baseTuyaMessage = handler.newInstance();
33+
messageHandler.put(baseTuyaMessage.type(), handler);
34+
} catch (Exception ignore) {
35+
log.error("ignore {} handler.", handler.getSimpleName());
36+
}
37+
});
38+
}
39+
40+
@SneakyThrows
41+
public static BaseTuyaMessage extract(SourceMessage sourceMessage, String sk) {
42+
String data;
43+
if (sourceMessage.getEncryptPayload() != null) {
44+
// problem left over by history
45+
data = sourceMessage.getEncryptPayload();
46+
} else {
47+
data = sourceMessage.getData();
48+
}
49+
String decryptData = AESBase64Utils.decrypt(data, sk.substring(8, 24));
50+
JSONObject messageBody = JSON.parseObject(decryptData);
51+
String bizCode = null;
52+
if (Objects.nonNull(messageBody) && messageBody.size() > 0) {
53+
bizCode = messageBody.getString("bizCode");
54+
}
55+
return generate(bizCode, sourceMessage, messageBody);
56+
}
57+
58+
@SneakyThrows
59+
public static BaseTuyaMessage generate(String bizCode, SourceMessage sourceMessage, JSONObject messageBody) {
60+
Class<? extends BaseTuyaMessage> msgHandler = messageHandler.getOrDefault(bizCode, UnknownMessage.class);
61+
BaseTuyaMessage tuyaMessage = msgHandler.newInstance();
62+
tuyaMessage.defaultBuild(sourceMessage, messageBody);
63+
return tuyaMessage;
64+
}
65+
}

tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/TuyaSpringBootStarterSampleApplication.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package com.tuya.open.spring.boot.sample;
22

3-
import com.tuya.connector.open.messaging.autoconfig.EnableMessaging;
43
import com.tuya.connector.spring.annotations.ConnectorScan;
54
import org.springframework.boot.SpringApplication;
65
import org.springframework.boot.autoconfigure.SpringBootApplication;
76

87
@ConnectorScan(basePackages = "com.tuya.open.spring.boot.sample.ability.api")
9-
@EnableMessaging(msgPaths = {"com.tuya.open.spring.boot.sample.ability.messaging.msg"})
8+
//@EnableMessaging(msgPaths = {"com.tuya.open.spring.boot.sample.ability.messaging.msg"})
109
@SpringBootApplication
1110
public class TuyaSpringBootStarterSampleApplication {
1211

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.tuya.open.spring.boot.sample.ability.api;
2+
3+
import com.tuya.connector.api.annotations.Body;
4+
import com.tuya.connector.api.annotations.GET;
5+
import com.tuya.connector.api.annotations.POST;
6+
import com.tuya.connector.api.annotations.Path;
7+
import com.tuya.connector.api.model.Result;
8+
import com.tuya.open.spring.boot.sample.ability.model.DeviceDetail;
9+
import com.tuya.open.spring.boot.sample.ability.model.DeviceProperties;
10+
import com.tuya.open.spring.boot.sample.ability.model.DeviceSpecification;
11+
import com.tuya.open.spring.boot.sample.ability.model.Firmware;
12+
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
public interface ThingConnector {
17+
@GET("/v2.0/cloud/thing/{device_id}")
18+
Result<DeviceDetail> getDeviceResult(@Path("device_id") String deviceId);
19+
20+
@GET("/v2.0/cloud/thing/{device_id}")
21+
DeviceDetail getDevice(@Path("device_id") String deviceId);
22+
23+
@GET("/v1.1/iot-03/devices/{device_id}")
24+
DeviceDetail getIndustryDevice(@Path("device_id") String deviceId);
25+
26+
@GET("/v2.0/cloud/thing/{device_id}/firmware")
27+
List<Firmware> getFirmware(@Path("device_id") String deviceId);
28+
29+
@GET("/v1.0/iot-03/devices/{device_id}/properties")
30+
List<Map<String, Object>> getDeviceExtProperties(@Path("device_id") String deviceId);
31+
32+
@GET("/v1.0/iot-03/devices/{device_id}/specification")
33+
DeviceSpecification getDeviceSpecification(@Path("device_id") String deviceId);
34+
35+
@GET("/v2.0/cloud/thing/{device_id}/model")
36+
Map<String, String> getDeviceModel(@Path("device_id") String deviceId);
37+
38+
@GET("/v2.0/cloud/thing/{device_id}/shadow/properties")
39+
DeviceProperties getDeviceProperties(@Path("device_id") String deviceId);
40+
41+
@GET("/v2.0/cloud/thing/{device_id}/state")
42+
Map<String, Object> getDeviceState(@Path("device_id") String deviceId);
43+
44+
@POST("/v2.0/cloud/thing/{device_id}/shadow/properties/issue")
45+
Result<Void> issueDeviceProperties(@Path("device_id") String deviceId, @Body Map<String, Object> param);
46+
}

tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/TuyaMessageListener.java

+21-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import com.tuya.connector.open.messaging.event.NameUpdateMessage;
44
import com.tuya.connector.open.messaging.event.StatusReportMessage;
55
import com.tuya.open.spring.boot.sample.ability.messaging.msg.DeviceNameUpdate;
6+
import com.tuya.open.spring.boot.sample.ability.messaging.msg.DeviceOfflineMessage;
7+
import com.tuya.open.spring.boot.sample.ability.messaging.msg.DeviceOnlineMessage;
8+
import com.tuya.open.spring.boot.sample.ability.messaging.msg.DevicePropertyMessage;
69
import lombok.extern.slf4j.Slf4j;
710
import org.springframework.context.event.EventListener;
811
import org.springframework.stereotype.Component;
@@ -17,12 +20,12 @@
1720
@Component
1821
public class TuyaMessageListener {
1922

20-
@EventListener
23+
// @EventListener
2124
public void updateStatusEvent(StatusReportMessage message) {
2225
log.info("StatusReport event happened: {}", message);
2326
}
2427

25-
@EventListener
28+
// @EventListener
2629
public void nameUpdateMessage(NameUpdateMessage message) {
2730
log.info("NameUpdate event happened: {}", message);
2831
}
@@ -32,4 +35,20 @@ public void deviceNameUpdateMsg(DeviceNameUpdate message) {
3235
log.info("deviceNameUpdateMsg event happened: {}", message);
3336
}
3437

38+
@EventListener
39+
public void deviceOfflineMsg(DeviceOfflineMessage msg) {
40+
log.warn("pulsar msg, DeviceOfflineMessage:{}", msg);
41+
}
42+
43+
@EventListener
44+
public void deviceOnlineMsg(DeviceOnlineMessage msg) {
45+
log.warn("pulsar msg, DeviceOnlineMessage:{}", msg);
46+
}
47+
48+
@EventListener
49+
public void devicePropertyMsg(DevicePropertyMessage msg) {
50+
log.warn("pulsar msg, DevicePropertyMessage:{}", msg);
51+
}
52+
53+
3554
}

tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/msg/DeviceNameUpdate.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
public class DeviceNameUpdate extends BaseTuyaMessage {
1010

1111
private String devId;
12-
private String produceId;
12+
private String productId;
1313
private String uid;
1414
private String name;
1515
private String uuid;
1616

1717
@Override
1818
public void defaultBuild(SourceMessage sourceMessage, JSONObject messageBody) {
1919
super.defaultBuild(sourceMessage, messageBody);
20-
this.devId = messageBody.getString("devId");
21-
this.produceId = messageBody.getString("produceId");
22-
this.uid = messageBody.getString("uid");
23-
this.name = messageBody.getString("name");
24-
this.uuid = messageBody.getString("uuid");
20+
this.devId = messageBody.getJSONObject("bizData").getString("devId");
21+
this.productId = messageBody.getJSONObject("bizData").getString("productId");
22+
this.uid = messageBody.getJSONObject("bizData").getString("uid");
23+
this.name = messageBody.getJSONObject("bizData").getString("name");
24+
this.uuid = messageBody.getJSONObject("bizData").getString("uuid");
2525
}
2626

2727
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.tuya.open.spring.boot.sample.ability.messaging.msg;
2+
3+
import com.alibaba.fastjson.JSONObject;
4+
import com.tuya.connector.open.messaging.SourceMessage;
5+
import com.tuya.connector.open.messaging.event.BaseTuyaMessage;
6+
import lombok.Getter;
7+
import lombok.Setter;
8+
9+
@Getter @Setter
10+
public class DeviceOfflineMessage extends BaseTuyaMessage {
11+
private String devId;
12+
private String productId;
13+
private String uid;
14+
private Long time;
15+
16+
@Override
17+
public void defaultBuild(SourceMessage sourceMessage, JSONObject messageBody) {
18+
super.defaultBuild(sourceMessage, messageBody);
19+
this.devId = messageBody.getJSONObject("bizData").getString("devId");
20+
this.productId = messageBody.getJSONObject("bizData").getString("productId");
21+
this.uid = messageBody.getJSONObject("bizData").getString("uid");
22+
this.time = messageBody.getJSONObject("bizData").getLong("time");
23+
}
24+
25+
@Override
26+
public String type() {
27+
return "deviceOffline";
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.tuya.open.spring.boot.sample.ability.messaging.msg;
2+
3+
import com.alibaba.fastjson.JSONObject;
4+
import com.tuya.connector.open.messaging.SourceMessage;
5+
import com.tuya.connector.open.messaging.event.BaseTuyaMessage;
6+
import lombok.Getter;
7+
import lombok.Setter;
8+
9+
@Getter @Setter
10+
public class DeviceOnlineMessage extends BaseTuyaMessage {
11+
private String devId;
12+
private String productId;
13+
private String uid;
14+
private Long time;
15+
16+
@Override
17+
public void defaultBuild(SourceMessage sourceMessage, JSONObject messageBody) {
18+
super.defaultBuild(sourceMessage, messageBody);
19+
this.devId = messageBody.getJSONObject("bizData").getString("devId");
20+
this.productId = messageBody.getJSONObject("bizData").getString("productId");
21+
this.uid = messageBody.getJSONObject("bizData").getString("uid");
22+
this.time = messageBody.getJSONObject("bizData").getLong("time");
23+
}
24+
25+
@Override
26+
public String type() {
27+
return "deviceOnline";
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.tuya.open.spring.boot.sample.ability.messaging.msg;
2+
3+
import com.alibaba.fastjson.JSONArray;
4+
import com.alibaba.fastjson.JSONObject;
5+
import com.tuya.connector.open.messaging.SourceMessage;
6+
import com.tuya.connector.open.messaging.event.BaseTuyaMessage;
7+
import lombok.Getter;
8+
import lombok.Setter;
9+
10+
import java.io.Serial;
11+
import java.io.Serializable;
12+
import java.util.List;
13+
14+
@Getter @Setter
15+
public class DevicePropertyMessage extends BaseTuyaMessage {
16+
17+
private String dataId;
18+
private String devId;
19+
private String productId;
20+
private List<PropertyItem> properties;
21+
22+
@Override
23+
public void defaultBuild(SourceMessage sourceMessage, JSONObject messageBody) {
24+
super.defaultBuild(sourceMessage, messageBody);
25+
this.dataId = messageBody.getJSONObject("bizData").getString("dataId");
26+
this.devId = messageBody.getJSONObject("bizData").getString("devId");
27+
this.productId = messageBody.getJSONObject("bizData").getString("productId");
28+
JSONArray jsonArray = messageBody.getJSONObject("bizData").getJSONArray("properties");
29+
if (jsonArray != null) {
30+
properties = jsonArray.stream().map(
31+
item -> {
32+
JSONObject jsonObject = (JSONObject) item;
33+
PropertyItem propertyItem = new PropertyItem();
34+
propertyItem.setCode(jsonObject.getString("code"));
35+
propertyItem.setValue(jsonObject.get("value"));
36+
propertyItem.setDpId(jsonObject.getString("dpId"));
37+
propertyItem.setTime(jsonObject.getLong("time"));
38+
return propertyItem;
39+
}
40+
).toList();
41+
}
42+
}
43+
44+
@Override
45+
public String type() {
46+
return "devicePropertyMessage";
47+
}
48+
49+
@Getter @Setter
50+
class PropertyItem implements Serializable {
51+
52+
@Serial
53+
private static final long serialVersionUID = -5316969945618066530L;
54+
55+
private String code;
56+
private Object value;
57+
private String dpId;
58+
private Long time;
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.tuya.open.spring.boot.sample.ability.model;
2+
3+
import lombok.Data;
4+
5+
import java.io.Serial;
6+
import java.io.Serializable;
7+
8+
@Data
9+
public class DeviceDetail implements Serializable {
10+
11+
@Serial
12+
private static final long serialVersionUID = -1389103173186462562L;
13+
14+
private String id; // 设备 ID
15+
private Long activeTime; // 设备的激活时间(时间戳秒数)
16+
private String category; // 设备的产品品类
17+
private Long createTime; // 设备的初次配网时间(时间戳秒数)
18+
private Long updateTime; // 设备的更新时间(时间戳秒数)
19+
private String customName; // 设备的自定义名称
20+
private String icon; // 设备的图标
21+
private String ip; // 设备的 IP 地址
22+
private Boolean isOnline; // 设备的在线状态
23+
private Boolean online; // 设备的在线状态
24+
private String lat; // 设备的纬度
25+
private String localKey; // 设备的局域网加密后的唯一密钥
26+
private String lon; // 设备的经度
27+
private String name; // 设备的名称
28+
private String productId; // 设备的产品 ID
29+
private String productName; // 设备的产品名称
30+
private Boolean sub; // 是否为子设备
31+
private String timeZone; // 设备的时区
32+
private String uuid; // 设备的 UUID
33+
private String bindSpaceId; // 空间 Id
34+
private String gatewayId; // 网关 ID
35+
private String nodeId; // 设备的节点 ID
36+
private String assetId; // 设备的资产 ID
37+
private String model; // 设备的型号
38+
private String sn; // 设备的序列号
39+
40+
}

0 commit comments

Comments
 (0)