mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-27 04:49:47 +01:00
93 lines
3.5 KiB
Python
93 lines
3.5 KiB
Python
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
|
|
|
|
TAG = __name__
|
|
|
|
|
|
class SimpleHttpServer:
|
|
def __init__(self, config: dict):
|
|
self.config = config
|
|
self.logger = setup_logging()
|
|
self.ota_handler = OTAHandler(config)
|
|
self.vision_handler = VisionHandler(config)
|
|
|
|
def _get_websocket_url(self, local_ip: str, port: int) -> str:
|
|
"""Get websocket address
|
|
|
|
Args:
|
|
local_ip: Local IP address
|
|
port: Port number
|
|
|
|
Returns:
|
|
str: websocket address
|
|
"""
|
|
server_config = self.config["server"]
|
|
websocket_config = server_config.get("websocket")
|
|
|
|
# Check if config is valid (not a placeholder "localhost", and not "127.0.0.1")
|
|
if websocket_config and websocket_config != "default":
|
|
return websocket_config
|
|
else:
|
|
return f"ws://{local_ip}:{port}/xiaozhi/v1"
|
|
|
|
async def start(self):
|
|
try:
|
|
server_config = self.config["server"]
|
|
read_config_from_api = self.config.get("read_config_from_api", False)
|
|
host = server_config.get("ip", "0.0.0.0")
|
|
port = int(server_config.get("http_port", 8003))
|
|
|
|
if port:
|
|
app = web.Application()
|
|
|
|
if not read_config_from_api:
|
|
# If console is not enabled and running as single module, add simple OTA interface for distributing websocket interface
|
|
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_options
|
|
),
|
|
# Download interface, only provides data/bin/*.bin download
|
|
web.get(
|
|
"/xiaozhi/ota/download/{filename}",
|
|
self.ota_handler.handle_download,
|
|
),
|
|
web.options(
|
|
"/xiaozhi/ota/download/{filename}",
|
|
self.ota_handler.handle_options,
|
|
),
|
|
]
|
|
)
|
|
# Add routes
|
|
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_options
|
|
),
|
|
]
|
|
)
|
|
|
|
# Run service
|
|
runner = web.AppRunner(app)
|
|
await runner.setup()
|
|
site = web.TCPSite(runner, host, port)
|
|
await site.start()
|
|
|
|
# Keep service running
|
|
while True:
|
|
await asyncio.sleep(3600) # Check every 1 hour
|
|
except Exception as e:
|
|
self.logger.bind(tag=TAG).error(f"HTTP server startup failed: {e}")
|
|
import traceback
|
|
|
|
self.logger.bind(tag=TAG).error(f"Error stack: {traceback.format_exc()}")
|
|
raise
|