Browse Source

摄像头若干接口

pull/3/head
yuan_yi 3 years ago
parent
commit
79d4f26ec1
  1. 106
      code/VideoAccess-VCMP/api/app/lib/controllers/camera/index.js
  2. 191
      code/VideoAccess-VCMP/api/app/lib/controllers/nvr/index.js
  3. 21
      code/VideoAccess-VCMP/api/app/lib/middlewares/paasRequest.js
  4. 6
      code/VideoAccess-VCMP/api/app/lib/models/vender.js
  5. 13
      code/VideoAccess-VCMP/api/app/lib/routes/camera/index.js
  6. 14
      code/VideoAccess-VCMP/api/app/lib/service/paasRequest.js

106
code/VideoAccess-VCMP/api/app/lib/controllers/camera/index.js

@ -1,12 +1,10 @@
'use strict'; 'use strict';
async function get (ctx, next) { async function getCameraProject (ctx, next) {
try { try {
const models = ctx.fs.dc.models; const models = ctx.fs.dc.models;
const { limit, page, orderBy, orderDirection } = ctx.query const { limit, page, orderBy, orderDirection } = ctx.query
const { userId } = ctx.fs.api const { userId, token } = ctx.fs.api
const aa = await ctx.app.fs.axyRequest.get('structures/types', { query: { token: 'b11b7e6d-032e-4e3e-8973-931c2c5ada5a' } })
let findOption = { let findOption = {
attributes: { exclude: ['delete', 'recycleTime',] }, attributes: { exclude: ['delete', 'recycleTime',] },
@ -32,6 +30,55 @@ async function get (ctx, next) {
} }
const cameraRes = await models.Camera.findAll(findOption) const cameraRes = await models.Camera.findAll(findOption)
const total = await models.Camera.count({
where: findOption.where
})
// 查在安心云绑定的数据
const cameraIds = cameraRes.map(c => {
return c.dataValues.id
})
const axbindCameraRes = await ctx.app.fs.axyRequest.get('vcmp/camera/project', { query: { token, cameraId: cameraIds.join(',') } })
for (let { dataValues: camera } of cameraRes) {
const corBindCamera = axbindCameraRes.find(b => b.cameraId == camera.id)
if (corBindCamera) {
camera.station = corBindCamera.stations
} else {
camera.station = []
}
}
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.status = 200;
ctx.body = cameraRes ctx.body = cameraRes
@ -42,6 +89,55 @@ async function get (ctx, next) {
} }
} }
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 = { module.exports = {
get, getCameraProject,
getCamera,
banned,
del,
}; };

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

@ -2,103 +2,128 @@
const moment = require('moment') const moment = require('moment')
async function edit (ctx, next) { async function edit (ctx, next) {
const transaction = await ctx.fs.dc.orm.transaction(); const transaction = await ctx.fs.dc.orm.transaction();
try { try {
const models = ctx.fs.dc.models; const models = ctx.fs.dc.models;
const { userId } = ctx.fs.api const { userId } = ctx.fs.api
const data = ctx.request.body; const data = ctx.request.body;
// 或取其他服务信息 // 或取其他服务信息
const nvrData = { const nvrData = {
channelCount: 8, channelCount: 8,
port: 8080, port: 8080,
} }
if (data.id) { if (data.id) {
// 修改 // 修改
const storageData = Object.assign({}, data, nvrData) const storageData = Object.assign({}, data, nvrData)
await models.Nvr.update(storageData, { await models.Nvr.update(storageData, {
where: { where: {
id: data.id id: data.id
}, },
transaction transaction
}) })
} else { } else {
// 添加 // 添加
const storageData = Object.assign({}, data, nvrData, { const storageData = Object.assign({}, data, nvrData, {
createTime: moment().format(), createTime: moment().format(),
createUserId: userId, createUserId: userId,
delete: false, delete: false,
}) })
await models.Nvr.create(storageData, { transaction }) await models.Nvr.create(storageData, { transaction })
} }
await transaction.commit(); await transaction.commit();
ctx.status = 204; ctx.status = 204;
} catch (error) { } catch (error) {
await transaction.rollback(); await transaction.rollback();
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400; ctx.status = 400;
ctx.body = {} ctx.body = {}
} }
} }
async function get (ctx) { async function get (ctx) {
const models = ctx.fs.dc.models; const models = ctx.fs.dc.models;
try { try {
const { limit, page, orderBy, orderDirection } = ctx.query const { userId, token } = ctx.fs.api
let findOption = { const { limit, page, orderBy, orderDirection } = ctx.query
attributes: { exclude: ['delete'] }, let findOption = {
where: { attributes: { exclude: ['delete'] },
delete: false, where: {
}, createUserId: userId,
order: [ delete: false,
[orderBy || 'id', orderDirection || 'DESC'] },
] order: [
} [orderBy || 'id', orderDirection || 'DESC']
if (limit) { ]
findOption.limit = limit }
} if (limit) {
if (page && limit) { findOption.limit = limit
findOption.offset = page * limit }
} if (page && limit) {
findOption.offset = page * limit
}
const res = await models.Nvr.findAll(findOption) const res = await models.Nvr.findAll(findOption)
const total = await models.Nvr.count() const total = await models.Nvr.count({
where: findOption.where
})
ctx.status = 200; ctx.status = 200;
ctx.body = { ctx.body = {
total: total, total: total,
data: res data: res
} }
} catch (error) { } catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400; ctx.status = 400;
ctx.body = {} ctx.body = {}
} }
} }
async function del (ctx, next) { async function del (ctx, next) {
try { const transaction = await ctx.fs.dc.orm.transaction();
const models = ctx.fs.dc.models; try {
const { nvrId } = ctx.params const models = ctx.fs.dc.models;
const { userId, token } = ctx.fs.api
const { nvrId } = ctx.params
await models.Nvr.destroy({ await models.Nvr.destroy({
where: { where: {
id: nvrId id: nvrId
} },
}) transaction
})
const cameraRes = await models.Camera.findAll({
where: {
nvrId
}
})
const cameraIds = cameraRes.map(c => c.id)
await models.Camera.destroy({
where: {
nvrId,
}
})
await ctx.app.fs.axyRequest.delete('vcmp/camera/project', { query: { token, cameraId: cameraIds.join(',') } })
ctx.status = 204; await transaction.commit();
} catch (error) { ctx.status = 204;
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); } catch (error) {
ctx.status = 400; await transaction.rollback();
ctx.body = {} ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
} ctx.status = 400;
ctx.body = {}
}
} }
module.exports = { module.exports = {
edit, edit,
get, get,
del, del,
}; };

