TRX 1 жил өмнө
parent
commit
7da5d4538b

+ 13 - 0
OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/iot/IotMainModel.java

@@ -5,6 +5,7 @@ import com.zhongshu.iot.client.model.baseParam.SuperModel;
 import com.zhongshu.iot.client.type.DataType;
 import com.zhongshu.iot.client.type.EventType;
 import com.zhongshu.iot.client.type.OperationType;
+import com.zhongshu.iot.client.type.RemoteUrlType;
 import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.AllArgsConstructor;
 import lombok.Data;
@@ -124,6 +125,18 @@ public class IotMainModel extends SuperModel {
 
     //--------------------------------关联设备时特有的属性 start------------------
 
+    @Schema(description = "业务地址类型")
+    private RemoteUrlType remoteUrlType;
+
+    private String remoteUrlTypeStr;
+
+    public String getRemoteUrlTypeStr() {
+        if (remoteUrlType != null) {
+            return remoteUrlType.getRemark();
+        }
+        return "";
+    }
+
     @Schema(description = "业务处理地址")
     private String remoteUrl;
 

+ 4 - 0
OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/iot/IotMainSearch.java

@@ -4,6 +4,7 @@ import com.github.microservice.types.FunctionType;
 import com.zhongshu.iot.client.model.baseParam.SuperSearchParam;
 import com.zhongshu.iot.client.type.DataType;
 import com.zhongshu.iot.client.type.IotDataType;
+import com.zhongshu.iot.client.type.RemoteUrlType;
 import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.AllArgsConstructor;
 import lombok.Data;
@@ -47,4 +48,7 @@ public class IotMainSearch extends SuperSearchParam {
 
     @Schema(description = "物模型类型")
     private IotDataType iotDataType;
+
+    @Schema(description = "业务地址类型")
+    private RemoteUrlType remoteUrlType;
 }

+ 19 - 0
OneCardIotClient/src/main/java/com/zhongshu/iot/client/type/RemoteUrlType.java

@@ -0,0 +1,19 @@
+package com.zhongshu.iot.client.type;
+
+import lombok.Getter;
+
+/**
+ * 业务地址类型
+ */
+public enum RemoteUrlType {
+    Intranet("内网"),
+    ExternalNetwork("外网"),
+    ;
+
+    @Getter
+    private String remark;
+
+    RemoteUrlType(String remark) {
+        this.remark = remark;
+    }
+}

+ 4 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/dao/iot/impl/IotMainDaoImpl.java

@@ -66,6 +66,10 @@ public class IotMainDaoImpl extends BaseImpl implements IotMainDaoExtend {
             criteria.and("gateWayId").is(param.getGateWayId());
         }
 
+        if (param.getRemoteUrlType() != null) {
+            criteria.and("remoteUrlType").is(param.getRemoteUrlType());
+        }
+
         // 模糊搜索
         List<Criteria> criterias = new ArrayList<>();
         if (StringUtils.isNotEmpty(param.getName())) {

+ 5 - 1
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/domain/iot/IotMain.java

@@ -6,6 +6,7 @@ import com.zhongshu.iot.client.model.iot.IotDict;
 import com.zhongshu.iot.client.type.DataType;
 import com.zhongshu.iot.client.type.EventType;
 import com.zhongshu.iot.client.type.IotDataType;
+import com.zhongshu.iot.client.type.RemoteUrlType;
 import com.zhongshu.iot.server.core.domain.base.SuperEntity;
 import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.AllArgsConstructor;
@@ -67,6 +68,9 @@ public class IotMain extends SuperEntity {
 
     //---------------------------------业务平台的地址 start ---------------------
 
+    @Schema(description = "业务地址类型")
+    private RemoteUrlType remoteUrlType = RemoteUrlType.ExternalNetwork;
+
     @Schema(description = "业务处理地址")
     private String remoteUrl;
 
@@ -75,7 +79,7 @@ public class IotMain extends SuperEntity {
 
     @Schema(description = "业务访问sk")
     private String sk;
-    
+
     //---------------------------------属性类型的  字段  start ------------------
     @Schema(description = "数据类型,number、Boolean、String 等")
     private DataType dataType;

+ 18 - 6
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/httpRequest/ApiRequestService.java

@@ -1,6 +1,7 @@
 package com.zhongshu.iot.server.core.httpRequest;
 
 import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.json.JSONUtil;
 import com.github.microservice.core.util.net.apache.MethodType;
 import com.github.microservice.http.APIResponseModel;
 import com.github.microservice.http.ApiConfParam;
@@ -12,6 +13,7 @@ import com.zhongshu.iot.server.core.service.other.RequestInfoService;
 import com.zhongshu.iot.server.core.util.net.apache.HttpClientUtil;
 import com.zhongshu.iot.server.core.util.net.apache.HttpModel;
 import com.zhongshu.iot.server.core.util.net.apache.ResponseModel;
+import com.zhongshu.opengateway.client.util.SignUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -19,6 +21,8 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.util.StopWatch;
 
+import java.util.HashMap;
+
 /**
  * @author TRX
  * @date 2024/6/25
@@ -59,8 +63,7 @@ public class ApiRequestService extends SuperService {
             StopWatch stopWatch = new StopWatch();
             stopWatch.start();
             String url = fullCardConf.getUrl() + fullCardConf.getCardServerName() + apiConfParam.getApiName();
-            ResponseModel request = HttpClientUtil.request(HttpModel.builder()
-                    .url(url).method(MethodType.Json).charset("utf-8").body(data).build());
+            ResponseModel request = HttpClientUtil.request(HttpModel.builder().url(url).method(MethodType.Json).charset("utf-8").body(data).build());
             if (request.getCode() == 200) {
                 responseModel = BeanUtil.copyProperties(request.getBody(), APIResponseModel.class);
             } else {
@@ -79,17 +82,27 @@ public class ApiRequestService extends SuperService {
         return responseModel;
     }
 
-
     public APIResponseModel requestAPI(String url, Object data) {
+        return requestAPI(url, data, "", "");
+    }
+
+    public APIResponseModel requestAPI(String url, Object data, String ak, String sk) {
         APIResponseModel responseModel = new APIResponseModel();
         try {
+            HashMap<String, Object> headers = new HashMap<>();
+            // 签名
+            if (StringUtils.isNotEmpty(ak) && StringUtils.isNotEmpty(sk)) {
+                String authorization = SignUtil.sign(JSONUtil.toJsonStr(data), ak, sk);
+                headers.put("Authorization", authorization);
+            }
             ApiConfParam apiConfParam = new ApiConfParam();
             apiConfParam.setApiName(url);
             apiConfParam.setMethodType(MethodType.Json.name());
             StopWatch stopWatch = new StopWatch();
             stopWatch.start();
-            ResponseModel request = HttpClientUtil.request(HttpModel.builder()
-                    .url(url).method(MethodType.Json).charset("utf-8").body(data).build());
+            ResponseModel request = HttpClientUtil.request(HttpModel.builder().url(url)
+                    .header(headers)
+                    .method(MethodType.Json).charset("utf-8").body(data).build());
             int code = request.getCode();
             if (code == 200) {
                 responseModel = BeanUtil.copyProperties(request.getBody(), APIResponseModel.class);
@@ -110,5 +123,4 @@ public class ApiRequestService extends SuperService {
         requestInfoService.addRequestInfo(data, responseModel);
         return responseModel;
     }
-
 }

+ 1 - 1
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/artemis/OperationMessageService.java

@@ -493,7 +493,7 @@ public class OperationMessageService {
                     stopWatch.start();
 
                     // 请求业务端处理
-                    APIResponseModel apiResponseModel = apiRequestService.requestAPI(remoteUrl, data);
+                    APIResponseModel apiResponseModel = apiRequestService.requestAPI(remoteUrl, data, iotMain.getAk(), iotMain.getSk());
                     messageResult.setResultData(apiResponseModel);
                     if (apiResponseModel.isSuccess()) {
                         // 处理成功

+ 1 - 1
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/device/GateWayInfoService.java

@@ -323,8 +323,8 @@ public class GateWayInfoService extends SuperService {
                     } else {
                     }
                     gateWay2Device.setGateWayInfo(gateWayInfo);
-                    gateWay2Device.setDeviceInfo(deviceInfo);
                     gateWay2Device.setGateWayId(gateWayInfo.getGateWayId());
+                    gateWay2Device.setDeviceInfo(deviceInfo);
                     gateWay2Device.setDeviceId(deviceInfo.getDeviceId());
 
                     gateWay2Device.setBindTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));

+ 9 - 3
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/iot/IotServiceImpl.java

@@ -434,16 +434,22 @@ public class IotServiceImpl extends SuperService {
             String replyRealIotTopic = replyIotTopic;
             if (StringUtils.isNotEmpty(iotTopic)) {
                 realIotTopic = realIotTopic.replaceAll(Pattern.quote("${deviceId}"), deviceId);
+            }
+            if (StringUtils.isNotEmpty(replyIotTopic)) {
                 replyRealIotTopic = replyRealIotTopic.replaceAll(Pattern.quote("${deviceId}"), deviceId);
             }
+
             DeviceInfo deviceInfo = deviceInfoDao.findTopByDeviceId(deviceId);
             if (ObjectUtils.isNotEmpty(deviceInfo)) {
                 GateWay2Device gateWay2Device = gateWay2DeviceDao.findTopByDeviceInfoOrderByUpdateTimeDesc(deviceInfo);
                 if (ObjectUtils.isNotEmpty(gateWay2Device)) {
                     String gateWayId = gateWay2Device.getGateWayInfo().getGateWayId();
-                    realIotTopic = realIotTopic.replaceAll(Pattern.quote("${gateWayId}"), gateWayId);
-                    replyRealIotTopic = replyRealIotTopic.replaceAll(Pattern.quote("${gateWayId}"), gateWayId);
-
+                    if (StringUtils.isNotEmpty(iotTopic)) {
+                        realIotTopic = realIotTopic.replaceAll(Pattern.quote("${gateWayId}"), gateWayId);
+                    }
+                    if (StringUtils.isNotEmpty(replyRealIotTopic)) {
+                        replyRealIotTopic = replyRealIotTopic.replaceAll(Pattern.quote("${gateWayId}"), gateWayId);
+                    }
                     iotMain.setGateWayId(gateWayId);
                 }
             }