'use strict'; async function getCameraProject (ctx, next) { try { const models = ctx.fs.dc.models; const { limit, page, orderBy, orderDirection, keyword, abilityId, type, venderId } = ctx.query const { userId, token } = ctx.fs.api let findOption = { attributes: { exclude: ['delete', 'recycleTime',] }, where: { createUserId: userId, recycleTime: null, delete: false }, order: [ [orderBy || 'id', orderDirection || 'DESC'] ], include: [{ model: models.CameraAbility }, { model: models.CameraKind }] } if (limit) { findOption.limit = limit } if (page && limit) { findOption.offset = page * limit } if (keyword) { findOption.where.$or = [{ name: { $like: `%${keyword}%` } }, { serialNo: { $like: `%${keyword}%` } }] } if (type) { findOption.where.type = type } if (abilityId) { findOption.where.abilityId = abilityId } if (venderId) { findOption.where.venderId = venderId } const cameraRes = await models.Camera.findAll(findOption) const total = await models.Camera.count({ where: findOption.where }) let cameraIds = [] let createUserIds = new Set() for (let c of cameraRes) { cameraIds.push(c.dataValues.id) createUserIds.add(c.dataValues.createUserId) } // 查在安心云绑定的数据 const axbindCameraRes = await ctx.app.fs.axyRequest.get('vcmp/camera/project', { query: { token, cameraId: cameraIds.join(',') } }) // 查对应创建者信息 const corUsers = await ctx.app.fs.authRequest.get(`user/${[...createUserIds].join(',')}/message`, { query: { token } }) for (let { dataValues: camera } of cameraRes) { const corBindCamera = axbindCameraRes.find(b => b.cameraId == camera.id) if (corBindCamera) { camera.station = corBindCamera.stations } else { camera.station = [] } const corUser = corUsers.find(u => u.id == camera.createUserId) if (corUser) { camera.createUser = { namePresent: corUser.namePresent } } else { camera.createUser = {} } } ctx.status = 200; ctx.body = { total: total, data: cameraRes } } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = {} } } async function getCamera (ctx) { try { const { models } = ctx.fs.dc; const { cameraId } = ctx.query const cameraRes = await models.Camera.findAll({ attributes: { exclude: ['delete', 'recycleTime',] }, where: { id: { $in: cameraId.split(',') } }, include: [{ model: models.CameraAbility }, { model: models.CameraKind }, { model: models.Vender }] }) ctx.status = 200; ctx.body = cameraRes } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = {} } } async function detail (ctx) { let errMsg = `获取摄像头详情失败` try { const { models } = ctx.fs.dc; const { cameraId } = ctx.params const { userId, token } = ctx.fs.api const { utils: { rtmp2others } } = ctx.app.fs const cameraRes = await models.Camera.findOne({ where: { id: cameraId } }) if (!cameraRes) { throw errMsg } const cameraProject = await ctx.app.fs.axyRequest.get('vcmp/camera/project', { query: { token, cameraId: cameraRes.id } }) const bindStations = [] for (let c of cameraProject) { for (let s of c.stations) { bindStations.push(s) } } const otherUrls = await rtmp2others(cameraRes.rtmp) ctx.status = 200; ctx.body = { ...cameraRes.dataValues, station: bindStations, videoUrl: otherUrls } } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: errMsg } } } async function getCameraListAll (ctx) { try { const { models } = ctx.fs.dc; const cameraRes = await models.Camera.findAll({ attributes: ['id', 'name', 'type'], where: { delete: false, recycleTime: null, } }) ctx.status = 200; ctx.body = cameraRes } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = {} } } async function banned (ctx) { try { const { models } = ctx.fs.dc; const data = ctx.request.body; // 向视频服务发送通知 // 库记录 await models.Camera.update({ forbidden: data.forbidden }, { where: { id: data.cameraId } }) ctx.status = 204; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = {} } } async function del (ctx) { try { const { models } = ctx.fs.dc; const { cameraId } = ctx.query const { token } = ctx.fs.api await models.cameraId.destroy({ where: { id: cameraId } }) await ctx.app.fs.axyRequest.delete('vcmp/camera/project', { query: { token, cameraId: cameraId.join(',') } }) ctx.status = 204; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = {} } } module.exports = { getCameraProject, getCamera, getCameraListAll, detail, banned, del, };