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.
378 lines
11 KiB
378 lines
11 KiB
const Environment = require("../model/environment.model");
|
|
const { getCoopIdByUUID } = require("./coop.service");
|
|
const { getHouseIdByUUID } = require("./house.service");
|
|
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 += parseFloat(i.temperature);
|
|
humidityTotal += parseFloat(i.humidity);
|
|
co2Total += parseFloat(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 += parseFloat(i.temperature);
|
|
humidityTotal += parseFloat(i.humidity);
|
|
co2Total += parseFloat(i.co2);
|
|
nh3Total += parseFloat(i.nh3);
|
|
illuminationTotal += parseFloat(i.illumination);
|
|
windSpeedTotal += parseFloat(i.wind_speed);
|
|
h2sTotal += parseFloat(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 += parseFloat(i.temperature);
|
|
humidityTotal += parseFloat(i.humidity);
|
|
co2Total += parseFloat(i.co2);
|
|
nh3Total += parseFloat(i.nh3);
|
|
illuminationTotal += parseFloat(i.illumination);
|
|
windSpeedTotal += parseFloat(i.wind_speed);
|
|
h2sTotal += parseFloat(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, henhouseId, farmId) {
|
|
try {
|
|
// 根据uuid查询各自的id
|
|
const coopInfo = await getCoopIdByUUID(henhouseId);
|
|
const houseInfo = await getHouseIdByUUID(farmId);
|
|
// position_index, positionM, log_time;
|
|
if (!env || !coopInfo || !houseInfo) {
|
|
return;
|
|
}
|
|
const p = env.map(async (item) => {
|
|
// 查询传过来的coop_id和house_id
|
|
if (item.layerIndex && item.lineIndex) {
|
|
const res = await Environment.create({
|
|
coop_id: coopInfo.id,
|
|
house_id: houseInfo.id,
|
|
temperature: item.temperature,
|
|
humidity: item.humidity,
|
|
co2: item.co2,
|
|
nh3: 0,
|
|
illumination: 0,
|
|
wind_speed: 0,
|
|
h2s: 0,
|
|
position_index: item.position_index,
|
|
layer_index: item.layerIndex,
|
|
line_index: item.lineIndex,
|
|
positionM,
|
|
log_time,
|
|
});
|
|
return res;
|
|
}
|
|
});
|
|
const real = await Promise.all(p);
|
|
return real;
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
// 获取环境实时监测数据
|
|
async getRealTimeData(house_id, coop_id, plies_number) {
|
|
const res = await Environment.findAll({
|
|
where: {
|
|
house_id,
|
|
coop_id,
|
|
},
|
|
order: [["log_time", "DESC"]],
|
|
});
|
|
const arr = res?.map((item) => item.dataValues);
|
|
let filterArr = [];
|
|
// 对arr进行筛选 将旧数据甩出
|
|
arr.map((item) => {
|
|
let temp = filterArr.filter(
|
|
(it) =>
|
|
it.line_index == item.line_index &&
|
|
it.layer_index == item.layer_index &&
|
|
it.positionM == item.positionM
|
|
);
|
|
if (temp && temp.length > 0) {
|
|
} else {
|
|
filterArr.push(item);
|
|
}
|
|
});
|
|
let lastArr = [];
|
|
filterArr.map((item) => {
|
|
const layer_index = item.layer_index;
|
|
if (plies_number == "1") {
|
|
if (layer_index == 1) {
|
|
// 第一层
|
|
const temp = lastArr.filter((i) => i.index == item.line_index);
|
|
if (temp && temp.length > 0) {
|
|
lastArr.map((itl) => {
|
|
if (itl.index == item.line_index) {
|
|
itl.data?.push(item);
|
|
}
|
|
});
|
|
} else {
|
|
lastArr.push({
|
|
index: item.line_index,
|
|
data: [item],
|
|
});
|
|
}
|
|
}
|
|
}
|
|
if (plies_number == "2") {
|
|
if (layer_index == 2) {
|
|
// 第二层
|
|
const temp = lastArr.filter((i) => i.index == item.line_index);
|
|
if (temp && temp.length > 0) {
|
|
lastArr.map((itl) => {
|
|
if (itl.index == item.line_index) {
|
|
itl.data?.push(item);
|
|
}
|
|
});
|
|
} else {
|
|
lastArr.push({
|
|
index: item.line_index,
|
|
data: [item],
|
|
});
|
|
}
|
|
}
|
|
}
|
|
if (plies_number == "3") {
|
|
if (layer_index == 3) {
|
|
// 第三层
|
|
const temp = lastArr.filter((i) => i.index == item.line_index);
|
|
if (temp && temp.length > 0) {
|
|
lastArr.map((itl) => {
|
|
if (itl.index == item.line_index) {
|
|
itl.data?.push(item);
|
|
}
|
|
});
|
|
} else {
|
|
lastArr.push({
|
|
index: item.line_index,
|
|
data: [item],
|
|
});
|
|
}
|
|
}
|
|
}
|
|
});
|
|
return lastArr;
|
|
}
|
|
}
|
|
|
|
module.exports = new EnvironmentService();
|