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.
171 lines
5.1 KiB
171 lines
5.1 KiB
module.exports = function (app, opts) {
|
|
async function getGbCameraLevel3ByStreamId ({ streamId }) {
|
|
const { models } = app.fs.dc
|
|
if (!streamId) {
|
|
let errMsg = '参数错误'
|
|
throw errMsg
|
|
}
|
|
|
|
const streamIdArr = [streamId]
|
|
|
|
const findChild = async (streamIdArr) => {
|
|
const childRes = await models.GbCamera.findAll({
|
|
where: {
|
|
parent: { $in: streamIdArr },
|
|
}
|
|
})
|
|
if (!childRes.length || childRes.some(c => c.level == 2)) {
|
|
return childRes
|
|
} else {
|
|
const nextLevelStreamIds = childRes.map(level => level.streamid)
|
|
return findChild(nextLevelStreamIds)
|
|
}
|
|
}
|
|
|
|
const cameraRes = await findChild(streamIdArr)
|
|
|
|
return cameraRes
|
|
}
|
|
|
|
async function getGbCameraLevel1ByStreamId ({ streamId, where = {} }) {
|
|
const { models } = app.fs.dc
|
|
if (!streamId) {
|
|
let errMsg = '参数错误'
|
|
throw errMsg
|
|
}
|
|
|
|
const findParent = async (streamId) => {
|
|
const parentRes = await models.GbCamera.findOne({
|
|
where: {
|
|
streamid: streamId,
|
|
...where
|
|
}
|
|
})
|
|
if (!parentRes || parentRes.level == 0) {
|
|
return parentRes
|
|
} if (!parentRes.parent) {
|
|
return
|
|
} else {
|
|
const lastLevelStreamId = parentRes.parent
|
|
return findParent(lastLevelStreamId)
|
|
}
|
|
}
|
|
|
|
const deviceRes = await findParent(streamId)
|
|
|
|
return deviceRes
|
|
}
|
|
|
|
async function verifyYingshiInfo ({ serialNo } = {}) {
|
|
const { varifyYingshiBelongSecretBySerialNo } = app.fs.utils
|
|
const beloneSecret = await varifyYingshiBelongSecretBySerialNo(serialNo)
|
|
if (!beloneSecret) {
|
|
let errMsg = '请联系管理员核验或新增萤石云权限'
|
|
throw errMsg
|
|
}
|
|
return beloneSecret
|
|
}
|
|
|
|
async function verifyIpcInfo ({ serialNo } = {}) {
|
|
const { models } = app.fs.dc
|
|
const gbCameraRes = await models.GbCamera.findOne({
|
|
where: {
|
|
streamid: serialNo,
|
|
// ipctype: 'ipc'
|
|
}
|
|
})
|
|
if (!gbCameraRes) {
|
|
let errMsg = '没有找到已接入的 IPC 服务信息'
|
|
throw errMsg
|
|
}
|
|
return gbCameraRes.dataValues
|
|
}
|
|
|
|
async function verifyCascadeInfo ({ streamId } = {}) {
|
|
const { models } = app.fs.dc
|
|
const gbCameraRes = await models.GbCamera.findOne({
|
|
where: {
|
|
streamid: streamId,
|
|
level: 0,
|
|
ipctype: '级联'
|
|
}
|
|
})
|
|
if (!gbCameraRes) {
|
|
let errMsg = '没有找到已接入的级联摄像头服务信息'
|
|
throw errMsg
|
|
}
|
|
return gbCameraRes.dataValues
|
|
}
|
|
|
|
async function getPlayUrl ({ topSerialNo, serialNo }) {
|
|
const { cameraPlayHost } = opts
|
|
|
|
if (!topSerialNo || !serialNo) {
|
|
return null
|
|
}
|
|
|
|
return {
|
|
liveUrl: {
|
|
sd: {
|
|
'WS-RAW': `${cameraPlayHost.ws}/jessica/${topSerialNo}/${serialNo}`,
|
|
'WS-FLV': `${cameraPlayHost.ws}/jessica/${topSerialNo}/${serialNo}.flv`,
|
|
'HTTP-FLV': `${cameraPlayHost.httpFlv}/hdl/${topSerialNo}/${serialNo}.flv`,
|
|
'HLS': `${cameraPlayHost.httpFlv}/hls/${topSerialNo}/${serialNo}.m3u8`,
|
|
'RTMP': `${cameraPlayHost.rtmp}/${topSerialNo}/${serialNo}`,
|
|
'RTSP': `${cameraPlayHost.rtsp}/${topSerialNo}/${serialNo}`,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
async function cameraStatePush ({ gbId, online, ipctype }) {
|
|
try {
|
|
const { models } = app.fs.dc
|
|
const { connected } = app.socket.sockets
|
|
const roomId = 'ROOM_' + Math.random() + '_' + gbId
|
|
let cameraName = ''
|
|
if (connected) {
|
|
// TODO 摄像头绑定关系查询
|
|
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: gbId,
|
|
createUserId: userInfo.id
|
|
}
|
|
})
|
|
// TODO 管理员判断
|
|
if (corCameraRes) {
|
|
cameraName = corCameraRes.name
|
|
connected[c].join(roomId)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
app.socket.to(roomId).emit('CAMERA_ONLINE', {
|
|
ipctype: ipctype,
|
|
online: online,
|
|
gbId: gbId,
|
|
name: cameraName
|
|
})
|
|
}
|
|
} catch (error) {
|
|
app.fs.logger.error(`utils: cameraStatePush, error: ${error}`);
|
|
}
|
|
}
|
|
|
|
return {
|
|
getGbCameraLevel1ByStreamId,
|
|
getGbCameraLevel3ByStreamId,
|
|
verifyYingshiInfo,
|
|
verifyIpcInfo,
|
|
verifyCascadeInfo,
|
|
getPlayUrl,
|
|
cameraStatePush,
|
|
}
|
|
}
|