Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
133 lines (122 loc) · 4.21 KB
/
Copy pathserver.js
File metadata and controls
133 lines (122 loc) · 4.21 KB
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
var http = require('http');
var port = 8000;
var sys = require('sys');
var exec = require('child_process').exec;
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 = [];
// repeat 4 times to increase chance that all switches are actually switched
// since most of the RF switches do not support bidirectional communication there are
// not many other possibilities to increase reliability of switch behaviour
var tries = 4;
for (var i = 0; i < tries; i++) {
for (var index in oSwitches.rows) {
// delay
if (oSwitches.rows[index].doc.delay !== undefined && (index > 0 || i > 0)) {
aFinalCommands.push('sleep ' + parseInt(oSwitches.rows[index].doc.delay) / 1000);
}
// off before on
if (oSwitches.rows[index].doc.offBeforeOn && i < tries/2 && sState == 'on') {
aFinalCommands.push(sControlSwitch(oSwitches.rows[index].doc, 'off'));
}
// on before off
if (oSwitches.rows[index].doc.onBeforeOff && i < tries/2 && sState == 'off') {
aFinalCommands.push(sControlSwitch(oSwitches.rows[index].doc, 'on'));
}
// actual action
if (i >= tries/2 || (sState == 'off' && !oSwitches.rows[index].doc.onBeforeOff) || (sState == 'on' && !oSwitches.rows[index].doc.offBeforeOn)) {
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);
});
}