https://support.wirenboard.com/t/dvizhok-pravil-primery-koda/483/78
Итак, имеем реле c адресом 45 на порту /dev/ttyMOD3
поднимаем ему скорость, заодно отключим взаимодействие входа 0 с матрицей:
export DEV_PORT=/dev/ttyMOD3
export DEV_ADDR=45
systemctl stop wb-mqtt-serial
modbus_client --debug -mrtu -pnone -s2 $DEV_PORT -a$DEV_ADDR -t0x06 -r110 1152
modbus_client --debug -mrtu -b115200 -pnone -s2 $DEV_PORT -a$DEV_ADDR -t0x06 -r16 3
systemctl start wb-mqtt-serial
Также меняем скорость порта через веб-итнтерфейс.
Ок, у нас реле на скорости 115200 и вход 0 не взаимодействует с контроллером.
Пишем скриптик:
//03_19_test_02.js
/**
* 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} ruleName - Name of the rule that will be defined.
* @param {string} trigger - Name of device and control in the following format: "<device>/<control>".
* @param {object} action - Object that defines relays that will be switched.
* Keys: "singlePress", "doublePress", "longPress".
* Each key contains an array of the following format: ["<device>", "control"].
* or function! Re-added bu Brainroot
* @param {number} timeToNextPress - Time (ms) after button up to wait for the next press before reseting the counter.
* @param {number} timeOfLongPress - Time (ms) after button down to be considered as as a long press.
*/
function onButtonPress(ruleName, trigger, action, timeToNextPress, timeOfLongPress) {
var buttonPressedCounter = 0;
var timerWaitNextShortPress = null;
var timerLongPress = null;
var isLongPress = false;
defineRule(ruleName, {
whenChanged: trigger,
then: function (newValue, devName, cellName) {
log.info("Enter rule")
// If button is pressed, wait for a long press
if (newValue) {
if (timerWaitNextShortPress)
clearTimeout(timerWaitNextShortPress);
timerLongPress = setTimeout(function () {
if (typeof action.longPress.func === "function") {
log.info("longPress is function")
action.longPress.func.apply(this, action.longPress.prop);
}
else{
dev[action.longPress[0]][action.longPress[1]] = !dev[action.longPress[0]+"/"+action.longPress[1]];
}
// 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.func === "function") {
log.info("singlePress is function")
action.singlePress.func.apply(this, action.singlePress.prop);
}
else{
dev[action.singlePress[0]][action.singlePress[1]] = !dev[action.singlePress[0]+"/"+action.singlePress[1]];
// log(">>>>>> short press - single <<<<<<");
}
break;
// Counter equals 2 - it's a double short press
case 2:
if (typeof action.doublePress.func === "function") {
log.info("doublePress is function")
action.doublePress.func.apply(this, action.doublePress.prop);
}
else{
dev[action.doublePress[0]][action.doublePress[1]] = !dev[action.doublePress[0]+"/"+action.doublePress[1]];
// log(">>>>>> short press - double <<<<<<");
}
break;
}
// Reset the counter
buttonPressedCounter = 0;
}, timeToNextPress);
}
isLongPress = false;
}
}
});
}
// Usage example:
onButtonPress(
"on_button_press",
"wb-mr6c_45/Input 0",
{
singlePress: {
func: singlePressHelper, prop: ["singlePress", "wb-mr6c_45/K1"]
},
doublePress: {
func: doublePressHelper, prop: ["doublePress", "wb-mr6c_45/K2"]
},
longPress: {
func: longPressHelper, prop:["longPress", "wb-mr6c_45/K3"]
}
},
300, 2000
);
singlePressHelper("kva-kva1", "wb-mr6c_45/K1")
doublePressHelper("kva-kva2", "wb-mr6c_45/K2")
longPressHelper("kva-kva3", "wb-mr6c_45/K3")
function singlePressHelper(paramString, param){
log.info("function singlePress:",paramString)
dev[param] = !dev[param]
}
function doublePressHelper(paramString, param){
log.info("function doublePress:",paramString)
dev[param] = !dev[param]
}
function longPressHelper(paramString, param){
log.info("function longPress:",paramString)
dev[param] = !dev[param]
}
Работает. Если дописать еще анализ couner из реле - то можно и усовешенствовать.