safesight/control/internal/service/device_versions.go
tian 98e375dc36 chore: 合并 safesight-control 仓库为 control/ 子目录
项目合并为单仓库:设备端(根) + 管理端(control/)。
两端 git 历史完整保留(git subtree 合并)。

git-subtree-dir: control
git-subtree-mainline: 923ab10d5c
git-subtree-split: 70738edb31
2026-08-02 11:13:26 +08:00

79 lines
2.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}