puzhibing
4 天以前 a0e03b646f2c444b0f09687d24e9995112b99017
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package com.ruoyi.other.util;
 
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.utils.SpringUtils;
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.stereotype.Service;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.*;
 
 
/**
 * 定时任务:储能放电情况 光伏发电情况
 */
@Slf4j
@Service
public class KsolarUtils {
 
 
    private static String token;
 
    private static String accessToken;
 
 
    private static List<String> stationIds= Arrays.asList("2025060710462417","2025060711331323","2025060715425437","2025060716010425");
 
    private static final String KSOLAR_URL = "http://111.230.136.62:8092";
 
    /**
     * 获取今日总发电量
     * @param time
     * @return
     */
    public static Double getTodayEnergy(String time) {
        try {
            HttpRequest get = HttpUtil.createGet("http://111.230.136.62:8092/station/list/earn");
            get.header("Authorization", "Bearer "+token);
            get.form("startDate", time);
            HttpResponse execute = get.execute();
            String body = execute.body();
            JSONObject jsonObject = JSONObject.parseObject(body);
            String code = jsonObject.get("code").toString();
            if(!"200".equals(code)){
                token = token();
                return getTodayEnergy(time);
            }
            Object data  = jsonObject.get("data");
            JSONObject jsonObject1 = JSONObject.parseObject(data.toString());
            Double earn = jsonObject1.getDouble("dayGeneration");
            return earn;
        } catch (Exception e) {
            e.printStackTrace();
            token = token();
            return getTodayEnergy(time);
        }
    }
    public static ArrayList<Map<String, Object>> getMonthEnergy(String time,String time1) {
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        try {
            HttpRequest get = HttpUtil.createGet("http://111.230.136.62:8092/protocol/user/statistic/elec/chart");
            get.header("Authorization", "Bearer "+token);
            get.form("stime", time);
            get.form("attribId", "201");
            HttpResponse execute = get.execute();
            String body = execute.body();
            if(body.contains("code")){
                token = token();
                return getMonthEnergy(time,time1);
            }
            JSONArray jsonArray = JSONArray.parseArray(body);
            for (Object o1 : jsonArray) {
                HashMap<String, Object> stringObjectHashMap = new HashMap<>();
                JSONObject jsonObject = JSONObject.parseObject(o1.toString()) ;
                stringObjectHashMap.put("time", jsonObject.getString("saveTime"));
                stringObjectHashMap.put("value", jsonObject.getString("attribValue"));
                list.add(stringObjectHashMap);
            }
            // 相同月份
            if(!time.equals(time1)){
                list.addAll(getMonthEnergy(time1,time1));
            }
 
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            token = token();
            return getMonthEnergy(time,time1);
        }
    }
 
 
 
    /**
     * 光伏累计
     * @return
     */
    public static Double getAllEnergy(String time) {
        try {
            HttpRequest get = HttpUtil.createGet("http://111.230.136.62:8092/station/list/earn");
            get.header("Authorization", "Bearer "+token);
            get.form("startDate", time);
            HttpResponse execute = get.execute();
            String body = execute.body();
            JSONObject jsonObject = JSONObject.parseObject(body);
            String code = jsonObject.get("code").toString();
            if(!"200".equals(code)){
                token = token();
                return getAllEnergy(time);
            }
            Object data  = jsonObject.get("data");
            JSONObject jsonObject1 = JSONObject.parseObject(data.toString());
            Double earn = jsonObject1.getDouble("totalGeneration");
            return earn;
        } catch (Exception e) {
            e.printStackTrace();
            token = token();
            return getAllEnergy(time);
        }
    }
 
 
    /**
     * 光伏碳减排
     * @return
     */
    public static BigDecimal getYearAllEnergy() {
        Double yearAllEnergy = getYearAllEnergy(stationIds.get(0));
        Double yearAllEnergy1 = getYearAllEnergy(stationIds.get(1));
        Double yearAllEnergy2 = getYearAllEnergy(stationIds.get(2));
        Double yearAllEnergy3 = getYearAllEnergy(stationIds.get(3));
        double v = yearAllEnergy + yearAllEnergy1 + yearAllEnergy2 + yearAllEnergy3;
        BigDecimal divide = new BigDecimal(v).multiply(new BigDecimal("0.1564")).divide(new BigDecimal("1000"), 2, RoundingMode.DOWN);
        return divide;
    }
 
 
 
