libushang 1 deň pred
rodič
commit
b2cf5dd4cd
4 zmenil súbory, kde vykonal 107 pridanie a 35 odobranie
  1. 5 5
      common/AvconH5API.py
  2. 4 0
      main.py
  3. BIN
      nssm.exe
  4. 98 30
      utils/hk_video_api.py

+ 5 - 5
common/AvconH5API.py

@@ -30,7 +30,7 @@ def get_token():
     }
     api_url = API_ROOT + "/token"
     print('param', params)
-    response = requests.get(url=api_url, params=params, timeout=15)
+    response = requests.get(url=api_url, params=params, timeout=5)
     print(response.text)
     if response.status_code == 200:
         result = response.json()
@@ -70,7 +70,7 @@ def get_region():
         "Authorization": "Bearer " + token
     }
     api_url = API_ROOT + "/region"
-    response = requests.get(url=api_url, headers=headers, timeout=15)
+    response = requests.get(url=api_url, headers=headers, timeout=5)
     
     if response.status_code == 200:
         result = response.json()
@@ -118,7 +118,7 @@ def get_group(region_id: str, parent_id: str = ''):
             "parent_id": parent_id
         }    
     api_url = API_ROOT + "/group"
-    response = requests.get(url=api_url, headers=headers, params=params, timeout=15)
+    response = requests.get(url=api_url, headers=headers, params=params, timeout=5)
     
     if response.status_code == 200:
         result = response.json()
@@ -232,7 +232,7 @@ def get_search_region(region_name: str):
         "region_name": region_name
     }
     api_url = API_ROOT + "/search/region"
-    response = requests.get(url=api_url, headers=headers, params=params, timeout=15)
+    response = requests.get(url=api_url, headers=headers, params=params, timeout=5)
     
     if response.status_code == 200:
         result = response.json()
@@ -289,7 +289,7 @@ def get_search_live_location(center_lat: float, center_lng: float, point_lat: fl
         "point_lng": point_lng
     }
     api_url = API_ROOT + "/search/live-location"
-    response = requests.get(url=api_url, headers=headers, params=params, timeout=15)
+    response = requests.get(url=api_url, headers=headers, params=params, timeout=5)
     
     if response.status_code == 200:
         result = response.json()

+ 4 - 0
main.py

@@ -31,7 +31,11 @@ app.mount('/static', StaticFiles(directory='static'), name='static')
 
 @app.middleware("http")
 async def process_authorization(request: Request, call_next):
+    start_time = time.time()
     response = await call_next(request)
+    process_time = (time.time() - start_time) * 1000  # 毫秒
+    logger.info(f"Request {request.method} {request.url} processed in {process_time:.2f}ms")
+    response.headers["X-Process-Time"] = str(process_time)
     return response
     
 @app.get("/", response_class=PlainTextResponse)

BIN
nssm.exe


+ 98 - 30
utils/hk_video_api.py

@@ -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):