const schedule = require('node-schedule'); const moment = require('moment') module.exports = function (app, opts) { const freshYingshiState = schedule.scheduleJob( // '* * 4 * * *', '*/3 * * * *', async () => { try { const startTime = moment() const { models } = app.fs.dc const { token4yingshi } = app.fs.utils const secretRes = await models.SecretYingshi.findAll() let deviceList = [] for (let s of secretRes) { const tokenYingshi = await token4yingshi(s.dataValues) // 查询所有设备 let pageStart = 0 while (pageStart >= 0) { const deviceRes = await app.fs.yingshiRequest.post('lapp/device/list', { query: { accessToken: tokenYingshi, pageStart, pageSize: 50 } }) if (deviceRes.code == 200) { if (deviceRes.data.length) { deviceList = deviceList.concat.apply(deviceList, deviceRes.data) for (let d of deviceRes.data) { const existD = await models.GbCamera.findOne({ where: { streamid: d.deviceSerial } }) let storageD = { level: 0, ipctype: 'yingshi', streamid: d.deviceSerial, online: d.status ? 'ON' : 'OFF', name: d.deviceName, } if (existD) { if (existD.online != storageD.online) { // 状态更新 await models.GbCamera.update(storageD, { where: { id: existD.id } }) // 状态推送 const { connected } = app.socket.sockets const roomId = 'ROOM_' + Math.random() + '_' + d.deviceSerial if (connected) { for (let c in connected) { connected[c].join(roomId) } app.socket.to(roomId).emit('TEST', { someProperty: `【星域 ROOM:${roomId}】呼叫自然选择号!!!`, }) } } } else { await models.GbCamera.create(storageD) } } pageStart++ } else { pageStart = -1 } } } } // console.log(deviceList); console.info(`萤石状态查询用时 ${moment().diff(startTime, 'seconds')} s`) } catch (error) { app.fs.logger.error(`sechedule: freshYingshiState, error: ${error}`); } }); return { freshYingshiState } }