使用http-server接收海康威视摄像头上传的报警信息
设备接入
-
部分海康设备可以作为客户端,使用http协议向上级发送报警等消息。
这种情况下,平台可使用http-server驱动接收报警,简单配置脚本即可解析上传的报警消息。海康设备配置页面
平台的配置页面
本次只解析报文的json部分
上传的报文示例:{ "ipAddress": "10.XXX.XXX.XXX", "portNo": 9505, "protocol": "HTTP", "macAddress": "80:be:af:XXX:XXX:XXX", "channelID": 1, "dateTime": "2023-11-13T08:00:00+08:00", "activePostCount": 1, "isDataRetransmission": false, "eventState": "active", "channelName": "Camera 01", "eventType": "AIOP_Video", "eventDescription": "AI Open Platform Video Detection", "taskID": "ca82e6570c900001", "AIOPData": { "errorcode": 0, "version": "2.0.0", "width": "1920", "height": "1080", "frameNum": 165704, "timeStamp": 6661635, "aitype": 1003, "targets": [{ "obj": { "modelID": "XXXXXXXXXXXXXXXXXXXXXXXXX", "id": 28, "type": 1, "confidence": 941, "valid": 1, "visible": 1, "rect": { "x": "0.320312", "y": "0.252717", "w": "0.170313", "h": "0.476902" } } }], "events": { "alertInfo": [{ "target": { "modelID": "XXXXXXXXXXXXXXXXXXXX", "id": 28, "type": 1, "confidence": 941, "region": { "rect": { "x": "0.320312", "y": "0.252717", "w": "0.170313", "h": "0.476902" } } }, "ruleInfo": { "ruleID": 1, "triggerType": 1073758209, "movDir": 0, "region": { "polygon": [{ "x": "0.125000", "y": "0.018000" }, { "x": "0.195000", "y": "0.982000" }, { "x": "0.823000", "y": "0.984000" }, { "x": "0.740000", "y": "0.021000" }] }, "ruleName": "XXXXXXX" } }] } }, "PID": "20231xxxxxxxxxxx" }
脚本
/** * 数据处理脚本 * * @param {Object} context 上下文信息 * @param {Object} request 请求信息 * @param {string} request.url 请求 url 信息 * @param {string} request.method 请求方式. 例如: GET, POST, PUT 等 * @param {string} request.remoteAddr 客户端地址 * @param {string} request.path 请求路径 * @param {Object} request.headers 请求头信息. 注: 值为字符串数组. 例: {"Content-Type": ["application/json"]} * @param {Object} request.pathVariables url 路径参数. * @param {Object} request.query url 中的请求参数. 注: 值为字符串数组. 例如: {"name": ["name1", "name2"]} * @param {string} request.rawQuery url 中的请求参数. * @param {Object} request.cookies cookie 信息. 例如: {} * @param {string} request.body 请求体 * @returns {Object} */ function handler(context, request) { let strs = request.body.split("--boundary") let alarmJson if (strs && strs.length != 0) { for (let i = 0; i < strs.length; i++) { const el = strs[i]; if (el === "" || el === "--") { continue } let ss = el.split("\r\n\r\n") if (ss && ss.length === 2) { const elHeader = ss[0] const elBody = ss[1] if (elHeader.match(/application\/json/i)) { alarmJson = elBody break } } } } if (alarmJson) { const data = JSON.parse(alarmJson); console.log(JSON.stringify(data)) const deviceId = request.query.deviceId; const values = data.payload; return { "statusCode": 200, "result": [{ "id": deviceId, "values": values }] } } else { return { "statusCode": 200 } } }
脚本中的alarmJson为解析后的json对象,具体返回的数据点可根据对象内容和实际业务提取,放到values中,即可将报文上传的数据发送到平台数据点上。