    private static Double getYearAllEnergy(String stationId) {
        try {
            HttpRequest get = HttpUtil.createGet("http://111.230.136.62:8092/protocol/station/statistic/elec/chart");
            get.header("Authorization", "Bearer "+token);
            get.form("stationId", stationId);
            get.form("attribId", "202");
            get.form("stime",LocalDate.now().getYear()+"" );
            HttpResponse execute = get.execute();
            String body = execute.body();
            JSONArray jsonArray = JSONArray.parseArray(body);
            Object o1 = jsonArray.get(0);
            JSONObject jsonObject = JSONObject.parseObject(o1.toString());
            String code = jsonObject.get("attribValue").toString();
            Double earn = Double.valueOf(code);
            return earn;
        } catch (Exception e) {
            e.printStackTrace();
            token = token();
            return getYearAllEnergy(stationId);
        }
    }
 
 
 
    public static String token() {
        TPlatformConfig config = SpringUtils.getBean(TPlatformConfigService.class).getOne(Wrappers.lambdaQuery(TPlatformConfig.class).eq(TPlatformConfig::getPlatformName, "科士达"));
        if (config == null) {
            config = new TPlatformConfig();
        }
        if (StringUtils.isBlank(config.getAccount()) || StringUtils.isBlank(config.getPassword())) {
            config.setAccount("环岛光伏");
            config.setPassword("cR8GGwIXMI462A8Dfl9eXg==");
        }
        HttpRequest post = HttpUtil.createPost("http://111.230.136.62:8092/authentication/form");
        post.basicAuth("kstar","kstarSecret");
        post.form("username", config.getAccount());
        post.form("password", config.getPassword());
        HttpResponse execute = post.execute();
        String body = execute.body();
        JSONObject jsonObject = JSONObject.parseObject(body);
        String token = jsonObject.getString("token");
        JSONObject jsonObject1 = JSONObject.parseObject(token);
        String access_token = jsonObject1.getString("access_token");
        return  access_token;
    }
 
 
    public static String getAccessToken(){
        HttpRequest post = HttpRequest.post("https://www.zsdcloud.cn:8443/openApi/getAccessToken.action?accessId=87bde117-1f29-4371-8d4d-82324d17ddc3");
        HttpResponse execute = post.execute();
        String body = execute.body();
        JSONObject jsonObject = JSONObject.parseObject(body);
        return jsonObject.getString("result");
    }
 
 
    /**
     * 消纳数据
     * @return
     */
    public static String getElectricity(){
        String valueTotal = "0";
        String value = "0";
        HttpRequest post = HttpRequest.post("https://www.zsdcloud.cn:8443/openApi/equipment/select.action");
        post.form("token", accessToken);
        post.form("keyCode", "869916072417147");
        post.form("version", "V2");
        HttpResponse execute = post.execute();
        String body = execute.body();
        JSONObject jsonObject = JSONObject.parseObject(body);
        String string = jsonObject.get("statusCode").toString();
        if(!string.equals("200")){
           accessToken =  getAccessToken();
           return getElectricity();
 
        }
        String string1 = jsonObject.getString("result");
        JSONObject jsonObject1 = JSONObject.parseObject(string1);
        String realInfo = jsonObject1.getString("childList");
        JSONArray jsonArray = JSONArray.parseArray(realInfo);
        for (Object o : jsonArray) {
            JSONObject jsonObject2 = JSONObject.parseObject(o.toString());
            String string2 = jsonObject2.get("valueList").toString();
            JSONArray jsonArray1 = JSONArray.parseArray(string2);
            for (Object o1 : jsonArray1) {
                JSONObject jsonObject3 = JSONObject.parseObject(o1.toString());
                if(jsonObject3.get("Name").toString().equals("系统累计充电量")){
                    valueTotal=  jsonObject3.get("Value").toString();
                }
                if(jsonObject3.get("Name").toString().equals("系统累计放电量")){
                    value=  jsonObject3.get("Value").toString();
                }
            }
        }
        return valueTotal+"_"+value;
    }
 
    /**
     * 获取用户的发电量和收益统计
     */
    public static JSONObject getListEarn(String time, String token) {
        try {
            HttpRequest get = HttpUtil.createGet(KSOLAR_URL.concat("/station/list/earn"));
            get.header("Authorization", "Bearer "+token);
            if(StringUtils.isNotEmpty(time)) {
                get.form("startDate", time);
            }
            HttpResponse execute = get.execute();
            String body = execute.body();
            JSONObject jsonObject = JSONObject.parseObject(body);
            String code = jsonObject.get("code").toString();
            if("200".equals(code)){
                Object data  = jsonObject.get("data");
                return JSONObject.parseObject(data.toString());
            }
            return null;
        } catch (Exception e) {
            log.error("获取用户的发电量和收益统计-调用第三方接口报错:", e);
            return null;
        }
    }
 
