Initial commit of secondary development sample code
This commit is contained in:
47
快速接收设备告警/http-server-demo/http-server-token/headers_demo1.py
Normal file
47
快速接收设备告警/http-server-demo/http-server-token/headers_demo1.py
Normal file
@ -0,0 +1,47 @@
|
||||
import requests
|
||||
|
||||
from api.http import Headers as BaseHeaders
|
||||
from logger import LOGGER
|
||||
|
||||
|
||||
class Headers(BaseHeaders):
|
||||
def __init__(self):
|
||||
"""
|
||||
初始化Headers类
|
||||
- `self.get_headers_url`: 获取token的URL地址,根据实际环境修改。
|
||||
- `self.timeout`: 请求超时时间,设置为5秒。
|
||||
- `interval`: 定时刷新headers的时间间隔,设置为10分钟(60秒 * 10)。
|
||||
"""
|
||||
self.get_headers_url = 'http://192.168.1.75:10000/token'
|
||||
self.timeout = 5
|
||||
interval = 60 * 10
|
||||
super().__init__(interval)
|
||||
|
||||
def _generate_headers(self):
|
||||
"""
|
||||
生成请求头的方法,_generate_headers方法名不允许修改
|
||||
通过向指定的URL发送GET请求获取token,并将token添加到请求头中
|
||||
:return: 请求头字典
|
||||
"""
|
||||
try:
|
||||
# 定义请求参数
|
||||
params = {
|
||||
'arg1': 'xxx',
|
||||
'arg2': 'xxx'
|
||||
}
|
||||
if self.get_headers_url is not None:
|
||||
# 发送GET请求获取token
|
||||
resp = requests.get(self.get_headers_url, params=params, timeout=self.timeout)
|
||||
LOGGER.info('Get headers resp {}'.format(resp))
|
||||
if resp.status_code == 200:
|
||||
token = resp.text
|
||||
headers = {
|
||||
'authorization': 'Bearer {}'.format(token)
|
||||
}
|
||||
return headers
|
||||
else:
|
||||
LOGGER.error('Get headers failed')
|
||||
return None
|
||||
except:
|
||||
LOGGER.exception('_generate_headers')
|
||||
return None
|
||||
35
快速接收设备告警/http-server-demo/http-server-token/headers_demo2.py
Normal file
35
快速接收设备告警/http-server-demo/http-server-token/headers_demo2.py
Normal file
@ -0,0 +1,35 @@
|
||||
import requests
|
||||
|
||||
from api.http import Headers as BaseHeaders
|
||||
from logger import LOGGER
|
||||
|
||||
|
||||
class Headers(BaseHeaders):
|
||||
def __init__(self):
|
||||
"""
|
||||
初始化Headers类
|
||||
- `self.get_headers_url`: 获取token的URL地址,根据实际环境修改。
|
||||
- `self.timeout`: 请求超时时间,设置为5秒。
|
||||
- `interval`: 定时刷新headers的时间间隔,设置为10分钟(60秒 * 10)。
|
||||
"""
|
||||
self.get_headers_url = None
|
||||
self.timeout = 5
|
||||
interval = 60 * 10
|
||||
super().__init__(interval)
|
||||
|
||||
def _generate_headers(self):
|
||||
"""
|
||||
生成请求头的方法,_generate_headers方法名不允许修改
|
||||
固定http请求头,authorization以及值可自定义
|
||||
:return: 请求头字典
|
||||
"""
|
||||
try:
|
||||
|
||||
headers = {
|
||||
'authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkXXX'
|
||||
}
|
||||
return headers
|
||||
|
||||
except:
|
||||
LOGGER.exception('_generate_headers')
|
||||
return None
|
||||
@ -0,0 +1,45 @@
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping(path = "")
|
||||
public class AlertController {
|
||||
|
||||
|
||||
/**
|
||||
* 目标平台接收告警及告警图片
|
||||
* @param alertMsg
|
||||
* @param authorization
|
||||
*/
|
||||
|
||||
@PostMapping(path = "/alert")
|
||||
public void getAlertMsg(@RequestBody AlertMsg alertMsg, @RequestHeader("Authorization") String authorization) {
|
||||
log.info("authorization:{}", authorization);
|
||||
log.info("示例接收告警及告警图片:{}", alertMsg);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 目标平台接收告警及告警视频
|
||||
*
|
||||
* @param alertVideo
|
||||
*/
|
||||
|
||||
@PostMapping(path = "/video")
|
||||
public void getAlertVideo(@RequestBody AlertVideo alertVideo,@RequestHeader("Authorization") String authorization) {
|
||||
log.info("authorization:{}", authorization);
|
||||
log.info("示例接收告警及告警视频:{}", alertVideo);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@GetMapping(path = "/token")
|
||||
public String token() {
|
||||
return RandomUtil.randomString(16);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
|
||||
@Data
|
||||
@ToString
|
||||
public class AlertMsg {
|
||||
private String id;
|
||||
@JsonProperty("alert_time")
|
||||
private Double alertTime;
|
||||
private Object device;
|
||||
private Object source;
|
||||
private Object alg;
|
||||
private String image;
|
||||
@JsonProperty("reserved_data")
|
||||
private Object reservedData;
|
||||
@JsonProperty("hazard_leve")
|
||||
private String hazardLeve;
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
|
||||
@Data
|
||||
@ToString
|
||||
public class AlertVideo {
|
||||
private String id;
|
||||
@JsonProperty("alert_time")
|
||||
private Double alertTime;
|
||||
private Object device;
|
||||
private Object source;
|
||||
private Object alg;
|
||||
private String video;
|
||||
@JsonProperty("hazard_leve")
|
||||
private String hazardLeve;
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
import os
|
||||
import sys
|
||||
|
||||
from flask import Flask
|
||||
|
||||
CURRENT_PATH = os.path.dirname(os.path.realpath(__file__))
|
||||
sys.path.append(CURRENT_PATH)
|
||||
|
||||
url_prefix = '/'
|
||||
|
||||
from app import alert
|
||||
|
||||
|
||||
def create_app():
|
||||
# 初始化Flask对象
|
||||
app_ = Flask(__name__)
|
||||
|
||||
# 注册蓝图
|
||||
app_.register_blueprint(alert.bp)
|
||||
return app_
|
||||
|
||||
|
||||
app = create_app()
|
||||
@ -0,0 +1,31 @@
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import secrets
|
||||
import time
|
||||
|
||||
from flask import Blueprint, request
|
||||
|
||||
from app import url_prefix
|
||||
|
||||
bp = Blueprint('alert', __name__, url_prefix=url_prefix)
|
||||
|
||||
|
||||
@bp.route('alert', methods=['POST'])
|
||||
def post_alert():
|
||||
# 获取token
|
||||
auth_header = request.headers.get('authorization')
|
||||
print(f"Authorization Header: {auth_header}")
|
||||
data = json.loads(request.get_data().decode('utf-8'))
|
||||
image = data.pop('image')
|
||||
print(data)
|
||||
with open('image.jpg', 'wb') as f:
|
||||
f.write(base64.b64decode(image.encode('utf-8')))
|
||||
return data
|
||||
|
||||
|
||||
@bp.route('/token', methods=['GET'])
|
||||
def gen_token():
|
||||
print(request.args)
|
||||
token = secrets.token_hex(32)
|
||||
return token
|
||||
BIN
快速接收设备告警/http-server-demo/http-server-token/python/image.jpg
Normal file
BIN
快速接收设备告警/http-server-demo/http-server-token/python/image.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 111 KiB |
@ -0,0 +1,4 @@
|
||||
from app import app
|
||||
|
||||
if '__main__' == __name__:
|
||||
app.run(host='0.0.0.0', port=10000, debug=False)
|
||||
Reference in New Issue
Block a user