石墨消解仪后端用nodejs编写,与嵌入式端交互和前端交互均用ws
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.
 

286 lines
8.2 KiB

const Environment = require("../model/environment.model");
const { isIntTime } = require("../utils/common");
const moment = require("moment");
const { Op } = require("sequelize");
class EnvironmentService {
async getEnvironmentByCoopId(house_id, coop_id) {
// 目前没有栋舍概念 暂且注释,取平均值
const res = await Environment.findAll({
where: {
coop_id,
house_id,
},
order: [["log_time", "DESC"]],
});
const arr = res.map((item) => item.dataValues);
if (arr && arr.length > 0) {
const len = arr.length;
let tempTotal = 0;
let humidityTotal = 0;
let co2Total = 0;
arr.map((i) => {
tempTotal += parseInt(i.temperature);
humidityTotal += parseInt(i.humidity);
co2Total += parseInt(i.co2);
});
return {
...arr[0],
temperature: (tempTotal / len).toFixed(2),
humidity: (humidityTotal / len).toFixed(2),
co2: (co2Total / len).toFixed(2),
};
}
return null;
}
async addNewLog(
house_id,
coop_id,
temperature,
humidity,
co2,
nh3,
illumination,
wind_speed,
h2s
) {
// 判断当前时间是否为正点 如果是整点则记录
// if (isIntTime()) {
const log_time = moment().format("YYYY-MM-DD HH:mm:ss");
const res = await Environment.create({
house_id,
coop_id,
temperature,
humidity,
co2,
nh3,
illumination,
wind_speed,
h2s,
log_time,
});
return res ? res.dataValues : null;
// }
// return null;
}
async getEnvironmentHistoryList(house_id, coop_id, time_id, indicator_id) {
let whereObj = {
house_id,
coop_id,
};
// 需要根据id进行筛选 7天、全部、24小时
if (time_id == "1") {
// 24小时
Object.assign(whereObj, {
log_time: {
[Op.lt]: new Date(),
[Op.gt]: new Date(new Date() - 24 * 60 * 60 * 1000),
},
});
}
if (time_id == "2") {
Object.assign(whereObj, {
log_time: {
[Op.lt]: new Date(),
[Op.gt]: new Date(new Date() - 7 * 24 * 60 * 60 * 1000),
},
});
}
const res = await Environment.findAll({
where: whereObj,
});
const arr = res.map((item) => item.dataValues);
// 对数据进行处理
let timeList = [];
let temperatureList = [];
let humidityList = [];
let co2List = [];
let nh3List = [];
let illuminationList = [];
let windSpeedList = [];
let h2sList = [];
if (time_id == "1") {
// 24小时的数据 需要展示该小时的平均数据 默认展示7条 前端可以滚动展示
let obj = {};
arr.map((item) => {
const hour = moment(item.log_time).format("HH");
if (obj[hour]) {
obj[hour] = [...obj[hour], item];
} else {
obj[hour] = [item];
}
});
timeList = Object.keys(obj)?.map((item) => item + ":00");
let dataObj = [];
Object.values(obj)?.map((item) => {
const len = item.length;
if (len != 0) {
let tempTotal = 0;
let humidityTotal = 0;
let co2Total = 0;
let nh3Total = 0;
let illuminationTotal = 0;
let windSpeedTotal = 0;
let h2sTotal = 0;
item.map((i) => {
tempTotal += parseInt(i.temperature);
humidityTotal += parseInt(i.humidity);
co2Total += parseInt(i.co2);
nh3Total += parseInt(i.nh3);
illuminationTotal += parseInt(i.illumination);
windSpeedTotal += parseInt(i.wind_speed);
h2sTotal += parseInt(i.h2s);
});
dataObj.push({
temperature: (tempTotal / len).toFixed(2),
humidity: (humidityTotal / len).toFixed(2),
co2: (co2Total / len).toFixed(2),
nh3: (nh3Total / len).toFixed(2),
illumination: (illuminationTotal / len).toFixed(2),
wind_speed: (windSpeedTotal / len).toFixed(2),
h2s: (h2sTotal / len).toFixed(2),
});
}
});
temperatureList = dataObj.map((item) => item.temperature);
humidityList = dataObj.map((item) => item.humidity);
co2List = dataObj.map((item) => item.co2);
nh3List = dataObj.map((item) => item.nh3);
illuminationList = dataObj.map((item) => item.illumination);
windSpeedList = dataObj.map((item) => item.wind_speed);
h2sList = dataObj.map((item) => item.h2s);
} else {
// 7天和全部的按照 每天进行汇总,计算出每天的平均并返回 默认展示7条 前端可以滚动展示
// TODO
let obj = {};
arr.map((item) => {
const hour = moment(item.log_time).format("YYYY-MM-DD");
if (obj[hour]) {
obj[hour] = [...obj[hour], item];
} else {
obj[hour] = [item];
}
});
timeList = Object.keys(obj);
let dataObj = [];
Object.values(obj)?.map((item) => {
const len = item.length;
if (len != 0) {
let tempTotal = 0;
let humidityTotal = 0;
let co2Total = 0;
let nh3Total = 0;
let illuminationTotal = 0;
let windSpeedTotal = 0;
let h2sTotal = 0;
item.map((i) => {
tempTotal += parseInt(i.temperature);
humidityTotal += parseInt(i.humidity);
co2Total += parseInt(i.co2);
nh3Total += parseInt(i.nh3);
illuminationTotal += parseInt(i.illumination);
windSpeedTotal += parseInt(i.wind_speed);
h2sTotal += parseInt(i.h2s);
});
dataObj.push({
temperature: (tempTotal / len).toFixed(2),
humidity: (humidityTotal / len).toFixed(2),
co2: (co2Total / len).toFixed(2),
nh3: (nh3Total / len).toFixed(2),
illumination: (illuminationTotal / len).toFixed(2),
wind_speed: (windSpeedTotal / len).toFixed(2),
h2s: (h2sTotal / len).toFixed(2),
});
}
});
temperatureList = dataObj.map((item) => item.temperature);
humidityList = dataObj.map((item) => item.humidity);
co2List = dataObj.map((item) => item.co2);
nh3List = dataObj.map((item) => item.nh3);
illuminationList = dataObj.map((item) => item.illumination);
windSpeedList = dataObj.map((item) => item.wind_speed);
h2sList = dataObj.map((item) => item.h2s);
}
// 根据所选监测数据进行筛选
if (indicator_id == "1") {
return {
yData: temperatureList,
xData: timeList,
};
}
if (indicator_id == "2") {
return {
yData: nh3List,
xData: timeList,
};
}
if (indicator_id == "3") {
return {
yData: humidityList,
xData: timeList,
};
}
if (indicator_id == "4") {
return {
yData: co2List,
xData: timeList,
};
}
if (indicator_id == "5") {
return {
yData: illuminationList,
xData: timeList,
};
}
if (indicator_id == "6") {
return {
yData: windSpeedList,
xData: timeList,
};
}
if (indicator_id == "7") {
return {
yData: h2sList,
xData: timeList,
};
}
return null;
}
// 因为目前设备并没有鸡场、鸡舍相关概念 默认为0,没有的数据默认为0
async reportEnvironmentData(env, positionM, log_time, coop_id, house_id) {
// position_index, positionM, log_time;
if (!env) {
return;
}
const p = env.map(async (item) => {
const res = await Environment.create({
coop_id: parseInt(coop_id),
house_id: parseInt(house_id),
temperature: item.temperature,
humidity: item.humidity,
co2: item.co2,
nh3: 0,
illumination: 0,
wind_speed: 0,
h2s: 0,
position_index: item.position_index,
positionM,
log_time,
});
return res;
});
const real = await Promise.all(p);
return real;
}
// 获取环境实时监测数据
async getRealTimeData(plies_number) {
const res = await Environment.findAll();
const arr = res?.map((item) => item.dataValues);
return arr;
}
}
module.exports = new EnvironmentService();