fix: accept any JSON format for alarm reports

This commit is contained in:
tian 2026-05-07 12:17:39 +08:00
parent 5d1b32c071
commit 25046d0a7b
2 changed files with 12 additions and 15 deletions

View File

@ -50,25 +50,22 @@ func (s *Server) handleAlarmReport(w http.ResponseWriter, r *http.Request) {
maxBytes := int64(1 << 20) // 1MB max
r.Body = http.MaxBytesReader(w, r.Body, maxBytes)
var alarm alarmRecord
var alarm map[string]any
if err := json.NewDecoder(r.Body).Decode(&alarm); err != nil {
errorJSON(w, http.StatusBadRequest, "invalid json: "+err.Error())
return
}
if strings.TrimSpace(alarm.Channel) == "" {
errorJSON(w, http.StatusBadRequest, "channel is required")
return
// Normalize: extract channel and rule_name from whatever format media-server sends
id, _ := alarm["id"].(string)
if id == "" {
id = fmt.Sprintf("alarm_%s_%s", time.Now().Format("20060102_150405"), randomHex(6))
}
if strings.TrimSpace(alarm.RuleName) == "" {
errorJSON(w, http.StatusBadRequest, "rule_name is required")
return
}
if alarm.ID == "" {
alarm.ID = fmt.Sprintf("alarm_%s_%s", time.Now().Format("20060102_150405"), randomHex(6))
}
if alarm.Timestamp == "" {
alarm.Timestamp = time.Now().Format(time.RFC3339)
if _, ok := alarm["timestamp"]; !ok {
alarm["timestamp"] = time.Now().Format(time.RFC3339)
}
alarm["id"] = id
alarm["received_at"] = time.Now().Format(time.RFC3339)
path := s.alarmsPath()
dir := filepath.Dir(path)
@ -95,8 +92,8 @@ func (s *Server) handleAlarmReport(w http.ResponseWriter, r *http.Request) {
return
}
s.recordAudit(r, "alarm.report", true, alarm.ID)
writeJSON(w, http.StatusOK, map[string]any{"ok": true, "id": alarm.ID})
s.recordAudit(r, "alarm.report", true, id)
writeJSON(w, http.StatusOK, map[string]any{"ok": true, "id": id})
}
// handleAlarmsRecent returns recent alarm records.

Binary file not shown.