|
|
|
'use strict';
|
|
|
|
|
|
|
|
const moment = require('moment')
|
|
|
|
|
|
|
|
async function createYingshi (ctx) {
|
|
|
|
let errMsg = '添加萤石摄像头失败'
|
|
|
|
const transaction = await ctx.fs.dc.orm.transaction();
|
|
|
|
try {
|
|
|
|
const { models } = ctx.fs.dc
|
|
|
|
const { userId, token } = ctx.fs.api
|
|
|
|
const { utils: { token4yingshi } } = ctx.app.fs
|
|
|
|
|
|
|
|
const {
|
|
|
|
id, name, cloudControl, highDefinition, memoryCard,
|
|
|
|
voice, kindId, abilityId, rtmp, serialNo, longitude, latitude,
|
|
|
|
} = ctx.request.body;
|
|
|
|
let handleCameraId = id
|
|
|
|
|
|
|
|
const serialNo_ = String(serialNo).toUpperCase()
|
|
|
|
const secretRes = await models.SecretYingshi.findAll()
|
|
|
|
let cameraBeloneSecretId = null
|
|
|
|
for (let s of secretRes) {
|
|
|
|
const tokenYingshi = await token4yingshi(s.dataValues)
|
|
|
|
// 检测设备所属
|
|
|
|
const cameraState = await ctx.app.fs.yingshiRequest.post('lapp/device/info', {
|
|
|
|
query: {
|
|
|
|
accessToken: tokenYingshi,
|
|
|
|
deviceSerial: serialNo_
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (cameraState.code == 200) {
|
|
|
|
cameraBeloneSecretId = s.dataValues.id
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cameraBeloneSecretId) {
|
|
|
|
errMsg = '请联系管理员核验或新增萤石云权限'
|
|
|
|
throw errMsg
|
|
|
|
}
|
|
|
|
|
|
|
|
let storageData = {
|
|
|
|
type: 'yingshi', name, cloudControl, highDefinition, memoryCard,
|
|
|
|
voice, longitude, latitude, kindId, rtmp,
|
|
|
|
serialNo: serialNo_,
|
|
|
|
yingshiSecretId: cameraBeloneSecretId
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handleCameraId) {
|
|
|
|
await models.Camera.update(storageData, {
|
|
|
|
where: {
|
|
|
|
id: handleCameraId,
|
|
|
|
},
|
|
|
|
transaction
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
storageData.createTime = moment().format()
|
|
|
|
storageData.createUserId = userId
|
|
|
|
storageData.forbidden = false
|
|
|
|
const createRes = await models.Camera.create(storageData, {
|
|
|
|
transaction
|
|
|
|
})
|
|
|
|
handleCameraId = createRes.id
|
|
|
|
}
|
|
|
|
|
|
|
|
await models.CameraAbilityBind.destroy({
|
|
|
|
where: {
|
|
|
|
cameraId: handleCameraId
|
|
|
|
},
|
|
|
|
transaction
|
|
|
|
})
|
|
|
|
if (abilityId && handleCameraId) {
|
|
|
|
let storageData = abilityId.map(aid => {
|
|
|
|
return {
|
|
|
|
cameraId: handleCameraId,
|
|
|
|
abilityId: aid
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (storageData.length) {
|
|
|
|
await models.CameraAbilityBind.bulkCreate(storageData, {
|
|
|
|
transaction
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await transaction.commit();
|
|
|
|
ctx.status = 204;
|
|
|
|
} catch (error) {
|
|
|
|
await transaction.rollback();
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {
|
|
|
|
message: errMsg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getNvrSteam (ctx) {
|
|
|
|
let errMsg = '获取 NVR 视频流失败'
|
|
|
|
try {
|
|
|
|
const { streamId } = ctx.query
|
|
|
|
const { utils: { getCameraLevel3ByStreamId } } = ctx.app.fs
|
|
|
|
|
|
|
|
const cameraRes = await getCameraLevel3ByStreamId({ streamId, errMsg })
|
|
|
|
|
|
|
|
ctx.status = 200;
|
|
|
|
ctx.body = cameraRes
|
|
|
|
} catch (error) {
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {
|
|
|
|
message: errMsg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createNvrCamera (ctx) {
|
|
|
|
let errMsg = '添加 NVR 摄像头失败'
|
|
|
|
const transaction = await ctx.fs.dc.orm.transaction();
|
|
|
|
try {
|
|
|
|
const { models } = ctx.fs.dc
|
|
|
|
const { userId, token } = ctx.fs.api
|
|
|
|
const { utils: { getCameraLevel3ByStreamId } } = ctx.app.fs
|
|
|
|
const data = ctx.request.body
|
|
|
|
const { data: camera, serialNo } = data
|
|
|
|
if (!serialNo || !camera) {
|
|
|
|
errMsg = '参数错误'
|
|
|
|
throw errMsg
|
|
|
|
}
|
|
|
|
|
|
|
|
const nvrRes = await models.Nvr.findOne({
|
|
|
|
where: {
|
|
|
|
serialNo
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!nvrRes) {
|
|
|
|
errMsg = '尚未添加相应的 NVR 设备'
|
|
|
|
throw errMsg
|
|
|
|
}
|
|
|
|
const addedCameraRes = await models.Camera.findAll({
|
|
|
|
where: {
|
|
|
|
nvrId: nvrRes.id
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const allCameraRes = await getCameraLevel3ByStreamId({ streamId: serialNo })
|
|
|
|
|
|
|
|
let createData = []
|
|
|
|
let updateData = []
|
|
|
|
for (let c of camera) {
|
|
|
|
const corCamera = allCameraRes.find(ac => ac.streamid == c.streamid)
|
|
|
|
if (corCamera) {
|
|
|
|
const addedData = addedCameraRes.find(ac => ac.serialNo == c.streamid)
|
|
|
|
if (addedData) {
|
|
|
|
updateData.push({
|
|
|
|
...addedData.dataValues,
|
|
|
|
serialNo: c.streamid,
|
|
|
|
name: c.name,
|
|
|
|
sip: corCamera.sipip,
|
|
|
|
cloudControl: c.cloudControl
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
createData.push({
|
|
|
|
serialNo: c.streamid,
|
|
|
|
name: c.name,
|
|
|
|
sip: corCamera.sipip,
|
|
|
|
cloudControl: c.cloudControl,
|
|
|
|
createTime: moment().format(),
|
|
|
|
createUserId: userId,
|
|
|
|
forbidden: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errMsg = '参数错误'
|
|
|
|
throw errMsg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (createData.length) {
|
|
|
|
await models.Camera.bulkCreate(createData, {
|
|
|
|
transaction
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (updateData.length) {
|
|
|
|
for (let u of updateData) {
|
|
|
|
await models.Camera.update(u, {
|
|
|
|
transaction
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await transaction.commit();
|
|
|
|
ctx.status = 204;
|
|
|
|
} catch (error) {
|
|
|
|
await transaction.rollback();
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {
|
|
|
|
message: errMsg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createIpcCamera (ctx) {
|
|
|
|
const transaction = await ctx.fs.dc.orm.transaction();
|
|
|
|
try {
|
|
|
|
const { models } = ctx.fs.dc
|
|
|
|
const { userId, token } = ctx.fs.api
|
|
|
|
|
|
|
|
const {
|
|
|
|
id, name, cloudControl, memoryCard,
|
|
|
|
voice, longitude, latitude, venderId, rtmp,
|
|
|
|
serialNo, kindId, abilityId,
|
|
|
|
} = ctx.request.body;
|
|
|
|
let handleCameraId = id
|
|
|
|
|
|
|
|
let storageData = {
|
|
|
|
type: 'ipc', name, cloudControl, memoryCard,
|
|
|
|
voice, longitude, latitude, rtmp, venderId,
|
|
|
|
serialNo, kindId,
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handleCameraId) {
|
|
|
|
await models.Camera.update(storageData, {
|
|
|
|
where: {
|
|
|
|
id: handleCameraId,
|
|
|
|
},
|
|
|
|
transaction
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
storageData.createTime = moment().format()
|
|
|
|
storageData.createUserId = userId
|
|
|
|
storageData.forbidden = false
|
|
|
|
const createRes = await models.Camera.create(storageData, {
|
|
|
|
transaction
|
|
|
|
})
|
|
|
|
handleCameraId = createRes.id
|
|
|
|
}
|
|
|
|
|
|
|
|
await models.CameraAbilityBind.destroy({
|
|
|
|
where: {
|
|
|
|
cameraId: handleCameraId
|
|
|
|
},
|
|
|
|
transaction
|
|
|
|
})
|
|
|
|
if (abilityId && handleCameraId) {
|
|
|
|
let storageData = abilityId.map(aid => {
|
|
|
|
return {
|
|
|
|
cameraId: handleCameraId,
|
|
|
|
abilityId: aid
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (storageData.length) {
|
|
|
|
await models.CameraAbilityBind.bulkCreate(storageData, {
|
|
|
|
transaction
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await transaction.commit();
|
|
|
|
ctx.status = 204;
|
|
|
|
} catch (error) {
|
|
|
|
await transaction.rollback();
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
createYingshi,
|
|
|
|
getNvrSteam,
|
|
|
|
createNvrCamera,
|
|
|
|
createIpcCamera,
|
|
|
|
};
|