112 lines
3.5 KiB
Python
112 lines
3.5 KiB
Python
import requests
|
||
import json
|
||
|
||
|
||
def load_config(config_path="stattioncfg.json"):
|
||
"""
|
||
从JSON配置文件加载配置信息
|
||
:param config_path: 配置文件路径(默认当前目录config.json)
|
||
:return: 配置字典(成功时),None(失败时)
|
||
"""
|
||
try:
|
||
with open(config_path, "r", encoding="utf-8") as f:
|
||
config = json.load(f)
|
||
|
||
# 验证必要字段
|
||
if "station_name" not in config:
|
||
print("配置错误:缺少 station_name 字段")
|
||
return None
|
||
|
||
return config
|
||
|
||
except FileNotFoundError:
|
||
print(f"配置文件 {config_path} 未找到")
|
||
return None
|
||
except json.JSONDecodeError:
|
||
print("配置文件解析失败,请检查JSON格式")
|
||
return None
|
||
|
||
|
||
def get_camera_rtsp_config(station_name):
|
||
"""
|
||
获取指定变电站的摄像头RTSP配置信息
|
||
:param station_name: 变电站名称(字符串)
|
||
:return: 包含摄像头配置的列表(成功时),None(失败时)
|
||
"""
|
||
# 接口地址
|
||
url = "http://192.168.110.229:38091/api/services/isas/VideoElectronicFence/GetVideoCameraRTSPConfigure"
|
||
|
||
# 请求参数
|
||
params = {
|
||
"stationName": station_name
|
||
}
|
||
|
||
try:
|
||
# 发送GET请求
|
||
response = requests.get(url, params=params, timeout=10)
|
||
|
||
if response.status_code != 200:
|
||
print(f"请求失败,HTTP状态码:{response.status_code}")
|
||
return None
|
||
|
||
json_data = response.json()
|
||
|
||
if not json_data.get("success", False):
|
||
print(f"接口返回错误:{json_data.get('result', {}).get('message', '未知错误')}")
|
||
return None
|
||
|
||
result = json_data.get("result", {})
|
||
if not (result.get("flag", False) and "resultData" in result):
|
||
print("未找到有效的摄像头配置数据")
|
||
return None
|
||
|
||
# 提取并处理摄像头配置
|
||
cameras = result["resultData"]
|
||
|
||
for cam in cameras:
|
||
# 转换ROI格式
|
||
roi = cam.get('roi', '0,0,0,0')
|
||
if isinstance(roi, str):
|
||
cam['roi'] = tuple(map(int, roi.split(',')))
|
||
elif isinstance(roi, list):
|
||
cam['roi'] = tuple(map(int, roi))
|
||
else:
|
||
cam['roi'] = (0, 0, 0, 0)
|
||
|
||
# 补全RTSP参数
|
||
# rtsp_url = cam['url']
|
||
# if 'transportmode' not in rtsp_url:
|
||
# connector = '&' if '?' in rtsp_url else '?'
|
||
# cam['url'] = f"{rtsp_url}{connector}"
|
||
|
||
print(f"成功获取 {result['totalCount']} 个摄像头配置")
|
||
return cameras
|
||
|
||
except requests.exceptions.RequestException as e:
|
||
print(f"请求发生异常:{str(e)}")
|
||
return None
|
||
except ValueError:
|
||
print("响应解析失败,无效的JSON格式")
|
||
return None
|
||
|
||
|
||
# 更新后的使用示例
|
||
if __name__ == "__main__":
|
||
# 从配置文件加载设置
|
||
config = load_config()
|
||
if not config:
|
||
exit(1)
|
||
|
||
station = config["station_name"]
|
||
print(f"正在查询变电站:{station}")
|
||
|
||
cameras = get_camera_rtsp_config(station)
|
||
|
||
if cameras:
|
||
print("\n摄像头列表:")
|
||
for idx, camera in enumerate(cameras, 1):
|
||
print(f"\n摄像头 {idx}:")
|
||
print(f"名称:{camera['name']}")
|
||
print(f"ID:{camera['id']}")
|
||
print(f"RTSP地址:{camera['url']}")
|
||
print(f"ROI区域:{camera['roi']}") |