package config import ( "crypto/rand" "encoding/hex" "os" "path/filepath" "gopkg.in/yaml.v3" ) type Config struct { DeviceUUID string `yaml:"device_uuid"` EdgeID string `yaml:"edge_id"` CloudURL string `yaml:"cloud_url"` MqttBroker string `yaml:"mqtt_broker"` MqttUser string `yaml:"mqtt_user"` MqttPass string `yaml:"mqtt_pass"` EdgeToken string `yaml:"edge_token"` StreamEnabled bool `yaml:"stream_enabled"` RTSPURLs []string `yaml:"rtsp_urls"` // 新增:自动按需拉流配置 StreamPullURL string `yaml:"stream_pull_url"` // 云端拉流网关地址 StreamProtocol string `yaml:"stream_protocol"` // 拉流协议: flv, rtsp, ws_flv AutoPull bool `yaml:"auto_pull"` // 是否启用自动按需拉流 InferSocket string `yaml:"infer_socket"` InferFPS int `yaml:"infer_fps"` InferWorkers int `yaml:"infer_workers"` ConfThreshold float64 `yaml:"conf_threshold"` DedupWindowSec int `yaml:"dedup_window_sec"` OTAUrl string `yaml:"ota_url"` Version string `yaml:"version"` configPath string `json:"-"` } func Load(path string) *Config { cfg := &Config{ EdgeID: "edge-unknown", CloudURL: "http://localhost:8004", InferSocket: "/tmp/edge-infer.sock", InferFPS: 5, InferWorkers: 2, ConfThreshold: 0.5, DedupWindowSec: 30, Version: "1.0.0", configPath: path, } cfg.loadUUID() if data, err := os.ReadFile(path); err == nil { _ = yaml.Unmarshal(data, cfg) } if cfg.InferWorkers <= 0 { cfg.InferWorkers = 1 } if cfg.DedupWindowSec <= 0 { cfg.DedupWindowSec = 30 } return cfg } func (c *Config) loadUUID() { uuidPath := "/opt/tianyan-edge/device.uuid" if data, err := os.ReadFile(uuidPath); err == nil { c.DeviceUUID = string(data) } else { b := make([]byte, 16) rand.Read(b) c.DeviceUUID = hex.EncodeToString(b) os.MkdirAll(filepath.Dir(uuidPath), 0755) os.WriteFile(uuidPath, []byte(c.DeviceUUID), 0644) } } func (c *Config) Save() error { data, err := yaml.Marshal(c) if err != nil { return err } return os.WriteFile(c.configPath, data, 0644) } func (c *Config) GetDeviceIdentity() string { if c.DeviceUUID != "" { return c.DeviceUUID } return c.EdgeID }