maochaoying 2 years ago
parent
commit
dc0f8d8a21
  1. 2
      src/mock/index.js
  2. 56
      src/service/environment.service.js

2
src/mock/index.js

@ -1,7 +1,7 @@
const sensor_data = {
command: "envDataReport",
messageId: "xxxxx-xxxxx-xxxxx-xxxxx",
timestamp: "2023-04-19 16:22:11", //unix时间戳ms
timestamp: "2023-04-17 16:22:11", //unix时间戳ms
positionM: 50, //横向距离
env: [
{

56
src/service/environment.service.js

@ -4,16 +4,31 @@ 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"]],
// where: {
// coop_id,
// house_id,
// },
// order: [["log_time", "DESC"]],
});
const arr = res.map((item) => item.dataValues);
if (arr && arr.length > 0) {
return arr[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;
}
@ -51,8 +66,8 @@ class EnvironmentService {
async getEnvironmentHistoryList(house_id, coop_id, time_id, indicator_id) {
let whereObj = {
house_id,
coop_id,
// house_id,
// coop_id,
};
// 需要根据id进行筛选 7天、全部、24小时
if (time_id == "1") {
@ -240,30 +255,6 @@ class EnvironmentService {
return;
}
const p = env.map(async (item) => {
// 首先查询 position_index positionM 有没有数据 有则更新 无则创建
const envInfo = await Environment.findOne({
where: {
position_index: item.position_index,
positionM,
},
});
if (envInfo) {
const res = await Environment.update(
{
temperature: item.temperature,
humidity: item.humidity,
co2: item.co2,
log_time,
},
{
where: {
position_index: item.position_index,
positionM,
},
}
);
return res;
} else {
const res = await Environment.create({
house_id: 0,
coop_id: 0,
@ -279,7 +270,6 @@ class EnvironmentService {
log_time,
});
return res;
}
});
const real = await Promise.all(p);
return real;

Loading…
Cancel
Save