# Движок правил: примеры кода

**URL:** <https://support.wirenboard.com/t/dvizhok-pravil-primery-koda/483>\
**Category:** Программное обеспечение\
**Created:** [March 18, 2016, 8:07pm UTC](https://support.wirenboard.com/t/dvizhok-pravil-primery-koda/483 "2016-03-18T20:07:05Z")\
**Posts on this page:** 1\
**Showing post:** 98

<div class="post-metadata">

**Author:** ![RA093](https://avatars.discourse-cdn.com/v2/letter/r/ecc23a/32.png) [@RA093](https://support.wirenboard.com/u/RA093)\
**Post date:** [August 10, 2020, 1:17pm UTC](https://support.wirenboard.com/t/dvizhok-pravil-primery-koda/483/98 "2020-08-10T13:17:40Z")

</div>

> [@BrainRoot](#):
>
> Давайте ваш скрипт целиком - посмотрю. Должно работать.

Кнопка в одном скрипте:

```
defineVirtualDevice("test_button1", {
  title: "Test dev 1",
  cells: {
    "switch_on": {
    type: "pushbutton",
    value: false,
    },
  }
});

```

Это скрипт в файле \_OnButtonPress.js, его взял где-то здесь. Как бы я не нажимал, всегда срабатывает:

longPress: {  
func: switchRelay,  
prop: [“wb-mr6c\_185”, “K2”]  
}

Дабл клик просто выкл и вкл быстро реле.

```
(function () {
'use strict';

var ActionButtons = {};

/**
 * Function that identifies what kind of press was performed: single, double or long press;
 * and assigns an action for each type of press.
 *
 * @param {string} trigger - Name of device and control in the following format: "<device>/<control>".
 * @param {object} action - Defines actions to be taken for each type of button press.
 * Key: "singlePress" or "doublePress" or "longPress" or "longRelease".
 * Value: Object of the following structure {func: <function name>, prop: <array of parameters to be passed>}
 * Example:
 * {
 * singlePress: {func: myFunc1, prop: ["wb-mr6c_1", "K1"]},
 * doublePress: {func: myFunc2, prop: ["wb-mrgbw-d_2", "RGB", "255;177;85"]},
 * longPress: {func: myFunc3, prop: []},
 * longRelease: {func: myFunc4, prop: []}
 * }
 * @param {number} timeToNextPress - Time (ms) after button up to wait for the next press before reseting the counter. Default is 300 ms.
 * @param {number} timeOfLongPress - Time (ms) after button down to be considered as as a long press. Default is 1000 ms (1 sec).
 */
ActionButtons.onButtonPress = function (trigger, action, timeToNextPress, timeOfLongPress) {
    var buttonPressedCounter = 0;
    var timerWaitNextShortPress = null;
    var timerLongPress = null;
    var isLongPress = false;

    var ruleName = "on_button_press_" + trigger.replace("/", "_");

    defineRule(ruleName, {
        whenChanged: trigger,
        then: function (newValue, devName, cellName) {

            // If button is pressed, wait for a long press
            if (newValue) {

                if (timerWaitNextShortPress) {
                    clearTimeout(timerWaitNextShortPress);
                }
                timerLongPress = setTimeout(function () {
                    if (typeof action.longPress === "object") {
                        if (typeof action.longPress.func === "function") {
                            action.longPress.func.apply(this, action.longPress.prop);
                        }
                    }
                    // log(">>>>>>> long press <<<<<<");
                    isLongPress = true; // Long press identified, we will skip short press
                    buttonPressedCounter = 0;
                }, timeOfLongPress);

            }

            // If button is released, then it is not a "long press", start to count clicks
            else {
                if (!isLongPress) {
                    clearTimeout(timerLongPress);
                    buttonPressedCounter += 1;
                    timerWaitNextShortPress = setTimeout(function () {
                        switch (buttonPressedCounter) {
                        // Counter equals 1 - it's a single short press
                        case 1:
                            if (typeof action.singlePress === "object") {
                                if (typeof action.singlePress.func === "function") {
                                    action.singlePress.func.apply(this, action.singlePress.prop);
                                }
                            }
                            // log(">>>>>> short press - single <<<<<<");
                            break;
                        // Counter equals 2 - it's a double short press
                        case 2:
                            if (typeof action.doublePress === "object") {
                                if (typeof action.doublePress.func === "function") {
                                    action.doublePress.func.apply(this, action.doublePress.prop);
                                }
                            }
                            // log(">>>>>> short press - double <<<<<<");
                            break;
                        }
                        // Reset the counter
                        buttonPressedCounter = 0;
                    }, timeToNextPress);
                }

                // Catch button released after long press
                else {
                    if (typeof action.longRelease === "object") {
                        if (typeof action.longRelease.func === "function") {
                            if (typeof action.longRelease.prop === "array") {
                                action.longRelease.func.apply(this, action.longRelease.prop);
                            } else {
                                action.longRelease.func.apply(this, []);
                            }
                        }
                    }
                    isLongPress = false;
                }
            }

        }
    });
};

// export as Node module / AMD module / browser variable
if (typeof exports === 'object' && typeof module !== 'undefined') {
    module.exports = ActionButtons;
} else if (typeof define === 'function' && define.amd) {
    define(ActionButtons);
} else {
    global.ActionButtons = ActionButtons;
}

}());

ActionButtons.onButtonPress(
"test_button1/switch_on",
{
    singlePress: {
        func: switchRelay,
        prop: ["wb-mr6c_157", "K1"]
    },
    doublePress: {
        func: switchRelay,
        prop: ["wb-mr6c_185", "K1"]
    },
    longPress: {
        func: switchRelay,
        prop: ["wb-mr6c_185", "K2"]
    }
},
300, 1000
);

/**
* Helper Functions
*/
function switchRelay(device, control) {
  dev[device][control] = !dev[device + "/" + control];
}

```

---

_[View the full topic](https://support.wirenboard.com/t/dvizhok-pravil-primery-koda/483)._
