yaowanxin
6 天以前 f225c678db097194de435851db553d78d17f0ed6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.ruoyi.device.controller;
 
 
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 org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.List;
 
 
@RestController
@RequestMapping("/weight")
public class WeightController {
 
    @PostMapping("/handleWeights")
    public Result handleWeights(@RequestBody WeightRequestDto weightRequest) {
            List<Double> weightList = weightRequest.getWeights();
            // 在这里添加数据处理逻辑,例如打印、存储到数据库等
            System.out.println("接收到的数据: " + weightList);
            processValueDataStream(weightList, WeightRequestDto.DENSITY);
            return Result.success();
    }
    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);
    }
}