ustcyc
2025-01-07 de5d55508afd27fb2b47e6d4d6fd9984525c222c
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
package com.zhitan.web.controller.realtimedata;
 
import com.zhitan.common.core.controller.BaseController;
import com.zhitan.common.core.domain.AjaxResult;
import com.zhitan.model.domain.EnergyIndex;
import com.zhitan.model.service.IEnergyIndexService;
import com.zhitan.realtimedata.domain.TagValue;
import com.zhitan.realtimedata.domain.TagValueResult;
import com.zhitan.realtimedata.service.RealtimeDatabaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@Controller
@RequestMapping("/rtdb")
public class RealtimeDataController extends BaseController {
 
  @Autowired
  private RealtimeDatabaseService realtimeDatabaseService;
 
  @Autowired
  private IEnergyIndexService energyIndexService;
 
  @GetMapping("/retrieve/{tagCodes}")
  @ResponseBody
  public AjaxResult getLiveData(@PathVariable("tagCodes") String[] tagCodes) {
    List<String> codes = new ArrayList<>(Arrays.asList(tagCodes));
    List<TagValue> tagValues = realtimeDatabaseService.retrieve(codes);
    List<EnergyIndex> energyIndices = energyIndexService.getEnergyIndexMeterByCodes(codes);
    Map<String, EnergyIndex> mapValue = energyIndices.stream()
        .collect(Collectors.toMap(EnergyIndex::getCode, m -> m));
    List<TagValueResult> results = new ArrayList<>();
    if (!tagValues.isEmpty()) {
      tagValues.forEach(value -> {
        TagValueResult result = new TagValueResult();
        if (mapValue.containsKey(value.getTagCode())) {
          EnergyIndex energyIndex = mapValue.get(value.getTagCode());
          result.setTagName(energyIndex.getName());
          result.setUnitId(energyIndex.getUnitId());
          result.setMeteName(energyIndex.getMeterName());
        }
 
        result.setTagCode(value.getTagCode());
        result.setValue(value.getValue());
        result.setDataTime(value.getDataTime());
        result.setQuality(value.getQuality());
        results.add(result);
      });
    }
    return AjaxResult.success(results);
  }
}