明树Git Lab

Commit 4778da46 authored by zfp1's avatar zfp1

update

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