zouyu
2 天以前 7c5e1ef7db84731610d5616b2ee7e6e63abd886d
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,47 +16,44 @@
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Objects;
@RestController
@RequestMapping("/weight")
public class WeightController {
    @ApiOperation("保存分析天平质量数据到Redis")
    @PostMapping("/handleWeights")
    public ResponseEntity<String> handleWeights(@RequestBody WeightRequestDto weightRequest) {
        try {
            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());
    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 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) {
                    // 如果解析失败,说明 Redis 中的值可能不是合法的 JSON 数组,创建空数组
                    valueArray = new JSONArray();
                }
            } else if (valueFromRedis instanceof Double) {
                valueArray.add(valueFromRedis);
            }
        }
        valueArray.addAll(weightList);
        RedisUtil.set(dataStream, valueArray.toJSONString());
    }
//    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);
//    }
}