完成获取子节点接口适配
This commit is contained in:
@@ -3,48 +3,46 @@ import urllib.parse
|
||||
import socket
|
||||
import json
|
||||
from http.server import ThreadingHTTPServer, SimpleHTTPRequestHandler
|
||||
import sys
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
from utils.hikvision_cam_utils import get_organization_list
|
||||
|
||||
# ========== 硬编码的树形数据 ==========
|
||||
# 节点结构:
|
||||
# {
|
||||
# "id": 1,
|
||||
# "name": "河北省",
|
||||
# "parent_id": None, # None 表示根节点
|
||||
# "is_leaf": False,
|
||||
# "stream_url": None # 叶子节点才会有值
|
||||
# }
|
||||
# ========== 海康威视 API 配置 ==========
|
||||
ROOT_PARENT_INDEX_CODE = "4fa15af07b6b400f94af1e35d8235c30"
|
||||
|
||||
nodes = {
|
||||
1: {"id": 1, "name": "河北省", "parent_id": None, "is_leaf": False, "stream_url": None},
|
||||
2: {"id": 2, "name": "河南省", "parent_id": None, "is_leaf": False, "stream_url": None},
|
||||
3: {"id": 3, "name": "石家庄市", "parent_id": 1, "is_leaf": False, "stream_url": None},
|
||||
4: {"id": 4, "name": "保定市", "parent_id": 1, "is_leaf": False, "stream_url": None},
|
||||
5: {"id": 5, "name": "郑州市", "parent_id": 2, "is_leaf": False, "stream_url": None},
|
||||
6: {"id": 6, "name": "长安区", "parent_id": 3, "is_leaf": True,
|
||||
"stream_url": "http://localhost:8355/stream.m3u8"},
|
||||
7: {"id": 7, "name": "桥西区", "parent_id": 3, "is_leaf": True,
|
||||
"stream_url": "https://example.com/live/qiaoxi.m3u8"},
|
||||
8: {"id": 8, "name": "竞秀区", "parent_id": 4, "is_leaf": True,
|
||||
"stream_url": "https://example.com/live/jingxiu.m3u8"},
|
||||
9: {"id": 9, "name": "莲池区", "parent_id": 4, "is_leaf": True,
|
||||
"stream_url": "https://example.com/live/lianchi.m3u8"},
|
||||
10: {"id": 10, "name": "中原区", "parent_id": 5, "is_leaf": True,
|
||||
"stream_url": "https://example.com/live/zhongyuan.m3u8"},
|
||||
}
|
||||
def transform_hikvision_node(item, parent_id=None):
|
||||
"""将海康威视返回的节点转换为前端期望的格式"""
|
||||
return {
|
||||
"id": item["indexCode"],
|
||||
"name": item["name"],
|
||||
"parent_id": parent_id or item.get("parentIndexCode"),
|
||||
"is_leaf": False, # 组织机构节点不是叶子节点
|
||||
"stream_url": None
|
||||
}
|
||||
|
||||
def get_children(parent_id):
|
||||
"""返回父节点下的直接子节点列表"""
|
||||
return [node for node in nodes.values() if node["parent_id"] == parent_id]
|
||||
"""返回父节点下的直接子节点列表(从海康威视 API 获取)"""
|
||||
if parent_id is None:
|
||||
parent_id = ROOT_PARENT_INDEX_CODE
|
||||
|
||||
try:
|
||||
result = get_organization_list(parent_id)
|
||||
if result.get("code") != "0":
|
||||
print(f"海康威视 API 返回错误: {result.get('msg')}")
|
||||
return []
|
||||
|
||||
items = result.get("data", {}).get("list", [])
|
||||
return [transform_hikvision_node(item, parent_id) for item in items]
|
||||
except Exception as e:
|
||||
print(f"调用海康威视 API 失败: {e}")
|
||||
return []
|
||||
|
||||
def get_node(node_id):
|
||||
"""根据 id 获取节点详情"""
|
||||
return nodes.get(node_id)
|
||||
"""根据 id 获取节点详情(暂不实现,后续可扩展)"""
|
||||
return None
|
||||
|
||||
def get_stream_url(node_id):
|
||||
"""获取叶子节点的视频流地址"""
|
||||
node = nodes.get(node_id)
|
||||
if node and node["is_leaf"]:
|
||||
return node["stream_url"]
|
||||
"""获取叶子节点的视频流地址(暂不实现,后续可扩展)"""
|
||||
return None
|
||||
|
||||
# ========== HTTP 处理器 ==========
|
||||
@@ -99,10 +97,9 @@ class APIHandler(SimpleHTTPRequestHandler):
|
||||
return
|
||||
|
||||
elif path.startswith('/api/children/'):
|
||||
# GET /api/children/3
|
||||
try:
|
||||
node_id = int(path.split('/')[-1])
|
||||
except (ValueError, IndexError):
|
||||
# GET /api/children/21020000
|
||||
node_id = path.split('/')[-1]
|
||||
if not node_id:
|
||||
self.send_error_json("Invalid node id", 400)
|
||||
return
|
||||
children = get_children(node_id)
|
||||
@@ -110,10 +107,9 @@ class APIHandler(SimpleHTTPRequestHandler):
|
||||
return
|
||||
|
||||
elif path.startswith('/api/node/'):
|
||||
# GET /api/node/3
|
||||
try:
|
||||
node_id = int(path.split('/')[-1])
|
||||
except (ValueError, IndexError):
|
||||
# GET /api/node/21020000
|
||||
node_id = path.split('/')[-1]
|
||||
if not node_id:
|
||||
self.send_error_json("Invalid node id", 400)
|
||||
return
|
||||
node = get_node(node_id)
|
||||
@@ -124,10 +120,9 @@ class APIHandler(SimpleHTTPRequestHandler):
|
||||
return
|
||||
|
||||
elif path.startswith('/api/stream/'):
|
||||
# GET /api/stream/6
|
||||
try:
|
||||
node_id = int(path.split('/')[-1])
|
||||
except (ValueError, IndexError):
|
||||
# GET /api/stream/21020000
|
||||
node_id = path.split('/')[-1]
|
||||
if not node_id:
|
||||
self.send_error_json("Invalid node id", 400)
|
||||
return
|
||||
url = get_stream_url(node_id)
|
||||
|
||||
Reference in New Issue
Block a user