addcamera.py
8.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# -*- coding: utf-8 -*-
# @Time : 2020/12/23 9:09
# @Author : Young Lee
# @Email : young_lee2017@163.com
from selenium.webdriver.common.keys import Keys
from jumppage import jump_page
import time
import math
expanded_bar = "./div[@class='el-tree-node__content']/span[contains(@class, 'el-tree-node__expand-icon')]"
add_bar_xpath = "./div[@class='el-tree-node__content']/span[@class='custom-tree-node']/span[@class='tree-btn']" \
"/i[@class='el-icon-plus']"
def ensure_all_expanded(expanded_all):
""" 确保所有的组织/地点处于展开状态 """
for list_ele in expanded_all:
whether_expanded = list_ele.find_element_by_xpath(expanded_bar).get_attribute("class")
if "is-leaf" not in whether_expanded and "expanded" not in whether_expanded:
list_ele.find_element_by_xpath(expanded_bar).click()
time.sleep(0.3)
def build_structure(driver, expanded_all, structure):
""" 创建组织/地点 """
videos_list = expanded_all[-1]
for i in range(len(structure)):
number = int(time.time() * math.pow(10, 6))
videos_list.find_element_by_xpath(add_bar_xpath).click()
if i == len(structure)-1:
print(f"开始创建地点 '{structure[i]}'")
# 选择地点
driver.find_element_by_css_selector("input[type='text'][placeholder='请选择']").click()
driver.find_element_by_css_selector(".el-scrollbar__view>li:nth-last-child(1)").click()
else:
print(f"开始创建组织 '{structure[i]}'")
# 填入编号
driver.find_element_by_xpath(
"//label[contains(text(), '编号')]/following-sibling::div[1]//input"
).send_keys(number)
# 填入名称
driver.find_element_by_xpath(
"//label[contains(text(), '名称')]/following-sibling::div[1]//input"
).send_keys(structure[i])
# 点击确定
driver.find_element_by_xpath(
"//div[@aria-label='添加']/div[@class='el-dialog__footer']//button//span[text()='确 定']"
).click()
# videos_list.find_element_by_xpath(expanded_bar).click()
time.sleep(0.5)
# 全部展开
ensure_all_expanded(expanded_all)
videos_list = videos_list.find_element_by_xpath(
f"./div[@role='group']/div[@role='treeitem']/div[@class='el-tree-node__content']/span[@class='custom-"
f"tree-node']/span/span[text()='{structure[i]}']/../../../..")
expanded_all.append(videos_list)
def add_various_info(driver, expanded_all, cameras_info):
""" 添加相机信息 """
camera_xpath = "./div[@role='group']/div[@role='treeitem']/div[@class='el-tree-node__content']/span[@class='custom-tree-node']/span/span"
camera_names = []
videos_list = expanded_all[-1]
camera_port = 554 # 端口固定为554
ensure_all_expanded(expanded_all)
time.sleep(0.5)
last_child_class = videos_list.find_element_by_xpath("./div[last()]").get_attribute("class")
if last_child_class == "el-tree-node__children":
camera_names = [ele.get_attribute("textContent") for ele in videos_list.find_elements_by_xpath(camera_xpath)]
for info in cameras_info:
camera_name = info["camera_name"]
camera_address = info["camera_address"]
if not camera_name:
print("未填写相机名称")
continue
if not camera_address:
print("未填写视频串")
continue
if camera_names and camera_name in camera_names:
print(f"相机 '{camera_name}' 已存在!")
continue
camera_number = info["camera_number"] if "camera_number" in info.keys() else int(time.time() * math.pow(10, 6))
print(f"开始添加相机 '{camera_name}'")
# 点开添加界面
videos_list.find_element_by_xpath(add_bar_xpath).click()
# 填入设备编号
number_ele = driver.find_element_by_css_selector("label[for='vchan_refid']+.el-form-item__content>div>input")
number_ele.send_keys(Keys.CONTROL, 'a')
number_ele.send_keys(camera_number)
# 填入设备名称
name_ele = driver.find_element_by_css_selector("div[aria-label='添加相机'] label[for='name']+.el-form-item__content>div>input")
name_ele.send_keys(Keys.CONTROL, 'a')
name_ele.send_keys(camera_name)
# 填入端口
port_ele = driver.find_element_by_css_selector("label[for='port']+.el-form-item__content>div>input")
port_ele.send_keys(Keys.CONTROL, 'a')
port_ele.send_keys(camera_port)
# 选择协议‘rtsp’
protocol_ele = driver.find_element_by_xpath("//label[@class='el-form-item__label'][text()='协议']/following-sib"
"ling::div[@class='el-form-item__content']/div")
protocol_ele.click()
protocol_ele.find_element_by_xpath("//span[text()='rtsp']").click()
# 填入视频串
address_ele = driver.find_element_by_css_selector("label[for='video_source_url']+.el-form-item__content>div>input")
address_ele.send_keys(Keys.CONTROL, 'a')
address_ele.send_keys(camera_address)
# 点击确定
driver.find_element_by_xpath(
"//div[contains(@class, 'treeBox')]//div[@aria-label='添加相机']/div[@class='el-dialog__footer']/span/"
"button/span[text()='确 定']").click()
# 捕捉添加返回消息
message = driver.find_element_by_css_selector("p[class='el-message__content']").get_attribute("textContent")
if message != "添加成功":
print(f"相机 '{camera_name}' 添加失败, 提示信息: {message}")
# 点击取消
driver.find_element_by_xpath(
"//div[contains(@class, 'treeBox')]//div[@aria-label='添加相机']/div[@class='el-dialog__footer']/span/"
"button/span[text()='取 消']").click()
# 展开
time.sleep(0.5)
ensure_all_expanded(expanded_all)
# 已添加相机校验
time.sleep(0.5)
last_child_class = videos_list.find_element_by_xpath("./div[last()]").get_attribute("class")
if last_child_class == "el-tree-node__children":
camera_names = [ele.get_attribute("textContent") for ele in videos_list.find_elements_by_xpath(camera_xpath)]
return [check_info['camera_name'] for check_info in cameras_info if check_info['camera_name'] in camera_names]
def add_camera(driver, group_path, cameras_info):
"""
添加相机资源
group_path: 组织结构,如'cs1/cs2'(组织:cs1, 地点:cs2)
camera_info: 相机信息,
[{'camera_name': <camera name>, 'camera_number': <camera_number>, 'camera_address': <camera_address>}, {...}]
camera_name: 相机名称, camera_number(可选): 设备编号, camera_address: 视频串
return: 返回已成功添加的相机名称
"""
common_xpath = "//div[contains(@class, 'treeBox')]//span[text()='手动添加相机资源']/../../../.."
jump_page(driver, "资源管理", "视频设备")
expanded_all = []
group_structure = group_path.split("/")
videos_list = driver.find_element_by_xpath(common_xpath)
expanded_all.append(videos_list)
# 组织/地点若存在则展开,不存在则添加
for i in range(len(group_structure)):
whether_expanded = videos_list.find_element_by_xpath(expanded_bar).get_attribute("class")
if "is-leaf" not in whether_expanded and "expanded" not in whether_expanded:
videos_list.find_element_by_xpath(expanded_bar).click()
time.sleep(0.5)
try:
find_xpath = f"./div[@role='group']/div[@role='treeitem']/div[@class='el-tree-node__content']/span[@class" \
f"='custom-tree-node']/span/span[text()='{group_structure[i]}']/../../../.."
videos_list = videos_list.find_element_by_xpath(find_xpath)
expanded_all.append(videos_list)
if i < len(group_structure) - 1:
print(f"组织 '{group_structure[i]}' 已存在!")
else:
print(f"地点 '{group_structure[i]}' 已存在!")
except Exception:
build_structure(driver, expanded_all, group_structure[i:])
break
# 添加相机
return add_various_info(driver, expanded_all, cameras_info)
if __name__ == "__main__":
from unitinit import *
login_fx(driver, login_url, user, passwd)
group_apth = "cs1/cs2"
group_apth1 = "危险品/ces111"
info1 = {"camera_name": "test1", "camera_number": "cs2", "camera_address": "rtsp://test1.264"}
info2 = {"camera_name": "test2", "camera_number": "cs5", "camera_address": "rtsp://test2.264"}
info3 = {"camera_name": "121", "camera_address": "rtsp://test2.264"}
cameras_info = [info1, info2]
print(add_camera(driver, group_apth1, [info3]))
time.sleep(5)
driver.quit()