You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
185 lines
7.7 KiB
185 lines
7.7 KiB
const moment = require('moment')
|
|
|
|
module.exports = function (app, opts) {
|
|
const cameraOnlinePush = app.fs.scheduleInit(
|
|
{
|
|
interval: '* */15 * * * *',
|
|
// interval: '*/15 * * * * *',
|
|
immediate: false,
|
|
proRun: false,
|
|
},
|
|
async () => {
|
|
try {
|
|
const { models } = app.fs.dc
|
|
const { pushBySms, pushByEmail } = app.fs.utils
|
|
const configRes = await models.CameraStatusPushConfig.findAll({
|
|
where: {
|
|
forbidden: false
|
|
},
|
|
include: [
|
|
{
|
|
model: models.CameraStatusPushMonitor,
|
|
attributes: ['cameraId'],
|
|
required: false,
|
|
duplicating: true
|
|
},
|
|
{
|
|
model: models.CameraStatusPushReceiver,
|
|
attributes: ['receiver'],
|
|
duplicating: false,
|
|
required: false,
|
|
},
|
|
],
|
|
})
|
|
|
|
const timeNow = moment().format()
|
|
for (let c of configRes) {
|
|
// 查配置信息所对应的摄像头15min内的在离线状态
|
|
const cameraIds = c.cameraStatusPushMonitors.map(m => m.cameraId)
|
|
const offlineStatusRes = await models.CameraStatusOfflineLog.findAll({
|
|
where: {
|
|
cameraId: { $in: cameraIds },
|
|
time: {
|
|
$between: [moment(timeNow).subtract(15, 'minutes').format(), timeNow]
|
|
}
|
|
},
|
|
include: [{
|
|
model: models.Camera,
|
|
attributes: ['name']
|
|
}],
|
|
order: [['time', 'ASC']],
|
|
})
|
|
|
|
|
|
|
|
if (offlineStatusRes.length) {
|
|
const cameraStatusMap = {}
|
|
// 当前逻辑
|
|
// 只要最后状态是离线 就做离线推送
|
|
// 只要出现一次上线 就做上线推送
|
|
for (let s of offlineStatusRes) {
|
|
if (cameraStatusMap[s.cameraId]) {
|
|
cameraStatusMap.status.push({
|
|
cameraId: s.cameraId,
|
|
status: s.status,
|
|
time: s.time,
|
|
})
|
|
} else {
|
|
cameraStatusMap[s.cameraId] = {
|
|
status: [{
|
|
cameraId: s.cameraId,
|
|
status: s.status,
|
|
time: s.time,
|
|
}],
|
|
name: s.camera.name,
|
|
}
|
|
}
|
|
}
|
|
let offArr = []
|
|
let onArr = []
|
|
for (let k in Object.keys(cameraStatusMap).sort()) {
|
|
const data = cameraStatusMap[k]
|
|
if (data.status[0].status == 'OFF') {
|
|
offArr.push({
|
|
name: data.name,
|
|
time: data.status[0].time,
|
|
})
|
|
}
|
|
const onLineIndex = data.status.findIndex(s => s.status == 'ON')
|
|
if (onLineIndex >= 0) {
|
|
const onLineLastOffIndex = data.status.findIndex((s, i) => s.status == 'OFF' && i > onLineIndex)
|
|
let onlineData = {
|
|
cameraId: data.status[onLineIndex].cameraId,
|
|
name: data.status[onLineIndex].name,
|
|
time: data.status[onLineIndex].time,
|
|
}
|
|
if (onLineLastOffIndex >= 0) {
|
|
onlineData.offTime = data.status[onLineLastOffIndex].time
|
|
}
|
|
onArr.push(onlineData)
|
|
}
|
|
}
|
|
|
|
// 查当前配置由谁配置
|
|
const corUser = await app.fs.authRequest.get(`user/${c.createUser}/message`, {
|
|
query: {
|
|
// TODO 这里拿不到 token 去鉴权 怎么能系统间鉴权呢
|
|
// 暂时取消了鉴权系统对这里的判断
|
|
token: ''
|
|
}
|
|
})
|
|
|
|
const receiver = c.cameraStatusPushReceivers.map(r => r.receiver)
|
|
// 离线推送
|
|
if (offArr.length && c.noticeWay && c.noticeWay.includes('offline')) {
|
|
if (c.pushWay == 'email') {
|
|
// 邮件
|
|
let text = `【${corUser[0].namePresent}】账号下的设备,截止【${moment(timeNow).format('YYYY年HH时mm分')}】,有${offArr.length}个设备掉线:\n`
|
|
text += offArr.map(o => `【${o.name}】于【${o.time}】掉线,`).join('\n')
|
|
text += `\n请及时处理!`
|
|
await pushByEmail({
|
|
email: receiver,
|
|
title: '',
|
|
text,
|
|
})
|
|
} else if (c.pushWay == 'phone') {
|
|
// 短信
|
|
let text = `【${corUser[0].namePresent}】账号下的`
|
|
if (offArr.length == 1) {
|
|
text += `【${offArr[0].name}】离线,请及时处理!【${moment(offArr[0].time).format('YYYY年HH时mm分')}】`
|
|
} else {
|
|
text += offArr.map(o => `【${o.name}】`).join('')
|
|
if (text.length > 35) {
|
|
text = text.substring(0, 35) + '...'
|
|
text += `等${offArr.length}个摄像头离线`
|
|
}
|
|
text += `,请及时处理!【${moment().format('MM月DD日HH时mm分')}-${moment(timeNow).subtract(15, 'minutes').format('MM月DD日HH时mm分')}】`
|
|
}
|
|
await pushBySms({
|
|
phone: receiver,
|
|
templateCode: '',
|
|
templateParam: '',
|
|
text
|
|
})
|
|
}
|
|
}
|
|
|
|
// 上线推送
|
|
if (onArr.length && c.noticeWay && c.noticeWay.includes('online')) {
|
|
if (c.pushWay == 'email') {
|
|
// 邮件
|
|
// const outTimeCameraOff = onArr.filter(a => !a.offTime)
|
|
// if (outTimeCameraOff.length) {
|
|
// let cameraIds = outTimeCameraOff.map(a => a.cameraId)
|
|
// const lastOfflineRes = await models.CameraStatus.findAll({
|
|
// where: {
|
|
// cameraId: { $in: cameraIds },
|
|
// status: 'OFF',
|
|
// time: {
|
|
// $lt: moment(timeNow).subtract(15, 'minutes').format()
|
|
// }
|
|
// },
|
|
// group: ['cameraId'],
|
|
// attributes: ['cameraId', [sequelize.fn('max', sequelize.col('time')), 'time']],
|
|
// })
|
|
|
|
// }
|
|
|
|
} else if (c.pushWay == 'phone') {
|
|
// 短信
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log('cameraOnlinePush')
|
|
} catch (error) {
|
|
|
|
}
|
|
}
|
|
)
|
|
|
|
return {
|
|
cameraOnlinePush,
|
|
}
|
|
}
|