package com.doverfuelingsolutions.issp.driver.tools;

import android.text.TextUtils;

import com.doverfuelingsolutions.issp.driver.tools.shell.BoardHelper;
import com.doverfuelingsolutions.issp.driver.tools.shell.CommandResult;
import com.doverfuelingsolutions.issp.driver.tools.shell.Shell;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class BoardTool {
    private static final String BOARD3_CMD = "getprop ro.product.model";
    private static final String BOARD3_TOKEN = "QJ-ICON28NL";

    /**
     * 根据主板类型自动获取打印机串口路径
     * @return
     */
    public String printUartPath(){
        String boardType = "";
        try {
            Class<?> cls = Class.forName("android.os.SystemProperties");
            Method method = cls.getMethod("get", String.class, String.class);
            boardType = (String) method.invoke(null, "ro.boottime.vendor.avb", "0");

        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }

        if (boardType.equals("SV6876580") || isICON28NL()) {
            // 新主板ICON28N, ICON28NL
            return "/dev/ttymxc1";
        } else {
            // 旧主板ICON39，ICON6
            return "/dev/ttyS3";
        }
    }

    public static boolean isICON28NL(){
        CommandResult cmd3 = Shell.SH.run(BOARD3_CMD);
        if (cmd3.isSuccessful()) {
            String res3 = cmd3.getStdout();
            return TextUtils.equals(res3, BOARD3_TOKEN);
        }

        return  false;
    }
}
