forked from thkl/homebridge-homematic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomeMaticRegaRequest.js
116 lines (85 loc) · 2.57 KB
/
HomeMaticRegaRequest.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
var http = require("http");
function HomeMaticRegaRequest(log, ccuip) {
this.log = log;
this.ccuIP = ccuip;
this.timeout = 60;
}
HomeMaticRegaRequest.prototype = {
script: function(script, callback) {
var that = this;
var ls = script;
var post_options = {
host: this.ccuIP,
port: "8181",
path: "/tclrega.exe",
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": script.length
},
};
var post_req = http.request(post_options, function(res) {
var data = "";
var that = this;
res.setEncoding("binary");
res.on("data", function(chunk) {
data += chunk.toString();
});
res.on("end", function() {
var pos = data.lastIndexOf("<xml><exec>");
var response = (data.substring(0, pos));
callback(response);
});
});
post_req.on("error", function(e) {
that.log("Error " + e + "while executing rega script " + ls);
callback(undefined);
});
post_req.on("timeout", function(e) {
that.log("timeout while executing rega script");
callback(undefined);
});
post_req.setTimeout(this.timeout * 1000);
post_req.write(script);
post_req.end();
},
getValue: function(channel, datapoint, callback) {
var that = this;
var script = "var d = dom.GetObject(\"" + channel + "." + datapoint + "\");if (d){Write(d.State());}";
this.script(script, function(data) {
if (data !== undefined) {
callback(data);
}
});
},
setValue: function(channel, datapoint, value) {
var script = "var d = dom.GetObject(\"" + channel + "." + datapoint + "\");if (d){d.State(\"" + value + "\");}";
this.script(script, function(data) {
});
},
setVariable: function(channel, value) {
var script = "var d = dom.GetObject(\"" + channel + "\");if (d){d.State(\"" + value + "\");}";
this.script(script, function(data) {
});
},
getVariable: function(channel, callback) {
var that = this;
var script = "var d = dom.GetObject(\"" + channel + "\");if (d){Write(d.State());}";
//this.log("RegaScript %s",script);
this.script(script, function(data) {
if (data !== undefined) {
// that.log.debug("Result is %s",data);
callback(data);
}
});
},
isInt: function(n){
return Number(n) === n && n % 1 === 0;
},
isFloat: function(n){
return n === Number(n) && n % 1 !== 0;
}
};
module.exports = {
HomeMaticRegaRequest: HomeMaticRegaRequest
}