-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
348 lines (295 loc) · 11.4 KB
/
app.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
346
347
348
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var ip = require('ip');
// custom modules
var net = require('net'); // module voor tcp communicatie
var udp = require('dgram'); //module voor udp communicatie
var request = require('request');//module voor JSON GET/POST naar externe partijen
var HelvarComm = require("./helvar/HelvarCommands.json"); //import alle commands
var HelvarConfig = require("./helvar/HelvarConfig.json"); //import configuratie gegevens
var jsondb= require('node-json-db');
var dbcomm = new jsondb("./data/commands",true,false);
var dbinfo = new jsondb("./data/info",true,false);
var dbworkgroup = new jsondb("./data/workgroup", true, true);
//variables
var debug = HelvarConfig.debug
var HelvarHost = HelvarConfig.HelvarHost;
var HelvarPortTCP = HelvarConfig.HelvarPortTCP;
var HelvarPortUDP = HelvarConfig.HelvarPortUDP;
var HelvarDesignerVersion = HelvarConfig.HelvarDesignerVersion;
var HelvarWebPort = HelvarConfig.HelvarWebPort;
var RestPort = HelvarConfig.RestPort;
var WorkGroup = HelvarConfig.WorkGroup;
var MSGuit = ">V:1,C:11,L:0,G:1,B:1,S:15,F:0#";
var MSGaan = ">V:1,C:11,L:0,G:1,B:1,S:1,F:0#";
var DiscoveredIp = [];
var ipAddress = ip.address()
var groups;
var HelvarPortBroadcast;
var runOnce = 0;
var groupsCount;
//Set UDP Broadcast PORT from designer Version
switch (HelvarDesignerVersion)
{
case "5.3":
HelvarPortBroadcast = 60000;
break;
default:
HelvarPortBroadcast = 4250;
}
//start
var app = express();
var routes= require('./routes/index');
var users = require('./routes/users');
//Debug info
function print(msg){
if (debug=="true"){
console.log(msg);
};
};
//HELVARNET
//Start : Workgroup Discover
function WorkGroupDiscover(){
/*
* Maak een lijst met alle gevonden ipadressen die reageren op multicast
*/
var clientDiscover = new udp.createSocket("udp4");
print("Helvar : UDP Discover Started");
clientDiscover.on('message', function (message, info) {
if (info.address != ipAddress) {
if (HelvarHost == "" || HelvarHost == null) {
print("Helvar : UDP Discover : Found router @ " + info.address);
HelvarHost = info.address.toString("utf8");
} else {
clientDiscover.close();
print("Helvar : UDP Discover Closed");
WorkGroupName()
};
}
});
//used to be clientDiscover.bind(4250,"255.255.255.255") 4250=Designer Port van de routers Designer 4 //Designer 5 => port 60000
clientDiscover.bind(HelvarPortBroadcast);
};
//Get WorkgroupName
function WorkGroupName(){
var clientUDP = new udp.createSocket("udp4");
clientUDP.on('listening', function(){
var listening= clientUDP.address();
WorkGroupRequest();
print("Helvar : UDP Listening on " + listening.address + " : " + listening.port);
});
clientUDP.on('message', function (msg, rinfo) {
print("Helvar : UDP recieved from: " + rinfo.address);
print("Helvar : UDP recieved from: " + rinfo.port);
print("Helvar : UDP recieved from: " + msg);
var msg1 = msg.toString("utf8"); // msg comes in as a buffer
WorkGroup = msg1.slice((msg1.indexOf('=') + 1 ),(msg1.length - 1));
print("Helvar : WorkGroup set : " + WorkGroup);
HelvarTcpConn();
clientUDP.close();
print("Helvar : UDP Connection Closed");
dbinfo.delete('/');
dbworkgroup.delete('/');
dbinfo.push('/workgroup', WorkGroup);
dbinfo.push('/router', HelvarHost);
});
clientUDP.bind(HelvarPortUDP);
};
//send Workgroup Name request
function WorkGroupRequest(){
var cmd=">V:1,C:107#";
var WorkGroupCmd = new Buffer(">V:1,C:107#","ascii");
var UDPsocket = new udp.createSocket("udp4");
UDPsocket.send(WorkGroupCmd, 0, WorkGroupCmd.length,HelvarPortUDP,HelvarHost,function (err,bytes){
//if (err) throw {print(err)};
print("Helvar : UDP Name Command Send");
});
};
WorkGroupDiscover();
//End : Workgroup Discover
//db fill static elements
dbcomm.push('/commands',HelvarComm);
// open tcpssocket to Helvar Router
function HelvarTcpConn(){
var jsonObj; //variabele met json string van listen
global.client = new net.Socket(); // open een nieuwe socket
client.setEncoding('utf8'); // set communicatie taal
client.connect(HelvarPortTCP, HelvarHost, function (err) {
if (err) {
print("tcp error" + err);
} else {
print('Helvar : TCP client connected to router : ' + HelvarHost);
//DO Query Routers
client.write(">V:1,C:108#");
print("Routers request send");
};
});
client.on('data', function (data) {
handlerTCP(data);
});
client.on('close', function () {
print('HELVAR : TCP Client connection closed');
});
};
//handle all incommming tcp data
function handlerTCP(data){
// create json string met alle correcte tekens
data = data.replace('>','{');
data = data.replace('?', '{');
data = data.replace('!', '{');
data = data.replace('V','"V');
data = data.replace(/:/g,'":"'); // /x/g staat voor alle voorkomende keren
data = data.replace(/,/g,'","');
data = data.replace('#','"}');
if (data.indexOf('=')>-1) {
data = data.replace('=','","response":["');
data = data.replace('}',']}');
};
jsonObj = JSON.parse(data);
print("Helvar : MSG recv. :" + data);
//Handle Query Routers
if (jsonObj.C == 108) {
jsonObj.response.forEach(function (V, K) {
value = JSON.stringify(V);
value = value.replace(/"/g, '');
value2 = value.replace('@', '');
value = value.split(".");
value = value[2] + "." + value[3]
dbworkgroup.push('/router/'+ value,
{
"IP": value2,
"ID": K,
"Dali": value,
"Name": "",
"Type": "",
"Subnet": "",
});
//DO Query Router Type
setTimeout(function () { client.write(">V:2,C:104@" + value + "#") }, 250 * K) ;
//DO Query Router Name
setTimeout(function () { client.write(">V:2,C:106@" + value + "#") }, 500 * K) ;
});
//DO Query Groups
client.write(">V:1,C:165#");
};
//Handle Query Router Type
if (data.indexOf('104@') > -1) {
var address = JSON.stringify(jsonObj.C);
address = address.replace(/"/g, '');
address = address.split('@');
if (jsonObj.response == 905) {
dbworkgroup.push('/router/' + address[1] + '/Type', jsonObj.response[0]);
dbworkgroup.push('/router/' + address[1] + '/Subnet', "1");
} else {
dbworkgroup.push('/router/' + address[1] + '/Type', jsonObj.response[0]);
dbworkgroup.push('/router/' + address[1] + '/Subnet', "2");
};
};
//Handle Query Router Name
if (data.indexOf('106@') > -1) {
var address = JSON.stringify(jsonObj.C);
address = address.replace(/"/g, '');
address = address.split('@');
dbworkgroup.push('/router/' + address[1] + '/Name', jsonObj.response[0]);
};
//Handle Query groups
if (jsonObj.C==165){
dbinfo.push('/groups', { "groups": jsonObj.response });
//DO Query Group Names( with a interval of 1000ms each)
jsonObj.response.forEach(function (value, index) { setTimeout(function () { client.write(">V:1,C:105,G:" + value + "#") }, 1000 * index) });
};
//Handle Query Group Names
if (jsonObj.C == 105) {
dbworkgroup.push('/groups/' + jsonObj.G, { "groupnumber": jsonObj.G, "groupname": jsonObj.response });
//DO Query Group devices (run at last response)
if (jsonObj.G == dbinfo.getData('/groups/groups[-1]')) {
var groups = dbinfo.getData('/groups/groups');
print(groups);
//groups.forEach(function (value, index) { setTimeout(function () { client.write(">V:1,C:164,G:" + value + "#"); print(value); }, 1000 * index) });
for (var key in groups) {
print(groups[key]);
};
};
};
//Handle Query Group devices
if (jsonObj.C == 164) {
dbworkgroup.push('/groups/' + jsonobj.G, { "devices": jsonobj.response });
}
//Check if message is from an IBASX group
if (HelvarConfig.Ibasx.indexOf(jsonObj.G) >= 0 && jsonObj.C == 11){
print("is IBASX");
}else{
print("no IBASX");
}
};
//end handle
//REST interface
var HVN = express(); // HVN = HelvarNet REST-API interface socket
var server= HVN.listen(RestPort,function(){print("Helvar : Rest-API Listening on port " + RestPort)});
//make request body json object
HVN.use(bodyParser.urlencoded({
extended: true
}));
HVN.use(bodyParser.json());
HVN.post('/DLG', function (req, res) {
var msg = (">V:2,C:13,G:" + req.body.group + ",L:" + req.body.level + "#");
client.write(msg);
print("Helvar : Rest API command send (DLG) : " + msg);
res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
res.redirect('back');
});
HVN.post('/RSG', function (req, res) {
var msg = (">V:2,C:11,G:" + req.body.group + ",B:" + req.body.block + "S:"+ req.body.scene +"#");
client.write(msg);
print("Helvar : Rest API command send (RSG) : " + msg);
res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
res.redirect('back');
});
HVN.get('/uit', function(req,res){
client.write(MSGuit);
setTimeout(function(){
res.end(JSON.stringify(jsonObj))},1000);
print("klaar uit");
});
HVN.get('/aan', function(req,res){
client.write(MSGaan);
setTimeout(function(){
res.end(JSON.stringify(jsonObj))},1000);
print("klaar aan");
});
//end Helvarnet
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
app.listen(HelvarWebPort, function (){
print("Helvar : Helvar Web Interface available on port " + HelvarWebPort)
});
module.exports = app;