From 9c040dbeaaac465e1ec6cae17c4ae3b1d82c136f Mon Sep 17 00:00:00 2001 From: maochaoying <925670706@qq.com> Date: Sat, 15 Apr 2023 14:36:44 +0800 Subject: [PATCH] 123 --- src/controller/environment.controller.js | 6 +++-- src/model/environment.model.js | 20 +++++++++------ src/service/environment.service.js | 42 +++++++++++++++++--------------- 3 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/controller/environment.controller.js b/src/controller/environment.controller.js index eb660f9..4ad9884 100644 --- a/src/controller/environment.controller.js +++ b/src/controller/environment.controller.js @@ -7,13 +7,14 @@ const Response = require("../utils/response"); class EnvironmentController { async detail(ctx, next) { - const { coop_id } = ctx.request.query; - const res = await getEnvironmentByCoopId(coop_id); + const { coop_id, house_id } = ctx.request.query; + const res = await getEnvironmentByCoopId(house_id, coop_id); ctx.body = Response(0, "获取当前鸡舍的环境检测结果成功", res); } async addLog(ctx, next) { const { + house_id, coop_id, temperature, humidity, @@ -24,6 +25,7 @@ class EnvironmentController { h2s, } = ctx.request.body; const res = await addNewLog( + house_id, coop_id, temperature, humidity, diff --git a/src/model/environment.model.js b/src/model/environment.model.js index 9107332..addc641 100644 --- a/src/model/environment.model.js +++ b/src/model/environment.model.js @@ -4,44 +4,50 @@ const seq = require("../db/seq"); const Environment = seq.define("chicken_environment", { // id 会被sequelize自动创建, 管理 + house_id: { + type: DataTypes.INTEGER, + allowNull: false, + comment: "鸡舍id", + }, coop_id: { type: DataTypes.INTEGER, allowNull: false, + unique: true, comment: "鸡舍id", }, temperature: { type: DataTypes.STRING, - allowNull: false, + allowNull: true, comment: "温度", }, humidity: { type: DataTypes.STRING, - allowNull: false, + allowNull: true, comment: "湿度", }, co2: { type: DataTypes.STRING, - allowNull: false, + allowNull: true, comment: "co2", }, nh3: { type: DataTypes.STRING, - allowNull: false, + allowNull: true, comment: "nh3", }, illumination: { type: DataTypes.STRING, - allowNull: false, + allowNull: true, comment: "光照强度", }, wind_speed: { type: DataTypes.STRING, - allowNull: false, + allowNull: true, comment: "风速", }, h2s: { type: DataTypes.STRING, - allowNull: false, + allowNull: true, comment: "h2s", }, log_time: { diff --git a/src/service/environment.service.js b/src/service/environment.service.js index b558338..1e03762 100644 --- a/src/service/environment.service.js +++ b/src/service/environment.service.js @@ -3,14 +3,18 @@ const { isIntTime } = require("../utils/common"); const moment = require("moment"); const { Op } = require("sequelize"); class EnvironmentService { - async getEnvironmentByCoopId(coop_id) { + async getEnvironmentByCoopId(house_id, coop_id) { const res = await Environment.findOne({ - coop_id, + where: { + coop_id, + house_id, + }, }); return res ? res.dataValues : null; } async addNewLog( + house_id, coop_id, temperature, humidity, @@ -21,22 +25,23 @@ class EnvironmentService { h2s ) { // 判断当前时间是否为正点 如果是整点则记录 - if (isIntTime()) { - const log_time = moment().format("YYYY-MM-DD HH:mm:ss"); - const res = await Environment.create({ - coop_id, - temperature, - humidity, - co2, - nh3, - illumination, - wind_speed, - h2s, - log_time, - }); - return res ? res.dataValues : null; - } - return null; + // 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(coop_id, time_id, indicator_id) { @@ -66,7 +71,6 @@ class EnvironmentService { }); const arr = res.map((item) => item.dataValues); // 根据传来的indicator_id 筛选出对应的数据 - console.log(arr); const temperatureList = arr.map((item) => item.temperature); const humidityList = arr.map((item) => item.humidity); const co2List = arr.map((item) => item.co2);