-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdht-server.js
111 lines (97 loc) · 3 KB
/
dht-server.js
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
var mysql = require('mysql'),
db_info = {
host: 'localhost',
user: 'dht',
password: 'dht',
database: 'dht_log'
};
var moment = require('moment');
var express = require('express'),
app = express();
var dht_router = express.Router();
// static
app.use(express.static('static'));
// route for /dht
// /date
dht_router.get('/date', function(req, res) {
var year = req.query.year || moment().year(),
month = req.query.month || moment().month() + 1,
date_of_month = req.query.date_of_month || moment().date();
var db_connection = mysql.createConnection(db_info);
db_connection.connect(function(error) {
if(error) {
console.error('error db connecting');
console.error(error);
res.send({error: true});
} else {
db_connection.query('SELECT DATE_FORMAT(FROM_UNIXTIME(created_at), "%k시 %i분") AS time, temperature, humidity FROM dht WHERE year = ? AND month = ? AND date_of_month = ? ORDER BY id ASC', [year, month, date_of_month], function(error, rows) {
if(error) {
console.error('error db query');
console.error('error');
res.send({error: true});
} else {
res.send({
error: false,
result: rows
});
}
db_connection.end();
});
}
});
});
// /hour_average
dht_router.get('/hour_average', function(req, res) {
var db_connection = mysql.createConnection(db_info);
db_connection.connect(function(error) {
if(error) {
console.error('error db connecting');
console.error(error);
res.send({error: true});
} else {
db_connection.query('SELECT CONCAT(hour, "시") AS hour, AVG(temperature) AS temperature, AVG(humidity) AS humidity FROM dht GROUP BY hour ORDER BY hour ASC', function(error, rows) {
if(error) {
console.error('error db query');
console.error('error');
res.send({error: true});
} else {
res.send({
error: false,
result: rows
});
}
db_connection.end();
});
}
});
});
// /date_average
dht_router.get('/date_average', function(req, res) {
var db_connection = mysql.createConnection(db_info);
db_connection.connect(function(error) {
if(error) {
console.error('error db connecting');
console.error(error);
res.send({error: true});
} else {
db_connection.query('SELECT CONCAT(month, "-", date_of_month) AS date_of_month, AVG(temperature) AS temperature, AVG(humidity) AS humidity FROM dht GROUP BY CONCAT(month, date_of_month) ORDER BY id ASC', function(error, rows) {
if(error) {
console.error('error db query');
console.error('error');
res.send({error: true});
} else {
res.send({
error: false,
result: rows
});
}
db_connection.end();
});
}
});
});
app.use('/dht', dht_router);
// app listen to port
app.listen(8080, function() {
console.log('dht-server.js started.');
});