yanderong
6 天以前 3ad5f38c2f98eb320a1860c51cc5e2c92c7b7469
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.ruoyi.other.util;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.other.domain.TPlatformConfig;
import com.ruoyi.other.service.TPlatformConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
 
/**
 * 华为:光伏数据对接
 *
 * @author Lux
 * @version 1.0.0
 * @since 2026/9/7 17:38
 */
@Slf4j
@Service
public class HuWeiUtils {
 
    private static final String API_HOST = "https://cn.fusionsolar.huawei.com";
    private static final String DEFAULT_USER_NAME = "mxxnycd";
    private static final String DEFAULT_SYSTEM_CODE = "mxxny888888";
 
    private static final String LOGIN_PATH = "/thirdData/login";
    private static final String STATIONS_PATH = "/thirdData/stations";
    private static final String DEV_LIST_PATH = "/thirdData/getDevList";
    private static final String Kpi_STATION_DAY_PATH = "/thirdData/getKpiStationDay";
 
    @Resource
    private RestTemplate restTemplate;
 
    @Resource
    private TPlatformConfigService platformConfigService;
 
    /** 登录华为 FusionSolar 并取得 XSRF-TOKEN。 */
    public String getXsrfToken() {
        TPlatformConfig config = platformConfigService.getOne(Wrappers.lambdaQuery(TPlatformConfig.class).eq(TPlatformConfig::getPlatformName, "华为"));
        if (config == null || StringUtils.isBlank(config.getAccount()) || StringUtils.isBlank(config.getPassword())) {
            return getXsrfToken(DEFAULT_USER_NAME, DEFAULT_SYSTEM_CODE);
        }
        return getXsrfToken(config.getAccount(), config.getPassword());
    }
 
    /** 使用指定账号登录并取得 XSRF-TOKEN。 */
    public String getXsrfToken(String userName, String systemCode) {
        JSONObject body = new JSONObject();
        body.put("userName", userName);
        body.put("systemCode", systemCode);
        HttpHeaders headers = new HttpHeaders();
        headers.set(HttpHeaders.CONTENT_TYPE, "application/json");
        ResponseEntity<String> response = restTemplate.exchange(API_HOST + LOGIN_PATH, HttpMethod.POST,
                new HttpEntity<>(body.toJSONString(), headers), String.class);
        String token = response.getHeaders().getFirst("XSRF-TOKEN");
        if (token == null) {
            List<String> cookies = response.getHeaders().get(HttpHeaders.SET_COOKIE);
            if (cookies != null) {
                for (String cookie : cookies) {
                    if (cookie.startsWith("XSRF-TOKEN=")) {
                        token = cookie.substring("XSRF-TOKEN=".length()).split(";", 2)[0];
                        break;
                    }
                }
            }
        }
        if (token == null || token.trim().isEmpty()) {
            throw new IllegalStateException("华为登录未返回XSRF-TOKEN");
        }
        return token;
    }
 
 
    /**
     * 获取电站列表
     *
     * @param pageNo 当前页
     * @param token 登陆后的token
     * @return 列表数据
     */
    public JSONArray getStations(Integer pageNo, String token) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);
        headers.setAccept(Collections.singletonList(org.springframework.http.MediaType.APPLICATION_JSON));
        headers.set("XSRF-TOKEN", token);
        headers.set(HttpHeaders.COOKIE, "XSRF-TOKEN=" + token);
        JSONObject body = new JSONObject();
        body.put("pageNo", pageNo == null || pageNo < 1 ? 1 : pageNo);
        ResponseEntity<String> response = restTemplate.exchange(API_HOST + STATIONS_PATH, HttpMethod.POST,
                new HttpEntity<>(body.toJSONString(), headers), String.class);
        JSONObject result = JSON.parseObject(response.getBody());
        if (result == null || !Boolean.TRUE.equals(result.getBoolean("success"))) {
            throw new IllegalStateException("华为电站列表接口调用失败:" + (result == null ? "空响应" : result.getString("message")));
        }
        JSONObject data = result.getJSONObject("data");
        return data == null || data.getJSONArray("list") == null ? new JSONArray() : data.getJSONArray("list");
    }
 
 
    /**
     * 获取设备列表
     *
     * @param stationCodes 电站ID,多个用逗号分隔
     * @param token 登陆后的token
     * @return 列表数据
     */
    public JSONArray getDevList(String stationCodes, String token) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);
        headers.setAccept(Collections.singletonList(org.springframework.http.MediaType.APPLICATION_JSON));
        headers.set("XSRF-TOKEN", token);
        headers.set(HttpHeaders.COOKIE, "XSRF-TOKEN=" + token);
        JSONObject body = new JSONObject();
        body.put("stationCodes", stationCodes);
        ResponseEntity<String> response = restTemplate.exchange(API_HOST + DEV_LIST_PATH, HttpMethod.POST,
                new HttpEntity<>(body.toJSONString(), headers), String.class);
        JSONObject result = JSON.parseObject(response.getBody());
        if (result == null || !Boolean.TRUE.equals(result.getBoolean("success"))) {
            throw new IllegalStateException("华为设备列表接口调用失败:" + (result == null ? "空响应" : result.getString("message")));
        }
        JSONArray dataList = result.getJSONArray("data");
        return dataList == null ? new JSONArray() : dataList;
    }
 
    /**
     * 获取设备列表
     *
     * @param stationCodes 电站ID,多个用逗号分隔
     * @param token 登陆后的token
     * @return 列表数据
     */
    public JSONArray getKpiStationDay(String stationCodes, Long collectTime, String token) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);
        headers.setAccept(Collections.singletonList(org.springframework.http.MediaType.APPLICATION_JSON));
        headers.set("XSRF-TOKEN", token);
        headers.set(HttpHeaders.COOKIE, "XSRF-TOKEN=" + token);
        JSONObject body = new JSONObject();
        body.put("stationCodes", stationCodes);
        body.put("collectTime", collectTime);
        ResponseEntity<String> response = restTemplate.exchange(API_HOST + Kpi_STATION_DAY_PATH, HttpMethod.POST,
                new HttpEntity<>(body.toJSONString(), headers), String.class);
        JSONObject result = JSON.parseObject(response.getBody());
        if (result == null || !Boolean.TRUE.equals(result.getBoolean("success"))) {
            throw new IllegalStateException("华为电站列表接口调用失败:" + (result == null ? "空响应" : result.getString("message")));
        }
        JSONArray dataList = result.getJSONArray("data");
        return dataList == null ? new JSONArray() : dataList;
    }
 
}