-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
336 lines (263 loc) · 9.56 KB
/
index.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*** ZWayCTTAutoTest Z-Way HA module *******************************************
Version: 1.1
(c) Z-Wave.Me, 2022
-----------------------------------------------------------------------------
Author: Yurkin Vitaliy <[email protected]>, Poltorak Serguei <[email protected]>
Implements ZWayCTTAutoTest support
******************************************************************************/
// ----------------------------------------------------------------------------
// --- Class definition, inheritance and setup
// ----------------------------------------------------------------------------
function ZWayCTTAutoTest (id, controller) {
// Call superconstructor first (AutomationModule)
ZWayCTTAutoTest.super_.call(this, id, controller);
this.bufferLen = 0;
this.buffer;
this.buttons = [ "»OK:SHOW«", "»YES-NO:SHOW«", "»OK-CANCEL:SHOW«", "»SKIP:SHOW«", "»SKIP:DISABLE«" ];
this.disableDebug = false;
this.noAction = false;
}
inherits(ZWayCTTAutoTest, AutomationModule);
_module = ZWayCTTAutoTest;
// ----------------------------------------------------------------------------
// --- Module instance initialized
// ----------------------------------------------------------------------------
ZWayCTTAutoTest.prototype.init = function (config) {
ZWayCTTAutoTest.super_.prototype.init.call(this, config);
var self = this;
this.setup();
this.webSocket = new sockets.websocket(9090);
this.webSocketClients = [];
this.webSocket.onconnect = function () {
self.debug("Client connected, sending ping and devices data");
self.webSocketClients.push(this);
}
this.webSocket.onmessage = function(event) {
var data = JSON.parse(decodeURIComponent(escape(event.data)).replace(/\t/g, "\\t"));
self.receive(data);
};
this.webSocket.onclose = function() {
if (this === self.webSocket) {
self.debug("Server websocket closed");
} else {
self.debug("Client disconnected");
}
}
this.webSocket.onerror = function(event) {
self.debug("ERROR: ", event.data);
}
ws.allowExternalAccess("ZWayCTTAutoTestReload", this.controller.auth.ROLE.ADMIN);
global["ZWayCTTAutoTestReload"] = function() {
var _buf = self.buffer;
self.controller.reinitializeModule("ZWayCTTAutoTest", "userModules/");
_.find(self.controller.registerInstances, function(i) { return i.meta.moduleName === "ZWayCTTAutoTest"}).receive({log: _buf.reverse().join("\n")});
return {
status: 200,
body: "Reloaded"
}
}
ws.allowExternalAccess("ZWayCTTAutoTestCheck", this.controller.auth.ROLE.ADMIN);
global["ZWayCTTAutoTestCheck"] = function() {
self.checkQuestions();
return {
status: 200,
body: "Reloaded"
}
}
ws.allowExternalAccess("ZWayCTTAutoTestParseCTTLog", this.controller.auth.ROLE.ADMIN);
global["ZWayCTTAutoTestParseCTTLog"] = function() {
self.parseCTTLog();
return {
status: 200,
body: "Parsed"
}
}
};
ZWayCTTAutoTest.prototype.stop = function () {
ws.revokeExternalAccess("ZWayCTTAutoTestReload");
delete global["ZWayCTTAutoTestReload"];
ws.revokeExternalAccess("ZWayCTTAutoTestCheck");
delete global["ZWayCTTAutoTestCheck"];
ws.revokeExternalAccess("ZWayCTTAutoTestParseCTTLog");
delete global["ZWayCTTAutoTestParseCTTLog"];
this.webSocketClients.forEach(function(cli) {
cli.close();
});
this.webSocket && this.webSocket.close();
ZWayCTTAutoTest.super_.prototype.stop.call(this);
};
// ----------------------------------------------------------------------------
// --- Module methods
// ----------------------------------------------------------------------------
ZWayCTTAutoTest.prototype.debug = function(msg) {
if (!this.disableDebug) {
console.log(this.getName() + ": " + msg);
}
};
ZWayCTTAutoTest.prototype.setup = function () {
var self = this;
executeFile(this.meta.location + "/" + "helpers.js");
executeFile(this.meta.location + "/" + "handlers.js");
ZWayCTTAutoTestHelpers.send = function(ret) {
self.sendButton(ret());
};
this.qa = ZWayCTTAutoTestQA(ZWayCTTAutoTestHelpers);
this.iq = ZWayCTTAutoTestIgnoreQ();
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
// find buffer length based on the longest question
this.qa.forEach(function(test) {
if (self.bufferLen < test.question.length) {
self.bufferLen = test.question.length;
}
test.question_orig = test.question;
test.question = test.question.map(function(line) {
return new RegExp(".*" + escapeRegExp(line).replace(/####/g, "(.*)") + ".*");
});
});
this.iq = this.iq.map(function(q) {
return new RegExp(".*" + escapeRegExp(q).replace(/####/g, "(.*)") + ".*");
});
this.iq.push(/^[ \t]*$/);
this.buffer = Array(this.bufferLen).map(function() { return "" });
};
ZWayCTTAutoTest.prototype.receive = function (message) {
var self = this;
if (message.log) {
var lines = message.log.split("\n");
lines.forEach(function(line) {
line = line.replace(/{color(:[^}]+)?}/g, ''); // remove {color:xxx} and {color}
// check ignore list
self.iq.forEach(function(q) {
if (line.match(q)) {
self.debug("Ignored message: " + line);
line = "";
}
});
if (line.length === 0) return;
self.debug("Received message: " + line);
// roll the buffer
for (var i = self.bufferLen - 1; i > 0; i--) {
self.buffer[i] = self.buffer[i - 1];
}
self.buffer[0] = line;
// match questions
var matched = self.qa.some(function(test) { // some is used to match only the first questions
var params = [];
for (var i = 0; i < test.question.length; i++) {
var m = self.buffer[i].match(test.question[test.question.length - 1 - i]);
//console.log(!!m, test.question[test.question.length - 1 - i].toString());
if (!m) return false;
var _params = [];
for (var k = 1; k < m.length; k++) {
_params.push(m[k]);
}
params = _params.concat(params); // we are stepping from end to start
}
params.unshift("zeroth param"); // params are numbered from 1
console.debug("Matched with params " + JSON.stringify(params));
ZWayCTTAutoTestHelpers.setParams(params);
// matched
if (self.noAction) return true; // don't take any action (for log parsing)
var ret;
if (test.action) {
ret = test.action();
}
if (test.answer) {
var answer = test.answer(ret);
if (answer) {
self.sendButton(answer);
}
}
return true;
});
if (!matched && self.buttons.some(function(button) { return line.match(button); })) {
if (line.match("»SKIP:SHOW«") || line.match("»SKIP:DISABLE«")) return; // skip those buttons
var answer = "undefined";
if (line.match("»OK-CANCEL:SHOW«") || line.match("»OK:SHOW«")) answer = "ok";
else if (line.match("»YES-NO:SHOW«")) answer = "yesNo";
self.buffer.map(function(l) { return self.buttons.some(function(button) { return l.match(button); }); }).slice(0, -1).reverse()
// reverse, remove time, \r and empty lines
var clear = false;
var buf = self.buffer.slice().map(function(l, i) {
if (clear) {
return "";
}
if (i > 0 && self.buttons.some(function(button) { return l.match(button); })) {
// found another button, so clear this and all subsequent lines
clear = true;
return "";
}
return l.replace(/^[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}\w*/, "").replace(/\r$/, "").trim();
}).filter(function(l) { // remove empty items
return l.length
}).reverse(); // reverse to match the question
// print the question
console.log("\n\t\t\t{\n\t\t\t\tquestion: " + JSON.stringify(buf) + ",\n\t\t\t\taction: undefined,\n\t\t\t\tanswer: " + answer + "\n\t\t\t},");
}
});
}
};
ZWayCTTAutoTest.prototype.sendButton = function (button) {
var response = {
button: button
}
var msg = JSON.stringify(response);
this.debug("Sending message: " + button);
this.webSocketClients.forEach(function(cli) {
cli.send(msg);
});
};
ZWayCTTAutoTest.prototype.checkQuestions = function () {
var self = this;
var zats_qa = fs.load(this.meta.location + "/" + "2023-10-30 CTT ZATS Test Interaction List.txt");
var lines = zats_qa.split("\n");
lines.forEach(function(line) {
if (line.match(/^.*\.cs:/)) return; // skip file names
if (line.match(/^\t\* message/)) return;
if (line.match(/^\t\* question/)) return;
if (line.match(/^\t\* resultQuestion/)) return;
if (line.match(/^\t\* userQuestion/)) return;
if (line.match(/^\t\* $/)) return;
if (line.match(/^\t\* "Open Wiki page in web browser ..."$/)) return;
var parts = line.split("\"");
if (parts.length < 3) return;
parts = parts.slice(1, -1);
parts = parts.map(function(l, i) { return i % 2 ? '####' : l; });
line = parts.join("");
// check ignore list
self.iq.forEach(function(q) {
if (line.match(q)) {
//self.debug("Ignored message: " + line);
line = "";
}
});
if (line.length === 0) return;
//self.debug("Checking message: " + line);
// match questions
if (!self.qa.some(function(test) {
var q = test.question_orig.join("\n");
self.buttons.forEach(function(b) {
q = q.replace("\n" + b, "");
});
//console.logJS("q", q);
//console.logJS("l", line);
return q == line;
})) {
self.debug("Questions not handled: " + line);
};
});
};
ZWayCTTAutoTest.prototype.parseCTTLog = function () {
var self = this;
console.log("Parsing file CTTLogs.txt");
var content = fs.loadText(this.meta.location + "/CTTLogs.txt");
this.disableDebug = true;
this.noAction = true;
console.log("Starting");
self.receive({log: content});
console.log("Done");
this.noAction = false;
this.disableDebug = false;
};