forked from gw0/BajtaHack2017-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
345 lines (289 loc) · 8.9 KB
/
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
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
var util = require("util");
var express = require('express');
var path = require('path');
var bodyParser= require('body-parser');
var multer = require('multer');
var mysql = require('mysql');
var groupBy = require('group-by');
var request = require('request');
var streznik = express();
var storage = multer.diskStorage({
destination: function (zahteva, file, callback) {
callback(null, __dirname+'/public/uploads/')
},
filename: function(zahteva, file, callback) {
callback(null, "room_"+zahteva.body.roomId+".map");
}
});
var upload = multer({ storage: storage });
streznik.use(upload.single('image'));
streznik.use(bodyParser.json());
streznik.use(express.static(path.join(__dirname, 'public')));
var fs = require('fs');
var http = require('http');
var httpServer = http.createServer(streznik);
pool = mysql.createPool({
host: "192.168.0.101",
user: "root",
password: "root",
database: "bajtahack",
charset: "UTF8_GENERAL_CI"
});
httpServer.listen(80, function(){
console.log("Streznik posluša na vratih 80.");
});
streznik.get("/", function(zahteva, odgovor){
console.log("Prejel sem zahtevo na /");
odgovor.sendFile(path.join(__dirname, 'public', 'dashboard.html'));
})
streznik.get("/room:id", function(zahteva, odgovor){
console.log("Prejel sem zahtevo na /room" +zahteva.params.id);
odgovor.sendFile(path.join(__dirname, 'public', 'room.html'));
})
streznik.post("/uploadRoomMap", function(zahteva, odgovor){
var idSobe=zahteva.body.roomId;
var imeSlike=zahteva.file.originalname;
console.log("Dobil sem zahtevek na /addRoomMap roomId=", zahteva.body.roomId," imeSlike=",imeSlike);
odgovor.redirect("/");
})
streznik.post("/addRoom", function(zahteva,odgovor){
pool.getConnection(function(napaka1, connection) {
var name = zahteva.body.roomName;
var roomDescri = zahteva.body.roomDescription;
if (!napaka1) {
console.log(name);
//console.log(roomDescription);
var post = {roomName: name, roomDescription: roomDescri, ipAddress:zahteva.body.ipAddress};
var query = connection.query('INSERT INTO room SET ?', post, function (error, results, fields) {
if (error) throw error;
//sth,
});
connection.release();
} else {
odgovor.json({
uspeh:false,
odgovor:"Napaka pri vzpostavitvi povezave z podatkovno bazo!"
});
}
});
})
streznik.post("/addController", function(zahteva,odgovor){
pool.getConnection(function(napaka1, connection) {
var romID = zahteva.body.roomID;
var ip = zahteva.body.ipAddress;
if (!napaka1) {
console.log(romID);
console.log(ip);
var post = {ipAddress: ip};
var query = connection.query('INSERT INTO controller SET ?', post, function (error, results, fields) {
if (error) throw error;
else{
var conID = results.insertId;
console.log(results)
console.log(conID)
var post1 = {roomID: romID};
var query1 = connection.query('UPDATE room SET controllerID =' +conID +' WHERE roomID = '+ romID, function (error, results, fields) {
if (error) throw error;
});
}
});
connection.release();
} else {
odgovor.json({
uspeh:false,
odgovor:"Napaka pri vzpostavitvi povezave z podatkovno bazo!"
});
}
});
})
streznik.post("/saveConfiguration", function(zahteva,odgovor){
pool.getConnection(function(napaka1, connection) {
var tabela = zahteva.body.lightsConfiguration;
if (!napaka1) {
console.log(zahteva.body.lightsConfiguration[0])
var query = connection.query('DELETE FROM room_lights WHERE roomID ='+zahteva.body.roomID, function (error, results, fields) {
if (error) throw error;
else{
for(var i = 0; i< tabela.length; i++){
var post = {roomID: zahteva.body.roomID, offsetX: tabela[i].offsetX, offsetY: tabela[i].offsetY, gpioPin: tabela[i].gpioPin, lightStatus: tabela[i].status};
var query = connection.query('INSERT INTO room_lights SET ?', post, function (error, results, fields) {
if (error) throw error;
});
}
}
});
connection.release();
} else {
odgovor.json({
uspeh:false,
odgovor:"Napaka pri vzpostavitvi povezave z podatkovno bazo!"
});
}
});
})
streznik.post("/removeRoom", function(zahteva,odgovor){
pool.getConnection(function(napaka1, connection) {
if (!napaka1) {
var query = connection.query('DELETE FROM room WHERE roomID ='+zahteva.body.roomID, function (error, results, fields) {
if (error) throw error;
});
connection.release();
} else {
odgovor.json({
uspeh:false,
odgovor:"Napaka pri vzpostavitvi povezave z podatkovno bazo!"
});
}
});
})
streznik.get("/getRooms", function(zahteva,odgovor){
pool.getConnection(function(napaka1, connection) {
if (!napaka1) {
var query = connection.query('SELECT * FROM room r', function (error, results, fields) {
if (error) throw error;
//console.log(results[0]);
var tabela_data = [];
console.log(results);
if(results.length==0){
console.log("RESULTS JE PRAZNA!!!!!!!!!!!!!!!!!!!!!!")
odgovor.send([]);
} else{
var objekt0 = {
id:results[0].roomID,
name:results[0].roomName,
controller:{ip:results[0].ipAddress, connection:results[0].connected, configuration: results[0].configured},
lights:[]
};
for(var i=1; i<results.length; i++){
if( results[i-1].roomID != results[i].roomID){
tabela_data.push(objekt0);
var objekt1={
id:results[i].roomID,
name:results[i].roomName,
controller:{ip:results[i].ipAddress, connection:results[i].connected, configuration: results[i].configured},
lights:[]
}
objekt0=objekt1;
}
}
tabela_data.push(objekt0);
odgovor.send(tabela_data);
console.log(tabela_data);
}
});
connection.release();
} else {
odgovor.json({
uspeh:false,
odgovor:"Napaka pri vzpostavitvi povezave z podatkovno bazo!"
});
}
});
})
streznik.get("/getRoomLight:id", function(zahteva,odgovor){
tableLights=[];
pool.getConnection(function(napaka1, connection) {
if (!napaka1) {
var query = connection.query('SELECT l.roomID,l.offsetX,l.offsetY,l.gpioPin,l.lightStatus FROM room_lights l WHERE l.roomID='+zahteva.params.id, function (error, results, fields) {
if (error) throw error;
for( var i=0; i<results.length;i++){
var lights={
offsetX:results[i].offsetX,
offsetY:results[i].offsetY,
gpioPin:results[i].gpioPin,
status:results[i].lightStatus,
selected:false
};
tableLights.push(lights);
}
console.log(tableLights);
odgovor.send(tableLights);
});
connection.release();
} else {
odgovor.json({
uspeh:false,
odgovor:"Napaka pri vzpostavitvi povezave z podatkovno bazo!"
});
}
});
})
streznik.post("/updateConfiguration", function(zahteva,odgovor){
var ip=zahteva.body.ip;
var controllerConfiguration=zahteva.body.configuration;
console.log("Posiljam na IP ", ip ," konfiguracija ", controllerConfiguration);
try{
request({
url: "http://"+ip+":40005/updateConf",
json: true,
method: 'POST',
body: controllerConfiguration
}, function(error, response, body) {
//console.log(response);
console.log(body);
//console.log(error)
});
}catch(e){
}
});
/*setInterval(function(){
var options = {
host: '192.168.0.130',
path: '/status',
port: '40005'
};
try{
callback = function(response) {
var str = ''
response.on('data', function (chunk) {
str += chunk;
});
response.on('error', function(error) {
console.log("Ni povezave z ",options.host);
});
response.on('end', function () {
console.log(str);
var conf = 0;
var conn = 0;
var a=JSON.parse(str);
if(a.configured == true) { conf = 1; conn = 1}
pool.getConnection(function(napaka1, connection) {
if (!napaka1) {
var query = connection.query('UPDATE room SET connected = '+conn+' , configured = '+conf+ ' WHERE ipAddress LIKE ?',options.host, function (error, results, fields) {
if (error) throw error;
});
connection.release();
} else {
}
})
});
}
}catch(e){
console.log(e);
}
try{
var req = http.request(options, callback);
req.on('error', function(error) {
console.log("Ni povezave z ",options.host);
});
req.end();
}catch(e){
console.log(e);
}
}, 5000);*/
streznik.post("/getIPAddress", function(zahteva,odgovor){
pool.getConnection(function(napaka1, connection) {
if (!napaka1) {
var query = connection.query('SELECT r.ipAddress FROM room r WHERE r.roomID ='+zahteva.body.roomID, function (error, results, fields) {
if (error) throw error;
odgovor.json({ip:results[0].ipAddress});
});
connection.release();
} else {
odgovor.json({
uspeh:false,
odgovor:"Napaka pri vzpostavitvi povezave z podatkovno bazo!"
});
}
});
});