From f8e6b6158b301d89ba2917f925974f4f5b70c443 Mon Sep 17 00:00:00 2001 From: tian <11429339@qq.com> Date: Thu, 7 May 2026 12:26:01 +0800 Subject: [PATCH] fix: alarm collector handles real media-server format --- internal/service/alarm_collector.go | 66 ++++++++++++++++++----------- 1 file changed, 42 insertions(+), 24 deletions(-) diff --git a/internal/service/alarm_collector.go b/internal/service/alarm_collector.go index e40f34e..f167952 100644 --- a/internal/service/alarm_collector.go +++ b/internal/service/alarm_collector.go @@ -89,18 +89,7 @@ func (c *AlarmCollector) fetchDeviceAlarms(dev *models.Device) ([]AlarmRecord, e } var resp struct { - Alarms []struct { - ID string `json:"id"` - Timestamp string `json:"timestamp"` - Channel string `json:"channel"` - RuleName string `json:"rule_name"` - RuleType string `json:"rule_type"` - ObjectLabel string `json:"object_label"` - Confidence float64 `json:"confidence"` - SnapshotURL string `json:"snapshot_url"` - ClipURL string `json:"clip_url"` - DurationMs int64 `json:"duration_ms"` - } `json:"alarms"` + Alarms []map[string]any `json:"alarms"` } if err := json.Unmarshal(body, &resp); err != nil { return nil, err @@ -109,23 +98,52 @@ func (c *AlarmCollector) fetchDeviceAlarms(dev *models.Device) ([]AlarmRecord, e newLastID := lastID alarms := make([]AlarmRecord, 0) for _, a := range resp.Alarms { - if a.ID == lastID { + id, _ := a["id"].(string) + if id == "" { + continue + } + if id == lastID { break // reached previously seen alarms } + // Extract fields from whatever format media-server sends + channel, _ := a["channel"].(string) + if channel == "" { + channel, _ = a["node_id"].(string) // media-server uses node_id + } + ruleName, _ := a["rule_name"].(string) + ruleType, _ := a["rule_type"].(string) + objectLabel, _ := a["object_label"].(string) + snapshotURL, _ := a["snapshot_url"].(string) + clipURL, _ := a["clip_url"].(string) + // Timestamp can be string (RFC3339) or number (unix millis) + var ts string + switch v := a["timestamp"].(type) { + case string: + ts = v + case float64: + ts = time.UnixMilli(int64(v)).Format(time.RFC3339) + } + confidence, _ := a["confidence"].(float64) + if confidence == 0 { + if score, ok := a["score"].(float64); ok { + confidence = score + } + } + durationMs, _ := a["duration_ms"].(float64) alarms = append(alarms, AlarmRecord{ - ID: a.ID, - Timestamp: a.Timestamp, - Channel: a.Channel, - RuleName: a.RuleName, - RuleType: a.RuleType, - ObjectLabel: a.ObjectLabel, - Confidence: a.Confidence, - SnapshotURL: a.SnapshotURL, - ClipURL: a.ClipURL, - DurationMs: a.DurationMs, + ID: id, + Timestamp: ts, + Channel: channel, + RuleName: ruleName, + RuleType: ruleType, + ObjectLabel: objectLabel, + Confidence: confidence, + SnapshotURL: snapshotURL, + ClipURL: clipURL, + DurationMs: int64(durationMs), }) if newLastID == "" { - newLastID = a.ID + newLastID = id } }