package com.pdaaphm.biz.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MatlabUtils {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    public void runMatExe(String url) {
        logger.info("exe 运行开始，url：{}", url);
        try {
            // 使用Process
//            // 启动程序
//            Process process = Runtime.getRuntime().exec(url);
//            process.waitFor();  // 等待进程结束

            // 使用 ProcessBuilder
//            ProcessBuilder processBuilder = new ProcessBuilder(url);
//            // 启动进程
//            Process process = processBuilder.start();
//            // 等待程序执行完成
//            process.waitFor();

            // 小夏的代码
            // 指定要执行的外部程序和参数
            ProcessBuilder processBuilder = new ProcessBuilder(url);
            // 启动外部程序
            Process process = processBuilder.start();

            // Thread to handle the output stream.
            Thread outputThread = new Thread(() -> {
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
                    String line;
                    while ((line = reader.readLine()) != null) {
                        logger.info(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });

            // Thread to handle the error stream.
            Thread errorThread = new Thread(() -> {
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
                    String line;
                    while ((line = reader.readLine()) != null) {
                        logger.error(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });

            outputThread.start();
            errorThread.start();

            // Wait for the external process to finish.
            int exitCode = process.waitFor();

            // Optionally, wait for the threads to finish reading.
            outputThread.join();
            errorThread.join();
            logger.info("外部程序执行完成，退出码：{}", exitCode);
            logger.info("exe 运行结束，url：{}", url);
        } catch (IOException | InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

//    public static void main(String[] args) {
//        String url = "D:\\pdaaphm\\algorithm_exe\\KNN\\bq_linear.exe";
//        try {
//            // 指定要执行的外部程序和参数
//            ProcessBuilder processBuilder = new ProcessBuilder(url);
//            // 启动外部程序
//            Process process = processBuilder.start();
//
//            // Thread to handle the output stream.
//            Thread outputThread = new Thread(() -> {
//                try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
//                    String line;
//                    while ((line = reader.readLine()) != null) {
//                    }
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            });
//
//            // Thread to handle the error stream.
//            Thread errorThread = new Thread(() -> {
//                try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
//                    String line;
//                    while ((line = reader.readLine()) != null) {
//                    }
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            });
//
//            outputThread.start();
//            errorThread.start();
//
//            // Wait for the external process to finish.
//            int exitCode = process.waitFor();
//
//            // Optionally, wait for the threads to finish reading.
//            outputThread.join();
//            errorThread.join();
//        } catch (IOException | InterruptedException e) {
//            e.printStackTrace();
//        }
//    }

    public static void main(String[] args) {
        try {
            // 指定 exe 文件的路径
            String exePath = "D:\\pdaaphm\\algorithm_exe\\KNN\\bq_linear.exe";
            // 启动程序
            Process process = Runtime.getRuntime().exec(exePath);
            process.waitFor();  // 等待进程结束
            System.out.println("Program executed successfully");
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
