From 0223fe7f92d3110e24ab3bf6d3625e3d0b396a8c Mon Sep 17 00:00:00 2001 From: maochaoying <925670706@qq.com> Date: Thu, 20 Apr 2023 17:07:47 +0800 Subject: [PATCH] 123 --- src/main.js | 40 ++++++++++++---- src/mock/index.js | 20 ++++---- src/service/environment.service.js | 96 ++++++++++++++++++++------------------ src/test/someFunc.js | 20 ++++---- src/utils/message.js | 58 ++++++++++++++--------- 5 files changed, 139 insertions(+), 95 deletions(-) diff --git a/src/main.js b/src/main.js index 9ff9f11..4cbd885 100644 --- a/src/main.js +++ b/src/main.js @@ -5,6 +5,7 @@ const { WEB_SOCKET_PORT, WEB_SOCKET_SERVER_HOST, } = require("./config/config.default"); +const moment = require("moment"); const { handleMessage } = require("./utils/message"); const WebSocket = require("ws"); @@ -29,18 +30,39 @@ const wss = new WebSocketServer({ wss.on("connection", function (ws) { //在connection事件中,回调函数会传入一个WebSocket的实例,表示这个WebSocket连接。 - console.log( - `websocket server is connected on ws:/${WEB_SOCKET_SERVER_HOST}:${WEB_SOCKET_PORT}` + ws.send( + JSON.stringify({ + command: "connectedServer", + timestamp: moment().valueOf(), + }), + (err) => { + if (err) { + console.log(`[SERVER] error:${err}`); + } + } ); ws.on("message", function (message) { console.log(`[SERVER] Received:${message}`); // 对接收来的消息进行处理分发 - handleMessage(message.toString()); - // 向客户端回传消息 表明已经接收到 - ws.send(`server received your message`, (err) => { - if (err) { - console.log(`[SERVER] error:${err}`); - } - }); + const res = handleMessage(message.toString()); + if (res) { + ws.send(JSON.stringify(res), (err) => { + if (err) { + console.log(`[SERVER] error:${err}`); + } + }); + } else { + ws.send( + JSON.stringify({ + command: "reveivedMessage", + timestamp: moment().valueOf(), + }), + (err) => { + if (err) { + console.log(`[SERVER] error:${err}`); + } + } + ); + } }); }); diff --git a/src/mock/index.js b/src/mock/index.js index 3dc2f9c..8dd43a5 100644 --- a/src/mock/index.js +++ b/src/mock/index.js @@ -3,13 +3,13 @@ const sensor_data = { messageId: "xxxxx-xxxxx-xxxxx-xxxxx", timestamp: "2023-04-16 16:22:11", //unix时间戳ms positionM: 50, //横向距离 - henhouseId: "cdc7c436-2f1d-4259-967a-bd15457b90a6", - farmId: "57180cf6-769b-4372-836c-193b62b82ade", + henhouseId: "a71a74f0-c6cd-4cac-b9b2-299a975c1b95", + farmId: "7b61e8d4-d614-4cd6-81b9-3d7867bd6df9", env: [ { mac: "00:28:f8:6f:a3:92", - layer_index: 1, //第几层 - line_index: 1, //第几列 + layerIndex: 1, //第几层 + lineIndex: 1, //第几列 // position_index: 11, vehicle_index: 1, co2: 500.0, @@ -18,8 +18,8 @@ const sensor_data = { }, { mac: "01:xx:xx:xx:xx:xx", - layer_index: 2, //第几层 - line_index: 1, //第几列 + layerIndex: 2, //第几层 + lineIndex: 1, //第几列 // position_index: 12, vehicle_index: 1, co2: 500.0, @@ -28,8 +28,8 @@ const sensor_data = { }, { mac: "xx:xx:xx:xx:xx:xx", - layer_index: 3, //第几层 - line_index: 1, //第几列 + layerIndex: 3, //第几层 + lineIndex: 1, //第几列 // position_index: 13, vehicle_index: 1, co2: 500.0, @@ -38,8 +38,8 @@ const sensor_data = { }, { mac: "xx:xx:xx:xx:xx:xx", - layer_index: 1, //第几层 - line_index: 2, //第几列 + layerIndex: 1, //第几层 + lineIndex: 2, //第几列 // position_index: 14, vehicle_index: 1, co2: 500.0, diff --git a/src/service/environment.service.js b/src/service/environment.service.js index 489220e..65f3c6e 100644 --- a/src/service/environment.service.js +++ b/src/service/environment.service.js @@ -21,9 +21,9 @@ class EnvironmentService { let humidityTotal = 0; let co2Total = 0; arr.map((i) => { - tempTotal += parseInt(i.temperature); - humidityTotal += parseInt(i.humidity); - co2Total += parseInt(i.co2); + tempTotal += parseFloat(i.temperature); + humidityTotal += parseFloat(i.humidity); + co2Total += parseFloat(i.co2); }); return { ...arr[0], @@ -126,13 +126,13 @@ class EnvironmentService { let windSpeedTotal = 0; let h2sTotal = 0; item.map((i) => { - tempTotal += parseInt(i.temperature); - humidityTotal += parseInt(i.humidity); - co2Total += parseInt(i.co2); - nh3Total += parseInt(i.nh3); - illuminationTotal += parseInt(i.illumination); - windSpeedTotal += parseInt(i.wind_speed); - h2sTotal += parseInt(i.h2s); + tempTotal += parseFloat(i.temperature); + humidityTotal += parseFloat(i.humidity); + co2Total += parseFloat(i.co2); + nh3Total += parseFloat(i.nh3); + illuminationTotal += parseFloat(i.illumination); + windSpeedTotal += parseFloat(i.wind_speed); + h2sTotal += parseFloat(i.h2s); }); dataObj.push({ temperature: (tempTotal / len).toFixed(2), @@ -177,13 +177,13 @@ class EnvironmentService { let windSpeedTotal = 0; let h2sTotal = 0; item.map((i) => { - tempTotal += parseInt(i.temperature); - humidityTotal += parseInt(i.humidity); - co2Total += parseInt(i.co2); - nh3Total += parseInt(i.nh3); - illuminationTotal += parseInt(i.illumination); - windSpeedTotal += parseInt(i.wind_speed); - h2sTotal += parseInt(i.h2s); + tempTotal += parseFloat(i.temperature); + humidityTotal += parseFloat(i.humidity); + co2Total += parseFloat(i.co2); + nh3Total += parseFloat(i.nh3); + illuminationTotal += parseFloat(i.illumination); + windSpeedTotal += parseFloat(i.wind_speed); + h2sTotal += parseFloat(i.h2s); }); dataObj.push({ temperature: (tempTotal / len).toFixed(2), @@ -252,35 +252,41 @@ class EnvironmentService { // 因为目前设备并没有鸡场、鸡舍相关概念 默认为0,没有的数据默认为0 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 || !coopInfo || !houseInfo) { - return; - } - const p = env.map(async (item) => { - // 查询传过来的coop_id和house_id - const res = await Environment.create({ - coop_id: coopInfo.id, - house_id: houseInfo.id, - temperature: item.temperature, - humidity: item.humidity, - co2: item.co2, - nh3: 0, - illumination: 0, - wind_speed: 0, - h2s: 0, - position_index: item.position_index, - layer_index: item.layer_index, - line_index: item.line_index, - positionM, - log_time, + try { + // 根据uuid查询各自的id + const coopInfo = await getCoopIdByUUID(henhouseId); + const houseInfo = await getHouseIdByUUID(farmId); + // position_index, positionM, log_time; + if (!env || !coopInfo || !houseInfo) { + return; + } + const p = env.map(async (item) => { + // 查询传过来的coop_id和house_id + if (item.layerIndex && item.lineIndex) { + const res = await Environment.create({ + coop_id: coopInfo.id, + house_id: houseInfo.id, + temperature: item.temperature, + humidity: item.humidity, + co2: item.co2, + nh3: 0, + illumination: 0, + wind_speed: 0, + h2s: 0, + position_index: item.position_index, + layer_index: item.layerIndex, + line_index: item.lineIndex, + positionM, + log_time, + }); + return res; + } }); - return res; - }); - const real = await Promise.all(p); - return real; + const real = await Promise.all(p); + return real; + } catch (error) { + console.log(error); + } } // 获取环境实时监测数据 diff --git a/src/test/someFunc.js b/src/test/someFunc.js index 9770f3a..29f9117 100644 --- a/src/test/someFunc.js +++ b/src/test/someFunc.js @@ -77,13 +77,15 @@ // console.log(getTimeLastDate("2023-03-28 12:12:12", 3)); -function isIntTime() { - const now = new Date(); - const hour = now.getHours(); //得到小时数 - const minute = now.getMinutes(); //得到分钟数 - const second = now.getSeconds(); //得到秒数 - console.log(minute); - console.log(second); -} +// function isIntTime() { +// const now = new Date(); +// const hour = now.getHours(); //得到小时数 +// const minute = now.getMinutes(); //得到分钟数 +// const second = now.getSeconds(); //得到秒数 +// console.log(minute); +// console.log(second); +// } + +// isIntTime(); -isIntTime(); +console.log(11 / 5); diff --git a/src/utils/message.js b/src/utils/message.js index 1d27bbc..8b2159a 100644 --- a/src/utils/message.js +++ b/src/utils/message.js @@ -9,13 +9,17 @@ const handleSensorMessage = (message) => { const positionM = message?.positionM; // 目前默认每一层有10个笼位置 目前500m // 每到一个笼位记录一次 - if (positionM % 50 == 0) { - const env = message?.env || []; - 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, henhouseId, farmId); - } + const env = message?.env || []; + const henhouseId = message?.henhouseId; + const farmId = message?.farmId; + const log_time = moment(message?.timestamp).format("YYYY-MM-DD HH:mm:ss"); + reportEnvironmentData( + env, + 10 * parseInt(parseInt(positionM) / 10), + log_time, + henhouseId, + farmId + ); }; const handleFeedMessage = async (message) => { const eventType = message?.eventType; @@ -30,21 +34,31 @@ const handleFeedMessage = async (message) => { }; const handleMessage = (message) => { - // 对message进行处理并分发到handleSensorMessage / handleFeedMessage - if (!isJsonString(message)) { - return; - } - const messageJson = JSON.parse(message); - const command = messageJson?.command; - switch (command) { - case "feedingEventReport": - handleFeedMessage(messageJson); - break; - case "envDataReport": - handleSensorMessage(messageJson); - break; - default: - return; + try { + // 对message进行处理并分发到handleSensorMessage / handleFeedMessage + if (!isJsonString(message)) { + return { + command: "messageNotJSON", + timestamp: moment().valueOf(), + }; + } + const messageJson = JSON.parse(message); + const command = messageJson?.command; + switch (command) { + case "feedingEventReport": + handleFeedMessage(messageJson); + break; + case "envDataReport": + handleSensorMessage(messageJson); + break; + default: + return; + } + } catch (error) { + return { + command: "ServerError", + timestamp: moment.valueOf(), + }; } }; module.exports = {