21
code/VideoAccess-VCMP/api/app/lib/middlewares/paasRequest.js

@ -1,21 +0,0 @@
'use strict';
const request = require('../service/request')
function factory (app, opts) {
if (opts.pssaRequest) {
try {
for (let r of opts.pssaRequest) {
if (r.name && r.root) {
app.fs[r.name] = new request(r.root)
} else {
throw 'opts.pssaRequest 参数错误!'
}
}
} catch (error) {
console.error(error)
process.exit(-1);
}
}
}
module.exports = factory;

6
code/VideoAccess-VCMP/api/app/lib/models/vender.js

@ -29,6 +29,12 @@ module.exports = dc => {
comment: "", comment: "",
indexes: [] indexes: []
}); });
const Camera = dc.models.Camera;
Camera.belongsTo(Vender, { foreignKey: 'venderId', targetKey: 'id' });
Vender.hasMany(Camera, { foreignKey: 'venderId', sourceKey: 'id' });
dc.models.Vender = Vender; dc.models.Vender = Vender;
return Vender; return Vender;
}; };

13
code/VideoAccess-VCMP/api/app/lib/routes/camera/index.js

@ -3,6 +3,15 @@
const camera = require('../../controllers/camera'); const camera = require('../../controllers/camera');
module.exports = function (app, router, opts) { module.exports = function (app, router, opts) {
app.fs.api.logAttr['GET/camera'] = { content: '获取摄像头列表', visible: false }; app.fs.api.logAttr['GET/camera/project'] = { content: '获取摄像头列表及项目绑定信息', visible: false };
router.get('/camera', camera.get); router.get('/camera/project', camera.getCameraProject);
app.fs.api.logAttr['GET/camera'] = { content: '获取摄像头信息', visible: false };
router.get('/camera', camera.getCamera);
app.fs.api.logAttr['PUT/camera/banned'] = { content: '禁用摄像头', visible: false };
router.put('/camera/banned', camera.banned);
app.fs.api.logAttr['DEL/camera'] = { content: '删除摄像头', visible: false };
router.delete('/camera', camera.del);
}; };

14
code/VideoAccess-VCMP/api/app/lib/service/paasRequest.js

@ -20,27 +20,27 @@ class paasRequest {
}; };
} }
get = (url, { query, header = {} }) => { get = (url, { query = {}, header = {} } = {}) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
request.get(this.#buildUrl(url)).set(header).query(query).end(this.#resultHandler(resolve, reject)); request.get(this.#buildUrl(url)).set(header).query(query).end(this.#resultHandler(resolve, reject));
}) })
} }
post = (url, data, query) => { post = (url, { data = {}, query = {}, header = {} } = {}) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
request.post(this.#buildUrl(url, this.root)).set(header).query(query).send(data).end(this.#resultHandler(resolve, reject)); request.post(this.#buildUrl(url)).set(header).query(query).send(data).end(this.#resultHandler(resolve, reject));
}) })
} }
put = (url, data) => { put = (url, { data = {}, header = {}, query = {}, } = {}) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
request.put(this.#buildUrl(url)).set(setHeader()).send(data).end(this.#resultHandler(resolve, reject)); request.put(this.#buildUrl(url)).set(header).query(query).send(data).end(this.#resultHandler(resolve, reject));
}) })
} }
delete = (url) => { delete = (url, { header = {} } = {}) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
request.delete(this.#buildUrl(url)).set(setHeader()).end(this.#resultHandler(resolve, reject)); request.delete(this.#buildUrl(url)).set(header).end(this.#resultHandler(resolve, reject));
}) })
} }
} }

Loading…
Cancel
Save