-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
159 lines (150 loc) · 4.84 KB
/
main.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
//variablen zur kommunikation
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var os = require('os');
var net = require('net');
var bunyan = require('bunyan');
var fs = require('fs');
//ESP Clients Array
var clients = new Array();
var jsonData;
//ESP MACAdressen which will be excepted on connection
var clientMAC = [];
//read ESP Mac Adress Whitelist from File
readMacWhitelist();
//Logger init
var log = bunyan.createLogger({
name: 'ESPServerLogger',
streams: [{
level: 'debug',
stream: process.stdout
}, {
level: 'info',
path: __dirname + '/log/info.log'
}, {
level: 'error',
path: __dirname + '/log/error.log'
}]
});
//TCP Server erzeugen!
server = net.createServer(function(sck) {
log.info("Client connected to TCP Server");
//on data received
sck.on('data', function(data) {
//data is an string so we convert it to json to use it in the actionMethod
try {
jsonData = undefined;
jsonData = JSON.parse(data);
} catch (e) {
log.debug("No JSON received");
}
log.info(jsonData);
//esp send mac address on connection to be added into esp array, else its an android device
if (jsonData.mac != undefined) {
for (i = 0; i < clientMAC.length - 1; i++) {
if (jsonData.mac == clientMAC[i].mac) {
sck.mac = clientMAC[i].mac;
sck.espid = clientMAC[i].id;
sck.name = clientMAC[i].name;
clients[i] = sck;
log.info("ESP connected.");
return;
}
}
} else {
log.info("Client command recieved via TCP (Android)");
if (jsonData != undefined) {
sendDataToEsp(jsonData);
}
}
});
//on socket disconnect
sck.on("end", function() {
log.info("Client disconnected from TCP Server");
try {
if (sck.mac != undefined) {
clients[clients.indexOf(sck)] = undefined;
} else {
log.debug("Android Device disconnected.")
}
} catch (e) {
log.error(e);
}
});
//on socket error (i.e. socket disconnect without closing)
sck.on('error', function(exc) {
log.error(exc);
try {
if (sck.mac != undefined) {
clients[clients.indexOf(sck)] = undefined;
} else {
log.debug("Android Device disconnected.")
}
} catch (e) {
log.error(e);
}
});
}).listen(8124, function() {
log.info("TCP Server listening on Port 8124");
});
//TCP Server Error
server.on("error", function(err) {
log.error(err);
});
//start HTTP Server
http.listen(62345, function() {
log.info('HTTP Server listening on Port 62345');
});
//on User Connection to HTTP Server send Website
app.get('/', function(req, res) {
app.use(express.static('public'));
res.sendFile(__dirname + '/public/index.html');
});
//connection between server and website (http server)
io.on('connection', function(socket) {
log.info(socket.handshake.address + " connected.");
//on esp start command (button "go" on website)
socket.on('go', function(jsonData) {
log.info("Client command recieved via HTTP");
sendDataToEsp(jsonData);
});
//on socket disconnect (http)
socket.on("disconnect", function(data) {
log.info("User disconnected from HTTP (Website)");
});
//on socket error (http)
socket.on("error", function(err) {
log.error(err);
});
});
//sends json Object with command for ESP tp ESP
function sendDataToEsp(jsonData) {
log.debug(jsonData);
//log ESP Mode to start
log.info("ESP: " + jsonData.Id + "\tColor: " + " R: " + jsonData.R + " G: " + jsonData.G + " B: " + jsonData.B + "\tMode: " + jsonData.Mode);
//Send to ESP
//only if esp is connected tell esp what to do
if (clients[jsonData.Id])
clients[jsonData.Id].write(JSON.stringify(jsonData));
else
log.error("ESP (ID: " + jsonData.Id + ") not connected");
}
function readMacWhitelist() {
fs.readFile(__dirname + '/espwhitelist.csv', (err, data) => {
if (!err) {
spData = data.toString().split('\n');
for (i = 0; i < spData.length - 1; i++) {
cD = spData[i].split(',');
clientMAC.push({
id: cD[0],
name: cD[1],
mac: cD[2]
});
}
} else {
log.error(err);
}
});
}