From 7c5e1ef7db84731610d5616b2ee7e6e63abd886d Mon Sep 17 00:00:00 2001 From: zouyu <2723363702@qq.com> Date: 星期五, 01 八月 2025 13:21:02 +0800 Subject: [PATCH] mqtt回调方法调整 --- cnas-device/src/main/java/com/ruoyi/device/controller/WeightController.java | 57 ++++++++++++++++++++++++++++++++++----------------------- 1 files changed, 34 insertions(+), 23 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 8c3dfd0..6e51c4c 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 @@ -1,10 +1,14 @@ package com.ruoyi.device.controller; +import cn.hutool.core.collection.CollectionUtil; 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; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiOperation; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -12,37 +16,44 @@ import org.springframework.web.bind.annotation.RestController; import java.util.List; +import java.util.Objects; @RestController @RequestMapping("/weight") public class WeightController { + @ApiOperation("淇濆瓨鍒嗘瀽澶╁钩璐ㄩ噺鏁版嵁鍒癛edis") @PostMapping("/handleWeights") - public ResponseEntity<String> handleWeights(@RequestBody WeightRequestDto weightRequest) { - try { - List<Double> weightList = weightRequest.getWeights(); - // 鍦ㄨ繖閲屾坊鍔犳暟鎹鐞嗛�昏緫锛屼緥濡傛墦鍗般�佸瓨鍌ㄥ埌鏁版嵁搴撶瓑 - System.out.println("鎺ユ敹鍒扮殑鏁版嵁: " + weightList); - processValueDataStream(weightList, WeightRequestDto.DENSITY); - - // 澶勭悊鎴愬姛杩斿洖 200 鐘舵�佺爜鍜屾秷鎭� - return ResponseEntity.ok("鏁版嵁澶勭悊鎴愬姛"); - } catch (Exception e) { - // 澶勭悊澶辫触杩斿洖 500 鐘舵�佺爜鍜岄敊璇秷鎭� - return ResponseEntity.internalServerError().body("鏁版嵁澶勭悊澶辫触: " + e.getMessage()); + public Result<String> handleWeights(@RequestBody WeightRequestDto weightRequest) { +// try { +// List<Double> weightList = weightRequest.getWeights(); +// // 鍦ㄨ繖閲屾坊鍔犳暟鎹鐞嗛�昏緫锛屼緥濡傛墦鍗般�佸瓨鍌ㄥ埌鏁版嵁搴撶瓑 +// System.out.println("鎺ユ敹鍒扮殑鏁版嵁: " + weightList); +// processValueDataStream(weightList, WeightRequestDto.DENSITY); +// +// // 澶勭悊鎴愬姛杩斿洖 200 鐘舵�佺爜鍜屾秷鎭� +// return ResponseEntity.ok("鏁版嵁澶勭悊鎴愬姛"); +// } catch (Exception e) { +// // 澶勭悊澶辫触杩斿洖 500 鐘舵�佺爜鍜岄敊璇秷鎭� +// return ResponseEntity.internalServerError().body("鏁版嵁澶勭悊澶辫触: " + e.getMessage()); +// } + if(Objects.nonNull(weightRequest) && CollectionUtil.isNotEmpty(weightRequest.getWeights())){ + RedisUtil.lSet(WeightRequestDto.DENSITY,weightRequest.getWeights()); + return Result.success(null,"淇濆瓨鎴愬姛"); } + return Result.fail(); } - private void processValueDataStream(List<Double> weightList, String dataStream) { - // 澶勭悊 weightList 涓� null 鐨勬儏鍐� - if (weightList == null) { - // 鑻� weightList 涓� null锛屽彲閫夋嫨鍒犻櫎 Redis 涓殑瀵瑰簲閿垨瀛樺叆绌烘暟缁勶紝杩欓噷閫夋嫨瀛樺叆绌烘暟缁� - RedisUtil.set(dataStream, "[]"); - return; - } - // 灏� weightList 杞崲涓� JSON 瀛楃涓插苟瀛樺叆 Redis - String jsonStr = JSONArray.toJSONString(weightList); - RedisUtil.set(dataStream, jsonStr); - } +// private void processValueDataStream(List<Double> weightList, String dataStream) { +// // 澶勭悊 weightList 涓� null 鐨勬儏鍐� +// if (weightList == null) { +// // 鑻� weightList 涓� null锛屽彲閫夋嫨鍒犻櫎 Redis 涓殑瀵瑰簲閿垨瀛樺叆绌烘暟缁勶紝杩欓噷閫夋嫨瀛樺叆绌烘暟缁� +// RedisUtil.set(dataStream, "[]"); +// return; +// } +// // 灏� weightList 杞崲涓� JSON 瀛楃涓插苟瀛樺叆 Redis +// String jsonStr = JSONArray.toJSONString(weightList); +// RedisUtil.set(dataStream, jsonStr); +// } } -- Gitblit v1.9.3