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

6
src/controller/environment.controller.js

@ -7,13 +7,14 @@ const Response = require("../utils/response");
class EnvironmentController { class EnvironmentController {
async detail(ctx, next) { 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); ctx.body = Response(0, "获取当前鸡舍的环境检测结果成功", res);
} }
async addLog(ctx, next) { async addLog(ctx, next) {
const { const {
house_id,
coop_id, coop_id,
temperature, temperature,
humidity, humidity,
@ -24,6 +25,7 @@ class EnvironmentController {
h2s, h2s,
} = ctx.request.body; } = ctx.request.body;
const res = await addNewLog( const res = await addNewLog(
house_id,
coop_id, coop_id,
temperature, temperature,
humidity, humidity,

20
src/model/environment.model.js

@ -4,44 +4,50 @@ const seq = require("../db/seq");
const Environment = seq.define("chicken_environment", { const Environment = seq.define("chicken_environment", {
// id 会被sequelize自动创建, 管理 // id 会被sequelize自动创建, 管理
house_id: {
type: DataTypes.INTEGER,
allowNull: false,
comment: "鸡舍id",
},
coop_id: { coop_id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
allowNull: false, allowNull: false,
unique: true,
comment: "鸡舍id", comment: "鸡舍id",
}, },
temperature: { temperature: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false,
allowNull: true,
comment: "温度", comment: "温度",
}, },
humidity: { humidity: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false,
allowNull: true,
comment: "湿度", comment: "湿度",
}, },
co2: { co2: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false,
allowNull: true,
comment: "co2", comment: "co2",
}, },
nh3: { nh3: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false,
allowNull: true,
comment: "nh3", comment: "nh3",
}, },
illumination: { illumination: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false,
allowNull: true,
comment: "光照强度", comment: "光照强度",
}, },
wind_speed: { wind_speed: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false,
allowNull: true,
comment: "风速", comment: "风速",
}, },
h2s: { h2s: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false,
allowNull: true,
comment: "h2s", comment: "h2s",
}, },
log_time: { log_time: {

42
src/service/environment.service.js

@ -3,14 +3,18 @@ const { isIntTime } = require("../utils/common");
const moment = require("moment"); const moment = require("moment");
const { Op } = require("sequelize"); const { Op } = require("sequelize");
class EnvironmentService { class EnvironmentService {
async getEnvironmentByCoopId(coop_id) {
async getEnvironmentByCoopId(house_id, coop_id) {
const res = await Environment.findOne({ const res = await Environment.findOne({
coop_id,
where: {
coop_id,
house_id,
},
}); });
return res ? res.dataValues : null; return res ? res.dataValues : null;
} }
async addNewLog( async addNewLog(
house_id,
coop_id, coop_id,
temperature, temperature,
humidity, humidity,
@ -21,22 +25,23 @@ class EnvironmentService {
h2s 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) { async getEnvironmentHistoryList(coop_id, time_id, indicator_id) {
@ -66,7 +71,6 @@ class EnvironmentService {
}); });
const arr = res.map((item) => item.dataValues); const arr = res.map((item) => item.dataValues);
// 根据传来的indicator_id 筛选出对应的数据 // 根据传来的indicator_id 筛选出对应的数据
console.log(arr);
const temperatureList = arr.map((item) => item.temperature); const temperatureList = arr.map((item) => item.temperature);
const humidityList = arr.map((item) => item.humidity); const humidityList = arr.map((item) => item.humidity);
const co2List = arr.map((item) => item.co2); const co2List = arr.map((item) => item.co2);

Loading…
Cancel
Save