-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
108 lines (102 loc) · 3.08 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
var http = require('http');
var port = 8000;
var sys = require('sys');
var exec = require('child_process').exec;
var child;
var url = require('url');
var ip = '0.0.0.0';
http.createServer(function (req, res) {
if(req.method == 'POST'){
var body = '';
req.on('data',function(data){
console.log(data);
body+=data;
});
req.on('end',function(){
res.writeHead(200,{'Content-Type':'application/json'});
res.end(JSON.stringify(oProcessRequest(JSON.parse(body))));
});
}
else{
res.writeHead(405);
res.end('Only post allowed, not '+req.method);
}
//console.log(req.headers);
//console.log(url.parse(req.url,true));
res.writeHead(200, {'Content-Type': 'application/json'});
//execBackgroundSomethingAndReturn(res,url.parse(req.url,true));
// res.end(JSON.stringify(url.parse(req.url,true)));
}).listen(port, ip);
console.log('Server running at http://'+ip+':'+port+'/');
function oProcessRequest(oRequest){
console.log(oRequest);
vGetRequest(oRequest.ids,oRequest.state);
for(var index in oRequest.ids){
// vGetRequest(oRequest.ids[index],oRequest.state);
}
return {succes:true};
}
function vGetRequest(aKeys,sState){
console.log(sState);
var getRequestOptions = {
hostname:'localhost',
port:5984,
path:'/switchit/_all_docs?include_docs=true',
method:'POST',
headers:{'Content-Type':'application/json'}
}
var req = http.request(getRequestOptions,function(response){
var sBody ='';
response.setEncoding('utf8');
response.on('data',function(data){
sBody += data;
});
response.on('end',function(){
console.log('body');
console.log(sBody);
oSwitches = JSON.parse(sBody);
aFinalCommands = [];
for(var index in oSwitches.rows){
aFinalCommands.push(sControlSwitch(oSwitches.rows[index].doc,sState));
}
vSendCommand(aFinalCommands.join(' && '));
});
});
req.on('error',function(error){
console.log('Problem with getting item: '+error.message);
});
oRequestDocs ={keys:aKeys};
req.write(JSON.stringify(oRequestDocs));
req.end();
}
var oExecutables = {};
oExecutables.action = __dirname+'/lights/action.o';
oExecutables.elro = __dirname+'/lights/elro.o';
oExecutables.kaku = __dirname+'/lights/kaku.o';
oExecutables.blokker = __dirname+'/lights/blokker.o';
function sControlSwitch(oSwitch,sState){
state = sState;
sResult = '';
switch(oSwitch.brand){
case 'action':
sResult = (oExecutables.action+' '+oSwitch.dip+' '+oSwitch.channel+' '+state);
break;
case 'elro':
sResult = (oExecutables.elro+' '+oSwitch.code+' '+oSwitch.socket+' '+state);
break;
case 'kaku':
sResult = (oExecutables.kaku+' '+oSwitch.address+' '+oSwitch.device+' '+state);
break;
case 'blokker':
sResult = (oExecutables.blokker+' '+oSwitch.device+' '+state);
}
return sResult;
}
function vSendCommand(sCommand){
console.log('sending command: '+sCommand);
exec(sCommand,function(error,stdout,stderr){
console.log(error);
console.log(stdout);
console.log(stderr);
});
}