-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
1106 lines (1035 loc) · 32.7 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Author: Nikolaev Dmitry (VI:RUS)
Licence: MIT
Version: 0.6.1
Date: 29.07.2020
Description: https://wiki.yaboard.com/s/oi
Source: https://github.com/subnetsRU/alice-command-skill
Yaboard: https://yaboard.com/task/5da05e4b75e2e73e5c847c84
*/
//TODO: Паузы между сценариями
const fs = require('fs');
const https = require('https');
const express = require('express'); //https://expressjs.com/ru/4x/api.html
const { includes, lowerCase, keys } = require('lodash');
const sprintf = require("sprintf-js").sprintf; //https://www.npmjs.com/package/sprintf-js
const { Console } = require('console');
const path = require('path');
const util = require('util');
const exit = process.exit;
const argv = process.argv.slice(2);
const readline = require("readline");
process.title = 'alice-command-skill';
const scriptStartTime = new Date();
var config = require("./config.js");
config.csrf = '';
config.port = config.port || 8443;
config.log.type = config.log.type || 'cli';
config.log.logfile = '';
config.login = config.login || '';
config.pass = config.pass || '';
config.cookie = config.cookie || '';
if (config.log.type == 'all' || config.log.type == 'log'){
config.log.folder = __dirname;
config.log.log_file_name = config.log.log_file_name || "acs.log";
config.log.logfile = config.log.folder + '/' + config.log.log_file_name;
}
var last_report = {};
var timers = refreshTimers();
var timers_active = {};
function refreshTimers(){
var timers = [];
if (fs.existsSync('./timers.json')) {
var tmp = fs.readFileSync('./timers.json').toString();
try {
tmp = json = JSON.parse(tmp);
if (Array.isArray(tmp)){
timers = tmp;
}
}
catch(error){
console.error('refreshTimers failed:',error);
}
}
return timers;
}
var timer_interval = (((typeof config.timer_period != "undefined" && parseInt(config.timer_period) > 0) ? (config.timer_period * 60) : (5*60)) * 1000);
var app = express();
const serverOptions = {
key: fs.readFileSync(config.ssl.key),
cert: fs.readFileSync(config.ssl.crt),
requestCert: false,
};
function get_cookie(){
let promise = new Promise((resolve, reject) => {
config.cookie = '';
request = 'login=' + config.login + '&passwd=' + config.pass;
let options = {
//https://passport.yandex.ru/passport?mode=auth&retpath=https://ya.ru
protocol: 'https:',
hostname: 'passport.yandex.ru',
path: '/passport?mode=auth&retpath=https://ya.ru',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
timeout: 2000,
agent: false
}
var req = https.request(options, (res) => {
console.debug(sprintf('get_cookie response code [%s] message [%s]', res.statusCode,res.statusMessage));
if (res.statusCode == 302){
cookies = '';
if (typeof res.headers['set-cookie'] != "undefined"){
//console.debug('get_cookie response headers:',res.headers['set-cookie']);
res.headers['set-cookie'].forEach(function(item, i, arr){
tmp_cookie = item.substring(0, item.indexOf('; ')) + ";";
cookies += tmp_cookie;
});
}
if (cookies){
config.cookie = cookies.trim();
console.info('get cookies success');
}else{
console.error('get cookies failed');
}
}else{
console.debug('get_cookie response headers:',res.headers);
reject('get_cookie response code #' + res.statusCode);
}
});
req.on('error', (error) => {
console.error(sprintf('get_cookie request failed [%s]',error));
reject(error);
});
req.on('timeout',(data) => {
console.error(sprintf('get_cookie timeout, aborting request'));
req.abort();
reject('get_cookie timeout');
});
req.on('close',() => {
console.debug(sprintf('get_cookie request closed'));
if (config.cookie){
resolve(config.cookie);
}
});
req.write(request);
req.end();
});
return promise;
}
function get_csrf(){
let promise = new Promise((resolve, reject) => {
csrf = '';
request = '';
let options = {
//https://yandex.ru/quasar/iot
protocol: 'https:',
hostname: 'yandex.ru',
path: '/quasar/iot',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie' : config.cookie,
},
timeout: 2000,
agent: false
}
//console.log('step3 opt',options);
var req = https.request(options, (res) => {
console.debug(sprintf('get_csrf response code [%s] message [%s]', res.statusCode,res.statusMessage));
if (res.statusCode != 200){
console.debug('get_csrf response headers:',res.headers);
if (config.login && config.pass){
console.info('Clean cookie for refresh next time');
config.cookie = '';
}
reject('get_csrf response code #' + res.statusCode);
}
res.on('data', (data) => {
data = data.toString();
if (!csrf){
//console.log('data',data);
let re = /csrfToken2\":\"(\S+:\d+)\",\"/; //"
let matches = data.match(re);
//console.log('matches',matches);
if (matches != null && typeof matches[1] != 'undefined'){
csrf = matches[1];
console.info('get csrf success');
}
}
});
});
req.on('error', (error) => {
console.error(sprintf('get_csrf request failed [%s]',error));
reject(error);
});
req.on('timeout',(data) => {
console.error('get_csrf timeout, aborting request');
req.abort();
reject('get_csrf timeout');
});
req.on('close',() => {
console.debug('get_csrf request closed');
if (csrf){
config.csrf = csrf;
resolve(config.csrf);
}else{
console.debug('csrf is empty');
reject('csrf is empty');
}
});
req.write(request);
req.end();
});
return promise;
}
function alice_run(scenario){
let promise = new Promise((resolve, reject) => {
if (typeof scenario == 'string'){
var tmp = scenario;
scenario = { id: tmp };
delete tmp;
}
if (typeof scenario.name == "undefined"){
scenario.name = 'unknown';
}
if (typeof scenario.id != "undefined"){
request = '';
let options = {
//https://iot.quasar.yandex.ru/m/user/scenarios/ + scenario_id + /actions
protocol: 'https:',
hostname: 'iot.quasar.yandex.ru',
path: '/m/user/scenarios/' + scenario.id + '/actions',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'x-csrf-token' : config.csrf,
'Cookie' : config.cookie,
},
timeout: 2000,
agent: false
}
//console.log('alice_run opt',options);
var req = https.request(options, (res) => {
console.debug(sprintf('alice_run response code [%s] message [%s]', res.statusCode,res.statusMessage));
last_report = { name: scenario.name, date: parseInt(new Date().getTime()/1000), status: res.statusCode };
console.debug('last_report',last_report);
if (res.statusCode == 200){
console.debug("Execute command: run scenario: ok");
resolve(res.statusCode);
}else{
console.debug('response headers:',res.headers);
if (res.statusCode == 403){
if (config.login && config.pass){
console.info('Clean cookie for refresh next time');
config.cookie = '';
}
}
reject(res.statusCode);
}
});
req.on('error', (error) => {
console.error(sprintf('alice_run request failed [%s]',error));
last_report = { name: scenario.name, date: parseInt(new Date().getTime()/1000), status: 'ошибка' };
console.debug('last_report',last_report);
reject("alice_run ошибка");
});
req.on('timeout',(data) => {
console.error(sprintf('timeout, aborting request'));
last_report = { name: scenario.name, date: parseInt(new Date().getTime()/1000), status: 'таймаут' };
console.debug('last_report',last_report);
req.abort();
reject('alice_run таймаут');
});
req.on('close',() => {
console.debug(sprintf('alice_run request closed'));
});
req.write(request);
req.end();
}else{
last_report = { name: scenario.name, date: parseInt(new Date().getTime()/1000), status: 'неизвестен' };
console.debug('last_report',last_report);
reject('id сценария неизвестен.');
}
});
return promise;
}
async function make_response(json,resp){
if (json){
let utterance = lowerCase(json.request.original_utterance);
if (!utterance){
utterance = "помощь";
}
try{
res = await wys(utterance);
//console.log('await wys fin',res);
if (typeof res.text != "undefined" && res.text){
resp.response.text = res.text;
}
if (typeof res.tts != "undefined" && res.tts){
resp.response.tts = res.tts;
}else{
resp.response.tts = resp.response.text;
}
if (typeof res.end_session != "undefined"){
resp.response.end_session = res.end_session;
}
}
catch (error){
console.error('words parser failed:',error);
}
if (typeof res != "undefined" && typeof res.cmd != "undefined"){
if (config.login && config.pass && !config.cookie){
let gc = await get_cookie();
}
try{
let csrf = await get_csrf();
}
catch(e){
console.error('csrf error:',e);
if (config.login && config.pass){
config.cookie = '';
}
}
if (config.csrf){
for(i = 0; i < res.cmd.length; i++){
let run = await alice_run(res.cmd[i]);
console.info('alice_run res:',run);
}
}else{
throw new Error('No csrf');
}
}
}
return resp;
}
async function wys(str){
let promise = new Promise((resolve, reject) => {
ret = {};
text = [];
search = {
enable: [],
disable: [],
intents: {},
};
var end = false;
var err = [];
var weekDays = {
'1': ['понедельник','понедельникам'],
'2': ['вторник','вторникам'],
'3': ['среда','среду','средам'],
'4': ['четверг','четвергам'],
'5': ['пятница','пятницу','пятницам'],
'6': ['суббота','субботу','субботам'],
'0': ['воскресенье','воскресеньям'],
};
if (str === 'ping') {
text.push('pong');
end = true;
}
if (end == false){
for(let intent of config.intents.help){
if (includes(str, intent)) {
text.push("Я могу выполнять несколько сценариев за одну команду. Например:");
text.push("Выключи люстру и включи лампу и телевизор.");
text.push("А так же могу выполнять сценарии по таймеру. Например:");
text.push("Добавь таймер выключи телевизор через 15 минут.");
text.push("Добавь таймер включи лампу в 21 час.");
text.push("Добавь таймер выключи люстру по будням в 21 час 15 минут.");
text.push("Подробнее в документации на wiki.yaboard.com");
text.push("Так же вы можете узнать стату последней операции по команде \"отчёт\".");
end = true;
}
}
}
if (end == false){
getIntents = keys(config.intents);
for (let i of getIntents){
//console.debug('getIntents',i);
if (typeof config.intents[i] == "object"){
//for (let item of config.intents[i]) {
for (ik = 0; ik < config.intents[i].length; ik++){
let item = config.intents[i][ik];
//console.debug('\t'+ ik + ': ' + item + ' => ' +includes(str, item));
if (includes(str, item)) {
if (typeof search.intents[i] == "undefined"){
search.intents[i] = 0;
}
search.intents[i]++;
}
}
}
}
//console.debug(search);
}
if (typeof config.scenarios == "undefined"){
text.push('Сценарии отсутствуют. Проверьте конфигурационный файл навыка.');
end = true;
}
if (end == false){
if (typeof search.intents['report'] != "undefined"){
if(typeof last_report.name != "undefined"){
let date = '';
if(typeof last_report.date != "undefined"){
let time = new Date((last_report.date * 1000));
date = sprintf("%s числа в %02d:%02d",time.getDate(),time.getHours(),time.getMinutes());
}
text.push(sprintf('Сценарий "%s" статус %s%s.' ,last_report.name,last_report.status,(date ? ' от ' + date : '')));
}else{
text.push('Отчёт отсутствует.');
}
}
}
if (end == false && (/\sчерез\s/).test(str)){
if ( (typeof search.intents['timers'] == "undefined") && (typeof search.intents['enable'] != "undefined" || typeof search.intents['disable'] != "undefined") ){
str = 'добавить таймер ' + str;
search.intents['timers'] = true;
}
}
if (end == false && (typeof search.intents['timers'] != "undefined")){
let regexp = '(какие\\s(есть|установлены|добавлены|созданы)|список)\\s' + config.intents.timers.join('|') + '[а-я]{1,2}?';
let re = new RegExp(regexp,'u');
let matches = str.match(re);
if (matches !== null){
if (timers.length > 0){
text.push("Всего таймеров: " + timers.length + '.');
timers.forEach(function(timer, i, arr){
let tmp = [];
console.log(timer);
tmp.push((i+1) + '. ' + timer.name);
tmp.push(config.intents[timer.action][0]);
let when = [];
if (typeof timer.days != "undefined"){
if (timer.days.length == 7){
when.push('ежедневно');
}else if(timer.days.sort().toString() == [6,0].sort().toString()){
when.push('по выходным');
}else if(timer.days.sort().toString() == [1,2,3,4,5].sort().toString()){
when.push('по будням');
}else{
for(let day of timer.days){
if(typeof weekDays[day] !== "undefined"){
when.push(weekDays[day][0]);
}
}
}
}
if (typeof timer.once != "undefined"){
let time = new Date((timer.once * 1000));
when.push('один раз');
timer.hour = time.getHours();
timer.min = time.getMinutes();
}
if (typeof timer.hour != "undefined"){
let tmp = 'час';
if (timer.hour == 0){
tmp = 'часов';
}else if ((timer.hour >= 2 && timer.hour < 5) || (timer.hour >= 22 && timer.hour < 25)){
tmp = 'часа';
}else if (timer.hour >= 5 && timer.hour < 21){
tmp = 'часов';
}
when.push('в ' + timer.hour + ' ' + tmp);
}
if (typeof timer.min != "undefined"){
when.push(timer.min + ' минут');
}
tmp.push(when.join(' '));
text.push(tmp.join(', ') + '.');
});
}else{
text.push("Таймеры отсутствуют.");
}
end = true;
}
}
if (end == false && (typeof search.intents['timers'] != "undefined")){
var actions = config.intents.action_add.concat(config.intents.action_del);
var en_dis = config.intents.enable.concat(config.intents.disable);
var action = false;
var saction = false;
var scenario = false;
var twhen = false;
var eSC = [];
var dSC = [];
for(let intent in config.scenarios.enable){
eSC.push(intent);
}
for(let intent in config.scenarios.disable){
dSC.push(intent);
}
let regexp = '^(' + actions.join('|') +')\\s(' + config.intents.timers.join('|') + ')\\s(' + en_dis.join('|') +')\\s(' + eSC.join('|') + ')\\s(.*)$';
let re = new RegExp(regexp,'u');
let matches = str.match(re);
if (matches !== null){
if (typeof matches[1] != "undefined"){
for(let intent of config.intents.action_add){
if(intent == matches[1]){
action = 'add';
}
}
for(let intent of config.intents.action_del){
if(intent == matches[1]){
action = 'delete';
}
}
}
if (typeof matches[3] != "undefined"){
for(let intent of config.intents.enable){
if(intent == matches[3]){
saction = 'enable';
}
}
for(let intent of config.intents.disable){
if(intent == matches[3]){
saction = 'disable';
}
}
}
if (typeof matches[4] != "undefined" && saction !== false){
if (typeof config.scenarios[saction] !== "undefined" && typeof config.scenarios[saction][matches[4]] != "undefined"){
scenario = {name: matches[4], id: config.scenarios[saction][matches[4]]};
}
}
if (typeof matches[5] != "undefined"){
if ((/(будням|будни|выходным|выходные|часов|часа|час|минут|минуты|утра|вечера)/).test(matches[5])){
twhen = matches[5];
}
}
}
if (scenario === false){
err.push('Название сценария не найдено.');
}else if (action === false){
err.push('Отсутствует действие с таймером.');
}else if (saction === false){
err.push('Отсутствует действие со сценарием.');
}else if (twhen === false){
err.push('Отсутствует указание времени.');
}
if (err.length == 0){
tcfg = {name: scenario.name, action: saction, id: scenario.id};
if ((/(будням|будни|выходным|выходные)/).test(twhen)){
if ((/(будням|будни)/).test(twhen)){
tcfg.days = [1,2,3,4,5];
}else if ((/(выходным|выходные)/).test(twhen)){
tcfg.days = [6,0];
}
}else{
let tmp = [];
for (const [key, value] of Object.entries(weekDays)) {
let regexp = '(' + value.join('|') + ')';
let re = new RegExp(regexp,'u');
let matches = twhen.match(re);
if (matches !== null){
tmp.push(parseInt(key));
}
}
if (tmp.length > 0){
tcfg.days = tmp;
}
}
if (typeof tcfg.days == "undefined"){
tcfg.days = [0,1,2,3,4,5,6];
}
regexp ='(в|с|через)\\s(\\d+)\\s(часов|часа|час|утра|вечера)';
re = new RegExp(regexp,'u');
matches = twhen.match(re);
if (matches != null){
if (typeof matches[2] != "undefined"){
tcfg.hour = parseInt(matches[2]);
if ((/вечера/).test(twhen)){
if (tcfg.hour < 12){
tcfg.hour = (tcfg.hour + 12);
}
}
}
}else{
if ((/в\sчас\sночи/).test(twhen)){
tcfg.hour = 1;
}
}
if (typeof tcfg.hour != "undefined" && tcfg.hour > 23){
err.push('Часы не могут быть больше 23.');
}
regexp = '\\s(\\d+)\\s(минут|минуты)';
re = new RegExp(regexp,'u');
matches = twhen.match(re);
if (matches != null){
if (typeof matches[1] != "undefined"){
tcfg.min = parseInt(matches[1]);
}
}
if (typeof tcfg.min != "undefined" && tcfg.min > 59){
err.push('Минуты не должны превышать 59.');
}
if ((/через/).test(twhen)){
tcfg.once = parseInt(new Date().getTime()/1000) + ((typeof tcfg.hour != "undefined") ? (tcfg.hour * 60 * 60) : 0) + ((typeof tcfg.min != "undefined") ? (tcfg.min * 60) : 0);
delete tcfg.days;
}
}
if (err.length == 0){
if (action == 'add'){
timers.push(tcfg);
text.push("Таймер добавлен.");
}else if(action == 'delete'){
let del = 0;
timers.forEach(function(timer, i, arr) {
if ( (typeof timer.action !== "undefined" && timer.action == tcfg.action) && (typeof timer.name !="undefined" && timer.name == tcfg.name) ){
if ( (typeof timer.days !="undefined" && timer.days.length == tcfg.days.length) && (typeof timer.hour != "undefined" && timer.hour == tcfg.hour) ){
timers.splice(i, 1);
del++;
}
}
});
if (del > 0){
text.push("Таймер удален.");
}else{
err.push("Таймер для удаления не найден.");
}
}
}
if (err.length == 0){
fs.writeFileSync('./timers.json',JSON.stringify(timers));
if (typeof tcfg != "undefined"){
console.debug('timer ' + action,tcfg);
}
}
if (err.length > 0){
text.push('Ошибки: ' + err.join(' '));
}
end = true;
}
if (end == false){
and = str.split(/\sи\s/ig);
//console.log('and',and);
let last = '';
for(let phrase of and){
//console.log(phrase);
let split = phrase.split(' ');
//console.log('split',split);
let f = false;
for(let intent of config.intents.enable){
let pos = split.indexOf(intent);
if(pos > -1){
last = 'enable';
delete split[pos];
search.enable.push(split.join(' ').trim());
f = true;
}
}
for(let intent of config.intents.disable){
let pos = split.indexOf(intent);
if(pos > -1){
last = 'disable';
delete split[pos];
search.disable.push(split.join(' ').trim());
f = true;
}
}
if (f === false && last){
search[last].push(split.join(' ').trim());
}
}
if (search.enable.length > 0){
let devices = [];
let no_devices = [];
for(let dev of search.enable){
//console.log('typeof ' + dev ,typeof config.scenarios.enable[dev],config.scenarios.enable[dev]);
if (typeof config.scenarios.enable[dev] != 'undefined' && config.scenarios.enable[dev].length > 0){
if (typeof ret.cmd == "undefined"){
ret.cmd = [];
}
ret.cmd.push({name: dev, id: config.scenarios.enable[dev]});
devices.push(dev);
}else{
no_devices.push(dev);
}
}
if (devices.length > 0){
text.push('Включаю');
text.push(devices.join(' и ') + '.');
}
if (no_devices.length > 0){
text.push('Не могу включить: '+ no_devices.join(', ') + '.');
}
}
if (search.disable.length > 0){
let devices = [];
let no_devices = [];
for(let dev of search.disable){
//console.log('typeof ' + dev ,typeof config.scenarios.disable[dev],config.scenarios.disable[dev]);
if (typeof config.scenarios.disable[dev] != 'undefined' && config.scenarios.disable[dev].length > 0){
if (typeof ret.cmd == "undefined"){
ret.cmd = [];
}
ret.cmd.push({name: dev, id: config.scenarios.disable[dev]});
devices.push(dev);
}else{
no_devices.push(dev);
}
}
if (devices.length > 0){
text.push('Выключаю');
text.push(devices.join(' и ') + '.');
}
if (no_devices.length > 0){
text.push('Не могу выключить: '+ no_devices.join(', ') + '.');
}
}
}
if (text.length == 0){
ret.text = "Я вас не поняла, повторите пожалуйста.";
ret.end_session = false;
}else{
ret.text = text.join(' ');
}
resolve(ret);
});
return promise;
}
async function proc_timers(){
//console.log('timers',timers);
var time = new Date();
var now = (time.getTime()/1000);
var day = time.getDay();
var start_time = false;
var end_time = false;
var res = false;
var cmds = [];
timers = refreshTimers();
timers.forEach(function(timer, i, arr){
res = false;
let key = JSON.stringify(timer);
if (typeof timer.name != "undefined"){
if (typeof timer.id == "undefined"){
res = 'Не указан ID сценария.';
}
if (typeof timer.once == "undefined"){
if (typeof timer.days == "undefined"){
res = 'Не указаны дни.';
}
if (typeof timer.hour == "undefined"){
res = 'Не указан час.';
}
start_time = false;
if (res == false){
for (let d of timer.days){
if (day == d){
start_time = new Date();
break;
}
}
if (start_time !== false){
start_time.setHours(timer.hour);
if (typeof timer.min != "undefined"){
start_time.setMinutes(timer.min);
}else{
start_time.setMinutes(0);
}
start_time.setSeconds(0,0);
end_time = parseInt(start_time.getTime()/1000) + ((timer_interval/1000) * 2);
start_time = parseInt(start_time.getTime()/1000);
start_time = start_time - (scriptStartTime.getSeconds() + 1);
}
}
}else{
start_time = new Date((timer.once * 1000));
start_time.setSeconds(0,0);
end_time = parseInt(start_time.getTime()/1000) + ((timer_interval/1000) * 2);
start_time = parseInt(start_time.getTime()/1000);
start_time = start_time - (scriptStartTime.getSeconds() + 1);
}
if (start_time !== false && (start_time <= now && now <= end_time)){
if (typeof timers_active[key] != "undefined"){
res = 'Был запущен';
}else{
res = 'запускаю';
cmds.push(timer);
if (typeof timer.once == "undefined"){
timers_active[key] = true;
}else{
timers.splice(i, 1);
fs.writeFileSync('./timers.json',JSON.stringify(timers));
}
}
}else{
delete timers_active[key];
}
var ctype = 'debug';
if (res != false){
ctype = 'info';
}
console[ctype](sprintf('Process timer [%s: %s] [%s - %s] => result [%s]',timer.action,timer.name,from_unixtime(start_time),from_unixtime(end_time),(res === false) ? 'not now' : res));
}
});
//console.debug('timers_active',timers_active);
if (cmds.length > 0){
console.debug('run [' + cmds.length + '] commands',cmds);
if (config.login && config.pass && !config.cookie){
let gc = await get_cookie();
}
try{
let csrf = await get_csrf();
}
catch(e){
console.error('csrf error:',e);
if (config.login && config.pass){
config.cookie = '';
}
}
if (config.csrf){
for (let command of cmds){
let run = await alice_run(command);
console.info('exec [' + command.name + '] result [' + run + ']');
}
}else{
throw new Error('No csrf');
}
}
}
var redirectConsole = function(path){
/*
* @param (string) path to log file
*/
var lpath = (typeof path == "undefined") ? "/dev/null" : path;
this.data = [];
this.origConsole = new console.Console(process.stdout,process.stderr);
var self = this;
this.main = function(type,arg){
var datetime = new Date();
var time = sprintf("%02d.%02d.%04d %02d:%02d:%02d",datetime.getDate(),(datetime.getMonth() + 1),datetime.getFullYear(),datetime.getHours(),datetime.getMinutes(),datetime.getSeconds());
if (type){
arg.unshift('['+type+']');
}
arg.unshift('['+time+']');
self.data.push(util.format.apply(null, arg) + '\n');
if (config.log.type == 'cli' || config.log.type == 'all'){
if (!type){
type = 'log';
}
let tmp = [];
for(let d of arg){
if(typeof d == "string"){
tmp.push(d);
}else{
tmp.push(sprintf("%j",d));
}
}
//self.origConsole[type.toLowerCase()](arg);
self.origConsole[type.toLowerCase()](tmp.join(' '));
}
}
this.debug = function(){
self.main('DEBUG',[].slice.call(arguments));
}
this.log = function(){
self.main(null,[].slice.call(arguments));
}
this.info = function(){
self.main('INFO',[].slice.call(arguments));
}
this.error = function(){
self.main('ERROR',[].slice.call(arguments));
}
this.warn = function(){
self.main('WARN',[].slice.call(arguments));
}
this.clear = function(){
self.data = [];
}
this.len = function(){
return self.data.length;
}
this.save = function(){
if (config.log.type == 'log' || config.log.type == 'all'){
let len = self.data.length;
for(i=0; i<len; i++){
fs.appendFileSync(lpath,self.data.shift());
}
}
}
setInterval(this.save, 500);
}
var base64 = function(){
var _encode = function _encode(buffer) {
return buffer.toString('base64')
.replace(/\+/g, '-') // Convert '+' to '-'
.replace(/\//g, '_') // Convert '/' to '_'
.replace(/=+$/, ''); // Remove ending '='
};
var _decode = function _decode(base64) {
// Add removed at end '='
base64 += Array(5 - base64.length % 4).join('=');
base64 = base64
.replace(/\-/g, '+') // Convert '-' to '+'
.replace(/\_/g, '/'); // Convert '_' to '/'
return new Buffer.from(base64, 'base64');
};
return {
encode: function encode(text){
var buffer = Buffer.from(text);
return _encode(buffer);
},
decode: function decode(base64){
return _decode(base64).toString('utf8');
},
};
}
var from_unixtime = function(date){
if (date > 0){
var u = new Date(date * 1000) || 0;
return sprintf("%02s.%02s.%04s %02s:%02s:%02s",u.getDate(),u.getMonth() + 1,u.getFullYear(),u.getHours(),u.getMinutes(),u.getSeconds());
}else{
return false;
}
}
app.get('/', function (req, res) {
res.sendStatus(400);
});
app.post('/', function (req, res) {
var bodyStr = '';
req.on("data",function(chunk){
bodyStr += chunk.toString();
});
req.on("end",function(){
//console.debug('Got body:', bodyStr);
async function reply(req,res,body){
//Default responce
var resp = {
version: '1.0',
response: {
text: "Что-то у меня пошло не так...",
tts: "Что-то у меня пошло не так.",
end_session: true,
},
};
var session_id = 'unknown';
var json = false;
var auth = false;
try {
json = JSON.parse(body);
resp.version = json.version;
console.debug('incoming JSON:',json);
if(typeof json.session != "undefined" && typeof json.session.session_id != "undefined"){
session_id = json.session.session_id;
}
if (json.request.command == "ping"){
resp.response.text = 'pong';
delete resp.response.tts;
json = false;
}
}
catch (error) {
console.error('json parse failed',error);
}
if (json !== false){
if (typeof json.session.user != "undefined" && typeof json.session.user.user_id != "undefined"){
for(let uid of config.auth.user_id){
if (uid === json.session.user.user_id){
console.debug('Authentication by user.user_id');
auth = true;
break;
}
}
}
if (auth == false){