政务数据资源中心(Government data Resource center) 03专项3期主要建设内容
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.
 
 
 
 

110 lines
3.4 KiB

'use strict';
const request = require("superagent");
const moment = require("moment");
function getFireAlarmList(opts) {
return async function (ctx, next) {
const models = ctx.fs.dc.models;
let errMsg = { message: '获取消防告警失败' }
try {
const res = await models.FireAlarm.findAll();
ctx.status = 200;
ctx.body = res;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = errMsg
}
}
}
// 新增消防告警
function addAlarm(opts) {
return async function (ctx, next) {
const models = ctx.fs.dc.models;
try {
const body = ctx.request.body
await models.FireAlarm.create(body)
ctx.status = 204;
ctx.body = { message: '新建消防告警成功' }
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = { message: '新建消防告警失败' }
}
}
}
// 修改消防告警
function updateAlarm(opts) {
return async function (ctx, next) {
try {
const models = ctx.fs.dc.models;
const { id } = ctx.params;
const body = ctx.request.body;
await models.FireAlarm.update(
body,
{ where: { id: id, } }
)
ctx.status = 204;
ctx.body = { message: '修改消防告警成功' }
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = { message: '修改消防告警失败' }
}
}
}
// 获取消防设备
function getFireDevice(opts) {
return async function (ctx, next) {
let rslt = {};
try {
let url = `${opts.tfApi}/device/alarm/statistic?_timeStamp=${new Date().getTime()}`;
const res = (await request.get(url).set("X-API-TOKEN", "9911510d-9dae-438d-9b6e-0545ef003c58")).body.data;
rslt = res.items;
ctx.status = 200;
ctx.body = rslt;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = { message: '获取消防设备失败' }
}
}
}
// 获取火情趋势
function getFireTrend(opts) {
return async function (ctx, next) {
let rslt = [];
try {
let url = `${opts.tfApi}/alarms/trend?key=1`;
const res = (await request.get(url).set("X-API-TOKEN", "9911510d-9dae-438d-9b6e-0545ef003c58")).body.data;
let start = moment().add(-6, 'day').startOf('day').valueOf();
let end = moment().endOf('day').valueOf();
res.map(item => {
const time = moment(item.time).valueOf();
if (time >= start && time <= end) {
rslt.push({ time: item.time, count: item.val })
}
})
ctx.status = 200;
ctx.body = rslt;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = { message: '获取火情趋势失败' }
}
}
}
module.exports = {
addAlarm,
updateAlarm,
getFireAlarmList,
getFireDevice,
getFireTrend
}