mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
70 lines
No EOL
2.4 KiB
Python
70 lines
No EOL
2.4 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 the 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")
|
|
|
|
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"]
|
|
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 the smart console is not enabled and only a single module is running, you need to add a simple OTA interface to send the 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_post),
|
|
]
|
|
)
|
|
# 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_post),
|
|
]
|
|
)
|
|
|
|
# Run the service
|
|
runner = web.AppRunner(app)
|
|
await runner.setup()
|
|
site = web.TCPSite(runner, host, port)
|
|
await site.start()
|
|
|
|
# Keep the service running
|
|
while True:
|
|
await asyncio.sleep(3600) # Check every 1 hour |