fix: alarm collector handles real media-server format

This commit is contained in:
tian 2026-05-07 12:26:01 +08:00
parent 4c90ef88c9
commit f8e6b6158b

View File

@ -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
}
}