From 548a50f98aa6cab54047028417dfbc9511123df6 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期三, 01 十一月 2023 11:22:43 +0800
Subject: [PATCH] 程序启动时初始化当前设备,选绑定的设备列表第一个

---
 service/device.go |   27 +++++++++++++++++++++++----
 1 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/service/device.go b/service/device.go
index f778f25..06b2e47 100644
--- a/service/device.go
+++ b/service/device.go
@@ -3,7 +3,7 @@
 import (
 	"apsClient/conf"
 	"apsClient/model"
-	"fmt"
+	"apsClient/pkg/logx"
 	"os"
 )
 
@@ -19,21 +19,40 @@
 	return deviceIds, nil
 }
 
+func InitCurrentDeviceID() (err error) {
+	currentDeviceID := ReadDeviceIDFromFile()
+	if currentDeviceID != "" {
+		conf.Conf.CurrentDeviceID = currentDeviceID
+		return
+	}
+	deviceList, err := GetDeviceIDList()
+	if err != nil {
+		return err
+	}
+	if len(deviceList) == 0 {
+		conf.Conf.CurrentDeviceID = conf.Conf.System.DeviceId
+		return nil
+	}
+	conf.Conf.CurrentDeviceID = deviceList[0]
+	SetDeviceIDToFile(conf.Conf.CurrentDeviceID)
+	return nil
+}
+
 const deviceIDFile = "currentDeviceID.txt"
 
 func SetDeviceIDToFile(deviceID string) {
 	err := os.WriteFile(deviceIDFile, []byte(deviceID), 0644)
 	if err != nil {
-		fmt.Printf("鏃犳硶鍐欏叆璁惧ID鍒版枃浠�: %v\n", err)
+		logx.Errorf("鏃犳硶鍐欏叆璁惧ID鍒版枃浠�: %v\n", err)
 	} else {
-		fmt.Println("璁惧ID宸插啓鍏ユ枃浠�")
+		logx.Infof("璁惧ID宸插啓鍏ユ枃浠�")
 	}
 }
 
 func ReadDeviceIDFromFile() string {
 	data, err := os.ReadFile(deviceIDFile)
 	if err != nil {
-		fmt.Printf("鏃犳硶璇诲彇璁惧ID鏂囦欢: %v\n", err)
+		logx.Errorf("鏃犳硶璇诲彇璁惧ID鏂囦欢: %v\n", err)
 		return ""
 	}
 	return string(data)

--
Gitblit v1.8.0