diff --git a/package.json b/package.json index 592165d..eb275c2 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "dependencies": { "bcryptjs": "^2.4.3", "dotenv": "^16.0.3", + "install": "^0.13.0", "jsonwebtoken": "^9.0.0", "koa": "^2.14.1", "koa-body": "^6.0.1", diff --git a/src/controller/house.controller.js b/src/controller/house.controller.js index 0603a21..605511e 100644 --- a/src/controller/house.controller.js +++ b/src/controller/house.controller.js @@ -8,9 +8,13 @@ const Response = require("../utils/response"); class HouseController { async list(ctx, next) { - const { id: belong, role } = ctx.state.user; - const res = await getHouseListById(belong, role); - ctx.body = Response(0, "查询鸡厂列表成功", res); + try { + const { id: belong, role } = ctx.state.user; + const res = await getHouseListById(belong, role); + ctx.body = Response(0, "查询鸡厂列表成功", res); + } catch (error) { + console.log(error); + } } async create(ctx, next) { diff --git a/src/mock/index.js b/src/mock/index.js index 158a9e3..3dc2f9c 100644 --- a/src/mock/index.js +++ b/src/mock/index.js @@ -3,12 +3,14 @@ const sensor_data = { messageId: "xxxxx-xxxxx-xxxxx-xxxxx", timestamp: "2023-04-16 16:22:11", //unix时间戳ms positionM: 50, //横向距离 - henhouseId: "2", - farmId: "15", + henhouseId: "cdc7c436-2f1d-4259-967a-bd15457b90a6", + farmId: "57180cf6-769b-4372-836c-193b62b82ade", env: [ { mac: "00:28:f8:6f:a3:92", - position_index: 11, + layer_index: 1, //第几层 + line_index: 1, //第几列 + // position_index: 11, vehicle_index: 1, co2: 500.0, temperature: 24.0, @@ -16,7 +18,9 @@ const sensor_data = { }, { mac: "01:xx:xx:xx:xx:xx", - position_index: 12, + layer_index: 2, //第几层 + line_index: 1, //第几列 + // position_index: 12, vehicle_index: 1, co2: 500.0, temperature: 24.0, @@ -24,7 +28,9 @@ const sensor_data = { }, { mac: "xx:xx:xx:xx:xx:xx", - position_index: 13, + layer_index: 3, //第几层 + line_index: 1, //第几列 + // position_index: 13, vehicle_index: 1, co2: 500.0, temperature: 24.0, @@ -32,7 +38,9 @@ const sensor_data = { }, { mac: "xx:xx:xx:xx:xx:xx", - position_index: 14, + layer_index: 1, //第几层 + line_index: 2, //第几列 + // position_index: 14, vehicle_index: 1, co2: 500.0, temperature: 24.0, diff --git a/src/model/coop.model.js b/src/model/coop.model.js index 9f913ba..f175de1 100644 --- a/src/model/coop.model.js +++ b/src/model/coop.model.js @@ -9,6 +9,11 @@ const Coop = seq.define("chicken_coop", { allowNull: false, comment: "鸡舍名称", }, + uuid: { + type: DataTypes.STRING, + allowNull: false, + comment: "uuid", + }, coop_cage_number: { type: DataTypes.INTEGER, allowNull: false, diff --git a/src/model/environment.model.js b/src/model/environment.model.js index be4395c..ecfb6dd 100644 --- a/src/model/environment.model.js +++ b/src/model/environment.model.js @@ -51,8 +51,8 @@ const Environment = seq.define("chicken_environment", { }, position_index: { type: DataTypes.STRING, - allowNull: false, - comment: "舍中的层数标识", + allowNull: true, + comment: "舍中的层数标识,暂时弃用", }, positionM: { type: DataTypes.STRING, @@ -65,6 +65,16 @@ const Environment = seq.define("chicken_environment", { defaultValue: DataTypes.NOW, comment: "记录时间", }, + layer_index: { + type: DataTypes.INTEGER, + allowNull: false, + comment: "鸡舍所处的层数", + }, + line_index: { + type: DataTypes.INTEGER, + allowNull: false, + comment: "鸡舍所处的列数", + }, }); // 强制同步数据库(创建数据表) diff --git a/src/model/house.model.js b/src/model/house.model.js index 5c2cb96..116755f 100644 --- a/src/model/house.model.js +++ b/src/model/house.model.js @@ -10,6 +10,11 @@ const House = seq.define("chicken_house", { unique: true, comment: "鸡场名称", }, + uuid: { + type: DataTypes.STRING, + allowNull: false, + comment: "uuid", + }, area: { type: DataTypes.STRING, allowNull: false, diff --git a/src/service/coop.service.js b/src/service/coop.service.js index 021b180..b4510bc 100644 --- a/src/service/coop.service.js +++ b/src/service/coop.service.js @@ -4,6 +4,7 @@ const Coop = require("../model/coop.model"); const { findDiffArr } = require("../utils/common"); const { getHouseById } = require("./house.service"); const { changeStatus } = require("./device.service"); +const { v4: uuidv4 } = require("uuid"); class CoopService { async getCoopListById(house_id) { const res = await Coop.findAll({ @@ -16,11 +17,13 @@ class CoopService { } async createCoop(coop_name, coop_cage_number, chicken_number, house_id) { + const uuid = uuidv4(); const res = await Coop.create({ coop_name, coop_cage_number, chicken_number, house_id, + uuid, }); // 创建新鸡舍时初始化 环控 和 设备表 const deviceRes = await changeStatus({ @@ -30,7 +33,15 @@ class CoopService { illumination: 0, air_conditioner: 0, }); + return res ? res.dataValues : null; + } + async getCoopIdByUUID(uuid) { + const res = await Coop.findOne({ + where: { + uuid, + }, + }); return res ? res.dataValues : null; } diff --git a/src/service/environment.service.js b/src/service/environment.service.js index 5877f69..489220e 100644 --- a/src/service/environment.service.js +++ b/src/service/environment.service.js @@ -1,4 +1,6 @@ const Environment = require("../model/environment.model"); +const { getCoopIdByUUID } = require("./coop.service"); +const { getHouseIdByUUID } = require("./house.service"); const { isIntTime } = require("../utils/common"); const moment = require("moment"); const { Op } = require("sequelize"); @@ -249,15 +251,19 @@ class EnvironmentService { } // 因为目前设备并没有鸡场、鸡舍相关概念 默认为0,没有的数据默认为0 - async reportEnvironmentData(env, positionM, log_time, coop_id, house_id) { + async reportEnvironmentData(env, positionM, log_time, henhouseId, farmId) { + // 根据uuid查询各自的id + const coopInfo = await getCoopIdByUUID(henhouseId); + const houseInfo = await getHouseIdByUUID(farmId); // position_index, positionM, log_time; - if (!env) { + if (!env || !coopInfo || !houseInfo) { return; } const p = env.map(async (item) => { + // 查询传过来的coop_id和house_id const res = await Environment.create({ - coop_id: parseInt(coop_id), - house_id: parseInt(house_id), + coop_id: coopInfo.id, + house_id: houseInfo.id, temperature: item.temperature, humidity: item.humidity, co2: item.co2, @@ -266,6 +272,8 @@ class EnvironmentService { wind_speed: 0, h2s: 0, position_index: item.position_index, + layer_index: item.layer_index, + line_index: item.line_index, positionM, log_time, }); @@ -290,7 +298,8 @@ class EnvironmentService { arr.map((item) => { let temp = filterArr.filter( (it) => - it.position_index == item.position_index && + it.line_index == item.line_index && + it.layer_index == item.layer_index && it.positionM == item.positionM ); if (temp && temp.length > 0) { @@ -298,60 +307,58 @@ class EnvironmentService { filterArr.push(item); } }); - console.log(filterArr); let lastArr = []; filterArr.map((item) => { - const position_index = item.position_index; - const pliesFlag = position_index.split("")[1]; + const layer_index = item.layer_index; if (plies_number == "1") { - if (["1", "4"].includes(pliesFlag)) { + if (layer_index == 1) { // 第一层 - const temp = lastArr.filter((i) => i.index == item.position_index); + const temp = lastArr.filter((i) => i.index == item.line_index); if (temp && temp.length > 0) { lastArr.map((itl) => { - if (itl.index == item.position_index) { + if (itl.index == item.line_index) { itl.data?.push(item); } }); } else { lastArr.push({ - index: item.position_index, + index: item.line_index, data: [item], }); } } } if (plies_number == "2") { - if (["2", "5"].includes(pliesFlag)) { + if (layer_index == 2) { // 第二层 - const temp = lastArr.filter((i) => i.index == item.position_index); + const temp = lastArr.filter((i) => i.index == item.line_index); if (temp && temp.length > 0) { lastArr.map((itl) => { - if (itl.index == item.position_index) { + if (itl.index == item.line_index) { itl.data?.push(item); } }); } else { lastArr.push({ - index: item.position_index, + index: item.line_index, data: [item], }); } } } if (plies_number == "3") { - if (["3", "6"].includes(pliesFlag)) { + if (layer_index == 3) { // 第三层 - const temp = lastArr.filter((i) => i.index == item.position_index); + const temp = lastArr.filter((i) => i.index == item.line_index); if (temp && temp.length > 0) { lastArr.map((itl) => { - if (itl.index == item.position_index) { + if (itl.index == item.line_index) { itl.data?.push(item); } }); } else { lastArr.push({ - index: item.position_index, + index: item.line_index, data: [item], }); } diff --git a/src/service/house.service.js b/src/service/house.service.js index 3231a36..480267e 100644 --- a/src/service/house.service.js +++ b/src/service/house.service.js @@ -2,6 +2,7 @@ const House = require("../model/house.model"); const Coop = require("../model/coop.model"); const { getAccountInfo } = require("./account.service"); const { initConfig } = require("./config.service"); +const { v4: uuidv4 } = require("uuid"); class HouseService { async getHouseListById(belong, role) { let res = null; @@ -31,11 +32,23 @@ class HouseService { return real; } + async getHouseIdByUUID(uuid) { + const res = await House.findOne({ + where: { + uuid, + }, + }); + return res ? res.dataValues : null; + } + async createHouse(belong, house_name, area) { + const uuid = uuidv4(); + const res = await House.create({ belong, house_name, area, + uuid, }); // 创建鸡场对应的config表哥 if (res) { diff --git a/src/utils/message.js b/src/utils/message.js index 026d7e4..1d27bbc 100644 --- a/src/utils/message.js +++ b/src/utils/message.js @@ -11,10 +11,10 @@ const handleSensorMessage = (message) => { // 每到一个笼位记录一次 if (positionM % 50 == 0) { const env = message?.env || []; - const coop_id = message?.henhouseId; - const house_id = message?.farmId; + const henhouseId = message?.henhouseId; + const farmId = message?.farmId; const log_time = moment(message?.timestamp).format("YYYY-MM-DD HH:mm:ss"); - reportEnvironmentData(env, positionM, log_time, coop_id, house_id); + reportEnvironmentData(env, positionM, log_time, henhouseId, farmId); } }; const handleFeedMessage = async (message) => { diff --git a/yarn.lock b/yarn.lock index b34ab1b..ecbd1bd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -980,6 +980,11 @@ ini@^1.3.5: resolved "https://registry.npmmirror.com/ini/-/ini-1.3.8.tgz#a29da425b48806f34767a4efce397269af28432c" integrity sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew== +install@^0.13.0: + version "0.13.0" + resolved "https://registry.npmmirror.com/install/-/install-0.13.0.tgz#6af6e9da9dd0987de2ab420f78e60d9c17260776" + integrity sha512-zDml/jzr2PKU9I8J/xyZBQn8rPCAY//UOYNmR01XwNwyfhEWObo2SWfSl1+0tm1u6PhxLwDnfsT/6jB7OUxqFA== + ip@^1.1.5: version "1.1.8" resolved "https://registry.npmmirror.com/ip/-/ip-1.1.8.tgz#ae05948f6b075435ed3307acce04629da8cdbf48" @@ -1935,7 +1940,7 @@ uuid@^8.3.2: uuid@^9.0.0: version "9.0.0" - resolved "https://registry.yarnpkg.com/uuid/-/uuid-9.0.0.tgz#592f550650024a38ceb0c562f2f6aa435761efb5" + resolved "https://registry.npmmirror.com/uuid/-/uuid-9.0.0.tgz#592f550650024a38ceb0c562f2f6aa435761efb5" integrity sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg== validator@^13.7.0: