|
|
@@ -3,17 +3,24 @@ package com.middle.platform.manage.biz.service;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.middle.platform.common.constant.Global;
|
|
|
import com.middle.platform.common.exception.BusinessException;
|
|
|
+import com.middle.platform.data.api.constant.TopicType;
|
|
|
import com.middle.platform.data.api.feign.DynamicTopicApi;
|
|
|
+import com.middle.platform.data.api.pojo.TopicDto;
|
|
|
import com.middle.platform.manage.biz.constant.UrlCategory;
|
|
|
import com.middle.platform.manage.biz.constant.UrlInit;
|
|
|
import com.middle.platform.manage.biz.constant.UrlProtocol;
|
|
|
+import com.middle.platform.manage.biz.constant.UrlType;
|
|
|
import com.middle.platform.manage.biz.domain.req.IotUrlPara;
|
|
|
import com.middle.platform.manage.biz.entity.IotProduct;
|
|
|
import com.middle.platform.manage.biz.entity.IotUrl;
|
|
|
import com.middle.platform.manage.biz.mapper.IotUrlMapper;
|
|
|
+import jakarta.annotation.Resource;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
/**
|
|
|
@@ -24,7 +31,10 @@ import java.util.Objects;
|
|
|
@RequiredArgsConstructor
|
|
|
public class IotUrlService {
|
|
|
private final IotUrlMapper iotUrlMapper;
|
|
|
+ @Resource
|
|
|
+ private IotProductService iotProductService;
|
|
|
private final DynamicTopicApi dynamicTopicApi;
|
|
|
+ private String topicPrefix = "/device/%s/*/";
|
|
|
|
|
|
/**
|
|
|
* 新增topic
|
|
|
@@ -33,9 +43,8 @@ public class IotUrlService {
|
|
|
* @return
|
|
|
*/
|
|
|
public Object saveUrl(IotUrlPara iotUrlPara) {
|
|
|
- if (Objects.isNull(iotUrlPara.getProductId())) {
|
|
|
- throw new BusinessException("产品id不能为空");
|
|
|
- }
|
|
|
+ //topi规则校验
|
|
|
+ checkTopic(iotUrlPara);
|
|
|
IotUrl iotUrl = new IotUrl();
|
|
|
iotUrl.setProductId(iotUrlPara.getProductId());
|
|
|
iotUrl.setCategory(iotUrlPara.getCategory());
|
|
|
@@ -46,20 +55,49 @@ public class IotUrlService {
|
|
|
iotUrl.setRemark(iotUrlPara.getRemark());
|
|
|
iotUrlMapper.insert(iotUrl);
|
|
|
//新增订阅的topic
|
|
|
-// dynamicTopicApi
|
|
|
+ if (Objects.equals(iotUrlPara.getType(), UrlType.MQTT) && (Objects.equals(iotUrlPara.getPermission(), TopicType.PUB) || Objects.equals(iotUrlPara.getPermission(), TopicType.BOTH))) {
|
|
|
+ dynamicTopicApi.saveTopic(new TopicDto(iotUrlPara.getUrl(), 0));
|
|
|
+ }
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 校验topic
|
|
|
+ *
|
|
|
+ * @param iotUrlPara
|
|
|
+ */
|
|
|
+ private void checkTopic(IotUrlPara iotUrlPara) {
|
|
|
+ //校验前缀是否为 /device/{productKey}/*/
|
|
|
+ IotProduct query = iotProductService.query(iotUrlPara.getProductId());
|
|
|
+ if (Objects.isNull(query)) {
|
|
|
+ throw new BusinessException("产品不存在");
|
|
|
+ }
|
|
|
+ //产品key
|
|
|
+ String code = query.getCode();
|
|
|
+ if (!StringUtils.startsWith(iotUrlPara.getUrl(), String.format(topicPrefix, code))) {
|
|
|
+ throw new BusinessException("topic必须以+" + String.format(topicPrefix, code) + "为前缀");
|
|
|
+ }
|
|
|
+ //todo topic合规校验
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 修改topic
|
|
|
*
|
|
|
* @param iotUrlPara
|
|
|
* @return
|
|
|
*/
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
public Object update(IotUrlPara iotUrlPara) {
|
|
|
if (Objects.isNull(iotUrlPara.getId())) {
|
|
|
throw new BusinessException("id不能为空");
|
|
|
}
|
|
|
+ checkTopic(iotUrlPara);
|
|
|
+ IotUrl iotUrlCheck = iotUrlMapper.selectById(iotUrlPara.getId());
|
|
|
+ if (Objects.isNull(iotUrlCheck)) {
|
|
|
+ throw new BusinessException("设备不存在");
|
|
|
+ }
|
|
|
+
|
|
|
IotUrl iotUrl = new IotUrl();
|
|
|
iotUrl.setId(iotUrlPara.getId());
|
|
|
iotUrl.setCategory(iotUrlPara.getCategory());
|
|
|
@@ -69,6 +107,12 @@ public class IotUrlService {
|
|
|
iotUrl.setType(iotUrlPara.getType());
|
|
|
iotUrl.setRemark(iotUrlPara.getRemark());
|
|
|
iotUrlMapper.updateById(iotUrl);
|
|
|
+ //取消订阅原topic 订阅topic
|
|
|
+ if (Objects.equals(iotUrlPara.getType(), UrlType.MQTT) && (Objects.equals(iotUrlPara.getPermission(), TopicType.PUB) || Objects.equals(iotUrlPara.getPermission(), TopicType.BOTH))) {
|
|
|
+ dynamicTopicApi.removeTopic(new TopicDto(iotUrlPara.getUrl(), 0));
|
|
|
+ dynamicTopicApi.saveTopic(new TopicDto(iotUrlPara.getUrl(), 0));
|
|
|
+
|
|
|
+ }
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
@@ -79,7 +123,18 @@ public class IotUrlService {
|
|
|
* @return
|
|
|
*/
|
|
|
public Object remove(Long id) {
|
|
|
- iotUrlMapper.deleteById(id);
|
|
|
+ IotUrl iotUrlCheck = iotUrlMapper.selectById(id);
|
|
|
+ if (Objects.isNull(iotUrlCheck)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ IotUrl iotUrl = new IotUrl();
|
|
|
+ iotUrl.setDeleteTime(LocalDateTime.now());
|
|
|
+ iotUrl.setDelFlag(Global.DEL);
|
|
|
+ iotUrlMapper.update(iotUrl, new LambdaQueryWrapper<IotUrl>().eq(IotUrl::getId, id).eq(IotUrl::getDelFlag, Global.UN_DEL));
|
|
|
+ //取消订阅
|
|
|
+ if (Objects.equals(iotUrlCheck.getType(), UrlType.MQTT) && (Objects.equals(iotUrlCheck.getPermission(), TopicType.PUB) || Objects.equals(iotUrlCheck.getPermission(), TopicType.BOTH))) {
|
|
|
+ dynamicTopicApi.removeTopic(new TopicDto(iotUrlCheck.getUrl(), 0));
|
|
|
+ }
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
@@ -91,7 +146,7 @@ public class IotUrlService {
|
|
|
* @return
|
|
|
*/
|
|
|
public Object getUrl(Long productId, Integer type) {
|
|
|
- return iotUrlMapper.selectList(new LambdaQueryWrapper<IotUrl>().eq(IotUrl::getProductId, productId).eq(IotUrl::getType, type));
|
|
|
+ return iotUrlMapper.selectList(new LambdaQueryWrapper<IotUrl>().eq(IotUrl::getProductId, productId).eq(IotUrl::getType, type).eq(IotUrl::getDelFlag, Global.UN_DEL));
|
|
|
}
|
|
|
|
|
|
|