golang呼叫模組程式實現互動輸入自動化,獲取imei及iccid

CHHC發表於2024-11-18

應用場景:在openwrt下呼叫移遠的測試程式,並實現輸入自動話,獲取imei

root@OpenWrt:~# ql-api-test
Test groups:

0:      ql_dsi

1:      ql_nw

2:      ql_sim

3:      ql_dev

4:      ql_voice

5:      ql_sms

6:      ql_adc

7:      ql_i2c

8:      ql_enit

9:      ql_fota

10:     ql_gpio

11:     ql_spi

12:     ql_uart

13:     ql_locator

14:     ql_lpm

15:     ql_audio

16:     ql_wifi

17:     ql_atc

please input command index(-1 exit):
3
Group Name:ql_dev, Supported test cases:

0:      ql_dev_init

1:      ql_dev_release

2:      ql_dev_get_imei

3:      ql_dev_get_firmware_version

4:      ql_dev_get_model

5:      ql_dev_get_sn

6:      ql_dev_set_modem_fun

7:      ql_dev_get_modem_fun

8:      ql_dev_forbidind

please input cmd index(-1 exit):
0
ql_dev_init ret = 0

please input cmd index(-1 exit):
2
ql_dev_get_imei ret = 0, imei:860503070780822;

please input cmd index(-1 exit):
-1
Test groups:

0:      ql_dsi

1:      ql_nw

2:      ql_sim

3:      ql_dev

4:      ql_voice

5:      ql_sms

6:      ql_adc

7:      ql_i2c

8:      ql_enit

9:      ql_fota

10:     ql_gpio

11:     ql_spi

12:     ql_uart

13:     ql_locator

14:     ql_lpm

15:     ql_audio

16:     ql_wifi

17:     ql_atc

please input command index(-1 exit):
-1

自動化指令:

方式1
echo -e "3\n0\n2\n-1\n-1" | ql-api-test

方式2
ql-api-test <<EOF
3
0
2
-1
-1
EOF

golang程式碼實現

process.go

package util

import (
    "bytes"
    "os/exec"
    "regexp"
    "runtime"
    "strings"
    "tboxconfig/util/common"
)

// 檢查程序是否在執行(適用於類Unix系統和Windows)
func GetIMEI() string {
    imei := ""

    if runtime.GOOS == "windows" {
        return "不支援windows獲取MEI"
    } else {
        echoCmd := exec.Command("echo", "-e", "3\n0\n2\n-1\n-1")

        // 建立 ql-api-test 命令
        qlApiTestCmd := exec.Command("ql-api-test")

        // 將 echo 命令的輸出作為 ql-api-test 命令的輸入
        echoOut, _ := echoCmd.StdoutPipe()
        qlApiTestCmd.Stdin = echoOut

        // 捕獲 ql-api-test 命令的標準輸出
        var stdout bytes.Buffer
        qlApiTestCmd.Stdout = &stdout

        // 啟動 echo 命令
        if err := echoCmd.Start(); err != nil {
            common.LogError2("啟動 echo 命令失敗:", err)
            return ""
        }

        // 啟動 ql-api-test 命令
        if err := qlApiTestCmd.Start(); err != nil {
            common.LogError2("啟動 ql-api-test 命令失敗:", err)
            return ""
        }

        // 等待 echo 命令完成
        if err := echoCmd.Wait(); err != nil {
            common.LogError2("echo 命令執行失敗:", err)
            return ""
        }

        // 等待 ql-api-test 命令完成
        if err := qlApiTestCmd.Wait(); err != nil {
            common.LogError2("ql-api-test 命令執行失敗:", err)
        }

        // 獲取標準輸出的內容
        output := stdout.String()
        common.LogInfo("imei輸出:" + output)

        // 使用正規表示式提取 imei
        re := regexp.MustCompile(`imei:([0-9]+);`)
        matches := re.FindStringSubmatch(output)
        if len(matches) > 1 {
            imei = matches[1]
        }
    }

    return imei
}

func GetICCID() string {
    iccid := ""
    if runtime.GOOS == "windows" {
        return "不支援windows獲取ICCID"
    } else {
        cmd := exec.Command("serial_atcmd", "AT+QCCID")

        // 捕獲命令的標準輸出和標準錯誤輸出
        var stdout bytes.Buffer
        var stderr bytes.Buffer
        cmd.Stdout = &stdout
        cmd.Stderr = &stderr

        // 啟動命令
        if err := cmd.Run(); err != nil {
            common.LogError2("命令執行失敗:", err)
            return ""
        }

        output := stdout.String()
        common.LogInfo("iccid輸出:" + output)

        // 提取 ICCID
        iccid = extractICCID(output)
    }

    return iccid
}

// 從輸出中提取 ICCID
func extractICCID(output string) string {
    // 假設輸出格式為 "+QCCID: 89860012345678901234" 或類似格式
    lines := strings.Split(output, "\n")
    for _, line := range lines {
        if strings.HasPrefix(line, "+QCCID:") {
            parts := strings.Split(line, ":")
            if len(parts) > 1 {
                return strings.TrimSpace(parts[1])
            }
        }
    }
    return ""
}

相關文章