forked from CobaltBlue-Team7/IoTProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_data.js
179 lines (149 loc) · 4.83 KB
/
send_data.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
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
var express = require('express')
var app = express()
var fs = require('fs')
// Connect to mysql
var mysql = require('mysql')
var connection = mysql.createConnection({
host : 'localhost',
user : 'sensor2',
password : 'Qw123456!@#$',
database : 'capstone'
});
connection.connect();
//To Write the current time.
var dateTime = require('node-datetime');
//To Get IP addr.
var os = require('os');
var interfaces = os.networkInterfaces();
var addresses = [];
for (var k in interfaces) {
for (var k2 in interfaces[k]) {
var address = interfaces[k][k2];
if (address.family === 'IPv4' && !address.internal) {
addresses.push(address.address);
}
}
}
var x_pos=new Array(50);
var y_pos=new Array(50);
count=0; // the number of input temperature data after start server.
//Work with http call command
//Write the trash record in local txt file and DB.
app.get('/', function(req,res){
//Send the current time and the amount of trash to Client.
if(req.query.temp && typeof req.query.temp!='undefined') {
var dt = dateTime.create();
var formatted = dt.format('Y-m-d H:M:S');
res.send(formatted+' Trash:'+req.query.temp);
console.log(formatted+' Trash:'+req.query.temp);
fs.appendFile('log_trash.txt',req.query.temp+' '+formatted+'\n', function(err){
if(err) throw err;
});
count=count+1;
if(count&1){// 50 = trash bin total number
var n=0;
connection.query('select * from tbl order by time desc limit 50',function(err,rows,fields){
if(err) throw err;
for (var k in rows) {
if (rows[k].amount_trash > 50.0)
console.log('floor: ', rows[k].floor, 'bin number: ', rows[k].amount_trash);
y_pos[n]=rows[k].floor;
x_pos[n]=rows[k].bin_no;
n++;
}
});
}
//Write to local TXT file.
fs.appendFile('log_trash.txt',req.query.temp+' '+formatted+'\n', function(err){
if(err) throw err;
});
data={};
/*data.seq=count++;
data.type='T'; //Means 'Temperature'
data.device='102';
data.unit='0';
data.ip=addresses;
data.value=req.query.temp;
*/
data.time = formatted;
data.amount_trash = req.query.temp;
data.bin_no = Math.floor(Math.random() * 10 + 1);
data.floor = Math.floor(Math.random() * 5 + 1);
//Insert data to DB by query
connection.query('INSERT INTO tbl SET ?',data,function(err,rows,cols){
if(err) throw err;
console.log('Done Insert Query');
});
}
else{
res.send('Unauthorized Access');
}
})
//Work with dump command
//Show recent 1440(one day) trash records.
app.get('/dump',function(req,res){
//Get Recent data from DB by query
connection.query('SELECT * from tbl',function(err,rows,cols){
if(err) throw err;
res.write('<html><head><title>The Amount of Trash in Trash Bin</title></head><body>');
res.write('<p><h1>Measured Amount of Trash @ AS 714, Sogang Univ.</h1></p>');
res.write('<p>Developed By Cobalt Blue Team</p>');
var dt = dateTime.create();
var formatted = dt.format('Y-m-d H:M:S');
res.write('<p>Dump '+rows.length+' data at '+formatted+'</p>');
//Parse data to send Client
//Send text-plain data
/*var dumpdata = '';
for(var i=0; i<rows.length;i++){
var row = rows[i];
dumpdata=dumpdata+i+'\t: Time:'+row.time+'\t Temp:'+row.temperature+'\n';
}
res.set('Content-Type','text/plain');
res.send(dumpdata);
*/
//Send HTML table
res.write('<table border="1">');
res.write('<tr><th>Seq.</th><th>Time</th><th>Trash</th></tr>');
for(var i=0;i<rows.length;i++){
var row=rows[i];
res.write('<tr>');
res.write('<td>'+i+'</td><td>'+row.time+'</td><td>'+row.amount_trash+'</td>');
res.write('</tr>');
}
res.end('</table></body></html>');
console.log('Dump Complete');
});
})
/*app.get('/graph',function(req,res){
console.log('Got app.get(graph)');
var html = fs.readFile('./graph1.html',function(err,html) {
html=" "+html;
console.log('read file');
var qstr = 'SELECT * from table1 ORDER BY time';
connection.query(qstr, function(err, rows, cols){
if(err) throw err;
var data="";
var comma = "";
for (var i=0; i<rows.length;i++){
r = rows[i];
var date=new Date(r.time).toLocaleString().split(' ');
//console.log(date);
var ymd=date[0].split('-');
var hms=date[1].split(':');
data+=comma+"[new Date("+ymd[0]+","+ymd[1]+","+ymd[2]+","+hms[0]+","+hms[1]+","+hms[2]+"),"+r.value + "]";
comma=",";
}
var header = "data.addColumn('date','Date/Time');";
header+="data.addColumn('number','Temp');";
html=html.replace("<%COLUMN%>",header);
html=html.replace("<%DATA%>",data);
res.writeHeader(200,{"Content-Type": "text/html"});
res.write(html);
res.end();
});
});
})*/
app.listen(5000, function(){
//console.log('Temperature Measuring Program listening on port 5000')
console.log('Program that Measures The Amount of Trash listening on port 5000')
})