'use strict'; const moment = require('moment'); async function record (ctx) { try { const { models } = ctx.fs.dc; const { statusCode, description = '', platform = 'yingshi', serialNo, channelNo } = ctx.request.body; // statusCode == 0 是自动恢复 const isRestore = statusCode == 0 let statusRes = isRestore ? null : await models.CameraStatus.findOne({ where: { status: statusCode, platform, } }) let alarmRes = null; if (!statusRes && !isRestore) { // 没有这种告警状态就新建一个 statusRes = await models.CameraStatus.create({ platform: platform, status: statusCode, describe: description, forbidden: false, }) } else { let findWhere = { confirmTime: null, serialNo, channelNo, platform, } if (isRestore) { } else { findWhere.description = description } if (statusRes) { findWhere.statusId = statusRes.id } alarmRes = await models.CameraStatusAlarm[isRestore ? 'findAll' : 'findOne']({ where: findWhere }) } if ( (!isRestore && alarmRes) || (isRestore && alarmRes && alarmRes.length) ) { let updateD = { updateTime: moment().format('YYYY-MM-DD HH:mm:ss'), } if (isRestore) { updateD.autoRestore = true updateD.confirmTime = moment().format('YYYY-MM-DD HH:mm:ss') } await models.CameraStatusAlarm.update(updateD, { where: { id: { $in: isRestore ? alarmRes.map(a => a.id) : [alarmRes.id] } } }) } else if (!isRestore) { const createTime = moment().format('YYYY-MM-DD HH:mm:ss'); alarmRes = await models.CameraStatusAlarm.create({ statusId: statusRes.id, description, createTime: createTime, serialNo, channelNo, platform, }) try { ctx.app.fs.pomsRequest.post(`alarm/video/added_log`, { data: { serial_no: serialNo, channel_no: channelNo, create_time: createTime, description: description, status_id: statusRes.id, } }) } catch (error) { ctx.fs.logger.error(` error: ${error}`); } } ctx.status = 204; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } async function confirm (ctx) { try { const { models } = ctx.fs.dc; const { alarmId, content } = ctx.request.body; await models.CameraStatusAlarm.update({ confirm: content, confirmTime: moment().format(), }, { where: { id: { $in: alarmId }, confirmTime: null } }) ctx.status = 204; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } module.exports = { record, confirm, };