This repository has been archived by the owner on May 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
rega.js
285 lines (245 loc) · 10.2 KB
/
rega.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
/**
* HomeMatic ReGaHss Schnittstelle für Node.js
*
* Version 0.6
*
* Copyright (c) 2013 http://hobbyquaker.github.io
*
* CC BY-NC 3.0
*
* Kommerzielle Nutzung nicht gestattet!
*
*/
var logger = require(__dirname+'/logger.js'),
http = require("http"),
fs = require('fs'),
xml2js = require('xml2js'),
iconv = require('iconv-lite'),
request = require('request'),
extend = require('extend');
var parser = new xml2js.Parser({explicitArray:false});
var rega = function(options) {
this.options = options;
if (options.ccuIp) {
request('http://'+options.ccuIp+'/ise/checkrega.cgi', function (error, response, body) {
if (!error && response.statusCode == 200) {
if (body == "OK") {
options.ready();
} else {
options.ready("ReGaHSS down");
}
} else {
options.ready("CCU unreachable");
}
});
} else {
options.ready("CCU unreachable");
}
};
rega.prototype = {
options: {},
pendingRequests: 0,
regaUp: function (success, error) {
},
checkTime: function (callback) {
this.script('WriteLine(system.Date("%F %X").ToTime().ToInteger());', function (data, err) {
if (!err) {
var ccuTime = parseInt(data, 10);
var localTime = Math.round(new Date().getTime() / 1000);
var diff = localTime - ccuTime;
logger.info("ccu.io time difference local-ccu " + diff.toString() + "s");
}
callback(0, err);
});
},
loadTranslation: function (lang, callback) {
var that = this;
if (!(lang == "de" || lang == "en" || lang == "tr")) {
lang = "de";
}
request.get({ url: 'http://' + that.options.ccuIp + '/webui/js/lang/'+lang+'/translate.lang.js', encoding: null }, function(err, res, body) {
if (res && res.statusCode == 200) {
try {
var HMIdentifier = {}, langJSON = {};
var str = unescape(iconv.decode(body, 'ISO-8859-1'));
var jscode = str.replace(/jQuery\./g, "");
eval(jscode);
logger.verbose(langJSON);
logger.info("ccu.io loaded translate.lang.js");
request.get({ url: 'http://' + that.options.ccuIp + '/webui/js/lang/'+lang+'/translate.lang.stringtable.js', encoding: null }, function(err, res, body) {
if (res.statusCode == 200) {
var str = unescape(iconv.decode(body, 'ISO-8859-1'));
var jscode = str.replace(/jQuery\./g, "");
try {
eval(jscode);
} catch (e) {
callback(langJSON);
}
logger.verbose(langJSON);
logger.info("ccu.io loaded translate.lang.stringtable.js");
} else {
callback(langJSON);
return;
}
request.get({ url: 'http://' + that.options.ccuIp + '/webui/js/lang/'+lang+'/translate.lang.extensionV.js', encoding: null }, function(err, res, body) {
if (res.statusCode == 200) {
var str = unescape(iconv.decode(body, 'ISO-8859-1'));
var jscode = str.replace(/jQuery\./g, "");
try {
eval(jscode);
} catch (e) {
callback(langJSON);
return;
}
logger.verbose(langJSON);
logger.info("ccu.io loaded translate.lang.extensionV.js");
}
callback(langJSON);
});
});
} catch (e) {
logger.error("ccu.io loadTranslation "+e);
callback(null);
}
} else {
callback(null);
}
});
},
loadStringTable: function (language, callback) {
var that = this;
language = language || "de";
that.loadTranslation(language, function (translation) {
request.get({ url: 'http://' + that.options.ccuIp + '/config/stringtable_de.txt', encoding: null }, function(err, res, body) {
var data = body;
var str = iconv.decode(data, 'ISO-8859-1');
var dataArr = str.split("\n");
var lang = {};
for (var i = 0; i < dataArr.length; i++) {
var line = dataArr[i];
if (line && line != "") {
var resultArr = line.match(/^([A-Z0-9_-]+)\|?([A-Z0-9_-]+)?=?([A-Z0-9_-]+)?[ \t]+(.+)$/);
if (resultArr) {
var text = resultArr[4];
if (translation && translation[language]) {
text = translation[language][text.replace(/\${([^}]*)}/,"$1")] || text;
}
if (!lang[resultArr[1]]) {
lang[resultArr[1]] = {};
}
if (resultArr[3]) {
if (!lang[resultArr[1]][resultArr[2]]) {
lang[resultArr[1]][resultArr[2]] = {};
}
if (!lang[resultArr[1]][resultArr[2]][resultArr[3]]) {
lang[resultArr[1]][resultArr[2]][resultArr[3]] = {};
}
lang[resultArr[1]][resultArr[2]][resultArr[3]].text = text;
} else if (resultArr[2]) {
if (!lang[resultArr[1]][resultArr[2]]) {
lang[resultArr[1]][resultArr[2]] = {};
}
lang[resultArr[1]][resultArr[2]].text = text;
} else {
lang[resultArr[1]].text = text;
}
}
}
}
logger.info("ccu.io stringtable loaded");
callback(lang);
});
});
},
addStringVariable: function (name, desc, str, callback) {
var script = "object test = dom.GetObject('"+name+"');\n" +
"if (test) {\n" +
"WriteLine('false');\n" +
"} else {\n" +
"object o = dom.CreateObject(OT_VARDP);\n" +
"o.Name('"+name+"');\n" +
"dom.GetObject(ID_SYSTEM_VARIABLES).Add(o.ID());\n" +
"o.DPInfo('"+desc+"');\n" +
"o.DPArchive(false);\n" +
"o.ValueUnit('');\n" +
"o.ValueType(20);\n" +
"o.ValueSubType(11);\n" +
"o.State('"+str+"');\n" +
"WriteLine(o.ID());\n" +
"}";
this.script(script, function (res) {
var id = parseInt(res, 10);
if (res == "false") {
logger.error("rega addStringVariable("+name+") already exists");
callback(false);
} else if (id > 0) {
callback(id);
} else {
logger.error("rega addStringVariable("+name+") unknown error");
callback(false);
}
});
},
runScriptFile: function (script, callback) {
//logger.verbose('rega --> ' + script + '.fn');
var that = this;
fs.readFile(__dirname+'/regascripts/'+script+'.fn', 'utf8', function (err, data) {
if (err) {
logger.error("rega runScriptFile "+err);
return false;
}
that.script(data, function (stdout, xml) {
callback(stdout, xml);
});
});
},
script: function (script, callback) {
var that = this;
logger.verbose('rega --> ' + script);
var post_options = {
host: this.options.ccuIp,
port: '8181',
path: '/rega.exe',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': script.length
}
};
this.pendingRequests += 1;
var post_req = http.request(post_options, function(res) {
var data = "";
res.setEncoding('utf8');
res.on('data', function (chunk) {
data += chunk.toString();
});
res.on('end', function () {
that.pendingRequests -= 1;
var pos = data.lastIndexOf("<xml>");
var stdout = (data.substring(0, pos));
var xml = (data.substring(pos));
parser.parseString(xml, function (err, result) {
logger.verbose('rega <-- ' + stdout);
if (callback) {
if (result && result.xml) {
callback(stdout, result.xml);
} else {
logger.error('rega invalid response:');
logger.error(JSON.stringify(data));
callback(stdout);
}
}
});
});
});
post_req.on('error', function(e) {
logger.error('rega post request error: ' + e.message);
if (callback) {
callback(null, 'rega post request error: ' + e.message);
}
});
post_req.write(script);
post_req.end();
}
};
module.exports = rega;