Ver código fonte

质检-断面仪
1、推送接口开发(成功)

LHB 6 dias atrás
pai
commit
32e467a36c

+ 52 - 51
blade-service/blade-manager/src/main/java/org/springblade/manager/service/impl/ProfilerOffsetServiceImpl.java

@@ -1,8 +1,11 @@
 package org.springblade.manager.service.impl;
 
+import cn.hutool.core.date.DateTime;
 import cn.hutool.core.util.URLUtil;
+import cn.hutool.http.HttpException;
 import cn.hutool.http.HttpRequest;
 import cn.hutool.http.HttpUtil;
+import cn.hutool.json.JSONConfig;
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
 import com.alibaba.nacos.common.utils.MD5Utils;
@@ -10,6 +13,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.codec.digest.HmacAlgorithms;
 import org.apache.commons.codec.digest.HmacUtils;
 import org.apache.commons.lang.RandomStringUtils;
@@ -29,6 +33,7 @@ import org.springblade.manager.service.ProfilerOffsetService;
 import org.springblade.manager.mapper.ProfilerOffsetMapper;
 import org.springblade.manager.service.ProfilerStandardSectionBeanService;
 import org.springblade.resource.feign.NewIOSSClient;
+import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.multipart.MultipartFile;
@@ -44,6 +49,7 @@ import java.util.*;
  * @createDate 2025-10-27 15:58:56
  */
 @Service
+@Slf4j
 public class ProfilerOffsetServiceImpl extends ServiceImpl<ProfilerOffsetMapper, ProfilerOffset>
         implements ProfilerOffsetService {
 
@@ -207,24 +213,25 @@ public class ProfilerOffsetServiceImpl extends ServiceImpl<ProfilerOffsetMapper,
 
     @Override
     @Transactional(rollbackFor = Exception.class)
+    @Scheduled(cron = "0 0 20 ? * FRI")
     public void push() {
-        String baseUrl = "";
+        //测试地址 http://192.168.100.1:18010/prod-api/data/openapi/v1/push
+        String baseUrl = "http://192.168.100.1:18010/prod-api";
         String url = "/data/openapi/v1/push";
         //第三方提供的key
-        String clientKey = "";
-        String secret = "";
+        String clientKey = "tunnel-overBreak-1013";
+        String secret = "73c6e839-bc38-fd04-7e2e-e49c6ccc4b41";
         //推送接口 应用id
-        String protocolApiId = "1976105951096934401";
+        String protocolApiId = "1977938405343174658";
         //当前时间戳
         String secTimestamp = String.valueOf(System.currentTimeMillis());
-        //随机字符串
-        String secNonce = RandomStringUtils.randomAlphanumeric(15);
-
 
         //获取数据
         List<ProfilerOffset> list = this.list(Wrappers.<ProfilerOffset>lambdaQuery().eq(ProfilerOffset::getPush, 0));
-        List<ProfilerOffsetPush> resultDTO = new ArrayList<>();
+        log.info("开始推送断面仪数据------{}", DateTime.now());
         list.forEach(f -> {
+            //随机字符串 每次推送都是唯一的
+            String secNonce = UUID.randomUUID().toString();
             //获取测量断面
             List<ProfilerStandardSectionBean> sectionBeans = beanService.list(Wrappers.<ProfilerStandardSectionBean>lambdaQuery().eq(ProfilerStandardSectionBean::getOffsetId, f.getId()));
             List<ProfilerSectionPush> sectionPushes = ProfilerSectionPush.parameterMapping(sectionBeans);
@@ -236,53 +243,47 @@ public class ProfilerOffsetServiceImpl extends ServiceImpl<ProfilerOffsetMapper,
             ProfilerOffsetPush profilerOffsetPush = ProfilerOffsetPush.parameterMapping(f);
             profilerOffsetPush.setMeasurement_section(sectionPushes);
             profilerOffsetPush.setReceipt_data(dataPushes);
-            resultDTO.add(profilerOffsetPush);
-        });
-        JSONObject jsonObject = new JSONObject();
-        //
-        jsonObject.set("protocolApiId", protocolApiId);
-        jsonObject.set("data", JSONUtil.toJsonStr(resultDTO));
-
-        //数据json字符串
-        String catLog = jsonObject.toString();
-        //http对象
-        HttpRequest post = HttpUtil.createPost(baseUrl + url);
-        //获取签名字符串
-        String sign = buildStringToSign(post, secTimestamp, secNonce);
+            //设置不忽略空值
+            JSONConfig config = JSONConfig.create().setIgnoreNullValue(false);
+            JSONObject jsonObject = new JSONObject(config);
+            jsonObject.set("protocolApiId", protocolApiId);
+            jsonObject.set("data", profilerOffsetPush);
 
-        //请求头
-        HashMap<String, String> catLogHeaders = new HashMap<>();
-        catLogHeaders.put("Content-Type", "application/json");
-        //应用ID
-        catLogHeaders.put("Sec-API-Key", clientKey);
-        //当前时间 毫秒值
-        catLogHeaders.put("Sec-Timestamp", secTimestamp);
-        //请求发起时的随机字符串,需要保证唯一性
-        catLogHeaders.put("Sec-Nonce", secNonce);
-        //签名
-        catLogHeaders.put("Sec-Signature", new HmacUtils(HmacAlgorithms.HMAC_SHA_256, secret).hmacHex(sign));
+            //数据json字符串
+            String catLog = jsonObject.toString();
+            //http对象
+            HttpRequest post = HttpUtil.createPost(baseUrl + url);
+            //获取签名字符串
+            String sign = "POST_" + url + "_" + secTimestamp + "_" + secNonce;
+            //请求头
+            HashMap<String, String> catLogHeaders = new HashMap<>();
+            catLogHeaders.put("Content-Type", "application/json");
+            //应用ID
+            catLogHeaders.put("Sec-API-Key", clientKey);
+            //当前时间 毫秒值
+            catLogHeaders.put("Sec-Timestamp", secTimestamp);
+            //请求发起时的随机字符串,需要保证唯一性
+            catLogHeaders.put("Sec-Nonce", secNonce);
+            //签名
+            catLogHeaders.put("Sec-Signature", new HmacUtils(HmacAlgorithms.HMAC_SHA_256, secret).hmacHex(sign));
 
-        String catLogBody = post.addHeaders(catLogHeaders).body(catLog).contentType("application/json").execute().body();
-        //响应结果
-        JSONObject result = JSONUtil.parseObj(catLogBody);
-        if ("200".equals(result.getStr("code"))) {
-            list.forEach(f -> f.setPush(1));
-        } else {
-            list.forEach(f -> f.setPush(2));
-        }
+            try {
+                String catLogBody = post.addHeaders(catLogHeaders).body(catLog).contentType("application/json").execute().body();
+                //响应结果
+                JSONObject result = JSONUtil.parseObj(catLogBody);
+                log.info("推送结果:{}",result);
+                if ("200".equals(result.getStr("code"))) {
+                    f.setPush(1);
+                } else {
+                    f.setPush(2);
+                }
+            } catch (HttpException e) {
+                f.setPush(2);
+            }
+        });
+        log.info("推送完成------{}", DateTime.now());
         this.updateBatchById(list);
     }
-
-
-    private static String buildStringToSign(HttpRequest request, String timestamp, String nonce) {
-        return request.getMethod() +
-                "_" +
-                URLUtil.getPath(request.getUrl()) +
-                "_" +
-                timestamp +
-                "_" +
-                nonce;
-    }
 }