imou/app.py

62 lines
1.8 KiB
Python
Raw Normal View History

2024-05-14 20:43:52 +08:00
"""
This script runs the application using a development server.
It contains the definition of routes and views for the application.
"""
import time
from flask import Flask, request, jsonify
import requests
import json
from timeset import timeset
app = Flask(__name__)
2024-05-14 21:42:53 +08:00
@app.route('/send/<int:ida>', methods=['GET','POST'])
def send_custom_json(ida):
2024-05-14 20:43:52 +08:00
time_stamp,nonce,sign=timeset()
custom_json = {
"system": {
"ver": "1.0",
"sign": sign,
"appId": "lc0be9b1a760e44634",
"time": time_stamp,
"nonce": nonce
},
"params": {
"token": "At_0000hz7d3dcf1dbd884b4ba94a26c621",
"deviceId": "9F01FD3PAZ9B6B4",
"channelId": "0",
2024-05-14 21:42:53 +08:00
"operation": ida,
2024-05-14 20:43:52 +08:00
"duration": "1000"
},
"id": "1"
}
target_url = 'https://openapi.lechange.cn:443/openapi/controlMovePTZ'
headers = {'Content-Type': 'application/json'}
try:
# <20><><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>JSON<4F><4E><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
json_payload = json.dumps(custom_json)
# <20><><EFBFBD><EFBFBD>POST<53><54><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ⲿAPI
response = requests.post(target_url, data=json_payload, headers=headers)
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ɹ<EFBFBD>
if response.status_code == 200:
print('Request sent successfully')
# <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>API<50><49><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD>
return jsonify(response.json())
else:
print(f'Failed to send request, status code: {response.status_code}')
return jsonify({'error': 'Failed to send request'}), 500
except requests.exceptions.RequestException as e:
print(f'Error occurred during request: {e}')
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
import os
HOST = os.environ.get('SERVER_HOST', 'localhost')
try:
PORT = int(os.environ.get('SERVER_PORT', '5555'))
except ValueError:
PORT = 5555
app.run(HOST, PORT)