maochaoying 2 years ago
parent
commit
9c040dbeaa
  1. 6
      src/controller/environment.controller.js
  2. 20
      src/model/environment.model.js
  3. 14
      src/service/environment.service.js

6
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,

20
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: {

14
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({
where: {
coop_id,
house_id,
},
});
return res ? res.dataValues : null;
}
async addNewLog(
house_id,
coop_id,
temperature,
humidity,
@ -21,9 +25,10 @@ class EnvironmentService {
h2s
) {
// 判断当前时间是否为正点 如果是整点则记录
if (isIntTime()) {
// if (isIntTime()) {
const log_time = moment().format("YYYY-MM-DD HH:mm:ss");
const res = await Environment.create({
house_id,
coop_id,
temperature,
humidity,
@ -35,8 +40,8 @@ class EnvironmentService {
log_time,
});
return res ? res.dataValues : null;
}
return 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);

Loading…
Cancel
Save