|
|
@@ -2,22 +2,20 @@ package com.coffee.aliyun;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.coffee.bus.bean.AliIotConfig;
|
|
|
+import com.coffee.bus.entity.BusDeviceEntity;
|
|
|
import com.coffee.bus.entity.BusDeviceRunningEntity;
|
|
|
-import com.coffee.bus.enums.DeviceEnum;
|
|
|
import com.coffee.bus.listener.event.bean.DeviceInfoEvent;
|
|
|
-import lombok.AllArgsConstructor;
|
|
|
+import com.coffee.bus.service.LocalBusDeviceService;
|
|
|
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;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import javax.jms.Message;
|
|
|
import javax.jms.MessageListener;
|
|
|
-import java.net.InetAddress;
|
|
|
-import java.net.UnknownHostException;
|
|
|
+import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
@@ -38,15 +36,20 @@ public class AliyunConsumerGroupService {
|
|
|
private final ApplicationContext applicationContext;
|
|
|
|
|
|
// 初始化订阅客户端
|
|
|
- // 阿里云账号信息
|
|
|
private final AliyunIotSubscribeClient client;
|
|
|
|
|
|
- public AliyunConsumerGroupService(ApplicationContext applicationContext, AliyunIotSubscribeClient client) {
|
|
|
+ // 设备Service
|
|
|
+ private final LocalBusDeviceService deviceService;
|
|
|
+
|
|
|
+ public AliyunConsumerGroupService(ApplicationContext applicationContext,
|
|
|
+ AliyunIotSubscribeClient client,
|
|
|
+ LocalBusDeviceService deviceService) {
|
|
|
this.applicationContext = applicationContext;
|
|
|
this.client = client;
|
|
|
+ this.deviceService = deviceService;
|
|
|
}
|
|
|
|
|
|
- @Value("${aliyun.data-access.enable:false}")
|
|
|
+ @Value("${aliyun.server-subscription.enable:false}")
|
|
|
private boolean isEnable;
|
|
|
|
|
|
// 开启服务端订阅
|
|
|
@@ -94,76 +97,69 @@ public class AliyunConsumerGroupService {
|
|
|
*/
|
|
|
private static final String IOTID = "iotId";
|
|
|
private static final String PRODUCTKEY = "productKey";
|
|
|
+ private static final String DEVICENAME = "deviceName";
|
|
|
private static final String TOPIC = "topic";
|
|
|
private static final String MESSAGEID = "messageId";
|
|
|
private static final String ITEMS = "items";
|
|
|
private static final String VALUE = "value";
|
|
|
private static final String STATUS = "status";
|
|
|
+ private static final String CONTENT = "content";
|
|
|
|
|
|
- private synchronized void processMessage(Message message) {
|
|
|
+ private void processMessage(Message message) {
|
|
|
|
|
|
- PlatformDataLog dataLog = new PlatformDataLog();
|
|
|
+ PlatformLog platformLog = new PlatformLog();
|
|
|
Map<String, Object> platformData = new HashMap<>();
|
|
|
try {
|
|
|
- byte[] body = message.getBody(byte[].class);
|
|
|
- String contentString = new String(body);
|
|
|
+ // 获取主题,消息id和内容
|
|
|
String topic = message.getStringProperty(TOPIC);
|
|
|
String messageId = message.getStringProperty(MESSAGEID);
|
|
|
+ JSONObject content = JSON.parseObject(new String(message.getBody(byte[].class)));
|
|
|
+ // 平台数据
|
|
|
platformData.put(TOPIC,topic);
|
|
|
platformData.put(MESSAGEID,messageId);
|
|
|
- platformData.put("content",contentString);
|
|
|
-
|
|
|
+ platformData.put(CONTENT,content);
|
|
|
log.info("阿里云物联网发送的数据:"+JSON.toJSONString(platformData));
|
|
|
|
|
|
- /**
|
|
|
- * platformData:
|
|
|
- * {
|
|
|
- * "topic":"/a1M7k1TAECc/ceshi001/thing/event/property/post",
|
|
|
- * "messageId":"1496410088574460416",
|
|
|
- * "content":"{\"deviceType\":\"CustomCategory\",
|
|
|
- * \"iotId\":\"0syOzMqwkGebZBGKa08d000000\",
|
|
|
- * \"requestId\":\"254\",\"checkFailedData\":{},
|
|
|
- * \"productKey\":\"a1M7k1TAECc\",\"gmtCreate\":1645606941721,
|
|
|
- * \"deviceName\":\"ceshi001\",
|
|
|
- * \"items\":{
|
|
|
- * \"total\":{\"value\":100,\"time\":1645606941717},
|
|
|
- * \"pumpCode\":{\"value\":\"5719512336330299\",\"time\":1645606941717},
|
|
|
- * \"dataNumber\":{\"value\":254,\"time\":1645606941717},
|
|
|
- * \"PCAInvalid\":{\"value\":0,\"time\":1645606941717},
|
|
|
- * \"alarm\":{\"value\":0,\"time\":1645606941717},
|
|
|
- * \"flowRate\":{\"value\":2,\"time\":1645606941717},
|
|
|
- * \"PCAValid\":{\"value\":0,\"time\":1645606941717},
|
|
|
- * \"forecast\":{\"value\":0,\"time\":1645606941717},
|
|
|
- * \"finished\":{\"value\":17,\"time\":1645606941717},
|
|
|
- * \"battery\":{\"value\":65,\"time\":1645606941717},
|
|
|
- * \"runStatus\":{\"value\":2,\"time\":1645606941717},
|
|
|
- * \"patientCode\":{\"value\":111110000,\"time\":1645606941717}}}"
|
|
|
- * }
|
|
|
- */
|
|
|
- // 日志编号
|
|
|
- dataLog.setCode(messageId);
|
|
|
- // 平台数据
|
|
|
- dataLog.setPlatformData(JSON.toJSONString(platformData));
|
|
|
- // 将内容转换成对象
|
|
|
- JSONObject content = JSON.parseObject(contentString);
|
|
|
- // 设备编号
|
|
|
- String deviceCode = content.getString(IOTID);
|
|
|
- String productKey = content.getString(PRODUCTKEY);
|
|
|
- // 设置平台和设备
|
|
|
- dataLog.setPlatformCode(PlatformType.ALIYUN);
|
|
|
- dataLog.setDeviceCode(deviceCode);
|
|
|
- dataLog.setPlatformProductCode(productKey);
|
|
|
+ // 设备名称
|
|
|
+ String deviceName = content.getString(DEVICENAME);
|
|
|
+ // 创建设备对象
|
|
|
+ Date now = new Date();
|
|
|
// 根据topic判断数据类型
|
|
|
if (topic.matches("^/as/mqtt/status/[\\w\\/]*")){//设备上下线状态
|
|
|
String status = content.getString(STATUS);
|
|
|
if (status.equals("online")){
|
|
|
- log.info(deviceCode+"设备上线");
|
|
|
+ log.info(deviceName+"设备上线");
|
|
|
}else if (status.equals("offline")){
|
|
|
- log.info(deviceCode+"设备下线");
|
|
|
+ log.info(deviceName+"设备下线");
|
|
|
}else {
|
|
|
- log.info(deviceCode+"设备未激活");
|
|
|
+ log.info(deviceName+"设备未激活");
|
|
|
}
|
|
|
}else if (topic.matches("[\\w\\/]*event/property/post$")){//设备属性上报
|
|
|
+ /**
|
|
|
+ * platformData:
|
|
|
+ * {
|
|
|
+ * "topic":"/a1M7k1TAECc/ceshi001/thing/event/property/post",
|
|
|
+ * "messageId":"1496410088574460416",
|
|
|
+ * "content":"{\"deviceType\":\"CustomCategory\",
|
|
|
+ * \"iotId\":\"0syOzMqwkGebZBGKa08d000000\",
|
|
|
+ * \"requestId\":\"254\",\"checkFailedData\":{},
|
|
|
+ * \"productKey\":\"a1M7k1TAECc\",\"gmtCreate\":1645606941721,
|
|
|
+ * \"deviceName\":\"ceshi001\",
|
|
|
+ * \"items\":{
|
|
|
+ * \"total\":{\"value\":100,\"time\":1645606941717},
|
|
|
+ * \"pumpCode\":{\"value\":\"5719512336330299\",\"time\":1645606941717},
|
|
|
+ * \"dataNumber\":{\"value\":254,\"time\":1645606941717},
|
|
|
+ * \"PCAInvalid\":{\"value\":0,\"time\":1645606941717},
|
|
|
+ * \"alarm\":{\"value\":0,\"time\":1645606941717},
|
|
|
+ * \"flowRate\":{\"value\":2,\"time\":1645606941717},
|
|
|
+ * \"PCAValid\":{\"value\":0,\"time\":1645606941717},
|
|
|
+ * \"forecast\":{\"value\":0,\"time\":1645606941717},
|
|
|
+ * \"finished\":{\"value\":17,\"time\":1645606941717},
|
|
|
+ * \"battery\":{\"value\":65,\"time\":1645606941717},
|
|
|
+ * \"runStatus\":{\"value\":2,\"time\":1645606941717},
|
|
|
+ * \"patientCode\":{\"value\":111110000,\"time\":1645606941717}}}"
|
|
|
+ * }
|
|
|
+ */
|
|
|
// 设备属性上报
|
|
|
|
|
|
|
|
|
@@ -171,36 +167,43 @@ public class AliyunConsumerGroupService {
|
|
|
DeviceInfoEvent deviceInfoEvent = new DeviceInfoEvent(this,new BusDeviceRunningEntity(),"123456");
|
|
|
applicationContext.publishEvent(deviceInfoEvent);
|
|
|
|
|
|
- }else if(topic.matches("[\\w\\/]+thing/liftcycle$")){// 设备生命周期
|
|
|
+ }else if(topic.matches("[\\w\\/]+thing/lifecycle$")){// 设备生命周期
|
|
|
+ // 获取生命周期类型
|
|
|
+ String action = content.getString("action");
|
|
|
+ if ("create".equals(action)){
|
|
|
+ // 创建设备
|
|
|
+ BusDeviceEntity device = new BusDeviceEntity();
|
|
|
+ device.setDeviceId(deviceName);
|
|
|
+ device.setCreateBy("1");
|
|
|
+ device.setUpdateBy("1");
|
|
|
+ device.setUpdateTime(now);
|
|
|
+ device.setTenantId("1");
|
|
|
+
|
|
|
+ // 配置信息
|
|
|
+ AliIotConfig config = new AliIotConfig();
|
|
|
+ config.setDeviceName(deviceName);
|
|
|
+ config.setDeviceSecret(content.getString("deviceSecret"));
|
|
|
+ config.setIotId(content.getString("iotId"));
|
|
|
+ config.setProductKey(content.getString("productKey"));
|
|
|
+
|
|
|
+ // 设置配置信息
|
|
|
+ device.setConfig(config);
|
|
|
+ deviceService.saveByDeviceId(device);
|
|
|
+ }else if ("delete".equals(action)){
|
|
|
+ // 删除设备
|
|
|
+ deviceService.removeByDeviceId(deviceName);
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 生命周期,删除设备
|
|
|
- * {"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);
|
|
|
+ log.error("未知的topic:"+topic);
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
-// e.printStackTrace();
|
|
|
// 错误标志
|
|
|
- dataLog.setIsError("1");
|
|
|
- dataLog.setErrorLog(e.getMessage());
|
|
|
+ platformLog.setIsError("1");
|
|
|
+ platformLog.setErrorLog(e.getMessage());
|
|
|
log.error("数据处理失败 ", e);
|
|
|
}
|
|
|
// 存储日志
|