修改项目结构
This commit is contained in:
58
cmii-uav-watchdog-agent/host_info/mboard_service.go
Normal file
58
cmii-uav-watchdog-agent/host_info/mboard_service.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"cmii-uav-watchdog-common/models"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// DefaultMotherboardInfo provides a default value for MotherboardInfo.
|
||||
var DefaultMotherboardInfo = models.MotherboardInfo{
|
||||
Manufacturer: "unknown",
|
||||
Product: "unknown",
|
||||
Version: "unknown",
|
||||
Serial: "unknown",
|
||||
}
|
||||
|
||||
// readFileWithWarning attempts to read a file and logs a warning if it fails.
|
||||
func readFileWithWarning(path string) ([]byte, error) {
|
||||
value, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
fmt.Printf("Warning: unable to read file %s: %v\n", path, err)
|
||||
return nil, err
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// GetMotherboardInfo retrieves motherboard information from the system files.
|
||||
func GetMotherboardInfo() models.MotherboardInfo {
|
||||
info := DefaultMotherboardInfo // 初始化为默认值
|
||||
|
||||
// 文件路径与对应字段的映射
|
||||
paths := map[string]*string{
|
||||
"/sys/class/dmi/id/board_vendor": &info.Manufacturer,
|
||||
"/sys/class/dmi/id/board_name": &info.Product,
|
||||
"/sys/class/dmi/id/board_version": &info.Version,
|
||||
"/sys/class/dmi/id/board_serial": &info.Serial,
|
||||
}
|
||||
|
||||
// 遍历路径并更新信息
|
||||
for path, field := range paths {
|
||||
if value, err := readFileWithWarning(path); err == nil {
|
||||
*field = strings.TrimSpace(string(value))
|
||||
}
|
||||
}
|
||||
|
||||
return info // 返回最终的主板信息
|
||||
}
|
||||
|
||||
/*
|
||||
Motherboard Information:
|
||||
Manufacturer: ASUSTeK COMPUTER INC.
|
||||
Product: ROG STRIX B450-F GAMING
|
||||
Version: Rev 1.xx
|
||||
Serial: 123456789012345678
|
||||
|
||||
|
||||
*/
|
||||
Reference in New Issue
Block a user