maochaoying 2 years ago
parent
commit
2a269c2886
  1. 13
      src/controller/control.controller.js
  2. 13
      src/controller/device.controller.js
  3. 5
      src/model/control.model.js
  4. 5
      src/model/device.model.js
  5. 4
      src/router/control.route.js
  6. 63
      src/service/control.service.js
  7. 14
      src/service/device.service.js

13
src/controller/control.controller.js

@ -1,18 +1,20 @@
const {
getSettingByCoopId,
changeSetting,
getControlInfoList,
} = require("../service/control.service");
const Response = require("../utils/response");
class ControlController {
async detail(ctx, next) {
const { coop_id } = ctx.request.query;
const res = await getSettingByCoopId(coop_id);
const { house_id, coop_id } = ctx.request.body;
const res = await getSettingByCoopId(house_id, coop_id);
ctx.body = Response(0, "获取环境控制设定成功", res);
}
async change(ctx, next) {
const {
house_id,
coop_id,
temperature_start,
temperature_end,
@ -23,6 +25,7 @@ class ControlController {
current_h2s,
} = ctx.request.body;
const res = await changeSetting(
house_id,
coop_id,
temperature_start,
temperature_end,
@ -34,6 +37,12 @@ class ControlController {
);
ctx.body = Response(0, "改变环境设定值成功", res);
}
async list(ctx, next) {
const { house_id, batch_number, coop_id } = ctx.request.body;
const res = await getControlInfoList(house_id, batch_number, coop_id);
ctx.body = Response(0, "查询智能控制列表成功", res);
}
}
module.exports = new ControlController();

13
src/controller/device.controller.js

@ -6,20 +6,21 @@ const Response = require("../utils/response");
class DeviceController {
async detail(ctx, next) {
const { coop_id } = ctx.request.query;
const res = await getStatusByCoopId(coop_id);
const { house_id, coop_id } = ctx.request.query;
const res = await getStatusByCoopId(house_id, coop_id);
ctx.body = Response(0, "获取鸡舍设备状态成功", res);
}
async change(ctx, next) {
const { coop_id, egg_gathering, illumination, air_conditioner } =
const { house_id, coop_id, egg_gathering, illumination, air_conditioner } =
ctx.request.body;
const res = await changeStatus(
const res = await changeStatus({
house_id,
coop_id,
egg_gathering,
illumination,
air_conditioner
);
air_conditioner,
});
ctx.body = Response(0, "改变鸡舍设备状态成功", res);
}
}

5
src/model/control.model.js

@ -88,6 +88,11 @@ const Control = seq.define("chicken_control", {
defaultValue: 0,
comment: "默认的h2s值",
},
house_id: {
type: DataTypes.INTEGER,
allowNull: false,
comment: "鸡场id",
},
coop_id: {
type: DataTypes.INTEGER,
allowNull: false,

5
src/model/device.model.js

@ -28,6 +28,11 @@ const Device = seq.define("chicken_device", {
allowNull: false,
comment: "鸡舍id",
},
house_id: {
type: DataTypes.INTEGER,
allowNull: false,
comment: "鸡场id",
},
});
// 强制同步数据库(创建数据表)

4
src/router/control.route.js

@ -3,10 +3,12 @@ const Router = require("koa-router");
const router = new Router({ prefix: "/control" });
const { auth } = require("../middleware/auth.middleware");
const { detail, change } = require("../controller/control.controller");
const { detail, change, list } = require("../controller/control.controller");
router.get("/detail", auth, detail);
router.post("/change", auth, change);
router.post("/list", auth, list);
module.exports = router;

63
src/service/control.service.js

@ -1,10 +1,69 @@
const Control = require("../model/control.model");
const Chicken = require("../model/chicken.model");
const Device = require("../model/device.model");
const Coop = require("../model/coop.model");
const { Op, Sequelize } = require("sequelize");
const { getCoopListById } = require("./coop.service");
class ControlService {
async getSettingByCoopId(coop_id) {
return null;
async getSettingByCoopId(house_id, coop_id) {
const res = await Control.findOne({
where: {
house_id,
coop_id,
},
});
return res ? res.dataValues : null;
}
async getControlInfoList(house_id, batch_number = "", coop_ids) {
const res = await getCoopListById(house_id);
// 查询该舍的环控信息
const p = res.map(async (item) => {
const coop_id = item.id;
const controlInfo = await Control.findOne({
where: {
house_id,
coop_id,
},
});
item.controlInfo = controlInfo;
// 设备信息
const deviceInfo = await Device.findOne({
where: {
house_id,
coop_id,
},
});
item.deviceInfo = deviceInfo;
// 查询批次号和日龄
const allChickenInfo = await Chicken.findAll({
where: {
coop_id: {
[Op.like]: `%${coop_id}%`,
},
batch_number: {
[Op.like]: `%${batch_number}%`,
},
},
});
const singleArr = allChickenInfo?.filter((item) =>
item.dataValues?.coop_id?.split(",").includes(coop_id + "")
);
if (singleArr && singleArr.length > 0) {
item.chickenInfo = singleArr[0];
}
return item;
});
let real = await Promise.all(p);
if (coop_ids) {
const a = real.filter((item) => item.id == coop_ids);
return a;
}
return real;
}
async changeSetting(
house_id,
coop_id,
temperature_start,
temperature_end,

14
src/service/device.service.js

@ -1,19 +1,27 @@
const Device = require("../model/device.model");
class DeviceService {
async getStatusByCoopId(coop_id) {
async getStatusByCoopId(house_id, coop_id) {
const res = await Device.findOne({
where: {
house_id,
coop_id,
},
});
return res ? res.dataValues : null;
}
async changeStatus(coop_id, egg_gathering, illumination, air_conditioner) {
async changeStatus({
house_id,
coop_id,
egg_gathering,
illumination,
air_conditioner,
}) {
// 先查询是否有
const deviceInfo = await Device.findOne({
where: {
coop_id,
house_id,
},
});
if (deviceInfo) {
@ -25,6 +33,7 @@ class DeviceService {
},
{
where: {
house_id,
coop_id,
},
}
@ -32,6 +41,7 @@ class DeviceService {
return res;
} else {
const res = await Device.create({
house_id,
coop_id,
egg_gathering,
illumination,

Loading…
Cancel
Save