-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (59 loc) · 2.08 KB
/
Copy pathserver.js
File metadata and controls
71 lines (59 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import express from 'express';
import mongoose from 'mongoose';
import dotenv from 'dotenv';
dotenv.config();
const app = express();
const PORT = process.env.PORT || 3000;
const MONGODB_URI = process.env.MONGODB_URI;
app.use(express.json()); // Đọc JSON từ body
// Kết nối MongoDB
mongoose.connect(MONGODB_URI)
.then(() => console.log('✅ Đã kết nối MongoDB'))
.catch(err => {
console.error('❌ Lỗi kết nối MongoDB:', err);
process.exit(1);
});
// Tạo dynamic model dựa trên sensor_id
function getSensorModel(sensorId) {
if (mongoose.models[sensorId]) {
return mongoose.models[sensorId]; // Dùng lại model nếu đã tồn tại
}
const schema = new mongoose.Schema({}, { strict: false });
return mongoose.model(sensorId, schema, sensorId); // sensorId = tên collection
}
// Route POST: ESP32 gửi dữ liệu
app.post('/stream_data', async (req, res) => {
const data = req.body;
console.log('📨 Payload nhận được:', req.body);
const { sensor_id } = data;
if (!sensor_id) {
return res.status(400).send('Thiếu sensor_id');
}
try {
const SensorModel = getSensorModel(sensor_id);
const entry = new SensorModel({ ...data, timestamp: new Date() });
await entry.save();
console.log(`📥 Dữ liệu từ ${sensor_id}:`, data);
res.status(200).send('Đã lưu thành công');
} catch (err) {
console.error('❌ Lỗi ghi dữ liệu:', err);
res.status(500).send('Lỗi server');
}
});
// Route GET: frontend lấy dữ liệu theo sensor_id
app.get('/sensor_data/:sensor_id', async (req, res) => {
const { sensor_id } = req.params;
const limit = parseInt(req.query.limit) || 100;
try {
const SensorModel = getSensorModel(sensor_id);
const data = await SensorModel.find().sort({ timestamp: -1 }).limit(limit);
res.json(data);
} catch (err) {
console.error('❌ Lỗi truy xuất:', err);
res.status(500).send('Không lấy được dữ liệu');
}
});
// Khởi động server
app.listen(PORT, () => {
console.log(`🚀 Server đang chạy tại http://localhost:${PORT}`);
});