package com.uavps.framework.udp.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.uavps.system.domain.UavpsAlgorithmParameter;
import com.uavps.system.udp.vo.Aircraft;
import com.uavps.system.udp.vo.AircraftFormation;
import com.uavps.system.udp.vo.PlatformAircraft;
import com.uavps.system.udp.vo.UdpData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.math.BigInteger;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class UdpDataUtils {

    protected static final Logger logger = LoggerFactory.getLogger(UdpDataUtils.class);
    // 真实目标数据包
    public static final byte REAL_TARGET_DATA_HEAD_1 = (byte) 0xBB;
    public static final byte REAL_TARGET_DATA_HEAD_2 = (byte) 0x14;

    // 噪声目标数据包
    public static final byte NOISE_TARGET_DATA_HEAD_1 = (byte) 0xEE;
    public static final byte NOISE_TARGET_DATA_HEAD_2 = (byte) 0x16;

    // 平台数据
    public static final byte PLATFORM_DATA_HEAD_1 = (byte) 0xEB;
    public static final byte PLATFORM_DATA_HEAD_2 = (byte) 0x90;

    public static String bizId = "";
    public static float time = 0;

    /**
     * 解析数据帧
     * @param frame
     */
    public static UdpData parseFrame(byte[] frame) {
        if (frame == null || frame.length < 4) {
            logger.warn("parseFrame warn, frame: {}", frame);
            return null;
        }

        int index = 0;
        byte start_1 = frame[index++];
        byte start_2 = frame[index++];
        UdpData udpData = new UdpData();
        if (start_1 == REAL_TARGET_DATA_HEAD_1 && start_2 == REAL_TARGET_DATA_HEAD_2) {
            // 真实目标数据包
            index++;
            // 数据区
            byte[] data = Arrays.copyOfRange(frame, index, frame.length - 1);
            List<Aircraft> aircrafts = parseTargetData(data);
            AircraftFormation formation = new AircraftFormation();
            formation.setFormationNumber("1");
            formation.setAircrafts(aircrafts);
            List<AircraftFormation> targetAircraft = new ArrayList<>();
            targetAircraft.add(formation);
            udpData.setBizId(bizId);
            udpData.setFinished("false");
            udpData.setType(UdpData.REAL_TARGET);
            udpData.setTargetAircraft(targetAircraft);
            return udpData;
        } else if (start_1 == NOISE_TARGET_DATA_HEAD_1 && start_2 == NOISE_TARGET_DATA_HEAD_2) {
            // 噪声目标数据包
            index++;
            // 数据区
            byte[] data = Arrays.copyOfRange(frame, index, frame.length - 1);
            List<Aircraft> aircrafts = parseNoiseData(data);
            AircraftFormation formation = new AircraftFormation();
            formation.setFormationNumber("1");
            formation.setAircrafts(aircrafts);
            List<AircraftFormation> targetAircraft = new ArrayList<>();
            targetAircraft.add(formation);

            udpData.setBizId(bizId);
            udpData.setFinished("false");
            udpData.setType(UdpData.NOISE_TARGET);
            udpData.setTargetAircraft(targetAircraft);
            return udpData;
        } else if (start_1 == PLATFORM_DATA_HEAD_1 && start_2 == PLATFORM_DATA_HEAD_2) {
            //平台数据
            index++;
            // 数据区
            byte[] data = Arrays.copyOfRange(frame, index, frame.length - 1);

            PlatformAircraft platformAircraft = parsePlatformData(data);
            udpData.setBizId(bizId);
            udpData.setFinished("false");
            udpData.setType(UdpData.PLANT_UAV);
            udpData.setPlatformAircraft(platformAircraft);
            return udpData;
        } else {
            logger.warn("parseFrame warn, frame: {}", frame);
            return null;
        }

    }

    public static List<Aircraft> parseTargetData(byte[] data) {
        // 目标集群数据长度
        int len = 22;
        // 目标集群数据的起始位置
        int start = 3;
        byte[] mainData = BitUtils.copyFrom(data, start, len);
        //System.out.println(BitUtils.toHexString(mainData));
        List<Aircraft> aircrafts = new ArrayList<>();
        Aircraft aircraft = parseTargetSubData(mainData);
        if (aircraft != null) {
            aircraft.setAircraftNumber("1");
            aircraft.setPiloted(true);
            aircrafts.add(aircraft);
        }
        // 目标 2~30 的引导数据
        Aircraft temp = null;
        for (int i = 1; i < 31; i++) {
            byte[] subData = BitUtils.copyFrom(data, start + len * i, len);
            //System.out.println(BitUtils.toHexString(subData));
            temp = parseTargetSubData(subData);
            if (temp == null) {
                break;
            } else {
                temp.setAircraftNumber(String.valueOf(i + 1));
                temp.setPiloted(false);
                aircrafts.add(temp);
            }
        }
        return aircrafts;
    }

    public static List<Aircraft> parseNoiseData(byte[] data) {
        // 噪声目标数据的长度
        int len = 32;
        // 噪声目标数据的起始位置
        int start = 3;
        byte[] mainData = BitUtils.copyFrom(data, start, len);
        //System.out.println(BitUtils.toHexString(mainData));
        List<Aircraft> aircrafts = new ArrayList<>();
        Aircraft aircraft = parseNoiseSubData(mainData);
        if (aircraft != null) {
            aircraft.setAircraftNumber("1");
            aircraft.setPiloted(true);
            aircrafts.add(aircraft);
        }
        // 目标 2~30 的引导数据
        Aircraft temp = null;
        for (int i = 1; i < 31; i++) {
            byte[] subData = BitUtils.copyFrom(data, start + len * i, len);
            //System.out.println(BitUtils.toHexString(subData));
            temp = parseNoiseSubData(subData);
            if (temp == null) {
                break;
            } else {
                temp.setAircraftNumber(String.valueOf(i + 1));
                temp.setPiloted(false);
                aircrafts.add(temp);
            }
        }
        return aircrafts;
    }

    public static PlatformAircraft parsePlatformData(byte[] data) {
        PlatformAircraft platformAircraft = new PlatformAircraft();
        byte[] xByte = BitUtils.copyFrom(data, 0, 4);
        double x = BitUtils.toInt(xByte) * 0.0000001;
        platformAircraft.setLongitude(x);
        byte[] yByte = BitUtils.copyFrom(data, 4, 4);
        double y = BitUtils.toInt(yByte) * 0.0000001;
        platformAircraft.setLatitude(y);
        byte[] hByte = BitUtils.copyFrom(data, 8, 2);
        short h = BitUtils.toShort(hByte);
        platformAircraft.setAltitude(h);

        byte[] azimuthAngleByte = BitUtils.copyFrom(data, 10, 2);
        double azimuthAngle = BitUtils.toShort(azimuthAngleByte) * 0.01 ;
        platformAircraft.setAzimuthAngle(azimuthAngle);

        byte[] pitchAngleByte = BitUtils.copyFrom(data, 12, 2);
        double pitchAngle = BitUtils.toShort(pitchAngleByte) * 0.01;
        platformAircraft.setPitchAngle(pitchAngle);

        byte[] rollAngleByte = BitUtils.copyFrom(data, 14, 2);
        double rollAngle = BitUtils.toShort(rollAngleByte) * 0.01;
        platformAircraft.setRollAngle(rollAngle);

        byte[] eastSpeedByte = BitUtils.copyFrom(data, 16, 2);
        double eastSpeed = BitUtils.toShort(eastSpeedByte) * 0.1;
        platformAircraft.setEastSpeed(eastSpeed);

        byte[] northSpeedByte = BitUtils.copyFrom(data, 18, 2);
        double northSpeed = BitUtils.toShort(northSpeedByte) * 0.1;
        platformAircraft.setNorthSpeed(northSpeed);

        byte[] skySpeedByte = BitUtils.copyFrom(data, 20, 2);
        double skySpeed = BitUtils.toShort(skySpeedByte) * 0.1;
        platformAircraft.setSkySpeed(skySpeed);

        byte[] trackAngleByte = BitUtils.copyFrom(data, 22, 2);
        short trackAngle = BitUtils.toShort(trackAngleByte) ;
        platformAircraft.setTrackAngle(trackAngle);

//        byte[] yearByte = BitUtils.copyFrom(data, 24, 2);
//        short year = BitUtils.toShort(yearByte);
//        byte[] monthByte = BitUtils.copyFrom(data, 26, 1);
//        int month = BitUtils.toInt(monthByte);
//        byte[] dayByte = BitUtils.copyFrom(data, 27, 1);
//        int day = BitUtils.toInt(dayByte);
//        byte[] hourByte = BitUtils.copyFrom(data, 28, 1);
//        int hour = BitUtils.toInt(hourByte);
//        byte[] minByte = BitUtils.copyFrom(data, 29, 1);
//        int min = BitUtils.toInt(minByte);
//        byte[] secondByte = BitUtils.copyFrom(data, 30, 1);
//        int second = BitUtils.toInt(secondByte);
//        byte[] millisecondByte = BitUtils.copyFrom(data, 31, 1);
//        int millisecond = BitUtils.toInt(millisecondByte);
//        platformAircraft.setUtcTime(String.format("%04d/%02d/%02d %02d:%02d:%02d %03d", year, month, day, hour, min, second, millisecond));
        return platformAircraft;
    }

    public static Aircraft parseTargetSubData(byte[] data) {
        byte[] idByte = BitUtils.copyFrom(data, 0, 2);
        //System.out.println(BitUtils.toHexString(idByte));
        short id = BitUtils.toShort(idByte);
        if (id == 0) return null;
        Aircraft af = new Aircraft();
        af.setType("R");
        byte[] xByte = BitUtils.copyFrom(data, 2, 4);
        //System.out.println(BitUtils.toHexString(xByte));
        double x = BitUtils.toInt(xByte) * 0.0000001;
        af.setLongitude(x);
        byte[] yByte = BitUtils.copyFrom(data, 6, 4);
        //System.out.println(BitUtils.toHexString(yByte));
        double y = BitUtils.toInt(yByte) * 0.0000001;
        double[] xy = WebMercator.lonLatToMeters(x, y);
        af.setCoordinateX(doubleToFloat(xy[0]));
        af.setCoordinateY(doubleToFloat(xy[1]));
        af.setLatitude(y);
        byte[] hByte = BitUtils.copyFrom(data, 10, 2);
        //System.out.println(BitUtils.toHexString(hByte));
        short h = BitUtils.toShort(hByte);
        af.setCoordinateZ(doubleToFloat(h));
        af.setAltitude(h);
        byte[] dvByte = BitUtils.copyFrom(data, 12, 2);
        //System.out.println(BitUtils.toHexString(dvByte));
        double dv = BitUtils.toShort(dvByte) * 0.1;
        af.setVelocityX(doubleToFloat(dv));
        af.setEastSpeed(dv);
        byte[] bvByte = BitUtils.copyFrom(data, 14, 2);
        //System.out.println(BitUtils.toHexString(bvByte));
        double bv = BitUtils.toShort(bvByte) * 0.1;
        af.setVelocityY(doubleToFloat(bv));
        af.setNorthSpeed(bv);
        byte[] tvByte = BitUtils.copyFrom(data, 16, 2);
        //System.out.println(BitUtils.toHexString(tvByte));
        double tv = BitUtils.toShort(tvByte) * 0.1;
        af.setVelocityZ(doubleToFloat(tv));
        af.setSkySpeed(tv);
        //byte[] timeByte = BitUtils.copyFrom(data, 18, 4);
        //System.out.println(BitUtils.toHexString(timeByte));
        //int time = BitUtils.toInt(timeByte);
        //System.out.println("real>> " + id + " " + x + " " + y + " " + h + " " + dv + " " + bv + " " + tv + " ");
        return af;
    }

    public static Aircraft parseNoiseSubData(byte[] data) {
        byte[] idByte = BitUtils.copyFrom(data, 0, 2);
        //System.out.println(BitUtils.toHexString(idByte));
        short id = BitUtils.toShort(idByte);
        //System.out.println(id);
        if (id == 0) return null;
        Aircraft af = new Aircraft();
        af.setType("N");
        byte[] xByte = BitUtils.copyFrom(data, 4, 4);
        //System.out.println(BitUtils.toHexString(xByte));
        int ix = BitUtils.toInt(xByte);
        double x = ix * 0.0000001;
        //System.out.println(x);
        //af.setCoordinateX(doubleToFloat(x));
        af.setLongitude(x);
        byte[] yByte = BitUtils.copyFrom(data, 8, 4);
        //System.out.println(BitUtils.toHexString(yByte));
        int iy = BitUtils.toInt(yByte);
        double y = iy * 0.0000001;
        //System.out.println(y);
        //af.setCoordinateY(doubleToFloat(y));
        af.setLatitude(y);
        double[] xy = WebMercator.lonLatToMeters(x, y);
        af.setCoordinateX(doubleToFloat(xy[0]));
        af.setCoordinateY(doubleToFloat(xy[1]));
        byte[] hByte = BitUtils.copyFrom(data, 12, 2);
        //System.out.println(BitUtils.toHexString(hByte));
        short h = BitUtils.toShort(hByte);
        //System.out.println(h);
        af.setCoordinateZ(doubleToFloat(h));
        af.setAltitude(h);
        byte[] dvByte = BitUtils.copyFrom(data, 22, 2);
        //System.out.println(BitUtils.toHexString(dvByte));
        double dv = BitUtils.toShort(dvByte) * 0.1;
        af.setVelocityX(doubleToFloat(dv));
        //System.out.println(dv);
        af.setEastSpeed(dv);
        byte[] bvByte = BitUtils.copyFrom(data, 24, 2);
        //System.out.println(BitUtils.toHexString(bvByte));
        double bv = BitUtils.toShort(bvByte) * 0.1;
        af.setVelocityY(doubleToFloat(bv));
        af.setNorthSpeed(bv);
        //System.out.println(bv);
        byte[] tvByte = BitUtils.copyFrom(data, 26, 2);
        //System.out.println(BitUtils.toHexString(tvByte));
        double tv = BitUtils.toShort(tvByte) * 0.1;
        af.setVelocityZ(doubleToFloat(tv));
        af.setSkySpeed(tv);
        return af;
    }

    public static void main(String[] args) throws JsonProcessingException, UnknownHostException {
        //byte[] targetData = hexStringToBytes(getTargetData());
        //parseFrame(targetData, udpData);
       // System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");

        /*String[] data = getNoiseData();
        UdpData udpData = new UdpData();
        ObjectMapper mapper = new ObjectMapper();
        for (int i = 0; i < data.length; i++) {
            byte[] noiseData = hexStringToBytes(data[i]);
            udpData = parseFrame(noiseData);
            String json = mapper.writeValueAsString(udpData);
            System.out.println(json);
        }*/

        //byte[] noiseData = hexStringToBytes(getNoiseData());
        //parseFrame(noiseData, udpData);
        /*System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
        String[] flatData = getFlatformData();
        for (int i = 0; i < flatData.length; i++) {
            byte[] flatData1 = hexStringToBytes(flatData[i]);
            parseFrame(flatData1);
        }*/
        //byte[] flatData = hexStringToBytes(getFlatdata());
        //parseFrame(flatData);
        //System.out.println(InetAddress.getByName("127.0.0.1"));

        /*String str = Integer.toHexString(255).toUpperCase();
        byte[] bry = hexStringToBytes(str.trim());
        System.out.println(BitUtils.toShort(bry));*/

//        byte[] b = hexStringToBytes("016991F7");
//        int i = BitUtils.toInt(b);
//        Double d = new BigDecimal(2.3659852).doubleValue() * 10000000;
//        System.out.println(BytesUtils.intToHex(d.intValue(), 4));
        System.out.println(BytesUtils.intToHex(1, 4));
    }

    private static String getTargetData() {
        String data = "bb 14 03 01 c2 01 01 00 cc b9 5c 3e 94 ef 64 11 7b 01 00 00 72 00 02 00 06 10 4a 03 02 00 f0 e1 5c 3e 97 f0 64 11 7b 01 f9 ff 75 00 02 00 06 10 4a 03 03 00 14 0a 5d 3e 9b f1 64 11 7b 01 f8 ff 79 00 02 00 06 10 4a 03 04 00 38 32 5d 3e 9e f2 64 11 7b 01 fb ff 75 00 fc ff 06 10 4a 03 05 00 5d 5a 5d 3e a2 f3 64 11 7b 01 fa ff 78 00 fc ff 06 10 4a 03 06 00 3c 0b 5d 3e 61 ce 64 11 7c 01 fa ff 74 00 00 00 06 10 4a 03 07 00 64 0c 5d 3e 27 ab 64 11 7c 01 fa ff 74 00 fc ff 06 10 4a 03 08 00 8c 0d 5d 3e ee 87 64 11 7c 01 fa ff 73 00 fc ff 06 10 4a 03 09 00 b3 0e 5d 3e b4 64 64 11 7d 01 fc ff 75 00 fc ff 06 10 4a 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 09 1c 1b";
        return data;
    }

    private static String[] getNoiseData() {
        //String data = "EE 16 01 01 01 01 00 00 27 13 00 00 07 d3 03 e8 00 05 00 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00";
        String[] data = readFiles("D:\\20250317\\one_target_enemy_16.json");
        return data;
    }

    private static String getFlatdata() {
        String data = "EB 90 80 00 00 14 27 00 00 02 ad x1 66 00 25 00 03 00 1b 09 79 02 f0 0x 9a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00";
        return data;
    }

    private static String[] getFlatformData() {
        //String data = "EB 90 80 00 01 db 2c 00 00 a8 49 26 04 00 01 00 01 00 2e 08 c1 04 50 00 84 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00";
        String[] data = readFiles("D:\\20250317\\1\\one_target_fixedwing_uav_16.json");
        return data;
    }

    public static byte[] hexStringToBytes(String hex) {
        hex = hex.replaceAll("\\s", "");
        // 补全偶数长度（如果长度为奇数，自动补前导0）
        if (hex.length() % 2 != 0) {
            hex = "0" + hex;
        }
        byte[] data = new BigInteger(hex, 16).toByteArray();
        // 处理符号位问题（BigInteger会保留符号位，可能需要调整）
        if (data[0] == 0) {
            return Arrays.copyOfRange(data, 1, data.length);
        }
        return data;
    }

    private static String lowEightBits(byte[] bytes) {
        int sum = 0;
        for (byte b : bytes) {
            sum += (b & 0xFF); // 将字节转为无符号整数（0~255）再累加
        }
        int lowEightBits = sum & 0xFF; // 取和的低8位
        return Integer.toHexString(lowEightBits).toUpperCase();
    }

    private static String[] readFiles(String path) {
        StringBuilder sb = new StringBuilder();
        try {
            File file = new File(path);
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            String str = null;
            while ((str = br.readLine()) != null) {
                sb.append(str);
            }
            br.close();
            fr.close();
            ObjectMapper mapper = new ObjectMapper();
            String[] strary = mapper.readValue(sb.toString(), String[].class);
            return strary;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static float doubleToFloat(double d) {
//        d = d / 100000; //缩小100000
        return (float) d;
    }

    public static String initAlgorithmParams(UavpsAlgorithmParameter parameter) {
        StringBuffer sbf = new StringBuffer();
        // 帧头
        sbf.append("BB1403");
        // 包ID
        if (parameter.getTargetTotal() < 30) {
            sbf.append(BytesUtils.intToHex(1, 2));
        } else if (parameter.getTargetTotal() < 60) {
            sbf.append(BytesUtils.intToHex(2, 2));
        } else if (parameter.getTargetTotal() < 90) {
            sbf.append(BytesUtils.intToHex(3, 2));
        } else if (parameter.getTargetTotal() < 120) {
            sbf.append(BytesUtils.intToHex(4, 2));
        }
        // 包循环计数
        sbf.append(BytesUtils.intToHex(0, 2));
        // 目标集群类型
        sbf.append(BytesUtils.intToHex(Integer.valueOf(parameter.getFormationType()), 2));
        // 目标1 ID
        sbf.append(BytesUtils.intToHex(parameter.getTargetId().intValue(), 4));
        // 经度
        Double longitude = parameter.getLongitude().doubleValue() * 10000000;
        sbf.append(BytesUtils.intToHex(longitude.intValue(), 8));
        // 纬度
        Double latitude = parameter.getLatitude().doubleValue() * 10000000;
        sbf.append(BytesUtils.intToHex(latitude.intValue(), 8));
        // 海拔
        sbf.append(BytesUtils.intToHex(parameter.getAltitude().intValue(), 4));
        // 东向速度
        Double eastSpeed = parameter.getEastSpeed().doubleValue() * 10;
        sbf.append(BytesUtils.intToHex(eastSpeed.intValue(), 4));
        // 北向速度
        Double northSpeed = parameter.getNorthSpeed().doubleValue() * 10;
        sbf.append(BytesUtils.intToHex(northSpeed.intValue(), 4));
        // 天向速度
        Double skySpeed = parameter.getSkySpeed().doubleValue() * 10;
        sbf.append(BytesUtils.intToHex(skySpeed.intValue(), 4));
        // UTC时间
        sbf.append(BytesUtils.intToHex(123456789, 8));
        // 目标2~30
        sbf.append(BytesUtils.intToHex(0, 638));
        // 预留
        sbf.append(BytesUtils.intToHex(0, 324));
        // 本次目标总数
        sbf.append(BytesUtils.intToHex(parameter.getTargetTotal().intValue(), 2));
        // 校验字
        byte[] bytes = hexStringToBytes(sbf.toString());
        sbf.append(lowEightBits(bytes));
        // 帧尾
        sbf.append("1B");
        /*// 包ID
        String pkgId = "";
        if (parameter.getTargetTotal() < 30) {
            pkgId = "1";
        } else if (parameter.getTargetTotal() < 60) {
            pkgId = "2";
        } else if (parameter.getTargetTotal() < 90) {
            pkgId = "3";
        } else if (parameter.getTargetTotal() < 120) {
            pkgId = "4";
        }
        sbf.append("{" +
                " \"数据内容标识码\":3," +
                " \"包ID\": " + pkgId + "," +
                " \"包循环计数\": 0," +
                " \"目标集群类型\": " + parameter.getFormationType() + "," +
                " \"目标\":[" +
                "    {" +
                "      \"目标ID\": " + parameter.getTargetId() + "," +
                "      \"经度\": " + parameter.getLongitude() + "," +
                "      \"纬度\": " + parameter.getLatitude() + "," +
                "      \"海拔\": " + parameter.getAltitude() + "," +
                "      \"东向速度\": " + parameter.getEastSpeed() + "," +
                "      \"北向速度\": " + parameter.getNorthSpeed() + "," +
                "      \"天向速度\": " + parameter.getSkySpeed() + "," +
                "      \"UTC时间\": " + parameter.getUtcTimes() +
                "    }" +
                " ]," +
                " \"本次目标总数\": " + parameter.getTargetTotal() +
                "}");*/
        return sbf.toString();
    }
}
