maochaoying 2 years ago
parent
commit
0223fe7f92
  1. 32
      src/main.js
  2. 20
      src/mock/index.js
  3. 44
      src/service/environment.service.js
  4. 20
      src/test/someFunc.js
  5. 22
      src/utils/message.js

32
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) => {
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}`);
}
}
);
}
});
});

20
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,

44
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,6 +252,7 @@ class EnvironmentService {
// 因为目前设备并没有鸡场、鸡舍相关概念 默认为0,没有的数据默认为0
async reportEnvironmentData(env, positionM, log_time, henhouseId, farmId) {
try {
// 根据uuid查询各自的id
const coopInfo = await getCoopIdByUUID(henhouseId);
const houseInfo = await getHouseIdByUUID(farmId);
@ -261,6 +262,7 @@ class EnvironmentService {
}
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,
@ -272,15 +274,19 @@ class EnvironmentService {
wind_speed: 0,
h2s: 0,
position_index: item.position_index,
layer_index: item.layer_index,
line_index: item.line_index,
layer_index: item.layerIndex,
line_index: item.lineIndex,
positionM,
log_time,
});
return res;
}
});
const real = await Promise.all(p);
return real;
} catch (error) {
console.log(error);
}
}
// 获取环境实时监测数据

20
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);

22
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);
}
reportEnvironmentData(
env,
10 * parseInt(parseInt(positionM) / 10),
log_time,
henhouseId,
farmId
);
};
const handleFeedMessage = async (message) => {
const eventType = message?.eventType;
@ -30,9 +34,13 @@ const handleFeedMessage = async (message) => {
};
const handleMessage = (message) => {
try {
// 对message进行处理并分发到handleSensorMessage / handleFeedMessage
if (!isJsonString(message)) {
return;
return {
command: "messageNotJSON",
timestamp: moment().valueOf(),
};
}
const messageJson = JSON.parse(message);
const command = messageJson?.command;
@ -46,6 +54,12 @@ const handleMessage = (message) => {
default:
return;
}
} catch (error) {
return {
command: "ServerError",
timestamp: moment.valueOf(),
};
}
};
module.exports = {
handleMessage,

Loading…
Cancel
Save