liujie
昨天 3803aa83014897b410a9aa0f6a7afc550976472d
发电对接三方(待测试)
2个文件已添加
241 ■■■■■ 已修改文件
ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/util/AESUtils.java 95 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/util/KsolarUtils.java 146 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/util/AESUtils.java
New file
@@ -0,0 +1,95 @@
package com.ruoyi.other.util;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
/**
 * Created by jhb on 2022-08-03 14:16
 */
public class AESUtils {
    public static final String DEFAULT_SECRET_KEY = "kstar_ase_keysfo";
    /**
     * 加密
     * @param sSrc 需要加密的字符串
     * @param sKey 此处使用AES-128-ECB加密模式,key需要为16位。
     * @return
     * @throws Exception
     */
    public static String Encrypt(String sSrc, String sKey) throws Exception {
        sKey = DEFAULT_SECRET_KEY;
        // 判断Key是否为16位
        if (sKey.length() != 16) {
            System.out.print("Key长度不是16位");
            return null;
        }
        byte[] raw = sKey.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
        return new Base64().encodeToString(encrypted);//此处使用BASE64做转码功能,同时能起到2次加密的作用。
    }
    /**
     * 解密
     * @param sSrc 需要解密的字符串
     * @param sKey 此处使用AES-128-ECB加密模式,key需要为16位。
     * @return
     * @throws Exception
     */
    public static String Decrypt(String sSrc, String sKey) throws Exception {
        sKey = DEFAULT_SECRET_KEY;
        try {
            // 判断Key是否正确
            if (sKey == null) {
                System.out.print("Key为空null");
                return null;
            }
            // 判断Key是否为16位
            if (sKey.length() != 16) {
                System.out.print("Key长度不是16位");
                return null;
            }
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = new Base64().decode(sSrc);//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original,"utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }
    public static void main(String[] args) throws Exception {
        /*
         * 此处使用AES-128-ECB加密模式,key需要为16位。
         */
        String cKey = "kstar_ase_keysfo";
        // 需要加密的字串
        String cSrc = "huandaoguangfu";
        System.out.println(cSrc);
        // 加密
        String enString = AESUtils.Encrypt(cSrc, cKey);
        System.out.println("加密后的字串是:" + enString);
        // 解密
        String DeString = AESUtils.Decrypt(enString, cKey);
        System.out.println("解密后的字串是:" + DeString);
    }
}
ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/util/KsolarUtils.java
New file
@@ -0,0 +1,146 @@
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.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.other.api.domain.TSystemConfiguration;
import com.ruoyi.other.mapper.TSystemConfigurationMapper;
import com.ruoyi.other.vo.ScreenStorageConfigVO;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
/**
 * 定时任务:储能放电情况 光伏发电情况
 */
@Service
public class KsolarUtils {
    private static String token;
    private static List<String> stationIds= Arrays.asList("2025060710462417","2025060711331323","2025060715425437","2025060716010425");
    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 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.1404")).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", "2025060710462417");
            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(LocalDate.now().getYear()+"");
        }
    }
    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 void main(String[] args) throws Exception {
        System.out.println(getYearAllEnergy("2025",""));
    }
}