Files
AI-tianyan/internal/config/config.go
master 6e88d2392f feat: add dynamic config reload, unique device identity (UUID), and MQTT support
- New StreamManager for dynamic RTSP/FLV stream lifecycle
- Dynamic worker scaling for inference
- Device UUID generation and persistence
- Telegraf config and NPU monitoring scripts
- .gitignore for build artifacts
2026-05-08 16:26:10 +08:00

86 lines
2.0 KiB
Go

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"`
RTSPURLs []string `yaml:"rtsp_urls"`
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
}