6 小时以前 bfcc71f500625a26886b43c43a8fdf2ea1152844
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.ruoyi.http.service.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.device.pojo.DeviceLedger;
import com.ruoyi.device.service.IDeviceLedgerService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.http.service.impl.RealTimeEnergyConsumptionServiceImpl;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@RestController
@RequestMapping("/iot")
@Api(tags = "数采接口")
public class JclyController extends BaseController {
 
    @Autowired
    private RealTimeEnergyConsumptionServiceImpl realTimeEnergyConsumptionService;
 
    @Autowired
    private IDeviceLedgerService deviceLedgerService;
 
    /**
     * 实时获取温湿度,二氧化碳数据
     */
    @GetMapping("/getRealData")
    public AjaxResult getRealData() {
        List<DeviceLedger> iotDevices = deviceLedgerService.list(new LambdaQueryWrapper<DeviceLedger>()
                .eq(DeviceLedger::getIsIotDevice, 1)
                .isNotNull(DeviceLedger::getExternalCode)
                .ne(DeviceLedger::getExternalCode, ""));
 
        Map<String, DeviceLedger> guidDeviceMap = iotDevices.stream()
                .filter(item -> StringUtils.isNotEmpty(item.getExternalCode()))
                .collect(Collectors.toMap(
                        item -> item.getExternalCode().trim(),
                        item -> item,
                        (oldValue, newValue) -> StringUtils.isNotEmpty(oldValue.getDeviceName()) ? oldValue : newValue,
                        LinkedHashMap::new
                ));
        List<String> guidList = new ArrayList<>(guidDeviceMap.keySet());
 
        List<Map<String, String>> maps = realTimeEnergyConsumptionService
                .getRealData(guidList);
        for (Map<String, String> item : maps) {
            String guid = item.get("guid");
            if (StringUtils.isNotEmpty(guid)) {
                DeviceLedger device = guidDeviceMap.get(guid.trim());
                if (device != null) {
                    if (StringUtils.isNotEmpty(device.getDeviceName())) {
                        item.put("deviceName", device.getDeviceName().trim());
                    }
                    if (StringUtils.isNotEmpty(device.getStorageLocation())) {
                        item.put("storageLocation", device.getStorageLocation().trim());
                    }
                }
            }
        }
        // 按 deviceName 第一个字符(数字)排序
        maps.sort((a, b) -> {
            String nameA = a.getOrDefault("deviceName", "");
            String nameB = b.getOrDefault("deviceName", "");
            int firstNumA = extractFirstNumber(nameA);
            int firstNumB = extractFirstNumber(nameB);
            return Integer.compare(firstNumA, firstNumB);
        });
        return AjaxResult.success(maps);
    }
 
    /**
     * 获取历史数据
     */
    @GetMapping("/getHistoryData")
    public AjaxResult getHistoryData(@RequestParam(value = "guid") String guid,
                                     @RequestParam(value = "startTime") long startTime,
                                     @RequestParam(value = "endTime") long endTime) {
        List<Map<String, String>> maps = realTimeEnergyConsumptionService.getHistoryData(guid, startTime, endTime);
        return AjaxResult.success(maps);
    }
 
    /**
     * 提取字符串开头的数字
     */
    private int extractFirstNumber(String str) {
        if (str == null || str.isEmpty()) {
            return Integer.MAX_VALUE;
        }
        StringBuilder numStr = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (Character.isDigit(c)) {
                numStr.append(c);
            } else {
                break;
            }
        }
        if (numStr.length() == 0) {
            return Integer.MAX_VALUE;
        }
        return Integer.parseInt(numStr.toString());
    }
 
}