告警时主动刷新定位地址
This commit is contained in:
@@ -17,7 +17,13 @@ class DeviceAlarmService(DatabaseServiceBase):
|
||||
def __init__(self):
|
||||
super().__init__(service_name="device_alarm_service")
|
||||
|
||||
async def record_alarm_event(self, *, device_id: str, source_msg_id: str = "010") -> int | None:
|
||||
async def record_alarm_event(
|
||||
self,
|
||||
*,
|
||||
device_id: str,
|
||||
source_msg_id: str = "010",
|
||||
resolve_address: bool = True,
|
||||
) -> int | None:
|
||||
db_session = await self.get_session()
|
||||
try:
|
||||
dao = DeviceAlarmDAO(db_session)
|
||||
@@ -36,7 +42,7 @@ class DeviceAlarmService(DatabaseServiceBase):
|
||||
source_msg_id=source_msg_id,
|
||||
location=location,
|
||||
)
|
||||
if alarm_id and location and location.get("lat") is not None and location.get("lng") is not None:
|
||||
if resolve_address and alarm_id and location and location.get("lat") is not None and location.get("lng") is not None:
|
||||
await task_manager.create_task(
|
||||
self.resolve_alarm_address(
|
||||
alarm_id=alarm_id,
|
||||
@@ -51,6 +57,76 @@ class DeviceAlarmService(DatabaseServiceBase):
|
||||
finally:
|
||||
await db_session.close()
|
||||
|
||||
async def get_alarm_event(
|
||||
self,
|
||||
*,
|
||||
alarm_id: int,
|
||||
device_id: str | None = None,
|
||||
) -> Mapping[str, Any] | None:
|
||||
db_session = await self.get_session()
|
||||
try:
|
||||
dao = DeviceAlarmDAO(db_session)
|
||||
return await dao.get_by_id(alarm_id=alarm_id, device_id=device_id)
|
||||
finally:
|
||||
await db_session.close()
|
||||
|
||||
async def update_alarm_location(
|
||||
self,
|
||||
*,
|
||||
alarm_id: int,
|
||||
location: Mapping[str, Any],
|
||||
) -> bool:
|
||||
db_session = await self.get_session()
|
||||
try:
|
||||
dao = DeviceAlarmDAO(db_session)
|
||||
return await dao.update_location_snapshot(alarm_id=alarm_id, location=location)
|
||||
finally:
|
||||
await db_session.close()
|
||||
|
||||
async def apply_alarm_location(
|
||||
self,
|
||||
*,
|
||||
alarm_id: int,
|
||||
device_id: str,
|
||||
location: Mapping[str, Any],
|
||||
) -> bool:
|
||||
if location.get("lat") is None or location.get("lng") is None:
|
||||
return False
|
||||
|
||||
updated = await self.update_alarm_location(alarm_id=alarm_id, location=location)
|
||||
if not updated:
|
||||
return False
|
||||
|
||||
address = str(location.get("address") or "").strip()
|
||||
if address:
|
||||
await self.update_alarm_address(
|
||||
alarm_id=alarm_id,
|
||||
address=address,
|
||||
status=ADDRESS_RESOLVE_SUCCESS,
|
||||
)
|
||||
return True
|
||||
|
||||
await self.resolve_alarm_address(
|
||||
alarm_id=alarm_id,
|
||||
device_id=device_id,
|
||||
lat=float(location["lat"]),
|
||||
lng=float(location["lng"]),
|
||||
)
|
||||
return True
|
||||
|
||||
async def resolve_alarm_current_location(self, *, alarm_id: int, device_id: str) -> bool:
|
||||
alarm = await self.get_alarm_event(alarm_id=alarm_id, device_id=device_id)
|
||||
if not alarm or alarm.get("lat") is None or alarm.get("lng") is None:
|
||||
return False
|
||||
|
||||
await self.resolve_alarm_address(
|
||||
alarm_id=alarm_id,
|
||||
device_id=device_id,
|
||||
lat=float(alarm["lat"]),
|
||||
lng=float(alarm["lng"]),
|
||||
)
|
||||
return True
|
||||
|
||||
async def resolve_alarm_address(
|
||||
self,
|
||||
*,
|
||||
|
||||
Reference in New Issue
Block a user