9 changed files with 259 additions and 0 deletions
-
8src/controller/chicken.controller.js
-
27src/controller/device.controller.js
-
92src/model/control.model.js
-
36src/model/device.model.js
-
3src/router/chicken.route.js
-
12src/router/device.route.js
-
22src/service/chicken.service.js
-
45src/service/device.service.js
-
14src/service/feeding.service.js
@ -0,0 +1,27 @@ |
|||||
|
const { |
||||
|
getStatusByCoopId, |
||||
|
changeStatus, |
||||
|
} = require("../service/device.service"); |
||||
|
const Response = require("../utils/response"); |
||||
|
|
||||
|
class DeviceController { |
||||
|
async detail(ctx, next) { |
||||
|
const { coop_id } = ctx.request.query; |
||||
|
const res = await getStatusByCoopId(coop_id); |
||||
|
ctx.body = Response(0, "获取鸡舍设备状态成功", res); |
||||
|
} |
||||
|
|
||||
|
async change(ctx, next) { |
||||
|
const { coop_id, egg_gathering, illumination, air_conditioner } = |
||||
|
ctx.request.body; |
||||
|
const res = await changeStatus( |
||||
|
coop_id, |
||||
|
egg_gathering, |
||||
|
illumination, |
||||
|
air_conditioner |
||||
|
); |
||||
|
ctx.body = Response(0, "改变鸡舍设备状态成功", res); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
module.exports = new DeviceController(); |
@ -0,0 +1,92 @@ |
|||||
|
const { DataTypes } = require("sequelize"); |
||||
|
|
||||
|
const seq = require("../db/seq"); |
||||
|
|
||||
|
// 鸡苗出栏
|
||||
|
const Control = seq.define("chicken_control", { |
||||
|
// id 会被sequelize自动创建, 管理
|
||||
|
temperature_start: { |
||||
|
type: DataTypes.STRING, |
||||
|
allowNull: false, |
||||
|
defaultValue: 0, |
||||
|
comment: "温度范围起始值温度", |
||||
|
}, |
||||
|
temperature_end: { |
||||
|
type: DataTypes.STRING, |
||||
|
allowNull: false, |
||||
|
defaultValue: 0, |
||||
|
comment: "温度范围结束值温度", |
||||
|
}, |
||||
|
temperature_default_start: { |
||||
|
type: DataTypes.STRING, |
||||
|
allowNull: false, |
||||
|
comment: "默认起始温度", |
||||
|
}, |
||||
|
temperature_default_end: { |
||||
|
type: DataTypes.STRING, |
||||
|
allowNull: false, |
||||
|
comment: "默认结束温度", |
||||
|
}, |
||||
|
humidity_start: { |
||||
|
type: DataTypes.STRING, |
||||
|
allowNull: false, |
||||
|
defaultValue: 0, |
||||
|
comment: "湿度范围起始值", |
||||
|
}, |
||||
|
humidity_end: { |
||||
|
type: DataTypes.STRING, |
||||
|
allowNull: false, |
||||
|
defaultValue: 0, |
||||
|
comment: "湿度范围结束值", |
||||
|
}, |
||||
|
humidity_default_start: { |
||||
|
type: DataTypes.STRING, |
||||
|
allowNull: false, |
||||
|
comment: "默认起始湿度", |
||||
|
}, |
||||
|
humidity_default_end: { |
||||
|
type: DataTypes.STRING, |
||||
|
allowNull: false, |
||||
|
comment: "默认结束湿度", |
||||
|
}, |
||||
|
current_co2: { |
||||
|
type: DataTypes.STRING, |
||||
|
allowNull: false, |
||||
|
comment: "当前co2含量", |
||||
|
}, |
||||
|
default_co2: { |
||||
|
type: DataTypes.STRING, |
||||
|
allowNull: false, |
||||
|
comment: "默认的co2值", |
||||
|
}, |
||||
|
current_nh3: { |
||||
|
type: DataTypes.STRING, |
||||
|
allowNull: false, |
||||
|
comment: "当前nh3含量", |
||||
|
}, |
||||
|
default_nh3: { |
||||
|
type: DataTypes.STRING, |
||||
|
allowNull: false, |
||||
|
comment: "默认的nh3值", |
||||
|
}, |
||||
|
current_h2s: { |
||||
|
type: DataTypes.STRING, |
||||
|
allowNull: false, |
||||
|
comment: "当前h2s含量", |
||||
|
}, |
||||
|
default_h2s: { |
||||
|
type: DataTypes.STRING, |
||||
|
allowNull: false, |
||||
|
comment: "默认的h2s值", |
||||
|
}, |
||||
|
coop_id: { |
||||
|
type: DataTypes.INTEGER, |
||||
|
allowNull: false, |
||||
|
comment: "鸡舍id", |
||||
|
}, |
||||
|
}); |
||||
|
|
||||
|
// 强制同步数据库(创建数据表)
|
||||
|
// Control.sync({ force: true });
|
||||
|
|
||||
|
module.exports = Control; |
@ -0,0 +1,36 @@ |
|||||
|
const { DataTypes } = require("sequelize"); |
||||
|
|
||||
|
const seq = require("../db/seq"); |
||||
|
|
||||
|
// 鸡苗品种
|
||||
|
const Device = seq.define("chicken_device", { |
||||
|
// id 会被sequelize自动创建, 管理
|
||||
|
egg_gathering: { |
||||
|
type: DataTypes.BOOLEAN, |
||||
|
allowNull: false, |
||||
|
defaultValue: 0, |
||||
|
comment: "急蛋设备开关", |
||||
|
}, |
||||
|
illumination: { |
||||
|
type: DataTypes.BOOLEAN, |
||||
|
allowNull: false, |
||||
|
defaultValue: 0, |
||||
|
comment: "光照开关", |
||||
|
}, |
||||
|
air_conditioner: { |
||||
|
type: DataTypes.BOOLEAN, |
||||
|
allowNull: false, |
||||
|
defaultValue: 0, |
||||
|
comment: "空调开关", |
||||
|
}, |
||||
|
coop_id: { |
||||
|
type: DataTypes.INTEGER, |
||||
|
allowNull: false, |
||||
|
comment: "鸡舍id", |
||||
|
}, |
||||
|
}); |
||||
|
|
||||
|
// 强制同步数据库(创建数据表)
|
||||
|
// Device.sync({ force: true });
|
||||
|
|
||||
|
module.exports = Device; |
@ -0,0 +1,12 @@ |
|||||
|
const Router = require("koa-router"); |
||||
|
|
||||
|
const router = new Router({ prefix: "/device" }); |
||||
|
|
||||
|
const { auth } = require("../middleware/auth.middleware"); |
||||
|
const { detail, change } = require("../controller/device.controller"); |
||||
|
|
||||
|
router.get("/detail", auth, detail); |
||||
|
|
||||
|
router.post("/change", auth, change); |
||||
|
|
||||
|
module.exports = router; |
@ -0,0 +1,45 @@ |
|||||
|
const Device = require("../model/device.model"); |
||||
|
class DeviceService { |
||||
|
async getStatusByCoopId(coop_id) { |
||||
|
const res = await Device.findOne({ |
||||
|
where: { |
||||
|
coop_id, |
||||
|
}, |
||||
|
}); |
||||
|
return res ? res.dataValues : null; |
||||
|
} |
||||
|
|
||||
|
async changeStatus(coop_id, egg_gathering, illumination, air_conditioner) { |
||||
|
// 先查询是否有
|
||||
|
const deviceInfo = await Device.findOne({ |
||||
|
where: { |
||||
|
coop_id, |
||||
|
}, |
||||
|
}); |
||||
|
if (deviceInfo) { |
||||
|
const res = await Device.update( |
||||
|
{ |
||||
|
egg_gathering, |
||||
|
illumination, |
||||
|
air_conditioner, |
||||
|
}, |
||||
|
{ |
||||
|
where: { |
||||
|
coop_id, |
||||
|
}, |
||||
|
} |
||||
|
); |
||||
|
return res; |
||||
|
} else { |
||||
|
const res = await Device.create({ |
||||
|
coop_id, |
||||
|
egg_gathering, |
||||
|
illumination, |
||||
|
air_conditioner, |
||||
|
}); |
||||
|
return res; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
module.exports = new DeviceService(); |
Write
Preview
Loading…
Cancel
Save
Reference in new issue