-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
481 lines (429 loc) · 15.6 KB
/
api.js
File metadata and controls
481 lines (429 loc) · 15.6 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// <사용 예시>
// # 수위 센서의 level_low 설정값을 5로 변경
// curl -X PUT \
// http://localhost:5000/api/sensor-settings/water_level/level_low \
// -H 'Authorization: Bearer your_auth_token' \
// -H 'Content-Type: application/json' \
// -d '{"value": 5}'
// # 응답
// {
// "message": "Setting updated successfully",
// "sensor_type": "water_level",
// "setting_name": "level_low",
// "new_value": 5,
// "updated_at": "2024-03-20T09:30:00.000Z"
// }
require('dotenv').config();
const express = require('express');
const sqlite3 = require('sqlite3').verbose();
const session = require('express-session');
const path = require('path');
const fs = require('fs');
// 환경변수 체크
if (!process.env.SESSION_SECRET) {
console.error('❌ Error: SESSION_SECRET is not set in environment variables');
process.exit(1);
}
if (!process.env.AUTH_TOKEN) {
console.error('❌ Error: AUTH_TOKEN is not set in environment variables');
process.exit(1);
}
const app = express();
const PORT = process.env.PORT || 5000;
// photos 디렉토리 설정
const PHOTOS_DIR = path.join(__dirname, 'photos');
if (!fs.existsSync(PHOTOS_DIR)) {
fs.mkdirSync(PHOTOS_DIR);
console.log('✅ Photos directory created');
}
// sensor_data.db 사용하도록 변경
const db = new sqlite3.Database('./sensor_data.db', (err) => {
if (err) {
console.error('❌ Database connection error:', err);
process.exit(1);
}
console.log('✅ Successfully connected to the database');
initializeTables();
});
// 모든 테이블 초기화 함수
function initializeTables() {
const tables = [
`CREATE TABLE IF NOT EXISTS tb_ph (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT,
sensor_id TEXT,
location TEXT,
pH_value REAL,
voltage REAL
)`,
`CREATE TABLE IF NOT EXISTS tb_water_level (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT,
sensor_id TEXT,
location TEXT,
water_level REAL,
voltage REAL
)`,
`CREATE TABLE IF NOT EXISTS tb_water_temperature (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT,
sensor_id TEXT,
location TEXT,
temperature REAL,
voltage REAL
)`,
`CREATE TABLE IF NOT EXISTS tb_illuminance (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT,
sensor_id TEXT,
location TEXT,
illuminance REAL,
voltage REAL
)`,
`CREATE TABLE IF NOT EXISTS tb_conductivity (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT,
sensor_id TEXT,
location TEXT,
conductivity REAL,
voltage REAL
)`,
`CREATE TABLE IF NOT EXISTS tb_air_pump (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT,
sensor_id TEXT,
location TEXT,
status TEXT,
voltage REAL
)`,
`CREATE TABLE IF NOT EXISTS tb_water_pump (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT,
sensor_id TEXT,
location TEXT,
status TEXT,
voltage REAL
)`,
`CREATE TABLE IF NOT EXISTS tb_alert (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT,
sensor_id TEXT,
location TEXT,
alert_type TEXT,
message TEXT
)`,
`CREATE TABLE IF NOT EXISTS tb_feed (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT,
sensor_id TEXT,
location TEXT,
feed_amount REAL,
status TEXT
)`,
`CREATE TABLE IF NOT EXISTS tb_sensor_settings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
sensor_type TEXT,
setting_name TEXT,
setting_value REAL,
updated_at TEXT,
UNIQUE(sensor_type, setting_name)
)`
];
tables.forEach(sql => {
db.run(sql, err => {
if (err) {
console.error('Table creation error:', err);
}
});
});
// 테이블 생성 후 기본설정값 초기화
initializeDefaultSettings();
}
// 기본 센서 설정값초기화 함수 추가
function initializeDefaultSettings() {
const defaultSettings = [
// 카메라 설정
{ sensor_type: 'camera', setting_name: 'capture_interval', setting_value: 3600 }, // 초 단위
// 먹이 모터 설정
{ sensor_type: 'feed', setting_name: 'feed_interval', setting_value: 43200 }, // 12시간(초)
// 기포 발생기 설정
{ sensor_type: 'air_pump', setting_name: 'ph_threshold_min', setting_value: 6.5 },
{ sensor_type: 'air_pump', setting_name: 'ph_threshold_max', setting_value: 7.5 },
{ sensor_type: 'air_pump', setting_name: 'conductivity_threshold_min', setting_value: 100 },
{ sensor_type: 'air_pump', setting_name: 'conductivity_threshold_max', setting_value: 500 },
// 수위 센서 설정
{ sensor_type: 'water_level', setting_name: 'level_low', setting_value: 10 },
{ sensor_type: 'water_level', setting_name: 'level_normal', setting_value: 20 },
{ sensor_type: 'water_level', setting_name: 'level_high', setting_value: 30 },
// pH 센서 설정
{ sensor_type: 'ph', setting_name: 'sensing_interval', setting_value: 300 }, // 5분(초)
{ sensor_type: 'ph', setting_name: 'alert_min', setting_value: 6.0 },
{ sensor_type: 'ph', setting_name: 'alert_max', setting_value: 8.0 },
// 전도도 센서 설정
{ sensor_type: 'conductivity', setting_name: 'sensing_interval', setting_value: 300 },
// 조도 센서 설정
{ sensor_type: 'illuminance', setting_name: 'sensing_interval', setting_value: 300 },
{ sensor_type: 'illuminance', setting_name: 'alert_max', setting_value: 1000 },
// 수온 센서 설정
{ sensor_type: 'water_temperature', setting_name: 'sensing_interval', setting_value: 300 },
{ sensor_type: 'water_temperature', setting_name: 'alert_min', setting_value: 20 },
{ sensor_type: 'water_temperature', setting_name: 'alert_max', setting_value: 30 }
];
const stmt = db.prepare(`
INSERT OR REPLACE INTO tb_sensor_settings
(sensor_type, setting_name, setting_value, updated_at)
VALUES (?, ?, ?, datetime('now'))
`);
db.serialize(() => {
db.run('BEGIN TRANSACTION');
defaultSettings.forEach(setting => {
stmt.run(setting.sensor_type, setting.setting_name, setting.setting_value, (err) => {
if (err) {
db.run('ROLLBACK');
console.error('Error inserting default settings:', err);
return;
}
});
});
db.run('COMMIT');
});
stmt.finalize();
}
// 세션 설정
const isProduction = process.env.NODE_ENV === 'production';
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
cookie: {
secure: isProduction,
httpOnly: true,
sameSite: 'strict'
}
}));
// 인증 미들웨어
app.use((req, res, next) => {
if (req.session.isAuthenticated) {
return next();
}
const token = req.headers['authorization'];
if (!token || token !== `Bearer ${process.env.AUTH_TOKEN}`) {
return res.status(401).json({ error: 'Unauthorized access' });
}
req.session.isAuthenticated = true;
next();
});
// POST 요청을 위한 JSON 파서 미들웨어 추가
app.use(express.json());
// 센서 데이터 저장 API 엔드포인트
app.post('/api/sensor-data/:sensorType', (req, res) => {
const { sensorType } = req.params;
const sensorData = req.body;
// 센서 타입별 테이블 매핑
const tableMap = {
'ph': 'tb_ph',
'water_level': 'tb_water_level',
'water_temperature': 'tb_water_temperature',
'illuminance': 'tb_illuminance',
'conductivity': 'tb_conductivity'
};
const tableName = tableMap[sensorType];
if (!tableName) {
return res.status(400).json({ error: 'Invalid sensor type' });
}
// 데이터 유효성 검사 및 변환 함수
const validateAndTransform = (data) => {
switch(sensorType) {
case 'ph':
return {
timestamp: data.timestamp,
sensor_id: data.sensor_id,
location: data.location,
pH_value: parseFloat(data.pH_value),
voltage: parseFloat(data.voltage)
};
case 'water_level':
return {
timestamp: data.timestamp,
sensor_id: data.sensor_id,
location: data.location,
water_level: parseFloat(data.water_level),
voltage: parseFloat(data.voltage)
};
case 'water_temperature':
return {
timestamp: data.timestamp,
sensor_id: data.sensor_id,
location: data.location,
temperature: parseFloat(data.temp_value),
voltage: parseFloat(data.voltage || 0) // 옵셔널
};
case 'illuminance':
return {
timestamp: data.timestamp,
sensor_id: data.sensor_id,
location: data.location,
illuminance: parseFloat(data.light_value),
voltage: parseFloat(data.voltage || 0)
};
case 'conductivity':
return {
timestamp: data.timestamp,
sensor_id: data.sensor_id,
location: data.location,
conductivity: parseFloat(data.tds_value),
voltage: parseFloat(data.voltage || 0)
};
default:
throw new Error('Invalid sensor type');
}
};
try {
const transformedData = validateAndTransform(sensorData);
// 데이터베이스에 저장
const columns = Object.keys(transformedData).join(', ');
const placeholders = Object.keys(transformedData).map(() => '?').join(', ');
const values = Object.values(transformedData);
db.run(
`INSERT INTO ${tableName} (${columns}) VALUES (${placeholders})`,
values,
function(err) {
if (err) {
res.status(500).json({ error: 'Failed to save sensor data', details: err.message });
} else {
res.json({
message: 'Sensor data saved successfully',
id: this.lastID
});
}
}
);
} catch (err) {
res.status(400).json({ error: 'Invalid sensor data format', details: err.message });
}
});
// 센서 설정값 조회 API
app.get('/api/sensor-settings/:sensorType', (req, res) => {
const { sensorType } = req.params;
db.all(
'SELECT * FROM tb_sensor_settings WHERE sensor_type = ? ORDER BY setting_name',
[sensorType],
(err, rows) => {
if (err) {
res.status(500).json({ error: 'Database error', details: err.message });
} else if (rows.length === 0) {
res.status(404).json({ error: 'No settings found for this sensor type' });
} else {
res.json({
sensor_type: sensorType,
settings: rows
});
}
}
);
});
// 특정 센서의 특정 설정값 조회 API
app.get('/api/sensor-settings/:sensorType/:settingName', (req, res) => {
const { sensorType, settingName } = req.params;
db.get(
'SELECT * FROM tb_sensor_settings WHERE sensor_type = ? AND setting_name = ?',
[sensorType, settingName],
(err, row) => {
if (err) {
res.status(500).json({ error: 'Database error', details: err.message });
} else if (!row) {
res.status(404).json({ error: 'Setting not found' });
} else {
res.json(row);
}
}
);
});
// 센서 설정값 업데이트 API
app.put('/api/sensor-settings/:sensorType/:settingName', (req, res) => {
const { sensorType, settingName } = req.params;
const { value } = req.body;
// 값 유효성 검사
if (value === undefined || value === null) {
return res.status(400).json({ error: 'Setting value is required' });
}
// 숫자 값으로 변환 시도
const numericValue = Number(value);
if (isNaN(numericValue)) {
return res.status(400).json({ error: 'Setting value must be a number' });
}
const timestamp = new Date().toISOString();
db.run(
`UPDATE tb_sensor_settings
SET setting_value = ?, updated_at = ?
WHERE sensor_type = ? AND setting_name = ?`,
[numericValue, timestamp, sensorType, settingName],
function(err) {
if (err) {
res.status(500).json({
error: 'Database error',
details: err.message
});
} else if (this.changes === 0) {
res.status(404).json({
error: 'Setting not found',
message: `No setting found for sensor type '${sensorType}' with name '${settingName}'`
});
} else {
res.json({
message: 'Setting updated successfully',
sensor_type: sensorType,
setting_name: settingName,
new_value: numericValue,
updated_at: timestamp
});
}
}
);
});
// 최근 사진 조회 API
app.get('/api/latest-photo', (req, res) => {
fs.readdir(PHOTOS_DIR, (err, files) => {
if (err) {
return res.status(500).json({ error: 'Failed to read directory' });
}
// 이미지 파일만 필터링
const imageFiles = files.filter(file => {
const ext = path.extname(file).toLowerCase();
return ['.jpg', '.jpeg', '.png', '.gif'].includes(ext);
});
if (imageFiles.length === 0) {
return res.status(404).json({ error: 'No photos found' });
}
// 각 파일의 수정 시간 확인
const fileStats = imageFiles.map(file => ({
name: file,
time: fs.statSync(path.join(PHOTOS_DIR, file)).mtime.getTime()
}));
// 수정 시간 기준 정렬
fileStats.sort((a, b) => b.time - a.time);
// 가장 최근 파일 전송
const latestFile = path.join(PHOTOS_DIR, fileStats[0].name);
res.sendFile(latestFile);
});
});
// 404 에러 처리
app.use((req, res) => {
res.status(404).json({
error: 'Invalid endpoint or table does not exist',
message: `The requested path '${req.path}' was not found`
});
});
// 종료 처리
process.on('SIGINT', () => {
db.close((err) => {
if (err) {
console.error('Error closing database:', err);
}
process.exit(err ? 1 : 0);
});
});
// 서버 시작
app.listen(PORT, () => {
console.log(`API server is running at http://localhost:${PORT}`);
});