完成获取子节点接口适配
This commit is contained in:
@@ -3,48 +3,46 @@ import urllib.parse
|
|||||||
import socket
|
import socket
|
||||||
import json
|
import json
|
||||||
from http.server import ThreadingHTTPServer, SimpleHTTPRequestHandler
|
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
|
||||||
|
|
||||||
# ========== 硬编码的树形数据 ==========
|
# ========== 海康威视 API 配置 ==========
|
||||||
# 节点结构:
|
ROOT_PARENT_INDEX_CODE = "4fa15af07b6b400f94af1e35d8235c30"
|
||||||
# {
|
|
||||||
# "id": 1,
|
|
||||||
# "name": "河北省",
|
|
||||||
# "parent_id": None, # None 表示根节点
|
|
||||||
# "is_leaf": False,
|
|
||||||
# "stream_url": None # 叶子节点才会有值
|
|
||||||
# }
|
|
||||||
|
|
||||||
nodes = {
|
def transform_hikvision_node(item, parent_id=None):
|
||||||
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},
|
return {
|
||||||
3: {"id": 3, "name": "石家庄市", "parent_id": 1, "is_leaf": False, "stream_url": None},
|
"id": item["indexCode"],
|
||||||
4: {"id": 4, "name": "保定市", "parent_id": 1, "is_leaf": False, "stream_url": None},
|
"name": item["name"],
|
||||||
5: {"id": 5, "name": "郑州市", "parent_id": 2, "is_leaf": False, "stream_url": None},
|
"parent_id": parent_id or item.get("parentIndexCode"),
|
||||||
6: {"id": 6, "name": "长安区", "parent_id": 3, "is_leaf": True,
|
"is_leaf": False, # 组织机构节点不是叶子节点
|
||||||
"stream_url": "http://localhost:8355/stream.m3u8"},
|
"stream_url": None
|
||||||
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 get_children(parent_id):
|
def get_children(parent_id):
|
||||||
"""返回父节点下的直接子节点列表"""
|
"""返回父节点下的直接子节点列表(从海康威视 API 获取)"""
|
||||||
return [node for node in nodes.values() if node["parent_id"] == parent_id]
|
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):
|
def get_node(node_id):
|
||||||
"""根据 id 获取节点详情"""
|
"""根据 id 获取节点详情(暂不实现,后续可扩展)"""
|
||||||
return nodes.get(node_id)
|
return None
|
||||||
|
|
||||||
def get_stream_url(node_id):
|
def get_stream_url(node_id):
|
||||||
"""获取叶子节点的视频流地址"""
|
"""获取叶子节点的视频流地址(暂不实现,后续可扩展)"""
|
||||||
node = nodes.get(node_id)
|
|
||||||
if node and node["is_leaf"]:
|
|
||||||
return node["stream_url"]
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# ========== HTTP 处理器 ==========
|
# ========== HTTP 处理器 ==========
|
||||||
@@ -99,10 +97,9 @@ class APIHandler(SimpleHTTPRequestHandler):
|
|||||||
return
|
return
|
||||||
|
|
||||||
elif path.startswith('/api/children/'):
|
elif path.startswith('/api/children/'):
|
||||||
# GET /api/children/3
|
# GET /api/children/21020000
|
||||||
try:
|
node_id = path.split('/')[-1]
|
||||||
node_id = int(path.split('/')[-1])
|
if not node_id:
|
||||||
except (ValueError, IndexError):
|
|
||||||
self.send_error_json("Invalid node id", 400)
|
self.send_error_json("Invalid node id", 400)
|
||||||
return
|
return
|
||||||
children = get_children(node_id)
|
children = get_children(node_id)
|
||||||
@@ -110,10 +107,9 @@ class APIHandler(SimpleHTTPRequestHandler):
|
|||||||
return
|
return
|
||||||
|
|
||||||
elif path.startswith('/api/node/'):
|
elif path.startswith('/api/node/'):
|
||||||
# GET /api/node/3
|
# GET /api/node/21020000
|
||||||
try:
|
node_id = path.split('/')[-1]
|
||||||
node_id = int(path.split('/')[-1])
|
if not node_id:
|
||||||
except (ValueError, IndexError):
|
|
||||||
self.send_error_json("Invalid node id", 400)
|
self.send_error_json("Invalid node id", 400)
|
||||||
return
|
return
|
||||||
node = get_node(node_id)
|
node = get_node(node_id)
|
||||||
@@ -124,10 +120,9 @@ class APIHandler(SimpleHTTPRequestHandler):
|
|||||||
return
|
return
|
||||||
|
|
||||||
elif path.startswith('/api/stream/'):
|
elif path.startswith('/api/stream/'):
|
||||||
# GET /api/stream/6
|
# GET /api/stream/21020000
|
||||||
try:
|
node_id = path.split('/')[-1]
|
||||||
node_id = int(path.split('/')[-1])
|
if not node_id:
|
||||||
except (ValueError, IndexError):
|
|
||||||
self.send_error_json("Invalid node id", 400)
|
self.send_error_json("Invalid node id", 400)
|
||||||
return
|
return
|
||||||
url = get_stream_url(node_id)
|
url = get_stream_url(node_id)
|
||||||
|
|||||||
Reference in New Issue
Block a user