package com.ruoyi.jianguan.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 org.springframework.stereotype.Service; import java.math.BigDecimal; import java.math.RoundingMode; import java.time.LocalDate; import java.util.*; /** * 定时任务:储能放电情况 光伏发电情况 */ @Service public class KsolarUtils { private static String token; private static String accessToken; private static List stationIds = Arrays.asList("2025060710462417", "2025060711331323", "2025060715425437", "2025060716010425"); /** * 获取今日总发电量 * * @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(); } return 0D; } public static ArrayList> getMonthEnergy(String time, String time1) { ArrayList> 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 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(); } return list; } /** * 光伏累计 * * @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(); } return 0D; } /** * 光伏碳减排 * * @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("1.564")).divide(new BigDecimal("10000"), 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(); } return 0D; } public static String token() { HttpRequest post = HttpUtil.createPost("http://111.230.136.62:8092/authentication/form"); post.basicAuth("kstar", "kstarSecret"); post.form("username", "环岛光伏"); post.form("password", "cR8GGwIXMI462A8Dfl9eXg=="); 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 void main(String[] args) throws Exception { Double allEnergy = getAllEnergy(LocalDate.now().toString()); System.out.println(allEnergy); } }