明树Git Lab

Commit a53e05ea authored by zfp1's avatar zfp1

update

parent 8b2e94e6
...@@ -24,19 +24,19 @@ const collectIndustryEnvironmentalTestingSchema = new Schema({ ...@@ -24,19 +24,19 @@ const collectIndustryEnvironmentalTestingSchema = new Schema({
/*---------------------------------------------*/ /*---------------------------------------------*/
pm10: { pm10: {
type: Number, type: Number, //μg/m³
}, },
pm25: { pm25: {
type: Number, type: Number, //μg/m³
}, },
temp: { temp: {
type: Number, type: Number, //温度 ℃
}, },
hum: { hum: {
type: Number, type: Number, //湿度 %RH
}, },
flag03: { O3: {
type: Number, type: Number, //臭氧 ppm
}, },
/*--------------------------------------------*/ /*--------------------------------------------*/
......
...@@ -7,7 +7,8 @@ let mongourl = `mongodb://root:letian2024.@172.16.0.16:27017/letian?authSource=a ...@@ -7,7 +7,8 @@ let mongourl = `mongodb://root:letian2024.@172.16.0.16:27017/letian?authSource=a
const server = net.createServer((socket) => { const server = net.createServer((socket) => {
socket.on('data', (data) => { socket.on('data', (data) => {
if(isHeartbeat(data)) { if (isHeartbeat(data)) {
//心跳
let dataString = data.toString("hex"); let dataString = data.toString("hex");
console.log(dataString, "----"); console.log(dataString, "----");
socket.write(Buffer.from([0xD0, 0x00])); socket.write(Buffer.from([0xD0, 0x00]));
...@@ -23,15 +24,15 @@ const server = net.createServer((socket) => { ...@@ -23,15 +24,15 @@ const server = net.createServer((socket) => {
if (gbkString.includes('N82V3VWYU84K36UH')) { if (gbkString.includes('N82V3VWYU84K36UH')) {
console.log(`N82V3VWYU84K36UH:${gbkString}`); console.log(`N82V3VWYU84K36UH:${gbkString}`);
// let obj = genInsertObj(gbkString, "N82V3VWYU84K36UH"); let obj = genInsertObj(gbkString, "N82V3VWYU84K36UH");
// console.log(obj); console.log(obj);
// connectAndInsert(obj); connectAndInsert(obj);
} }
if (gbkString.includes('WO6936WQZ0L1Q4A1')) { if (gbkString.includes('WO6936WQZ0L1Q4A1')) {
console.log(`WO6936WQZ0L1Q4A1:${gbkString}`); console.log(`WO6936WQZ0L1Q4A1:${gbkString}`);
// let obj = genInsertObj(gbkString, "WO6936WQZ0L1Q4A1"); let obj = genInsertObj(gbkString, "WO6936WQZ0L1Q4A1");
// console.log(obj); console.log(obj);
// connectAndInsert(obj); connectAndInsert(obj);
} }
}); });
...@@ -68,33 +69,57 @@ const isHeartbeat = (buffer) => { ...@@ -68,33 +69,57 @@ const isHeartbeat = (buffer) => {
function genInsertObj(gbkString, deviceNumber) { function genInsertObj(gbkString, deviceNumber) {
let retString = gbkString.slice(deviceNumber); // 0[N82V3VWYU84K36UH{"sensorDatas":[{"flag":"PM10","value":56}, {"flag":"PM2.5","value":35}]}0yN82V3VWYU84K36UH{"sensorDatas":[{"flag":"TEMP","value":26.5}, {"flag":"O3","value":0.02}, {"flag":"HUM","value":78.5}]}
let ret = JSON.parse(retString); let splitArr = gbkString.split(deviceNumber);
let arr = [];
for (let index = 0; index < splitArr.length; index++) {
let element = splitArr[index];
if (element.startsWith('{')) {
if (!element.endsWith('}')) {
element = element.slice(0, -2);
}
arr.push(element)
}
}
let obj = { let obj = {
deviceNumber: deviceNumber, deviceNumber: deviceNumber,
} }
for (let index = 0; index < ret.sensorDatas.length; index++) { for (let index = 0; index < arr.length; index++) {
const element = ret.sensorDatas[index]; const element = arr[index];
if (element.flag == "PM10") { try {
obj.pm10 = element.value; let jsonObj = JSON.parse(element);
// console.log(jsonObj, "===")
let sensorDatas = jsonObj.sensorDatas;
for (let i = 0; i < sensorDatas.length; i++) {
const s = sensorDatas[i];
if (s.flag == "PM10") {
obj.pm10 = s.value;
} }
if (element.flag == "PM2.5") { if (s.flag == "PM2.5") {
obj.pm25 = element.value; obj.pm25 = s.value;
} }
if (element.flag == "TEMP") { if (s.flag == "TEMP") {
obj.temp = element.value; obj.temp = s.value;
} }
if (element.flag == "HUM") { if (s.flag == "HUM") {
obj.hum = element.value; obj.hum = s.value;
} }
if (element.flag == "03") { if (s.flag == "O3") {
obj.flag03 = element.value; obj.O3 = s.value;
} }
} }
} catch (error) {
console.log("JSON解析出错", error,)
}
}
return obj; return obj;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment