NEWS
Как отловить результат выполнения exec в родительской функции?
-
В скрипте 2 проблемы
- Как цикл for выполнять с учётом завершения функций vol_up() или vol_down() внутри цикла.
Метод "result = vol_up()" не помогает.
setTimeout (vol_up(), 1000) вместо vol_up также не помогает- задержки между выполнением нет.
- Как функцию vol_up() завершиться только после выполнения внутри setState ?
function vol_up() { var cmd = '{ echo "VU"; sleep 1;} | telnet 192.168.1.81 8102'; exec(cmd, function callback(error, stdout, stderr){ var str = stdout; var re = /VOL/; var tagList = str.split(re); curvol=tagList[1]; var a = parseInt(curvol)/2; console.log('vol_up str='+str); console.log('vol_up a='+a); setState("javascript.0.current_volume",a); //Завершит vol_up() только после присвоения? }); } function vol_down() { var cmd = '{ echo "VD"; sleep 1;} | telnet 192.168.1.81 8102'; exec(cmd, function callback(error, stdout, stderr){ var str = stdout; var re = /VOL/; var tagList = str.split(re); curvol=tagList[1]; var a = parseInt(curvol)/2; console.log('vol_down str='+str); console.log('vol_down a='+a); setState("javascript.0.current_volume",a); }); } function set_volume(vol){ target_vol=vol; cmd = '{ echo "?V"; sleep 1;} | telnet 192.168.1.81 8102'; exec(cmd, function callback(error, stdout, stderr){ var str = stdout; var re = /VOL/; var tagList = str.split(re); curvol=tagList[1]; var a = parseInt(curvol)/2; setState("javascript.0.current_volume",a); //var i =0; console.log('Start str='+str); console.log ('a='+a); console.log ('target_vol='+target_vol); var count = Math.abs(a-target_vol); console.log ('count='+count); for (i=0; i< count; i++) { if (a>target_vol) { vol_down(); // Ожидать выполнения функции? } if (a<target_vol) {/vol_up();/Ожидать/выполнения/функции?/}/})/set_volume(45);<e=""></target_vol)>
В лог выводится вот такая ерунда , а должно быть пошаговое увеличение/уменьшение:
23:12:52.476 [info] javascript.0 script.js.test.Скрипт1: registered 0 subscriptions and 0 schedules 23:12:53.558 [info] javascript.0 script.js.test.Скрипт1: Start str=Trying 192.168.1.81... Connected to 192.168.1.81\. Escape character is '^]'. VOL051 23:12:53.559 [info] javascript.0 script.js.test.Скрипт1: a=25.5 23:12:53.559 [info] javascript.0 script.js.test.Скрипт1: target_vol=45 23:12:53.559 [info] javascript.0 script.js.test.Скрипт1: count=19.5 23:12:54.487 [info] javascript.0 script.js.test.Скрипт1: vol_up str=Trying 192.168.1.81... Connected to 192.168.1.81\. Escape character is '^]'. VOL061 VOL061 23:12:54.487 [info] javascript.0 script.js.test.Скрипт1: vol_up a=30.5 23:12:54.491 [info] javascript.0 script.js.test.Скрипт1: vol_up str=Trying 192.168.1.81... Connected to 192.168.1.81\. Escape character is '^]'. VOL061 VOL061 23:12:54.492 [info] javascript.0 script.js.test.Скрипт1: vol_up a=30.5 23:12:54.494 [info] javascript.0 script.js.test.Скрипт1: vol_up str=Trying 192.168.1.81... Connected to 192.168.1.81\. Escape character is '^]'. VOL061 VOL061 23:12:54.494 [info] javascript.0 script.js.test.Скрипт1: vol_up a=30.5 23:12:54.499 [info] javascript.0 script.js.test.Скрипт1: vol_up str=Trying 192.168.1.81... Connected to 192.168.1.81\. Escape character is '^]'. VOL061 VOL061 23:12:54.499 [info] javascript.0 script.js.test.Скрипт1: vol_up a=30.5 23:12:54.503 [info] javascript.0 script.js.test.Скрипт1: vol_up str=Trying 192.168.1.81... Connected to 192.168.1.81\. Escape character is '^]'. VOL061 VOL061 23:12:54.503 [info] javascript.0 script.js.test.Скрипт1: vol_up a=30.5 23:12:54.507 [info] javascript.0 script.js.test.Скрипт1: vol_up str=Trying 192.168.1.81... Connected to 192.168.1.81\. Escape character is '^]'. 23:12:54.507 [info] javascript.0 script.js.test.Скрипт1: vol_up a=NaN 23:12:54.511 [info] javascript.0 script.js.test.Скрипт1: vol_up str=Trying 192.168.1.81... Connected to 192.168.1.81\. Escape character is '^]'. 23:12:54.511 [info] javascript.0 script.js.test.Скрипт1: vol_up a=NaN 23:12:54.515 [info] javascript.0 script.js.test.Скрипт1: vol_up str=Trying 192.168.1.81... Connected to 192.168.1.81\. Escape character is '^]'. 23:12:54.515 [info] javascript.0 script.js.test.Скрипт1: vol_up a=NaN 23:12:54.516 [info] javascript.0 script.js.test.Скрипт1: vol_up str=Trying 192.168.1.81... Connected to 192.168.1.81\. Escape character is '^]'. 23:12:54.516 [info] javascript.0 script.js.test.Скрипт1: vol_up a=NaN 23:12:54.522 [info] javascript.0 script.js.test.Скрипт1: vol_up str=Trying 192.168.1.81... Connected to 192.168.1.81\. Escape character is '^]'. 23:12:54.522 [info] javascript.0 script.js.test.Скрипт1: vol_up a=NaN 23:12:54.526 [info] javascript.0 script.js.test.Скрипт1: vol_up str=Trying 192.168.1.81... Connected to 192.168.1.81\. Escape character is '^]'. 23:12:54.526 [info] javascript.0 script.js.test.Скрипт1: vol_up a=NaN 23:12:54.530 [info] javascript.0 script.js.test.Скрипт1: vol_up str=Trying 192.168.1.81... Connected to 192.168.1.81\. Escape character is '^]'. 23:12:54.530 [info] javascript.0 script.js.test.Скрипт1: vol_up a=NaN 23:12:54.535 [info] javascript.0 script.js.test.Скрипт1: vol_up str=Trying 192.168.1.81... Connected to 192.168.1.81\. Escape character is '^]'. 23:12:54.535 [info] javascript.0 script.js.test.Скрипт1: vol_up a=NaN 23:12:54.540 [info] javascript.0 script.js.test.Скрипт1: vol_up str=Trying 192.168.1.81... Connected to 192.168.1.81\. Escape character is '^]'. 23:12:54.540 [info] javascript.0 script.js.test.Скрипт1: vol_up a=NaN 23:12:54.540 [info] javascript.0 script.js.test.Скрипт1: vol_up str=Trying 192.168.1.81... Connected to 192.168.1.81\. Escape character is '^]'. 23:12:54.540 [info] javascript.0 script.js.test.Скрипт1: vol_up a=NaN 23:12:54.543 [info] javascript.0 script.js.test.Скрипт1: vol_up str=Trying 192.168.1.81... Connected to 192.168.1.81\. Escape character is '^]'. 23:12:54.544 [info] javascript.0 script.js.test.Скрипт1: vol_up a=NaN 23:12:54.550 [info] javascript.0 script.js.test.Скрипт1: vol_up str=Trying 192.168.1.81... Connected to 192.168.1.81\. Escape character is '^]'. 23:12:54.550 [info] javascript.0 script.js.test.Скрипт1: vol_up a=NaN 23:12:54.552 [info] javascript.0 script.js.test.Скрипт1: vol_up str=Trying 192.168.1.81... Connected to 192.168.1.81\. Escape character is '^]'. 23:12:54.552 [info] javascript.0 script.js.test.Скрипт1: vol_up a=NaN 23:12:54.557 [info] javascript.0 script.js.test.Скрипт1: vol_up str=Trying 192.168.1.81... Connected to 192.168.1.81\. Escape character is '^]'. 23:12:54.557 [info] javascript.0 script.js.test.Скрипт1: vol_up a=NaN 23:12:54.562 [info] javascript.0 script.js.test.Скрипт1: vol_up str=Trying 192.168.1.81... Connected to 192.168.1.81\. Escape character is '^]'. 23:12:54.562 [info] javascript.0 script.js.test.Скрипт1: vol_up a=NaN
-
Попробуй так, код не тестил. Могут быть ошибки
function vol_down_up(cmd, callback) { exec(cmd, function callback(error, stdout, stderr){ var str = stdout; var re = /VOL/; var tagList = str.split(re); curvol=tagList[1]; var a = parseInt(curvol)/2; console.log('vol str='+str); console.log('vol a='+a); setState("javascript.0.current_volume",a); callback (a); }); } function set_volume(vol){ target_vol=vol; cmd = '{ echo "?V"; sleep 1;} | telnet 192.168.1.81 8102'; exec(cmd, function callback(error, stdout, stderr){ var str = stdout; var re = /VOL/; var tagList = str.split(re); curvol=tagList[1]; var a = parseInt(curvol)/2; setState("javascript.0.current_volume",a); //var i =0; console.log('Start str='+str); console.log ('a='+a); console.log ('target_vol='+target_vol); var count = Math.abs(a-target_vol); console.log ('count='+count); var timerId; var cmd; if (a>target_vol){ cmd = '{ echo "VD"; sleep 1;} | telnet 192.168.1.81 8102'; timerId = setTimeout(function vol_down_up(cmd, a_return) { if (a_return <= target_vol){ clearTimeout(timerId); // если достигли нужного значения, выходим из рекурсии } else timerId = setTimeout(vol_down_up(cmd, a_return), 1000); // или продолжаем вызывать функцию }, 1000); } if (a<target_vol){ cmd="{ echo "VU"; sleep 1;} | telnet 192.168.1.81 8102" ;/timerid="setTimeout(function" vol_down_up(cmd,/a_return)/{/if/(a_return="">= target_vol){ clearTimeout(timerId); // если достигли нужного значения, выходим из рекурсии } else timerId = setTimeout(vol_down_up(cmd, a_return), 1000); // или продолжаем вызывать функцию }, 1000); } }) } set_volume(45);</target_vol){>
-
host.home 2018-04-06 18:27:47.343 info Restart adapter system.adapter.javascript.0 because enabled host.home 2018-04-06 18:27:47.342 error instance system.adapter.javascript.0 terminated with code 0 (OK) javascript.0 2018-04-06 18:27:47.335 info terminating javascript.0 2018-04-06 18:27:47.333 error at vol_down_up (script.js.test.Скрипт2:46:45) javascript.0 2018-04-06 18:27:47.333 error at vol_down_up (script.js.test.Скрипт2:46:45) javascript.0 2018-04-06 18:27:47.333 error at vol_down_up (script.js.test.Скрипт2:46:45) javascript.0 2018-04-06 18:27:47.333 error at vol_down_up (script.js.test.Скрипт2:46:45) javascript.0 2018-04-06 18:27:47.333 error at vol_down_up (script.js.test.Скрипт2:46:45) javascript.0 2018-04-06 18:27:47.333 error at vol_down_up (script.js.test.Скрипт2:46:45) javascript.0 2018-04-06 18:27:47.333 error at vol_down_up (script.js.test.Скрипт2:46:45) javascript.0 2018-04-06 18:27:47.333 error at vol_down_up (script.js.test.Скрипт2:46:45) javascript.0 2018-04-06 18:27:47.333 error at vol_down_up (script.js.test.Скрипт2:46:45) javascript.0 2018-04-06 18:27:47.333 error at vol_down_up (script.js.test.Скрипт2:43:54) javascript.0 2018-04-06 18:27:47.333 error RangeError: Maximum call stack size exceeded javascript.0 2018-04-06 18:27:47.333 error uncaught exception: Maximum call stack size exceeded javascript.0 2018-04-06 18:27:46.320 info script.js.test.Скрипт2: count=15.5 javascript.0 2018-04-06 18:27:46.320 info script.js.test.Скрипт2: target_vol=45 javascript.0 2018-04-06 18:27:46.320 info script.js.test.Скрипт2: a=29.5 javascript.0 2018-04-06 18:27:46.320 info VOL059 javascript.0 2018-04-06 18:27:46.320 info Escape character is '^]'. javascript.0 2018-04-06 18:27:46.320 info Connected to 192.168.1.81. javascript.0 2018-04-06 18:27:46.320 info script.js.test.Скрипт2: Start str=Trying 192.168.1.81... javascript.0 2018-04-06 18:27:44.912 info script.js.test.Скрипт2: registered 0 subscriptions and 0 schedules javascript.0 2018-04-06 18:27:44.907 info Start javascript script.js.test.Скрипт2
-
Попробуй так, код не тестил. Могут быть ошибки `
Вот они:host.home 2018-04-06 18:27:47.343 info Restart adapter system.adapter.javascript.0 because enabled host.home 2018-04-06 18:27:47.342 error instance system.adapter.javascript.0 terminated with code 0 (OK) javascript.0 2018-04-06 18:27:47.335 info terminating javascript.0 2018-04-06 18:27:47.333 error at vol_down_up (script.js.test.Скрипт2:46:45) javascript.0 2018-04-06 18:27:47.333 error at vol_down_up (script.js.test.Скрипт2:46:45) javascript.0 2018-04-06 18:27:47.333 error at vol_down_up (script.js.test.Скрипт2:46:45) javascript.0 2018-04-06 18:27:47.333 error at vol_down_up (script.js.test.Скрипт2:46:45) javascript.0 2018-04-06 18:27:47.333 error at vol_down_up (script.js.test.Скрипт2:46:45) javascript.0 2018-04-06 18:27:47.333 error at vol_down_up (script.js.test.Скрипт2:46:45) javascript.0 2018-04-06 18:27:47.333 error at vol_down_up (script.js.test.Скрипт2:46:45) javascript.0 2018-04-06 18:27:47.333 error at vol_down_up (script.js.test.Скрипт2:46:45) javascript.0 2018-04-06 18:27:47.333 error at vol_down_up (script.js.test.Скрипт2:46:45) javascript.0 2018-04-06 18:27:47.333 error at vol_down_up (script.js.test.Скрипт2:43:54) javascript.0 2018-04-06 18:27:47.333 error RangeError: Maximum call stack size exceeded javascript.0 2018-04-06 18:27:47.333 error uncaught exception: Maximum call stack size exceeded javascript.0 2018-04-06 18:27:46.320 info script.js.test.Скрипт2: count=15.5 javascript.0 2018-04-06 18:27:46.320 info script.js.test.Скрипт2: target_vol=45 javascript.0 2018-04-06 18:27:46.320 info script.js.test.Скрипт2: a=29.5 javascript.0 2018-04-06 18:27:46.320 info VOL059 javascript.0 2018-04-06 18:27:46.320 info Escape character is '^]'. javascript.0 2018-04-06 18:27:46.320 info Connected to 192.168.1.81. javascript.0 2018-04-06 18:27:46.320 info script.js.test.Скрипт2: Start str=Trying 192.168.1.81... javascript.0 2018-04-06 18:27:44.912 info script.js.test.Скрипт2: registered 0 subscriptions and 0 schedules javascript.0 2018-04-06 18:27:44.907 info Start javascript script.js.test.Скрипт2