|
|
@@ -2,11 +2,13 @@ package com.coffee.aliyun;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.coffee.bus.entity.BusPumpEntity;
|
|
|
+import com.coffee.bus.entity.BusDeviceUsingEntity;
|
|
|
import com.coffee.bus.listener.event.bean.DeviceInfoEvent;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
@@ -32,52 +34,35 @@ import java.util.concurrent.TimeUnit;
|
|
|
@Slf4j
|
|
|
public class AliyunConsumerGroupService {
|
|
|
|
|
|
- @Autowired
|
|
|
- private ApplicationContext applicationContext;
|
|
|
+ private final ApplicationContext applicationContext;
|
|
|
|
|
|
- @Value("${aliyun.data-access.enable:false}")
|
|
|
- private boolean isEnable;
|
|
|
-
|
|
|
- static {
|
|
|
+ // 初始化订阅客户端
|
|
|
+ // 阿里云账号信息
|
|
|
+ private final AliyunIotSubscribeClient client;
|
|
|
|
|
|
+ public AliyunConsumerGroupService(ApplicationContext applicationContext, AliyunIotSubscribeClient client) {
|
|
|
+ this.applicationContext = applicationContext;
|
|
|
+ this.client = client;
|
|
|
}
|
|
|
|
|
|
+ @Value("${aliyun.data-access.enable:false}")
|
|
|
+ private boolean isEnable;
|
|
|
+
|
|
|
// 开启服务端订阅
|
|
|
@PostConstruct
|
|
|
public void subscribe(){
|
|
|
- if (isEnable){
|
|
|
- log.info("允许开启订阅");
|
|
|
- }else {
|
|
|
+ if (!isEnable){
|
|
|
log.info("订阅禁止");
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
- // 获取ip地址
|
|
|
- InetAddress addr = null;
|
|
|
- try {
|
|
|
- addr = InetAddress.getLocalHost();
|
|
|
- } catch (UnknownHostException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- String ip = addr.getHostAddress();
|
|
|
- // 初始化订阅客户端
|
|
|
- // 阿里云账号信息
|
|
|
- AliyunIotSubscribeClient client =
|
|
|
- new AliyunIotSubscribeClient(
|
|
|
- "LTAI4FhB19MgQuviGxwA3aod",
|
|
|
- "cQQVkATR0yv2G9CEtfjAhEGBepPDRs",
|
|
|
- "cn-shanghai",
|
|
|
- "DEFAULT_GROUP",
|
|
|
- "1177450762772738",
|
|
|
- "","DEFAULT_GROUP" + ip
|
|
|
- );
|
|
|
+ log.info("允许开启订阅");
|
|
|
try {
|
|
|
// 开启订阅
|
|
|
client.start(messageListener);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
- System.out.println("订阅成功。。。。。。。。。。。");
|
|
|
+ log.info("阿里云物联网订阅成功。。。。。。。。。。。");
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -113,6 +98,7 @@ public class AliyunConsumerGroupService {
|
|
|
private static final String ITEMS = "items";
|
|
|
private static final String VALUE = "value";
|
|
|
private static final String STATUS = "status";
|
|
|
+
|
|
|
private synchronized void processMessage(Message message) {
|
|
|
|
|
|
PlatformDataLog dataLog = new PlatformDataLog();
|
|
|
@@ -122,9 +108,12 @@ public class AliyunConsumerGroupService {
|
|
|
String contentString = new String(body);
|
|
|
String topic = message.getStringProperty(TOPIC);
|
|
|
String messageId = message.getStringProperty(MESSAGEID);
|
|
|
- platformData.put("topic",topic);
|
|
|
- platformData.put("messageId",messageId);
|
|
|
+ platformData.put(TOPIC,topic);
|
|
|
+ platformData.put(MESSAGEID,messageId);
|
|
|
platformData.put("content",contentString);
|
|
|
+
|
|
|
+ log.info("阿里云物联网发送的数据:"+JSON.toJSONString(platformData));
|
|
|
+
|
|
|
/**
|
|
|
* platformData:
|
|
|
* {
|
|
|
@@ -175,11 +164,35 @@ public class AliyunConsumerGroupService {
|
|
|
}
|
|
|
}else if (topic.matches("[\\w\\/]*event/property/post$")){//设备属性上报
|
|
|
// 设备属性上报
|
|
|
- DeviceInfoEvent deviceInfoEvent = new DeviceInfoEvent(this,new BusPumpEntity(),"123456");
|
|
|
+
|
|
|
+
|
|
|
+ // 发布设备信息事件
|
|
|
+ DeviceInfoEvent deviceInfoEvent = new DeviceInfoEvent(this,new BusDeviceUsingEntity(),"123456");
|
|
|
applicationContext.publishEvent(deviceInfoEvent);
|
|
|
|
|
|
- }else{// 其他的topic
|
|
|
- System.out.println("未知topic:"+topic);
|
|
|
+ }else if(topic.matches("[\\w\\/]+thing/liftcycle$")){// 设备生命周期
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生命周期,删除设备
|
|
|
+ * {"topic":"/a1M7k1TAECc/456789/thing/lifecycle",
|
|
|
+ * "messageId":"1511903665358296576",
|
|
|
+ * "content":"{\"iotId\":\"hGMEfTMBv9O6gyvpMSvi000000\",\"action\":\"delete\",
|
|
|
+ * \"messageCreateTime\":1649300898278,\"productKey\":\"a1M7k1TAECc\",
|
|
|
+ * \"deviceName\":\"456789\"}"}
|
|
|
+ *
|
|
|
+ * 生命周期,添加设备
|
|
|
+ * {"topic":"/a1M7k1TAECc/ceshidevice007/thing/lifecycle",
|
|
|
+ * "messageId":"1511904535630241792",
|
|
|
+ * "content":"{\"iotId\":\"KsCWSsSf86fjD6ux16sC000000\",
|
|
|
+ * \"deviceSecret\":\"989561a8d3bd939bfb8321bafc61b64c\",
|
|
|
+ * \"action\":\"create\",\"messageCreateTime\":1649301105776,
|
|
|
+ * \"productKey\":\"a1M7k1TAECc\",\"deviceName\":\"ceshidevice007\"}"}
|
|
|
+ *
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+ }else {
|
|
|
+ log.info("未知topic:"+topic);
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|