项目合并为单仓库:设备端(根) + 管理端(control/)。 两端 git 历史完整保留(git subtree 合并)。 git-subtree-dir: control git-subtree-mainline:923ab10d5cgit-subtree-split:70738edb31
79 lines
2.5 KiB
Go
79 lines
2.5 KiB
Go
package service
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"strings"
|
||
|
||
"safesight-control/internal/models"
|
||
)
|
||
|
||
// DeviceVersionInfo 汇总设备上 agent /v1/versions 返回的版本信息
|
||
// (agent 构建注入的版本 + C++ media 引擎版本 + 设备上模型数量)
|
||
type DeviceVersionInfo struct {
|
||
AgentVersion string `json:"agent_version"`
|
||
AgentBuildID string `json:"agent_build_id"`
|
||
AgentBuildType string `json:"agent_build_type"`
|
||
AgentGitSHA string `json:"agent_git_sha"`
|
||
AgentBinary string `json:"agent_binary"`
|
||
|
||
MediaSupported bool `json:"media_supported"`
|
||
MediaVersion string `json:"media_version"`
|
||
MediaBinary string `json:"media_binary"`
|
||
|
||
ModelCount int `json:"model_count"`
|
||
}
|
||
|
||
// FetchDeviceVersions 调用 edge agent 的 /v1/versions 接口,获取
|
||
// agent / media 引擎 / 模型清单的版本信息。设备离线或接口不可用时返回 error。
|
||
func FetchDeviceVersions(agent *AgentClient, device *models.Device) (*DeviceVersionInfo, error) {
|
||
if agent == nil || device == nil || strings.TrimSpace(device.IP) == "" || device.AgentPort <= 0 {
|
||
return nil, nil
|
||
}
|
||
body, status, err := agent.Do("GET", device.IP, device.AgentPort, "/v1/versions", nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if status != 200 {
|
||
return nil, fmt.Errorf("agent returned status %d", status)
|
||
}
|
||
var raw struct {
|
||
Agent map[string]any `json:"agent"`
|
||
MediaServer map[string]any `json:"media_server"`
|
||
Models struct {
|
||
Count int `json:"count"`
|
||
} `json:"models"`
|
||
}
|
||
if err := json.Unmarshal(body, &raw); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
info := &DeviceVersionInfo{}
|
||
if v, ok := raw.Agent["version"].(string); ok {
|
||
info.AgentVersion = v
|
||
}
|
||
if v, ok := raw.Agent["build_id"].(string); ok {
|
||
info.AgentBuildID = v
|
||
}
|
||
if v, ok := raw.Agent["build_type"].(string); ok {
|
||
info.AgentBuildType = v
|
||
}
|
||
if v, ok := raw.Agent["git_sha"].(string); ok {
|
||
info.AgentGitSHA = v
|
||
}
|
||
if v, ok := raw.Agent["binary"].(string); ok {
|
||
info.AgentBinary = v
|
||
}
|
||
if v, ok := raw.MediaServer["supported"].(bool); ok {
|
||
info.MediaSupported = v
|
||
}
|
||
if v, ok := raw.MediaServer["version"].(string); ok {
|
||
info.MediaVersion = v
|
||
}
|
||
if v, ok := raw.MediaServer["binary"].(string); ok {
|
||
info.MediaBinary = v
|
||
}
|
||
info.ModelCount = raw.Models.Count
|
||
return info, nil
|
||
}
|