明树Git Lab

Commit 4778da46 authored by zfp1's avatar zfp1

update

parent 5cc65650
......@@ -22,63 +22,81 @@ const collectIndustryEnvironmentalTestingSchema = new Schema({
comment: "采集时间"
},
/*---------------------------------------------*/
ambientTemperature: {
pm10: {
type: Number,
comment: "环境温度",
},
ambientTemperatureUnit: {
type: String,
comment1: "环境温度单位",
},
ambientHumidity: {
pm25: {
type: Number,
comment: "环境湿度",
},
ambientHumidityUnit: {
type: String,
comment1: "环境湿度单位",
},
airQuality: {
temp: {
type: Number,
comment: "空气质量",
},
airQualityUnit: {
type: String,
comment1: "空气质量单位",
},
differentialPressure: {
hum: {
type: Number,
comment: "压差",
},
differentialPressureUnit: {
type: String,
comment1: "压差单位",
},
miCcrobialCount: {
flag03: {
type: Number,
comment: "微生物数",
},
miCcrobialCountUnit: {
type: String,
comment1: "微生物数单位",
},
particleConcentration: {
type: Number,
comment: "颗粒物浓度",
},
particleConcentrationUnit: {
type: String,
comment1: "颗粒物浓度单位",
},
chemicalPollutant: {
type: Number,
comment: "化学污染物",
},
chemicalPollutantUnit: {
type: String,
comment1: "化学污染物单位",
},
/*--------------------------------------------*/
// ambientTemperature: {
// type: Number,
// comment: "环境温度",
// },
// ambientTemperatureUnit: {
// type: String,
// comment1: "环境温度单位",
// },
// ambientHumidity: {
// type: Number,
// comment: "环境湿度",
// },
// ambientHumidityUnit: {
// type: String,
// comment1: "环境湿度单位",
// },
// airQuality: {
// type: Number,
// comment: "空气质量",
// },
// airQualityUnit: {
// type: String,
// comment1: "空气质量单位",
// },
// differentialPressure: {
// type: Number,
// comment: "压差",
// },
// differentialPressureUnit: {
// type: String,
// comment1: "压差单位",
// },
// miCcrobialCount: {
// type: Number,
// comment: "微生物数",
// },
// miCcrobialCountUnit: {
// type: String,
// comment1: "微生物数单位",
// },
// particleConcentration: {
// type: Number,
// comment: "颗粒物浓度",
// },
// particleConcentrationUnit: {
// type: String,
// comment1: "颗粒物浓度单位",
// },
// chemicalPollutant: {
// type: Number,
// comment: "化学污染物",
// },
// chemicalPollutantUnit: {
// type: String,
// comment1: "化学污染物单位",
// },
createdAt: {
type: Date,
......@@ -104,5 +122,5 @@ const collectIndustryEnvironmentalTestingSchema = new Schema({
});
const CollectIET = mongoose.model('CollectIET', collectIndustryEnvironmentalTestingSchema, 'CollectIET');
const CollectIET = mongoose.model('CollectIET', collectIndustryEnvironmentalTestingSchema, 'collectIET');
module.exports = CollectIET;
\ No newline at end of file
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const moment = require('moment');
// const mongoose = require('mongoose');
// const Schema = mongoose.Schema;
// const moment = require('moment');
/**
环境检测-------------
*/
const collectIndustryQualityInformationSchema = new Schema({
name: {
type: String,
comment: "设备名称"
},
deviceNumber: {
type: String,
required: true,
index: true,
comment: "设备编号"
},
time: {
type: Date,
get: v => moment(v).format("YYYY-MM-DD HH:mm:ss"),
comment: "采集时间"
},
/* ------------------------------ */
energy: {
type: Number,
comment: "能量",
},
energyUnit: {
type: String,
comment1: "能量单位",
},
protein: {
type: Number,
comment: "蛋白质",
},
proteinUnit: {
type: String,
comment1: "蛋白质单位",
},
fat: {
type: Number,
comment: "脂肪",
},
fatUnit: {
type: String,
comment1: "脂肪单位",
},
carbohydrate: {
type: Number,
comment: "碳水化合物",
},
carbohydrateUnit: {
type: String,
comment1: "碳水化合物单位",
},
sodium: {
type: Number,
comment: "钠",
},
sodiumUnit: {
type: String,
comment1: "钠单位",
},
water: {
type: Number,
comment: "水分",
},
waterUnit: {
type: String,
comment1: "水分单位",
},
// /**
// 环境检测-------------
// */
// const collectIndustryQualityInformationSchema = new Schema({
// name: {
// type: String,
// comment: "设备名称"
// },
// deviceNumber: {
// type: String,
// required: true,
// index: true,
// comment: "设备编号"
// },
// time: {
// type: Date,
// get: v => moment(v).format("YYYY-MM-DD HH:mm:ss"),
// comment: "采集时间"
// },
// /* ------------------------------ */
// energy: {
// type: Number,
// comment: "能量",
// },
// energyUnit: {
// type: String,
// comment1: "能量单位",
// },
// protein: {
// type: Number,
// comment: "蛋白质",
// },
// proteinUnit: {
// type: String,
// comment1: "蛋白质单位",
// },
// fat: {
// type: Number,
// comment: "脂肪",
// },
// fatUnit: {
// type: String,
// comment1: "脂肪单位",
// },
// carbohydrate: {
// type: Number,
// comment: "碳水化合物",
// },
// carbohydrateUnit: {
// type: String,
// comment1: "碳水化合物单位",
// },
// sodium: {
// type: Number,
// comment: "钠",
// },
// sodiumUnit: {
// type: String,
// comment1: "钠单位",
// },
// water: {
// type: Number,
// comment: "水分",
// },
// waterUnit: {
// type: String,
// comment1: "水分单位",
// },
heavyMetal: {
type: Number,
comment: "重金属",
},
heavyMetalUnit: {
type: String,
comment1: "重金属单位",
},
impurity: {
type: Number,
comment: "杂质",
},
impurityUnit: {
type: String,
comment1: "杂质单位",
},
// heavyMetal: {
// type: Number,
// comment: "重金属",
// },
// heavyMetalUnit: {
// type: String,
// comment1: "重金属单位",
// },
// impurity: {
// type: Number,
// comment: "杂质",
// },
// impurityUnit: {
// type: String,
// comment1: "杂质单位",
// },
createdAt: {
type: Date,
default: Date.now,
get: v => moment(v).format("YYYY-MM-DD HH:mm:ss"),
// createdAt: {
// type: Date,
// default: Date.now,
// get: v => moment(v).format("YYYY-MM-DD HH:mm:ss"),
},
updatedAt: {
type: Date,
default: Date.now,
get: v => moment(v).format("YYYY-MM-DD HH:mm:ss"),
// },
// updatedAt: {
// type: Date,
// default: Date.now,
// get: v => moment(v).format("YYYY-MM-DD HH:mm:ss"),
},
del: {
type: Number,
default: 0,
comment1: '默认0 , 1:表示删除,若有其他隐藏业务 不要混用此字段。'
}
}, {
toJSON: {
getters: true,
}
});
// },
// del: {
// type: Number,
// default: 0,
// comment1: '默认0 , 1:表示删除,若有其他隐藏业务 不要混用此字段。'
// }
// }, {
// toJSON: {
// getters: true,
// }
// });
const CollectIQI = mongoose.model('CollectIQI', collectIndustryQualityInformationSchema, 'CollectIQI');
module.exports = CollectIQI;
\ No newline at end of file
// const CollectIQI = mongoose.model('CollectIQI', collectIndustryQualityInformationSchema, 'CollectIQI');
// module.exports = CollectIQI;
\ No newline at end of file
......@@ -4,6 +4,7 @@ const iconv = require('iconv-lite');
let mongourl = `mongodb://root:letian2024.@172.16.0.16:27017/letian?authSource=admin`; //内网
const server = net.createServer((socket) => {
socket.on('data', (data) => {
const gbkString = iconv.decode(data, 'gbk');
......@@ -19,11 +20,15 @@ const server = net.createServer((socket) => {
socket.on('N82V3VWYU84K36UH', (data) => {
const gbkString = iconv.decode(data, 'gbk');
console.log(`N82V3VWYU84K36UH${gbkString}`);
console.log(`N82V3VWYU84K36UH:${gbkString}`);
let obj = genInsertObj(gbkString, "N82V3VWYU84K36UH");
connectAndInsert(obj);
});
socket.on('WO6936WQZ0L1Q4A1', (data) => {
const gbkString = iconv.decode(data, 'gbk');
console.log(`WO6936WQZ0L1Q4A1${gbkString}`);
console.log(`WO6936WQZ0L1Q4A1:${gbkString}`);
let obj = genInsertObj(gbkString, "WO6936WQZ0L1Q4A1");
connectAndInsert(obj);
});
socket.on('end', () => {
......@@ -37,17 +42,46 @@ server.listen(3001, () => {
})
function genInsertObj(gbkString, deviceNumber) {
let retString = gbkString.slice(deviceNumber);
let ret = JSON.parse(retString);
let obj = {
deviceNumber: deviceNumber,
}
for (let index = 0; index < ret.sensorDatas.length; index++) {
const element = ret.sensorDatas[index];
if (element.flag == "PM10") {
obj.pm10 = element.value;
}
if (element.flag == "PM2.5") {
obj.pm25 = element.value;
}
if (element.flag == "TEMP") {
obj.temp = element.value;
async function connectAndInsert() {
}
if (element.flag == "HUM") {
obj.hum = element.value;
}
if (element.flag == "03") {
obj.flag03 = element.value;
}
}
return obj;
}
async function connectAndInsert(data) {
// 创建 MongoDB 客户端
const client = new MongoClient(mongourl, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 5000, // 5秒连接超时
});
try {
// 连接到 MongoDB 服务器
console.log(' 正在连接 MongoDB...');
await client.connect();
......@@ -55,31 +89,14 @@ async function connectAndInsert() {
// 获取数据库和集合
const db = client.db("letian");
const collection = db.collection("COLLECTION_NAME");
// 创建示例数据
const userData = {
name: '张三',
email: 'zhangsan@example.com',
age: 30,
createdAt: new Date(),
tags: ['nodejs', 'developer'],
address: {
city: '北京',
country: '中国'
}
};
const collection = db.collection("collectIET");
// 插入单个文档
console.log(' 正在插入数据...');
const insertResult = await collection.insertOne(userData);
const insertResult = await collection.insertOne(data);
console.log(`✅ 数据插入成功,文档ID: ${insertResult.insertedId}`);
} catch (err) {
console.error('❌ 数据库操作出错:', err);
} finally {
// 关闭连接
await client.close();
console.log('🔌 MongoDB 连接已关闭');
}
}
\ No newline at end of file
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