xiaozhi-esp32-server/main/xiaozhi-server/core/http_server.py
MOLVMIN c6d7786b79 feat(vision): 新增工位触发端点,放卡即可让设备拍照识别
- trigger_handler.py:新增 POST /mcp/vision/trigger {device_id},
  在活动连接中按 device_id 找到设备,主动调用其拍照工具(动态查找 take_photo),
  经 vision_handler 识别后返回规范手办名;不碰串口、不写卡。
  设备未在线/未识别等业务结果统一返回 HTTP 200,便于机器客户端解析。
- websocket_server.py:给 WebSocketServer 加 active_connections 注册表
  (基于镜像版打的最小补丁),供触发端点按 device_id 反查连接。
- http_server.py / app.py:SimpleHttpServer 接收 ws_server 引用并注册触发路由。
- vision_handler.py:识别返回增加 figurine 规范名字段(带下划线),供触发端点提取。
- docker-compose:挂载新增/改动的服务端文件使其在容器内生效。

配合 zebbingo-ai-agent 的 tools/figurine-capture-station 桌面工位使用。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-20 16:13:51 +08:00

77 lines
2.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import asyncio
from aiohttp import web
from config.logger import setup_logging
from core.api.ota_handler import OTAHandler
from core.api.vision_handler import VisionHandler
from core.api.trigger_handler import TriggerHandler
TAG = __name__
class SimpleHttpServer:
def __init__(self, config: dict, ws_server=None):
self.config = config
self.logger = setup_logging()
self.ota_handler = OTAHandler(config)
self.vision_handler = VisionHandler(config)
# 工位触发端点需要按 device_id 找设备连接,故需 WebSocketServer 引用
self.trigger_handler = TriggerHandler(config, ws_server)
def _get_websocket_url(self, local_ip: str, port: int) -> str:
"""获取websocket地址
Args:
local_ip: 本地IP地址
port: 端口号
Returns:
str: websocket地址
"""
server_config = self.config["server"]
websocket_config = server_config.get("websocket")
if websocket_config and "你" not in websocket_config:
return websocket_config
else:
return f"ws://{local_ip}:{port}/xiaozhi/v1/"
async def start(self):
server_config = self.config["server"]
host = server_config.get("ip", "0.0.0.0")
port = int(server_config.get("http_port", 8003))
if port:
app = web.Application()
read_config_from_api = server_config.get("read_config_from_api", False)
if not read_config_from_api:
# 如果没有开启智控台,只是单模块运行,就需要再添加简单OTA接口,用于下发websocket接口
app.add_routes(
[
web.get("/xiaozhi/ota/", self.ota_handler.handle_get),
web.post("/xiaozhi/ota/", self.ota_handler.handle_post),
web.options("/xiaozhi/ota/", self.ota_handler.handle_post),
]
)
# 添加路由
app.add_routes(
[
web.get("/mcp/vision/explain", self.vision_handler.handle_get),
web.post("/mcp/vision/explain", self.vision_handler.handle_post),
web.options("/mcp/vision/explain", self.vision_handler.handle_post),
# 工位触发端点:放卡后让指定设备拍照识别并返回规范手办名
web.post("/mcp/vision/trigger", self.trigger_handler.handle_post),
web.options("/mcp/vision/trigger", self.trigger_handler.handle_options),
]
)
# 运行服务
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, host, port)
await site.start()
# 保持服务运行
while True:
await asyncio.sleep(3600) # 每隔 1 小时检查一次