(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) {
log.info("LongPress ActionButtons.onButtonPress")//Это лог. Он попадает в /var/log/messages
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) {
log.info("LongPress defineRule")//Это лог. Он попадает в /var/log/messages
// 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(
"wb-mio-gpio_157:1/IN14", //Вход, за которым следим.
{
singlePress: {
func: switchRelay,
prop: ["wb-mio-gpio_157:2", "K1"]
},
doublePress: {
func: switchRelay, prop: ["wb-mio-gpio_157:2", "K2"]
//func: switchDimmerRGB,
//prop: ["wb-mr6c_10", "K2", "wb-mrgbw-d_24"]
},
longPress: {
func: switchRelay, prop: ["wb-mio-gpio_157:2", "K3"]
//func: setRandomRGB,
//prop: ["wb-mr6c_10", "K2", "wb-mrgbw-d_24"]
}
},
300, 1000
);
/**
* Helper Functions
*/
function switchRelay(device, control) { //Принимает в параметрах устройство и выход. Переключает состояние выхода на противоположное.
log.info("LongPress switchRelay" ,device, control)//Это лог. Он попадает в /var/log/messages
dev[device][control] = !dev[device + "/" + control];
}
function switchDimmerRGB(relayDevice, relayControl, dimmerDevice) {
dev[relayDevice][relayControl] = true;
if (dev[dimmerDevice + "/RGB"] !== "0;0;0") {
dev[dimmerDevice]["RGB"] = "0;0;0";
}
else {
dev[dimmerDevice]["RGB"] = dev[relayDevice + "/RGB"];
}
}
function setRandomRGB(relayDevice, relayControl, dimmerDevice) {
dev[relayDevice][relayControl] = true;
dev[relayDevice + "/RGB"] = "" + Math.floor(Math.random() * 255) + ";" + Math.floor(Math.random() * 255) + ";" + Math.floor(Math.random() * 255);
dev[dimmerDevice]["RGB"] = dev[relayDevice + "/RGB"];
}
Все вставляем в один скрипт.
singlePress
doublePress
longPress
вызывают хелпер-функции соответственно. В них можно уже то что нужно прописать.