From f225c678db097194de435851db553d78d17f0ed6 Mon Sep 17 00:00:00 2001 From: yaowanxin <3588231647@qq.com> Date: 星期一, 04 八月 2025 09:11:34 +0800 Subject: [PATCH] 调整方法返回格式 --- cnas-device/src/main/java/com/ruoyi/device/controller/WeightController.java | 43 +++++++++++++------------------------------ 1 files changed, 13 insertions(+), 30 deletions(-) diff --git a/cnas-device/src/main/java/com/ruoyi/device/controller/WeightController.java b/cnas-device/src/main/java/com/ruoyi/device/controller/WeightController.java index 2838296..4590de3 100644 --- a/cnas-device/src/main/java/com/ruoyi/device/controller/WeightController.java +++ b/cnas-device/src/main/java/com/ruoyi/device/controller/WeightController.java @@ -2,6 +2,7 @@ import com.alibaba.fastjson.JSONArray; +import com.ruoyi.common.core.domain.Result; import com.ruoyi.common.utils.RedisUtil; import com.ruoyi.device.constant.DCResistanceMqttConstants; import com.ruoyi.device.dto.WeightRequestDto; @@ -19,40 +20,22 @@ public class WeightController { @PostMapping("/handleWeights") - public ResponseEntity<String> handleWeights(@RequestBody WeightRequestDto weightRequest) { - try { + public Result handleWeights(@RequestBody WeightRequestDto weightRequest) { List<Double> weightList = weightRequest.getWeights(); // 鍦ㄨ繖閲屾坊鍔犳暟鎹鐞嗛�昏緫锛屼緥濡傛墦鍗般�佸瓨鍌ㄥ埌鏁版嵁搴撶瓑 System.out.println("鎺ユ敹鍒扮殑鏁版嵁: " + weightList); - processThreeTimesValueDataStream(weightList, WeightRequestDto.DENSITY); - - // 澶勭悊鎴愬姛杩斿洖 200 鐘舵�佺爜鍜屾秷鎭� - return ResponseEntity.ok("鏁版嵁澶勭悊鎴愬姛"); - } catch (Exception e) { - // 澶勭悊澶辫触杩斿洖 500 鐘舵�佺爜鍜岄敊璇秷鎭� - return ResponseEntity.internalServerError().body("鏁版嵁澶勭悊澶辫触: " + e.getMessage()); - } + processValueDataStream(weightList, WeightRequestDto.DENSITY); + return Result.success(); } - - private void processThreeTimesValueDataStream(List<Double> weightList, String dataStream) { - // 浠� Redis 鑾峰彇宸插瓨鍌ㄧ殑鍊� - Object valueFromRedis = RedisUtil.get(dataStream); - JSONArray valueArray = new JSONArray(); - - - if (valueFromRedis != null) { - if (valueFromRedis instanceof String) { - try { - valueArray = JSONArray.parseArray((String) valueFromRedis); - } catch (Exception e) { - // 濡傛灉瑙f瀽澶辫触锛岃鏄� Redis 涓殑鍊煎彲鑳戒笉鏄悎娉曠殑 JSON 鏁扮粍锛屽垱寤虹┖鏁扮粍 - valueArray = new JSONArray(); - } - } else if (valueFromRedis instanceof Double) { - valueArray.add(valueFromRedis); - } + private void processValueDataStream(List<Double> weightList, String dataStream) { + // 澶勭悊 weightList 涓� null 鐨勬儏鍐� + if (weightList == null) { + // 鑻� weightList 涓� null锛屽彲閫夋嫨鍒犻櫎 Redis 涓殑瀵瑰簲閿垨瀛樺叆绌烘暟缁勶紝杩欓噷閫夋嫨瀛樺叆绌烘暟缁� + RedisUtil.set(dataStream, "[]"); + return; } - valueArray.addAll(weightList); - RedisUtil.set(dataStream, valueArray.toJSONString()); + // 灏� weightList 杞崲涓� JSON 瀛楃涓插苟瀛樺叆 Redis + String jsonStr = JSONArray.toJSONString(weightList); + RedisUtil.set(dataStream, jsonStr); } } -- Gitblit v1.9.3