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.
		
		
		
		
		
			
		
			
				
					
					
						
							174 lines
						
					
					
						
							7.0 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							174 lines
						
					
					
						
							7.0 KiB
						
					
					
				
								const schedule = require('node-schedule');
							 | 
						|
								const moment = require('moment')
							 | 
						|
								
							 | 
						|
								module.exports = function (app, opts) {
							 | 
						|
								   const freshYingshiState = app.fs.scheduleInit(
							 | 
						|
								      {
							 | 
						|
								         interval: '* */5 * * * *',
							 | 
						|
								         // interval: '*/10 * * * *',
							 | 
						|
								         immediate: false,
							 | 
						|
								         proRun: true,
							 | 
						|
								      },
							 | 
						|
								      async () => {
							 | 
						|
								
							 | 
						|
								         try {
							 | 
						|
								            const startTime = moment()
							 | 
						|
								            const { models } = app.fs.dc
							 | 
						|
								            const { token4yingshi, getYingshiPlayUrl, cameraStatePush } = 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) {
							 | 
						|
								
							 | 
						|
								                     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) {
							 | 
						|
								                              // 状态更新
							 | 
						|
								                              if (storageD.online == 'ON' && !existD.playUrl) {
							 | 
						|
								                                 // 播放地址更新
							 | 
						|
								                                 const playUrlRes = await getYingshiPlayUrl({ deviceSerial: d.deviceSerial, token: tokenYingshi })
							 | 
						|
								                                 storageD.playUrl = playUrlRes
							 | 
						|
								                              }
							 | 
						|
								                              // 在离线状态更新
							 | 
						|
								                              await models.GbCamera.update(storageD, {
							 | 
						|
								                                 where: {
							 | 
						|
								                                    id: existD.id
							 | 
						|
								                                 }
							 | 
						|
								                              })
							 | 
						|
								                              // 状态向前端页面推送
							 | 
						|
								                              cameraStatePush({
							 | 
						|
								                                 gbId: existD.id,
							 | 
						|
								                                 online: storageD.online,
							 | 
						|
								                                 ipctype: storageD.ipctype,
							 | 
						|
								                              })
							 | 
						|
								                              // 记录在离线日志
							 | 
						|
								                              const cameraRes = await models.Camera.findAll({
							 | 
						|
								                                 where: {
							 | 
						|
								                                    gbId: existD.id
							 | 
						|
								                                 }
							 | 
						|
								                              })
							 | 
						|
								                              const nowTime = moment().format()
							 | 
						|
								                              let bulkCreateData = cameraRes.map(c => {
							 | 
						|
								                                 return {
							 | 
						|
								                                    cameraId: c.id,
							 | 
						|
								                                    status: storageD.online,
							 | 
						|
								                                    time: nowTime
							 | 
						|
								                                 }
							 | 
						|
								                              })
							 | 
						|
								                              if (bulkCreateData.length) {
							 | 
						|
								                                 await models.CameraStatusOfflineLog.bulkCreate(bulkCreateData)
							 | 
						|
								                              }
							 | 
						|
								                           }
							 | 
						|
								                        } else {
							 | 
						|
								                           // 播放地址更新
							 | 
						|
								                           const playUrlRes = await getYingshiPlayUrl({ deviceSerial: d.deviceSerial, token: tokenYingshi })
							 | 
						|
								                           storageD.playUrl = playUrlRes
							 | 
						|
								                           const yingshiRes = await models.GbCamera.create(storageD)
							 | 
						|
								                           await models.Camera.update({
							 | 
						|
								                              gbId: yingshiRes.id
							 | 
						|
								                           }, {
							 | 
						|
								                              where: {
							 | 
						|
								                                 serialNo: d.deviceSerial
							 | 
						|
								                              }
							 | 
						|
								                           })
							 | 
						|
								                        }
							 | 
						|
								                     }
							 | 
						|
								
							 | 
						|
								                     if (deviceRes.data.length == 50) {
							 | 
						|
								                        pageStart++
							 | 
						|
								                     } else {
							 | 
						|
								                        pageStart = -1
							 | 
						|
								                     }
							 | 
						|
								                  }
							 | 
						|
								               }
							 | 
						|
								            }
							 | 
						|
								            // console.log(deviceList);
							 | 
						|
								            console.info(`萤石设备 ${deviceList.length} 状态查询用时 ${moment().diff(startTime, 'seconds')} s`)
							 | 
						|
								         } catch (error) {
							 | 
						|
								            app.fs.logger.error(`sechedule: freshYingshiState, error: ${error}`);
							 | 
						|
								         }
							 | 
						|
								      });
							 | 
						|
								
							 | 
						|
								   const freshYingshiPlayUrl = app.fs.scheduleInit(
							 | 
						|
								      {
							 | 
						|
								         // interval: '0 0 4 */1 *',
							 | 
						|
								         // interval: '*/30 * * * *',
							 | 
						|
								         interval: '0 34 5 1 * *',
							 | 
						|
								         immediate: true,
							 | 
						|
								         proRun: true,
							 | 
						|
								      },
							 | 
						|
								      async () => {
							 | 
						|
								         try {
							 | 
						|
								            const startTime = moment()
							 | 
						|
								            const { models } = app.fs.dc
							 | 
						|
								            const { token4yingshi, getYingshiPlayUrl } = app.fs.utils
							 | 
						|
								            const secretRes = await models.SecretYingshi.findAll()
							 | 
						|
								            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) {
							 | 
						|
								                     for (let d of deviceRes.data) {
							 | 
						|
								                        const playUrlRes = await getYingshiPlayUrl({ deviceSerial: d.deviceSerial, token: tokenYingshi })
							 | 
						|
								                        await models.GbCamera.update({
							 | 
						|
								                           playUrl: playUrlRes,
							 | 
						|
								                        }, {
							 | 
						|
								                           where: {
							 | 
						|
								                              streamid: d.deviceSerial
							 | 
						|
								                           }
							 | 
						|
								                        })
							 | 
						|
								                     }
							 | 
						|
								
							 | 
						|
								                     if (deviceRes.data.length == 50) {
							 | 
						|
								                        pageStart++
							 | 
						|
								                     } else {
							 | 
						|
								                        pageStart = -1
							 | 
						|
								                     }
							 | 
						|
								                  }
							 | 
						|
								               }
							 | 
						|
								            }
							 | 
						|
								            console.info(`萤石设备播放地址更新查询用时 ${moment().diff(startTime, 'seconds')} s`)
							 | 
						|
								         } catch (error) {
							 | 
						|
								            app.fs.logger.error(`sechedule: freshYingshiPlayUrl, error: ${error}`);
							 | 
						|
								         }
							 | 
						|
								      }
							 | 
						|
								   )
							 | 
						|
								   return {
							 | 
						|
								      freshYingshiState,
							 | 
						|
								      freshYingshiPlayUrl
							 | 
						|
								   }
							 | 
						|
								}
							 |