diff --git a/src/mock/index.js b/src/mock/index.js index c3a0958..afe3336 100644 --- a/src/mock/index.js +++ b/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: [ { diff --git a/src/service/environment.service.js b/src/service/environment.service.js index 4b80cf4..7db146c 100644 --- a/src/service/environment.service.js +++ b/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,46 +255,21 @@ class EnvironmentService { return; } const p = env.map(async (item) => { - // 首先查询 position_index positionM 有没有数据 有则更新 无则创建 - const envInfo = await Environment.findOne({ - where: { - position_index: item.position_index, - positionM, - }, + const res = await Environment.create({ + house_id: 0, + coop_id: 0, + 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, }); - 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, - 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; - } + return res; }); const real = await Promise.all(p); return real;