fix: 修复6个边缘侧问题 + 单元测试

1. 删除 ConfigAgent (与 MQTT 配置更新重叠且未启用)
2. OTA 完整安装流程: 解压tar.gz -> 校验ELF -> 替换二进制 -> systemctl重启
3. StreamManager stopAll 添加 Wait() 防止僵尸进程
4. InferClient socket 读写添加 timeout 防止永久阻塞
5. Heartbeat 集成 NPU 监控 (npu-smi + fallback 脚本)
6. 配置更新时动态重启 ffmpeg 以应用新 fps
This commit is contained in:
2026-05-08 23:58:08 +08:00
parent 88b2e71a77
commit 97a77d4745
11 changed files with 843 additions and 186 deletions

View File

@@ -22,10 +22,15 @@ type Frame struct {
TS float64
}
type streamProcess struct {
cmd *exec.Cmd
fps int
}
type StreamManager struct {
cfg *config.Config
frames chan<- Frame
processes map[string]*exec.Cmd
processes map[string]*streamProcess // url -> {cmd, fps}
mu sync.Mutex
updates <-chan *config.Config
}
@@ -34,14 +39,14 @@ func NewStreamManager(cfg *config.Config, frames chan<- Frame, updates <-chan *c
return &StreamManager{
cfg: cfg,
frames: frames,
processes: make(map[string]*exec.Cmd),
processes: make(map[string]*streamProcess),
updates: updates,
}
}
func (sm *StreamManager) Run(ctx context.Context) {
// Initial start
sm.applyStreams(ctx, sm.cfg.RTSPURLs)
sm.applyStreams(ctx, sm.cfg.RTSPURLs, sm.cfg.InferFPS)
for {
select {
@@ -49,19 +54,19 @@ func (sm *StreamManager) Run(ctx context.Context) {
sm.stopAll()
return
case newCfg := <-sm.updates:
log.Printf("stream: applying new config, urls=%d", len(newCfg.RTSPURLs))
sm.applyStreams(ctx, newCfg.RTSPURLs)
log.Printf("stream: applying new config, urls=%d fps=%d", len(newCfg.RTSPURLs), newCfg.InferFPS)
sm.applyStreams(ctx, newCfg.RTSPURLs, newCfg.InferFPS)
}
}
}
func (sm *StreamManager) applyStreams(ctx context.Context, urls []string) {
func (sm *StreamManager) applyStreams(ctx context.Context, urls []string, fps int) {
sm.mu.Lock()
defer sm.mu.Unlock()
// Identify URLs to remove (present in sm.processes but not in new urls)
toRemove := map[string]*exec.Cmd{}
for url, cmd := range sm.processes {
toRemove := map[string]*streamProcess{}
for url, sp := range sm.processes {
found := false
for _, u := range urls {
if u == url {
@@ -70,27 +75,35 @@ func (sm *StreamManager) applyStreams(ctx context.Context, urls []string) {
}
}
if !found {
toRemove[url] = cmd
toRemove[url] = sp
}
}
// Stop removed streams
for url, cmd := range toRemove {
for url, sp := range toRemove {
log.Printf("stream: stopping %s", url)
cmd.Process.Kill()
cmd.Wait()
sm.stopProcess(sp)
delete(sm.processes, url)
}
// Start new streams
// Restart streams with changed fps
for url, sp := range sm.processes {
if sp.fps != fps {
log.Printf("stream: restarting %s (fps %d -> %d)", url, sp.fps, fps)
sm.stopProcess(sp)
delete(sm.processes, url)
}
}
// Start new / restarted streams
for i, url := range urls {
if _, exists := sm.processes[url]; !exists {
go sm.streamLoop(ctx, i, url)
go sm.streamLoop(ctx, i, url, fps)
}
}
}
func (sm *StreamManager) streamLoop(ctx context.Context, idx int, url string) {
func (sm *StreamManager) streamLoop(ctx context.Context, idx int, url string, fps int) {
deviceID := fmt.Sprintf("cam-%03d", idx)
for {
select {
@@ -99,9 +112,17 @@ func (sm *StreamManager) streamLoop(ctx context.Context, idx int, url string) {
default:
}
// Use current config fps (may have been updated)
currentFps := fps
sm.mu.Lock()
if sm.cfg != nil && sm.cfg.InferFPS > 0 {
currentFps = sm.cfg.InferFPS
}
sm.mu.Unlock()
cmd := exec.CommandContext(ctx, "ffmpeg",
"-i", url,
"-vf", fmt.Sprintf("fps=%d", sm.cfg.InferFPS),
"-vf", fmt.Sprintf("fps=%d", currentFps),
"-f", "image2pipe", "-vcodec", "mjpeg", "-q:v", "5", "-",
)
stdout, err := cmd.StdoutPipe()
@@ -111,20 +132,29 @@ func (sm *StreamManager) streamLoop(ctx context.Context, idx int, url string) {
continue
}
sp := &streamProcess{cmd: cmd, fps: currentFps}
sm.mu.Lock()
sm.processes[url] = cmd
sm.processes[url] = sp
sm.mu.Unlock()
if err := cmd.Start(); err != nil {
log.Printf("stream[%d] start failed: %v", idx, err)
sm.mu.Lock()
delete(sm.processes, url)
sm.mu.Unlock()
time.Sleep(5 * time.Second)
continue
}
log.Printf("stream[%d] connected url=%s", idx, url)
log.Printf("stream[%d] connected fps=%d url=%s", idx, currentFps, url)
sm.readFrames(ctx, stdout, idx, deviceID, url)
cmd.Wait()
// Clean up from map after exit
sm.mu.Lock()
delete(sm.processes, url)
sm.mu.Unlock()
select {
case <-ctx.Done():
return
@@ -175,12 +205,20 @@ func (sm *StreamManager) readFrames(ctx context.Context, r io.Reader, idx int, d
}
}
func (sm *StreamManager) stopProcess(sp *streamProcess) {
if sp == nil || sp.cmd == nil || sp.cmd.Process == nil {
return
}
sp.cmd.Process.Kill()
sp.cmd.Wait() // Prevent zombie processes
}
func (sm *StreamManager) stopAll() {
sm.mu.Lock()
defer sm.mu.Unlock()
for url, cmd := range sm.processes {
for url, sp := range sm.processes {
log.Printf("stream: stopping all %s", url)
cmd.Process.Kill()
sm.stopProcess(sp)
delete(sm.processes, url)
}
}