Помогите упростить скрипты

как завернуть
defineRule({
when: function() {
return dev[“wb-gpio/EXT1_IN11”];
},
then: function (newValue, devName, cellName) {

dev[“wb-gpio”][“EXT3_R3A1”] = !dev[“wb-gpio”][“EXT3_R3A1”];
}
});

по рекомендации с вики - со списокм соответвия в конце -вход-выход
https://wirenboard.com/wiki/Rule_Examples#.D0.A1.D0.BE.D0.B7.D0.B4.D0.B0.D0.BD.D0.B8.D0.B5_.D0.BE.D0.B4.D0.BD.D0.BE.D1.82.D0.B8.D0.BF.D0.BD.D1.8B.D1.85_.D0.BF.D1.80.D0.B0.D0.B2.D0.B8.D0.BB

function makeMotionDetector(name, timeout_ms, detector_control, relay_control) {
var motion_timer_id = null;
defineRule(name, {
whenChanged: “wb-gpio/” + detector_control,
then: function(newValue, devName, cellName) {
if (!newValue) {
dev[“wb-gpio”][relay_control] = true;
if (motion_timer_id) {
clearTimeout(motion_timer_id);
}

          motion_timer_id = setTimeout(function() {
              dev["wb-gpio"][relay_control] = false;
              motion_timer_id = null;
          }, timeout_ms);
      }
  }

});
}

makeMotionDetector(“motion_detector_1”, 20000, “EXT1_DR1”, “EXT2_R3A1”);
makeMotionDetector(“motion_detector_2”, 10000, “EXT1_DR2”, “EXT2_R3A2”);
makeMotionDetector(“motion_detector_3”, 10000, “EXT1_DR3”, “EXT2_R3A3”);

Здравствуйте!
Я вижу задачу такой: при значении “motion detector_Х” == false включить на заданный промежуток времени одно из реле. Правила создаются и работают, все сделано правильно.

Несколько строк кода, думаю, можно удалить из скрипта для упрощения:

function makeMotionDetector(name, timeout_ms, detector_control, relay_control) {
    var motion_timer_id = null;
    defineRule(name, {
        whenChanged: "wb-gpio/" + detector_control,
        then: function (newValue, devName, cellName) {
            if (!newValue) {
                dev["wb-gpio"][relay_control] = true;

                // останавливать таймер необязательно, так как он все равно будет перезапущен
                // if (motion_timer_id) {
                //     clearTimeout(motion_timer_id);
                // }

                motion_timer_id = setTimeout(function () {
                    dev["wb-gpio"][relay_control] = false;
                    //не обязательно очищать id таймера
                    //motion_timer_id = null;
                }, timeout_ms);
            }
        },
    });
}

//detevcetor control заменил для теста
makeMotionDetector("motion_detector_1", 20000, “EXT1_DR1”, "EXT2_R3A1");
makeMotionDetector("motion_detector_2", 10000, “EXT1_DR2”, "EXT2_R3A2");
makeMotionDetector("motion_detector_3", 10000, “EXT1_DR3”, "EXT2_R3A3");

function listPush(name, detector_control, relay_control) {

defineRule(name, {
when: function() {
return dev[detector_control];
},
then: function(newValue, devName, cellName) {
dev[relay_control] = !dev[relay_control];
}
});
}

listPush(“2.2”, “wb-gpio/EXT1_IN14”, “wb-mr6c_200/K1”);
listPush(“1.2”, “wb-gpio/EXT1_IN13”, “wb-mr6c_200/K2”);
listPush(“6.3”, “wb-gpio/EXT1_IN12”, “wb-mr6c_200/K3”);

спасибо, параллельно решил тоже)

Также можно имена правил не задавать. При этом будут создаваться анонимные правила:

function makeMotionDetector(timeout_ms, detector_control, relay_control) {
    var motion_timer_id = null;
    defineRule({
        whenChanged: "wb-gpio/" + detector_control,
        then: function (newValue, devName, cellName) {
            if (!newValue) {
                dev["wb-gpio"][relay_control] = true;

                // останавливать таймер необязательно, так как он все равно будет перезапущен
                // if (motion_timer_id) {
                //     clearTimeout(motion_timer_id);
                // }

                    motion_timer_id = setTimeout(function () {
                    dev["wb-gpio"][relay_control] = false;
                   //не обязательно очищать id таймера
                   //motion_timer_id = null;
                }, timeout_ms);
            }
        },
    });
}

//detevcetor control заменил для теста
makeMotionDetector(3000, "EXT2_R3A4", "EXT2_R3A1");
makeMotionDetector(2000, "EXT2_R3A5", "EXT2_R3A2");
makeMotionDetector(1000, "EXT2_R3A6", "EXT2_R3A3");

Эта тема была автоматически закрыта через 7 дней после последнего ответа. В ней больше нельзя отвечать.