fix(service): Alarm creation without duration - aligns with ai_edge changes

Changes:
1. Modified create_from_mqtt to parse first_frame_time from MQTT data
2. Removed duration_minutes processing logic
3. Set duration_ms=None and last_frame_time=None on alarm creation
4. Updated _determine_alarm_level to handle duration_ms=None (returns level 2 for leave_post)

This ensures alarms are created with status=NEW and no duration/end time,
which will be populated later when the alarm is resolved.

Test: test_alarm_create_no_duration.py validates the new behavior.

Related: Task 2 of alarm status management fix

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-13 09:50:53 +08:00
parent 789dc6a373
commit 683791d1c9
2 changed files with 74 additions and 10 deletions

View File

@@ -0,0 +1,53 @@
"""
Test alarm creation without duration
Verify:
1. Alarm creation with duration_ms as None
2. last_frame_time should be None
3. first_frame_time should have value
4. alarm_status should be NEW
5. handle_status should be UNHANDLED
"""
import sys
sys.path.insert(0, 'C:/Users/16337/PycharmProjects/service')
from app.services.alarm_event_service import AlarmEventService
def test_create_alarm_no_duration():
"""Test creating alarm without duration"""
service = AlarmEventService()
mqtt_data = {
"alert_type": "leave_post",
"camera_id": "cam001",
"roi_id": "roi001",
"timestamp": "2026-02-12 14:30:00",
"first_frame_time": "2026-02-12 14:23:00",
"message": "Leave post alarm",
"confidence": 0.95,
# No longer sending duration_minutes
}
print("Creating alarm test...")
alarm = service.create_from_mqtt(mqtt_data)
# Verify
assert alarm is not None, "Alarm creation failed"
assert alarm.alarm_status == "NEW", f"Status should be NEW, but got {alarm.alarm_status}"
assert alarm.handle_status == "UNHANDLED", f"Handle status should be UNHANDLED, but got {alarm.handle_status}"
assert alarm.duration_ms is None, f"duration_ms should be None, but got {alarm.duration_ms}"
assert alarm.last_frame_time is None, f"last_frame_time should be None, but got {alarm.last_frame_time}"
assert alarm.first_frame_time is not None, "first_frame_time should have value"
assert alarm.alarm_level == 2, f"alarm_level should be 2 (general level), but got {alarm.alarm_level}"
print("[PASS] Test passed: Alarm created without duration")
print(f" alarm_id: {alarm.alarm_id}")
print(f" alarm_type: {alarm.alarm_type}")
print(f" alarm_status: {alarm.alarm_status}")
print(f" alarm_level: {alarm.alarm_level}")
print(f" duration_ms: {alarm.duration_ms}")
print(f" first_frame_time: {alarm.first_frame_time}")
print(f" last_frame_time: {alarm.last_frame_time}")
print(f" event_time: {alarm.event_time}")
if __name__ == "__main__":
test_create_alarm_no_duration()