feat: 实现 Python 与 Java SDK 连接收发及其余接口

This commit is contained in:
Nixevol
2026-09-30 07:06:30 +08:00
parent 9650cbff76
commit d357082f3d
27 changed files with 4515 additions and 1 deletions
+58
View File
@@ -0,0 +1,58 @@
"""帧编解码与注册地址推导。"""
from __future__ import annotations
import json
from typing import Any
from urllib.parse import urlparse, urlunparse
def dumps(obj: dict[str, Any]) -> bytes:
return json.dumps(obj, ensure_ascii=False, separators=(",", ":")).encode("utf-8")
def loads(data: bytes | str) -> dict[str, Any]:
if isinstance(data, bytes):
data = data.decode("utf-8")
return json.loads(data)
def register_url_from_connect(connect_url: str) -> str:
"""从连接地址推出注册 HTTP 地址(DEVELOPMENT 6.9)。"""
raw = connect_url.strip()
if "://" not in raw:
raw = "ws://" + raw
u = urlparse(raw)
scheme = u.scheme.lower()
if scheme in ("wss", "mqtts", "https"):
http_scheme = "https"
elif scheme in ("ws", "mqtt", "http"):
http_scheme = "http"
else:
http_scheme = "https" if scheme.endswith("s") else "http"
# 去掉 /mqtt 路径
path = u.path or ""
if path.endswith("/mqtt"):
path = path[: -len("/mqtt")]
path = path.rstrip("/") + "/api/client/register"
return urlunparse((http_scheme, u.netloc, path, "", "", ""))
def up_topic(endpoint_id: str) -> str:
return f"nix/c/{endpoint_id}/up"
def down_topic(endpoint_id: str) -> str:
return f"nix/c/{endpoint_id}/down"
def normalize_mqtt_ws_url(url: str) -> str:
"""保证 WebSocket 路径以 /mqtt 结尾。"""
raw = url.strip()
if "://" not in raw:
raw = "ws://" + raw
u = urlparse(raw)
path = u.path or ""
if not path.endswith("/mqtt"):
path = path.rstrip("/") + "/mqtt"
return urlunparse((u.scheme, u.netloc, path, u.params, u.query, u.fragment))