    /**
     * 获取用户的电站状态统计
     */
    public static JSONObject getListStatus(String token) {
        try {
            HttpRequest get = HttpUtil.createGet(KSOLAR_URL.concat("/station/list/status"));
            get.header("Authorization", "Bearer "+token);
            HttpResponse execute = get.execute();
            String body = execute.body();
            JSONObject jsonObject = JSONObject.parseObject(body);
            String code = jsonObject.get("code").toString();
            if("200".equals(code)){
                Object data  = jsonObject.get("data");
                return JSONObject.parseObject(data.toString());
            }
           return null;
        } catch (Exception e) {
            log.error("获取用户的电站状态统计-调用第三方接口报错:", e);
            return null;
        }
    }
 
    /**
     * 电站列表信息
     */
    public static JSONArray getStationListInfo(Integer pageNumber, Integer pageSize, String token) {
        try {
            HttpRequest get = HttpUtil.createGet(KSOLAR_URL.concat("/station/list/info"));
            get.header("Authorization", "Bearer "+token);
            get.form("pageNumber", pageNumber);
            get.form("pageSize", pageSize);
            HttpResponse execute = get.execute();
            String body = execute.body();
            JSONObject jsonObject = JSONObject.parseObject(body);
            String code = jsonObject.get("code").toString();
            if("0".equals(code)){
                String rows = jsonObject.get("rows").toString();
                return JSONArray.parseArray(rows);
            }
            return null;
        } catch (Exception e) {
            log.error("电站列表信息-调用第三方接口报错:", e);
            return null;
        }
    }
 
 
    /**
     * 电站基本信息列表
     */
    public static JSONArray getStationListBase(Integer pageNumber, Integer pageSize, String token) {
        try {
            HttpRequest post = HttpUtil.createPost(KSOLAR_URL.concat("/station/list/base"));
            post.header("Authorization", "Bearer "+token);
            post.header("Content-Type", "application/json");
            JSONObject param = new JSONObject();
            param.put("pageNumber", pageNumber);
            param.put("pageSize", pageSize);
 
            HttpResponse execute = post.body(param.toJSONString()).execute();
            String body = execute.body();
            JSONObject jsonObject = JSONObject.parseObject(body);
            String code = jsonObject.get("code").toString();
            if("0".equals(code)){
                String rows = jsonObject.get("rows").toString();
                return JSONArray.parseArray(rows);
            }
            return null;
        } catch (Exception e) {
            log.error("电站列表信息-调用第三方接口报错:", e);
            return null;
        }
    }
 
 
    /**
     * 逆变器列表
     */
    public static JSONArray getCollectBaseList(Integer pageNumber, Integer pageSize, String token) {
        try {
            HttpRequest post = HttpUtil.createPost(KSOLAR_URL.concat("/collect/manager/base/list"));
            post.header("Authorization", "Bearer "+token);
            post.header("Content-Type", "application/json");
            JSONObject param = new JSONObject();
            param.put("pageNumber", pageNumber);
            param.put("pageSize", pageSize);
 
            HttpResponse execute = post.body(param.toJSONString()).execute();
            String body = execute.body();
            JSONObject jsonObject = JSONObject.parseObject(body);
            String code = jsonObject.get("code").toString();
            if("0".equals(code)){
                String rows = jsonObject.get("rows").toString();
                return JSONArray.parseArray(rows);
            }
            return null;
        } catch (Exception e) {
            log.error("逆变器列表-调用第三方接口报错:", e);
            return null;
        }
    }
 
    /**
     * 电站发电量和收益统计明细
     */
    public static JSONArray getStationElecList(String startDate, String endDate, String token) {
        try {
            HttpRequest get = HttpUtil.createGet(KSOLAR_URL.concat("/protocol/station/statistic/elec/detail"));
            get.header("Authorization", "Bearer "+token);
            if(StringUtils.isNotEmpty(startDate)) {
                get.form("startDate", startDate);
            }
            if(StringUtils.isNotEmpty(endDate)) {
                get.form("endDate", endDate);
            }
            HttpResponse execute = get.execute();
            String body = execute.body();
            return JSONArray.parseArray(body);
        } catch (Exception e) {
            log.error("电站的电量和收益统计查询-调用第三方接口报错:", e);
            return null;
        }
    }
 
    /**
     * 查询单个电站的发电量和收益
     */
    public static JSONObject getStationEarnDetail(String stationId, String token) {
        try {
            HttpRequest get = HttpUtil.createGet(KSOLAR_URL.concat("/station/detail/earn"));
            get.header("Authorization", "Bearer "+token);
            if(StringUtils.isNotEmpty(stationId)) {
                get.form("stationId", stationId);
            }
            HttpResponse execute = get.execute();
            String body = execute.body();
            JSONObject jsonObject = JSONObject.parseObject(body);
            Object data  = jsonObject.get("data");
            return JSONObject.parseObject(data.toString());
        } catch (Exception e) {
            log.error("电站的电量和收益统计查询-调用第三方接口报错:", e);
            return null;
        }
    }
 
 
}