-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsocket.js
44 lines (42 loc) · 1.06 KB
/
socket.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
var http = require('http');
var net = require('net');
socket = net.connect('/var/run/lirc/lircd',connected);
socket.setEncoding('utf8');
serverPort = 8000;
serverIp = '127.0.0.1';
lastSend = null;
idList = ["Schakelaar C"];
function connected(){
socket.on('data',function(data){
if(lastSend == null || ((new Date()).getTime() - lastSend.getTime()) > 1200){
if(data.indexOf('KEY_POWER2') > -1){
data = {ids:idList,state:"on"};
console.log('on');
postRequest(data);
}
else{
data = {ids:idList,state:"off"};
postRequest(data);
console.log('off');}
}
lastSend = new Date();
});
console.log("Now listening...");
};
function postRequest(a_oData){
var postParams = {
hostname:serverIp,
port:serverPort,
method:'POST',
headers:{'Content-Type':'application/json'}
}
var req = http.request(postParams,function(response){
var sBody = '';
response.setEncoding('utf8');
response.on('data',function(data){sBody += data;});
response.on('end',function(){console.log(sBody);});
});
console.log(a_oData);
req.write(JSON.stringify(a_oData));
req.end();
}