|
@@ -1,4 +1,4 @@
|
|
|
-package com.zswl.dataservice.service.mqtt;
|
|
|
|
|
|
|
+package com.zswl.dataservice.service.artemis;
|
|
|
|
|
|
|
|
import cn.hutool.json.JSONObject;
|
|
import cn.hutool.json.JSONObject;
|
|
|
import com.google.gson.JsonObject;
|
|
import com.google.gson.JsonObject;
|
|
@@ -7,6 +7,8 @@ import com.zswl.dataservice.dao.mqtt.OperationMessageDao;
|
|
|
import com.zswl.dataservice.domain.mqtt.DeviceInfo;
|
|
import com.zswl.dataservice.domain.mqtt.DeviceInfo;
|
|
|
import com.zswl.dataservice.domain.mqtt.OperationMessage;
|
|
import com.zswl.dataservice.domain.mqtt.OperationMessage;
|
|
|
import com.zswl.dataservice.model.mqtt.*;
|
|
import com.zswl.dataservice.model.mqtt.*;
|
|
|
|
|
+import com.zswl.dataservice.service.mqtt.DeviceInfoService;
|
|
|
|
|
+import com.zswl.dataservice.service.mqtt.GateWayInfoService;
|
|
|
import com.zswl.dataservice.utils.DateUtils;
|
|
import com.zswl.dataservice.utils.DateUtils;
|
|
|
import com.zswl.dataservice.utils.bean.BeanUtils;
|
|
import com.zswl.dataservice.utils.bean.BeanUtils;
|
|
|
import com.zswl.dataservice.utils.mqtt.MqttTopicUtils;
|
|
import com.zswl.dataservice.utils.mqtt.MqttTopicUtils;
|
|
@@ -20,6 +22,7 @@ import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
+import java.util.Date;
|
|
|
import java.util.UUID;
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -47,27 +50,31 @@ public class OperationMessageService {
|
|
|
@Autowired
|
|
@Autowired
|
|
|
DeviceInfoDao deviceInfoDao;
|
|
DeviceInfoDao deviceInfoDao;
|
|
|
|
|
|
|
|
|
|
+ // 保存90天
|
|
|
|
|
+ private Long ttlMill = 90 * 24L * 60 * 60 * 1000L;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 给设备下发指令
|
|
* 给设备下发指令
|
|
|
|
|
+ *
|
|
|
* @param deviceId 设备ID
|
|
* @param deviceId 设备ID
|
|
|
* @param command 指令,如:on,off
|
|
* @param command 指令,如:on,off
|
|
|
* @param data 指令数据
|
|
* @param data 指令数据
|
|
|
* @return
|
|
* @return
|
|
|
*/
|
|
*/
|
|
|
- public ResultContent sendMessage(String deviceId,String command, JSONObject data) {
|
|
|
|
|
|
|
+ public ResultContent sendMessage(String deviceId, String command, JSONObject data) {
|
|
|
DeviceInfo deviceInfo = deviceInfoDao.findTopById(deviceId);
|
|
DeviceInfo deviceInfo = deviceInfoDao.findTopById(deviceId);
|
|
|
- if (ObjectUtils.isEmpty(deviceInfo)){
|
|
|
|
|
|
|
+ if (ObjectUtils.isEmpty(deviceInfo)) {
|
|
|
return ResultContent.buildFail(String.format("设备不存在:%s", deviceId));
|
|
return ResultContent.buildFail(String.format("设备不存在:%s", deviceId));
|
|
|
}
|
|
}
|
|
|
try {
|
|
try {
|
|
|
// 消息的TTL时间
|
|
// 消息的TTL时间
|
|
|
- Long ttl = 10*1000L;
|
|
|
|
|
|
|
+ Long ttl = 10 * 1000L;
|
|
|
// 消息的ID
|
|
// 消息的ID
|
|
|
String messageId = "";
|
|
String messageId = "";
|
|
|
OperationMessage message = new OperationMessage();
|
|
OperationMessage message = new OperationMessage();
|
|
|
if (data.containsKey("messageId")) {
|
|
if (data.containsKey("messageId")) {
|
|
|
messageId = data.getStr("messageId");
|
|
messageId = data.getStr("messageId");
|
|
|
- }else {
|
|
|
|
|
|
|
+ } else {
|
|
|
messageId = UUID.randomUUID().toString();
|
|
messageId = UUID.randomUUID().toString();
|
|
|
data.append("messageId", messageId);
|
|
data.append("messageId", messageId);
|
|
|
}
|
|
}
|
|
@@ -85,18 +92,31 @@ public class OperationMessageService {
|
|
|
JsonObject jsonObject = new JsonObject();
|
|
JsonObject jsonObject = new JsonObject();
|
|
|
jsonObject.addProperty("id", messageId);
|
|
jsonObject.addProperty("id", messageId);
|
|
|
jsonObject.addProperty("data", data.toString());
|
|
jsonObject.addProperty("data", data.toString());
|
|
|
- jsonObject.addProperty("time", DateUtils.paresTime(System.currentTimeMillis(), DateUtils.patternyyyySSS));
|
|
|
|
|
|
|
+ jsonObject.addProperty("time", System.currentTimeMillis());
|
|
|
jsonObject.addProperty("ttl", ttl);
|
|
jsonObject.addProperty("ttl", ttl);
|
|
|
mqClient.sendObject(topic, jsonObject.toString());
|
|
mqClient.sendObject(topic, jsonObject.toString());
|
|
|
log.info("mqtt msg 发送成功");
|
|
log.info("mqtt msg 发送成功");
|
|
|
|
|
|
|
|
operationMessageDao.save(message);
|
|
operationMessageDao.save(message);
|
|
|
- }catch (Exception e){
|
|
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
}
|
|
}
|
|
|
return ResultContent.buildSuccess();
|
|
return ResultContent.buildSuccess();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 添加
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param entity
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public ResultContent addOperationMessage(OperationMessage entity) {
|
|
|
|
|
+ entity.setTime(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
|
|
|
|
|
+ entity.setTtl(new Date(System.currentTimeMillis() + ttlMill));
|
|
|
|
|
+ operationMessageDao.save(entity);
|
|
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 指令列表
|
|
* 指令列表
|
|
|
*
|
|
*
|