|
@@ -10,57 +10,125 @@ import base64
|
|
|
from requests.packages.urllib3.exceptions import InsecureRequestWarning
|
|
|
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
|
|
|
|
|
|
+HKSDK_BASE_URL = f"https://19.155.243.66/artemis"
|
|
|
+
|
|
|
+def __hksdk_generate_signature(path):
|
|
|
+ app_key = "28132932"
|
|
|
+ app_secret = "vPUDL4ilt2mENr8XEfa7"
|
|
|
+
|
|
|
+ # 设置请求头
|
|
|
+ timestamp = str(int(time.time() * 1000))
|
|
|
+ nonce = str(uuid.uuid4())
|
|
|
+ headers = {
|
|
|
+ "Accept": "*/*",
|
|
|
+ "Content-Type": "application/json",
|
|
|
+ "x-ca-key": app_key,
|
|
|
+ "x-ca-nonce": nonce,
|
|
|
+ "x-ca-timestamp": timestamp,
|
|
|
+ "x-ca-signature-headers": "x-ca-key,x-ca-nonce,x-ca-timestamp"
|
|
|
+ }
|
|
|
+ # 拼接签名字符串
|
|
|
+ nonce = headers.get("x-ca-nonce")
|
|
|
+ timestamp = headers.get("x-ca-timestamp")
|
|
|
+ sign_str = "POST\n*/*\napplication/json" + "\nx-ca-key:" + app_key + "\nx-ca-nonce:" + \
|
|
|
+ nonce + "\nx-ca-timestamp:" + timestamp + "\n" + \
|
|
|
+ "/artemis"+path
|
|
|
+
|
|
|
+ # 生成签名
|
|
|
+ signature = hmac.new(app_secret.encode(), sign_str.encode(), hashlib.sha256).digest()
|
|
|
+ headers["x-ca-signature"]=base64.b64encode(signature).decode()
|
|
|
+ return headers
|
|
|
+
|
|
|
def get_video_url(id: str, protocol: str):
|
|
|
video_url = ""
|
|
|
- # api_url = "http://10.181.7.217:8081/previewURLs"
|
|
|
- api_url = "http://10.181.7.236:8081/previewURLs"
|
|
|
- params = {
|
|
|
+ path = "/api/video/v1/cameras/previewURLs"
|
|
|
+ url = f"{HKSDK_BASE_URL}{path}"
|
|
|
+ body = {
|
|
|
"cameraIndexCode": id,
|
|
|
- "protocol": protocol
|
|
|
+ "streamType":1, # 子码流
|
|
|
+ "protocol": protocol,
|
|
|
+ "transmode": 0, # 使用TCP传输
|
|
|
+ "expand": "transcode=1&videotype=h264&resolution=D1"
|
|
|
}
|
|
|
- print('param', params)
|
|
|
- response = requests.post(url=api_url, params=params, timeout=15)
|
|
|
- print(response.text)
|
|
|
+
|
|
|
+ if protocol == 'hlss':
|
|
|
+ body['streamType'] = 0
|
|
|
+
|
|
|
+ headers = __hksdk_generate_signature(path)
|
|
|
+ response = requests.post(url, headers=headers, json=body, verify=False)
|
|
|
if response.status_code == 200:
|
|
|
- result = response.json()
|
|
|
- if result['errcode'] == 0:
|
|
|
- video_url = result['data']
|
|
|
+ data = response.json()
|
|
|
+ if isinstance(data, dict) and data['code'] == '0':
|
|
|
+ video_url = data['data']['url']
|
|
|
return video_url
|
|
|
|
|
|
def indexCode(id: str):
|
|
|
- api_url = "http://10.181.7.236:8081/indexCode"
|
|
|
- params = {
|
|
|
+ # api_url = "http://10.181.7.236:8081/indexCode"
|
|
|
+ # params = {
|
|
|
+ # "cameraIndexCode": id
|
|
|
+ # }
|
|
|
+ # # print('param', params)
|
|
|
+ # response = requests.post(url=api_url, params=params, timeout=15)
|
|
|
+ # # print(response.text)
|
|
|
+ # if response.status_code == 200:
|
|
|
+ # result = response.json()
|
|
|
+ # if result['errcode'] == 0:
|
|
|
+ # data = result['data']
|
|
|
+ # if isinstance(data,str):
|
|
|
+ # data = json.loads(data)
|
|
|
+ # return data
|
|
|
+
|
|
|
+ path = "/api/resource/v1/cameras/indexCode"
|
|
|
+ url = f"{HKSDK_BASE_URL}{path}"
|
|
|
+ body = {
|
|
|
"cameraIndexCode": id
|
|
|
}
|
|
|
- # print('param', params)
|
|
|
- response = requests.post(url=api_url, params=params, timeout=15)
|
|
|
- # print(response.text)
|
|
|
+
|
|
|
+ headers = __hksdk_generate_signature(path)
|
|
|
+ response = requests.post(url, headers=headers, json=body, verify=False)
|
|
|
+ print(response.text)
|
|
|
if response.status_code == 200:
|
|
|
- result = response.json()
|
|
|
- if result['errcode'] == 0:
|
|
|
- data = result['data']
|
|
|
- if isinstance(data,str):
|
|
|
- data = json.loads(data)
|
|
|
- return data
|
|
|
+ data = response.json()
|
|
|
+ if isinstance(data, dict) and data['code'] == '0':
|
|
|
+ return data['data']
|
|
|
+ return ""
|
|
|
|
|
|
def controlling(id: str, action: int, command: str, speed: int, presetIndex: str):
|
|
|
- api_url = "http://10.181.7.236:8081/controlling"
|
|
|
- params = {
|
|
|
+ # api_url = "http://10.181.7.236:8081/controlling"
|
|
|
+ # params = {
|
|
|
+ # "cameraIndexCode": id,
|
|
|
+ # "action": action,
|
|
|
+ # "command": command,
|
|
|
+ # "speed": speed,
|
|
|
+ # "presetIndex": presetIndex
|
|
|
+ # }
|
|
|
+ # print('param', params)
|
|
|
+ # response = requests.post(url=api_url, params=params, timeout=15)
|
|
|
+ # print(response.text)
|
|
|
+ # if response.status_code == 200:
|
|
|
+ # result = response.json()
|
|
|
+ # if result['errcode'] == 0:
|
|
|
+ # data = result['data']
|
|
|
+ # return data
|
|
|
+
|
|
|
+ path = "/api/video/v1/ptzs/controlling"
|
|
|
+ url = f"{HKSDK_BASE_URL}{path}"
|
|
|
+ body = {
|
|
|
"cameraIndexCode": id,
|
|
|
"action": action,
|
|
|
"command": command,
|
|
|
"speed": speed,
|
|
|
"presetIndex": presetIndex
|
|
|
}
|
|
|
- print('param', params)
|
|
|
- response = requests.post(url=api_url, params=params, timeout=15)
|
|
|
+
|
|
|
+ headers = __hksdk_generate_signature(path)
|
|
|
+ response = requests.post(url, headers=headers, json=body, verify=False)
|
|
|
print(response.text)
|
|
|
if response.status_code == 200:
|
|
|
- result = response.json()
|
|
|
- if result['errcode'] == 0:
|
|
|
- data = result['data']
|
|
|
- return data
|
|
|
-
|
|
|
+ data = response.json()
|
|
|
+ if isinstance(data, dict) and data['code'] == '0':
|
|
|
+ return data['data']
|
|
|
+ return ""
|
|
|
|
|
|
class HikvisionAPI:
|
|
|
def __init__(self):
|