Движок правил wb-rules: различия между версиями

Строка 78: Строка 78:




В веб-интерфейсе в разделе Devices должно появиться новое устройство "Send custom command to RS-485 port"
В веб-интерфейсе в разделе Devices должно появиться новое устройство "Send custom command to RS-485 port".
 
 
6. Добавим функцию для конфигурирования порта.
 
 
<pre>
function setup_port() {
    runShellCommand("stty -F /dev/ttyNSC0 ospeed 9600 ispeed 9600 raw clocal -crtscts -parenb -echo cs8");
}
 
</pre>
 
 
7. Опишем правила на вклчение и выключение переключателя
 
<pre>
defineRule("_rs485_switch_on", {
  asSoonAs: function () {
    return dev.rs485_cmd.enabled;
  },
  then: function() {
    runShellCommand("/usr/bin/printf '\\xff\\xff\\x0a\\x01\\xff\\x00\\x00\\x0a' > /dev/ttyNSC0");
  }
});
 
defineRule("_rs485_switch_off", {
  asSoonAs: function () {
    return !dev.rs485_cmd.enabled;
  },
  then: function() {
    runShellCommand("/usr/bin/printf '\\xff\\xff\\x0a\\x01\\x00\\x00\\x00\\x0b' >/dev/ttyNSC0");
  }
});
 
</pre>
 
Обратите внимание на двойное экранирование.
 
 
 
 
7. Собираем всё вместе
 
Полное содержимое файла с правилами:
 
<pre>
root@wirenboard:~# cat  /etc/wb-rules/rs485_cmd.js
defineVirtualDevice("rs485_cmd", {
    title: "Send custom command to RS-485 port",
    cells: {
enabled: {
    type: "switch",
    value: false
},
    }
});
 
 
function setup_port() {
    runShellCommand("stty -F /dev/ttyNSC0 ospeed 9600 ispeed 9600 raw clocal -crtscts -parenb -echo cs8");
}
 
defineRule("_rs485_switch_on", {
  asSoonAs: function () {
    return dev.rs485_cmd.enabled;
  },
  then: function() {
    runShellCommand("/usr/bin/printf '\\xff\\xff\\x0a\\x01\\xff\\x00\\x00\\x0a' > /dev/ttyNSC0");
  }
});
 
defineRule("_rs485_switch_off", {
  asSoonAs: function () {
    return !dev.rs485_cmd.enabled;
  },
  then: function() {
    runShellCommand("/usr/bin/printf '\\xff\\xff\\x0a\\x01\\x00\\x00\\x00\\x0b' >/dev/ttyNSC0");
  }
});
</pre>