完善牧安云哨-后端
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
# consumer.py
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
import aio_pika
|
||||
|
||||
from config.rabbitMQ import *
|
||||
from models.AnalysisRequest import AnalysisRequest
|
||||
from models.SentinelRecordRequest import SentinelRecordRequest
|
||||
from service.vision import process_vehicle_animal_image
|
||||
|
||||
|
||||
async def mq_new_analysis_test(req: dict):
|
||||
"""将分析请求发送到 RabbitMQ 队列(异步版)"""
|
||||
connection = await aio_pika.connect_robust(
|
||||
f"amqp://{RABBIT_USER}:{RABBIT_PASSWORD}@{RABBIT_HOST}/{RABBIT_VHOST}"
|
||||
)
|
||||
|
||||
async with connection:
|
||||
channel = await connection.channel()
|
||||
# 声明队列,确保队列存在
|
||||
queue = await channel.declare_queue(QUEUE_NAME, durable=True)
|
||||
|
||||
message_body = json.dumps(req)
|
||||
message = aio_pika.Message(
|
||||
body=message_body.encode(),
|
||||
delivery_mode=aio_pika.DeliveryMode.PERSISTENT, # 持久化
|
||||
)
|
||||
|
||||
await channel.default_exchange.publish(message, routing_key=QUEUE_NAME)
|
||||
|
||||
|
||||
async def mq_pull_analysis_async_test():
|
||||
"""
|
||||
从队列拉取分析任务并处理
|
||||
process_func: 一个函数,接收 AnalysisRequest 对象处理分析逻辑
|
||||
"""
|
||||
connection = await aio_pika.connect_robust(
|
||||
f"amqp://{RABBIT_USER}:{RABBIT_PASSWORD}@{RABBIT_HOST}/{RABBIT_VHOST}"
|
||||
)
|
||||
async with connection:
|
||||
queue_name = QUEUE_NAME
|
||||
channel = await connection.channel()
|
||||
await channel.set_qos(prefetch_count=1)
|
||||
queue = await channel.declare_queue(queue_name, durable=True)
|
||||
|
||||
async with queue.iterator() as queue_iter:
|
||||
async for message in queue_iter:
|
||||
async with message.process():
|
||||
data = json.loads(message.body)
|
||||
req = AnalysisRequest(**data)
|
||||
print(f"收到任务: {req}")
|
||||
await asyncio.sleep(5) # 模拟处理
|
||||
print(f"完成任务: {req}")
|
||||
|
||||
|
||||
async def sentinel_new_analysis(req: SentinelRecordRequest):
|
||||
"""将分析请求发送到 RabbitMQ 队列(异步版)"""
|
||||
connection = await aio_pika.connect_robust(
|
||||
f"amqp://{RABBIT_USER}:{RABBIT_PASSWORD}@{RABBIT_HOST}/{SENTINEL_VHOST}"
|
||||
)
|
||||
|
||||
async with connection:
|
||||
channel = await connection.channel()
|
||||
# 声明队列,确保队列存在
|
||||
queue = await channel.declare_queue(QUEUE_NAME, durable=True)
|
||||
|
||||
message_body = json.dumps(req.model_dump())
|
||||
message = aio_pika.Message(
|
||||
body=message_body.encode(),
|
||||
delivery_mode=aio_pika.DeliveryMode.PERSISTENT, # 持久化
|
||||
)
|
||||
|
||||
await channel.default_exchange.publish(message, routing_key=QUEUE_NAME)
|
||||
|
||||
|
||||
async def sentinel_pull_analysis_async():
|
||||
"""
|
||||
从队列拉取分析任务并处理
|
||||
process_func: 一个函数,接收 AnalysisRequest 对象处理分析逻辑
|
||||
"""
|
||||
connection = await aio_pika.connect_robust(
|
||||
f"amqp://{RABBIT_USER}:{RABBIT_PASSWORD}@{RABBIT_HOST}/{SENTINEL_VHOST}"
|
||||
)
|
||||
async with connection:
|
||||
queue_name = QUEUE_NAME
|
||||
channel = await connection.channel()
|
||||
await channel.set_qos(prefetch_count=1)
|
||||
queue = await channel.declare_queue(queue_name, durable=True)
|
||||
|
||||
async with queue.iterator() as queue_iter:
|
||||
async for message in queue_iter:
|
||||
async with message.process():
|
||||
data = json.loads(message.body)
|
||||
req = SentinelRecordRequest(**data)
|
||||
print(f"收到任务: {req}")
|
||||
await process_vehicle_animal_image(req) # 处理
|
||||
print(f"完成任务: {req}")
|
||||
@@ -4,10 +4,15 @@ from uuid import UUID
|
||||
import config.minIO as minIO
|
||||
import db.postgres as pg
|
||||
from agent.licenseImageAgent import get_license_response
|
||||
from agent.vehicleImageAgent import get_vehicle_response
|
||||
from config.minIO import minio_client
|
||||
from config.yolo import YOLOSingleton
|
||||
from db.postgres import get_dept_id_by_iot_user_name, get_dept_ids_by_dept_id
|
||||
from db.postgres.sentinel import update_sentinel_record
|
||||
from llm.ticketLLM import *
|
||||
from llm.ticketLLMv2 import get_ticket_response_v2
|
||||
from models.SentinelRecordRequest import SentinelRecordRequest
|
||||
from routers.WS import ws_manager
|
||||
|
||||
|
||||
def process_ticket_image(
|
||||
@@ -178,3 +183,27 @@ def process_silkworm_cocoon_image(
|
||||
"postprocess_time_ms": speed_json.get("postprocess"),
|
||||
"details": results_json.get("class_counts"),
|
||||
}
|
||||
|
||||
|
||||
async def process_vehicle_animal_image(
|
||||
data: SentinelRecordRequest,
|
||||
):
|
||||
# 通过设备id获得组织id
|
||||
dept_id = get_dept_id_by_iot_user_name(data.DeviceId)
|
||||
# 得到动物类型
|
||||
oss_url = minIO.get_temp_url("sentinel", "vehicle_image/" + data.VehicleImage)
|
||||
analysis_result = await get_vehicle_response(oss_url)
|
||||
livestock_type = analysis_result.get("livestock_type", "")
|
||||
remark = analysis_result.get("remark", "")
|
||||
|
||||
available_departments = get_dept_ids_by_dept_id(dept_id)
|
||||
|
||||
await ws_manager.noticeSentinel(
|
||||
{
|
||||
"content": f"载有{livestock_type}的车辆即将进入关卡,请准备检查",
|
||||
"type": "vehicle_alert",
|
||||
},
|
||||
available_departments,
|
||||
)
|
||||
# 保存到数据库
|
||||
return update_sentinel_record(data.Id, livestock_type, remark, dept_id)
|
||||
|
||||
Reference in New Issue
Block a user