|
|
@@ -2,11 +2,15 @@ package com.zhongshu.iot.server.core.service.iot;
|
|
|
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
+import com.github.microservice.busInfoModel.thing.IotThingSendParam;
|
|
|
+import com.github.microservice.dataConfig.IotIdentifierConfig;
|
|
|
import com.github.microservice.models.iot.IotSendByIdParam;
|
|
|
import com.github.microservice.models.iot.IotSendGateWayParam;
|
|
|
import com.github.microservice.models.iot.IotSendParam;
|
|
|
+import com.github.microservice.models.property.PostGetPropertyParam;
|
|
|
import com.github.microservice.net.ResultContent;
|
|
|
import com.github.microservice.net.ResultMessage;
|
|
|
+import com.github.microservice.types.FunctionType;
|
|
|
import com.zhongshu.iot.client.model.iot.IotMainSearch;
|
|
|
import com.zhongshu.iot.client.type.IotDataType;
|
|
|
import com.zhongshu.iot.client.type.OperationBusType;
|
|
|
@@ -19,6 +23,7 @@ import com.zhongshu.iot.server.core.domain.iot.IotMain;
|
|
|
import com.zhongshu.iot.server.core.domain.iot.device.DeviceInfo;
|
|
|
import com.zhongshu.iot.server.core.domain.iot.device.OperationMessage;
|
|
|
import com.zhongshu.iot.server.core.domain.iot.device.OperationMessageResult;
|
|
|
+import com.zhongshu.iot.server.core.service.artemis.OperationMessageService;
|
|
|
import com.zhongshu.iot.server.core.service.base.SuperService;
|
|
|
import com.zhongshu.iot.server.core.util.CommonUtil;
|
|
|
import com.zhongshu.iot.server.core.util.DateUtils;
|
|
|
@@ -61,10 +66,18 @@ public class IotSendMessageService extends SuperService {
|
|
|
@Autowired
|
|
|
private MQClient mqClient;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private OperationMessageService operationMessageService;
|
|
|
+
|
|
|
// 保存90天
|
|
|
@Value("${artemisstore.time}")
|
|
|
public Long ttlMill = 30 * 24L * 60 * 60 * 1000L;
|
|
|
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
public ResultContent sendIotMsgById(IotSendByIdParam param) {
|
|
|
if (StringUtils.isEmpty(param.getId())) {
|
|
|
return ResultContent.buildFail("id不能为空");
|
|
|
@@ -80,22 +93,63 @@ public class IotSendMessageService extends SuperService {
|
|
|
iotSendParam.setData(param.getData());
|
|
|
iotSendParam.setId(param.getId());
|
|
|
iotSendParam.setIsImitate(Boolean.TRUE);
|
|
|
+ iotSendParam.setIotThingId(iotMain.getIotThingId());
|
|
|
return sendIotMessage(iotSendParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 发送信息
|
|
|
+ * 下发得到物模型的属性值
|
|
|
+ * @param param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent sendGetIotProperty(IotThingSendParam param) {
|
|
|
+ if (StringUtils.isEmpty(param.getId()) && StringUtils.isEmpty(param.getIotThingId())) {
|
|
|
+ return ResultContent.buildFail("id和iotThingId不能同时为空");
|
|
|
+ }
|
|
|
+ String iotThingId = "";
|
|
|
+ List<IotMain> iotMains = new ArrayList<>();
|
|
|
+ if (StringUtils.isNotEmpty(param.getIotThingId())) {
|
|
|
+ iotMains = iotMainDao.findByIotThingId(param.getIotThingId());
|
|
|
+ iotThingId = param.getIotThingId();
|
|
|
+ } else if (StringUtils.isNotEmpty(param.getId())) {
|
|
|
+ IotMain iotMain = iotMainDao.findTopById(param.getId());
|
|
|
+ if (ObjectUtils.isNotEmpty(iotMain)) {
|
|
|
+ iotMains.add(iotMain);
|
|
|
+ }
|
|
|
+ iotThingId = iotMain.getIotThingId();
|
|
|
+ }
|
|
|
+ if (ObjectUtils.isEmpty(iotMains)) {
|
|
|
+ return ResultContent.buildFail("没有找到属性");
|
|
|
+ }
|
|
|
+ PostGetPropertyParam propertyParam = new PostGetPropertyParam();
|
|
|
+ for (IotMain iotMain : iotMains) {
|
|
|
+ propertyParam.addProperty(iotMain.getDeviceId(), iotMain.getIdentifier());
|
|
|
+ }
|
|
|
+ JSONObject data = JSONUtil.parseObj(propertyParam);
|
|
|
+
|
|
|
+ // 组装发送数据
|
|
|
+ IotSendParam sendParam = new IotSendParam();
|
|
|
+ sendParam.setIotThingId(iotThingId);
|
|
|
+ sendParam.setFunctionType(FunctionType.Server);
|
|
|
+ sendParam.setIdentifier(IotIdentifierConfig.getAttrs);
|
|
|
+ sendParam.setData(data);
|
|
|
+ return sendIotMessage(sendParam);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送信息 (统一入口方法)
|
|
|
*
|
|
|
* @param param
|
|
|
* @return
|
|
|
*/
|
|
|
public ResultContent sendIotMessage(IotSendParam param) {
|
|
|
- log.info("****************sendIotMessage***************设备ID={} 功能类型={}", param.getDeviceId(), param.getIdentifier());
|
|
|
+ log.info("************sendIotMessage*********设备ID={} 功能类型={}", param.getDeviceId(), param.getIdentifier());
|
|
|
+
|
|
|
IotMainSearch search = new IotMainSearch();
|
|
|
BeanUtils.copyProperties(param, search);
|
|
|
- // 真实设备的topic
|
|
|
+ // 物模型的配置
|
|
|
search.setIotDataType(IotDataType.Device);
|
|
|
- Pageable pageable = PageRequest.of(0, Integer.MAX_VALUE);
|
|
|
+ Pageable pageable = PageRequest.of(0, 100);
|
|
|
Page<IotMain> page = iotMainDao.page(pageable, search);
|
|
|
|
|
|
List<IotMain> list = page.getContent();
|
|
|
@@ -104,8 +158,7 @@ public class IotSendMessageService extends SuperService {
|
|
|
return ResultContent.buildFail("没有对应的设备物模型");
|
|
|
}
|
|
|
// list 根据 topic 去重
|
|
|
- list = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(
|
|
|
- () -> new TreeSet<>(Comparator.comparing(IotMain::getRealIotTopic))), ArrayList::new));
|
|
|
+ list = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(IotMain::getRealIotTopic))), ArrayList::new));
|
|
|
|
|
|
DeviceInfo deviceInfo = null;
|
|
|
DeviceInfo gateWayInfo = null;
|
|
|
@@ -121,10 +174,12 @@ public class IotSendMessageService extends SuperService {
|
|
|
dataId = CommonUtil.UUID();
|
|
|
}
|
|
|
|
|
|
+ // 业务数据
|
|
|
+ JSONObject data = param.getData();
|
|
|
// 组装要发送的数据
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
jsonObject.set("id", dataId);
|
|
|
- jsonObject.set("data", param.getData());
|
|
|
+ jsonObject.set("data", data);
|
|
|
jsonObject.set("timeStr", DateUtils.paresTime(System.currentTimeMillis(), DateUtils.patternyyyySSS));
|
|
|
jsonObject.set("time", System.currentTimeMillis());
|
|
|
jsonObject.set("ttl", param.getTtl());
|
|
|
@@ -138,15 +193,12 @@ public class IotSendMessageService extends SuperService {
|
|
|
entity.setMessageId(CommonUtil.UUID()); // 消息ID
|
|
|
entity.setOperationBusType(OperationBusType.IotThing);
|
|
|
entity.setTimes();
|
|
|
- entity.setClientId(""); // 终端ID
|
|
|
- entity.setTopic(""); // topic名称
|
|
|
entity.setOperationType(OperationType.Push);
|
|
|
- entity.setMessageClass("");
|
|
|
entity.setData(jsonObject);
|
|
|
entity.setTtlTime(param.getTtl());
|
|
|
entity.setSendTime(System.currentTimeMillis());
|
|
|
entity.setDataId(dataId);
|
|
|
- entity.setIsTimeOut(false);
|
|
|
+ entity.setExpireAt(param.getTtl() + System.currentTimeMillis());
|
|
|
entity.setEvent(param.getEvent());
|
|
|
|
|
|
if (ObjectUtils.isNotEmpty(gateWayInfo)) {
|
|
|
@@ -157,7 +209,7 @@ public class IotSendMessageService extends SuperService {
|
|
|
entity.setDeviceId(deviceInfo.getDeviceId());
|
|
|
entity.setDeviceInfo(deviceInfo);
|
|
|
}
|
|
|
-
|
|
|
+ // 消息自动生成时间
|
|
|
entity.setTtl(new Date(System.currentTimeMillis() + ttlMill));
|
|
|
entity.setReceiveTime(System.currentTimeMillis());
|
|
|
entity.setReceiveTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
|
|
|
@@ -171,32 +223,22 @@ public class IotSendMessageService extends SuperService {
|
|
|
jsonObject.set("id", messageId);
|
|
|
|
|
|
OperationMessageResult messageResult = new OperationMessageResult();
|
|
|
- messageResult.setTimes();
|
|
|
- messageResult.setProductCode(iotMain.getProductCode());
|
|
|
- messageResult.setIotThingId(iotMain.getIotThingId());
|
|
|
- messageResult.setMessageId(messageId);
|
|
|
- messageResult.setDataId(entity.getDataId());
|
|
|
+ operationMessageService.buildOperationMessageResultIotInfo(messageResult, iotMain);
|
|
|
messageResult.setOperationMessage(entity);
|
|
|
-
|
|
|
- // 所属属性信息
|
|
|
- messageResult.setIotMain(iotMain);
|
|
|
- messageResult.setIotTemplate(iotMain.getIotTemplate());
|
|
|
-
|
|
|
- // 设备ID
|
|
|
- messageResult.setDeviceId(iotMain.getDeviceId());
|
|
|
- // 分组code
|
|
|
- messageResult.setProjectCode(iotMain.getProjectCode());
|
|
|
messageResult.setGateWayId(entity.getGateWayId());
|
|
|
- messageResult.setRealIotTopic(topic);
|
|
|
- messageResult.setData(jsonObject);
|
|
|
messageResult.setOperationType(entity.getOperationType());
|
|
|
|
|
|
+ messageResult.setMessageId(messageId);
|
|
|
+ messageResult.setDataId(entity.getDataId());
|
|
|
+ messageResult.setRealIotTopic(topic);
|
|
|
+ messageResult.setData(data);
|
|
|
Boolean isSendSuccess = Boolean.TRUE;
|
|
|
String sendMsg = "下发成功";
|
|
|
try {
|
|
|
mqClient.sendObject(topic, JSONUtil.toJsonStr(jsonObject), messageId);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
+ isSendSuccess = false;
|
|
|
sendMsg = String.format("下发出错:%s", e.getMessage());
|
|
|
}
|
|
|
messageResult.setIsSendSuccess(isSendSuccess);
|