Browse Source

Merge branch 'dev_trial' of https://gitea.free-sun.vip/free-sun/FS-IOT into dev_trial

release_0.0.2
wenlele 2 years ago
parent
commit
a6c4ac075f
  1. 3
      code/VideoAccess-VCMP/api/app/lib/controllers/camera/create.js
  2. 16
      code/VideoAccess-VCMP/api/app/lib/controllers/nvr/index.js
  3. 9
      code/VideoAccess-VCMP/api/app/lib/models/camera.js
  4. 9
      code/VideoAccess-VCMP/api/app/lib/models/gb_camera.js
  5. 167
      code/VideoAccess-VCMP/api/app/lib/schedule/freshYingshiMsg.js
  6. 116
      code/VideoAccess-VCMP/api/app/lib/schedule/freshYingshiState.js
  7. 7
      code/VideoAccess-VCMP/api/app/lib/utils/rtmp2others.js

3
code/VideoAccess-VCMP/api/app/lib/controllers/camera/create.js

@ -210,6 +210,7 @@ async function createNvrCamera (ctx) {
updateData.push({
...addedData.dataValues,
serialNo: c.streamid,
topSerialNo: serialNo,
name: c.name,
sip: corCamera.sipip,
cloudControl: c.cloudControl,
@ -219,6 +220,7 @@ async function createNvrCamera (ctx) {
createData.push({
type: 'nvr',
serialNo: c.streamid,
topSerialNo: serialNo,
name: c.name,
sip: corCamera.sipip,
cloudControl: c.cloudControl,
@ -496,6 +498,7 @@ async function createCascadeCamera (ctx) {
externalDomain,
cascadeType,
serialNo: corGbCamera.streamid,
topSerialNo: cameraParentRes.streamid,
sip: corGbCamera.sipip,
name: c.name,
gbId: corGbCamera.id,

16
code/VideoAccess-VCMP/api/app/lib/controllers/nvr/index.js

@ -110,7 +110,8 @@ async function get (ctx) {
order: [
[orderBy || 'id', orderDirection || 'DESC']
],
include: []
include: [],
distinct: true
}
let gbNvrOption = {
model: models.GbCamera,
@ -154,12 +155,9 @@ async function get (ctx) {
findOption.include.push(gbNvrOption)
const nvrRes = await models.Nvr.findAll(findOption)
const total = await models.Nvr.count({
where: findOption.where
})
const nvrRes = await models.Nvr.findAndCountAll(findOption)
const nvrIds = nvrRes.map(r => r.id)
const nvrIds = nvrRes.rows.map(r => r.id)
const cameraRes = await models.Camera.findAll({
where: {
nvrId: { $in: nvrIds }
@ -180,7 +178,7 @@ async function get (ctx) {
// 查用户信息
const createUserRes = await ctx.app.fs.authRequest.get(`user/${[...createUserIds].join(',') || -1}/message`, { query: { token } })
for (let { dataValues: n } of nvrRes) {
for (let { dataValues: n } of nvrRes.rows) {
const corCameras = cameraRes.filter(c => c.nvrId == n.id)
const corBind = axbindCameraRes.filter(b => corCameras.some(c => c.id == b.cameraId))
const corCreateUser = createUserRes.find(u => u.id == n.createUserId)
@ -200,8 +198,8 @@ async function get (ctx) {
ctx.status = 200;
ctx.body = {
total: total,
data: nvrRes
total: nvrRes.count,
data: nvrRes.rows
}
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);

9
code/VideoAccess-VCMP/api/app/lib/models/camera.js

@ -69,6 +69,15 @@ module.exports = dc => {
field: "serial_no",
autoIncrement: false
},
topSerialNo: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
comment: "gb设备 level=0 的 steamid ",
primaryKey: false,
field: "top_serial_no",
autoIncrement: false
},
cloudControl: {
type: DataTypes.BOOLEAN,
allowNull: true,

9
code/VideoAccess-VCMP/api/app/lib/models/gb_camera.js

@ -140,6 +140,15 @@ module.exports = dc => {
primaryKey: false,
field: "ipctype",
autoIncrement: false
},
playUrl: {
type: DataTypes.JSONB,
allowNull: true,
defaultValue: null,
comment: null,
primaryKey: false,
field: "playUrl",
autoIncrement: false
}
}, {
tableName: "gbCamera",

167
code/VideoAccess-VCMP/api/app/lib/schedule/freshYingshiMsg.js

@ -0,0 +1,167 @@
const schedule = require('node-schedule');
const moment = require('moment')
module.exports = function (app, opts) {
const freshYingshiState = schedule.scheduleJob(
// '* * 4 * * *',
'*/10 * * * *',
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) {
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
let cameraName = ''
if (connected) {
for (let c in connected) {
const { client: { conn: { request: { _query } } } } = connected[c]
if (_query && _query.token) {
let userInfo = await app.redis.hget(_query.token, 'userInfo');
if (userInfo) {
userInfo = JSON.parse(userInfo)
const corCameraRes = await models.Camera.findOne({
where: {
gbId: existD.id,
createUserId: userInfo.id
}
})
// TODO 管理员判断
if (corCameraRes) {
cameraName = corCameraRes.name
connected[c].join(roomId)
}
}
}
}
app.socket.to(roomId).emit('CAMERA_ONLINE', {
ipctype: 'yingshi',
online: storageD.online,
gbId: existD.id,
name: cameraName
})
}
}
} else {
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 = schedule.scheduleJob(
// '* * 4 * * *',
'*/1 * * * *',
async () => {
const protocolMap = {
}
try {
const { models } = app.fs.dc
const { token4yingshi } = 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) {
let deviceSerials = deviceRes.data.map(d => d.deviceSerial)
const devicePlayUrlRes = await app.fs.yingshiRequest.post('lapp/v2/live/address/get', {
query: {
accessToken: tokenYingshi,
deviceSerial: deviceSerials[0],
protocol: 3
}
})
if (deviceRes.data.length == 50) {
pageStart++
} else {
pageStart = -1
}
}
}
}
} catch (error) {
app.fs.logger.error(`sechedule: freshYingshiPlayUrl, error: ${error}`);
}
}
)
return {
freshYingshiState,
freshYingshiPlayUrl
}
}

116
code/VideoAccess-VCMP/api/app/lib/schedule/freshYingshiState.js

@ -1,116 +0,0 @@
const schedule = require('node-schedule');
const moment = require('moment')
module.exports = function (app, opts) {
const freshYingshiState = schedule.scheduleJob(
// '* * 4 * * *',
'*/1 * * * *',
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
let cameraName = ''
if (connected) {
for (let c in connected) {
const { client: { conn: { request: { _query } } } } = connected[c]
if (_query && _query.token) {
let userInfo = await app.redis.hget(_query.token, 'userInfo');
if (userInfo) {
userInfo = JSON.parse(userInfo)
const corCameraRes = await models.Camera.findOne({
where: {
gbId: existD.id,
createUserId: userInfo.id
}
})
// TODO 管理员判断
if (corCameraRes) {
cameraName = corCameraRes.name
connected[c].join(roomId)
}
}
}
}
app.socket.to(roomId).emit('CAMERA_ONLINE', {
ipctype: 'yingshi',
online: storageD.online,
gbId: existD.id,
name: cameraName
})
}
}
} else {
const yingshiRes = await models.GbCamera.create(storageD)
await models.Camera.update({
gbId: yingshiRes.id
}, {
where: {
serialNo: d.deviceSerial
}
})
}
}
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
}
}

7
code/VideoAccess-VCMP/api/app/lib/utils/rtmp2others.js

@ -28,7 +28,12 @@ module.exports = function (app, opts) {
}
}
const getYingshiPlayUrl = async (deviceSerial) => {
}
return {
rtmp2others
rtmp2others,
getYingshiPlayUrl,
}
}
Loading…
Cancel
Save