maochaoying 2 years ago
parent
commit
fc32f791fb
  1. 10
      src/constant/message.js
  2. 11
      src/controller/feeding.controller.js
  3. 4
      src/mock/index.js
  4. 14
      src/model/feeding.model.js
  5. 20
      src/service/feeding.service.js
  6. 3
      src/service/overview.service.js
  7. 16
      src/utils/message.js

10
src/constant/message.js

@ -0,0 +1,10 @@
const FEEDING_EVENT = {
START: "startFeeding",
PAUSE: "pauseFeeding",
RESUME: "resumeFeeding",
STOP: "stopFeeding",
};
module.exports = {
FEEDING_EVENT,
};

11
src/controller/feeding.controller.js

@ -9,8 +9,15 @@ class FeedingController {
}
async add(ctx, next) {
const { coop_id, feeding, water_intake } = ctx.request.body;
const res = await addFeeding(coop_id, feeding, water_intake);
const { house_id, coop_id, feeding, water_intake, log_time } =
ctx.request.body;
const res = await addFeeding({
house_id,
coop_id,
feeding,
water_intake,
log_time,
});
ctx.body = Response(0, "增加饲喂记录成功", res);
}
}

4
src/mock/index.js

@ -42,8 +42,8 @@ const sensor_data = {
const feed_data = {
command: "feedingEventReport",
messageId: "xxxxx-xxxxx-xxxxx-xxxxx",
eventType: "startFeeding/pauseFeeding/resumeFeeding/stopFeeding",
timestamp: 231245, //unix时间戳ms
eventType: "startFeeding",
timestamp: "2023-04-19 16:22:11", //unix时间戳ms
};
module.exports = {

14
src/model/feeding.model.js

@ -4,6 +4,11 @@ const seq = require("../db/seq");
const Feeding = seq.define("chicken_feeding", {
// id 会被sequelize自动创建, 管理
house_id: {
type: DataTypes.INTEGER,
allowNull: false,
comment: "鸡场id",
},
coop_id: {
type: DataTypes.INTEGER,
allowNull: false,
@ -11,14 +16,19 @@ const Feeding = seq.define("chicken_feeding", {
},
feeding: {
type: DataTypes.STRING,
allowNull: false,
allowNull: true,
comment: "饲喂量",
},
water_intake: {
type: DataTypes.STRING,
allowNull: false,
allowNull: true,
comment: "饮水量",
},
log_time: {
type: DataTypes.DATE,
allowNull: false,
comment: "记录时间,目前记录开始喂食时间",
},
});
// 强制同步数据库(创建数据表)

20
src/service/feeding.service.js

@ -49,11 +49,29 @@ class FeedingService {
return total;
}
async addFeeding(coop_id, feeding, water_intake) {
async getNewestFeedInfo() {
const res = await Feeding.findAll({ order: [["id", "DESC"]], limit: 1 });
const arr = res?.map((item) => item.dataValues);
if (arr && arr.length > 0) {
return arr[0];
}
return null;
}
// 目前喂食先针对所有鸡舍一起,硬件那边没有记录设备是哪个鸡场哪个鸡舍,喂食量先暂时为0,不记录
async addFeeding({
house_id = 0,
coop_id = 0,
feeding = 0,
water_intake = 0,
log_time,
}) {
const res = await Feeding.create({
house_id,
coop_id,
feeding,
water_intake,
log_time,
});
return res ? res.dataValues : null;
}

3
src/service/overview.service.js

@ -2,6 +2,7 @@ const Chicken = require("../model/chicken.model");
const { getDayLife, haveSame } = require("../utils/common");
const { getDieTotal } = require("./die.service");
const { getEnvironmentByCoopId } = require("./environment.service");
const { getNewestFeedInfo } = require("./feeding.service");
const {
getBatchCoopByIds,
getEmptyCoopList,
@ -109,6 +110,8 @@ class OverviewService {
const coop_id = item.id;
const house_id = info.house_id;
const environment = await getEnvironmentByCoopId(house_id, coop_id);
const feedInfo = await getNewestFeedInfo();
item.feedInfo = feedInfo;
item.environment = environment;
return item;
});

16
src/utils/message.js

@ -1,7 +1,21 @@
const { isJsonString } = require("./common");
const { FEEDING_EVENT } = require("../constant/message");
const moment = require("moment");
const { addFeeding } = require("../service/feeding.service");
const handleSensorMessage = (message) => {};
const handleFeedMessage = (message) => {};
const handleFeedMessage = async (message) => {
console.log(message);
const eventType = message?.eventType;
const timestamp = message?.timestamp;
// 目前只关注多会开始喂食 将时间记录到数据库中
if (eventType == FEEDING_EVENT.START) {
// 插入数据库喂食记录
const res = await addFeeding({
log_time: moment(timestamp).format("YYYY-MM-DD HH:mm:ss"),
});
}
};
const handleMessage = (message) => {
// 对message进行处理并分发到handleSensorMessage / handleFeedMessage

Loading…
Cancel
Save