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

Материал из Wiren Board
Строка 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>

Версия 17:02, 5 мая 2015

Полная документация доступна здесь.

Примеры

Отправка команд по RS-485

Для примера отправим команду устройству на порт /dev/ttyNSC0 (неоптоизолированный RS-485 на Wiren Board 4). Для этого будем использовать движок правил и возможность выполнения произвольных шелл-команд. Подробнее см. в документации.

С помощью движка правил создадим виртуальное устройство с контролом типа switch (переключатель).

При включении переключателя будем отправлять команду (уст. Яркость кан. 00=0xff) для Uniel UCH-M141:

FF FF 0A 01 FF 00 00 0A


При выключении переключателя будем отправлять команду (уст. Яркость кан. 00=0x00) для Uniel UCH-M141:

FF FF 0A 01 00 00 00 0B



1. Настройка порта

Для настройки порта /dev/ttyNSC0 на скорость 9600 надо выполнить следующую команду

stty -F /dev/ttyNSC0 ospeed 9600 ispeed 9600 raw clocal -crtscts -parenb -echo cs8

2. Отправка команды

Отправка данных делается следующей шелл-командой:

/usr/bin/printf '\xFF\xFF\x0A\x01\xD1\x06\x00\xE2' >/dev/ttyNSC0

где "\xFF\xFF\x0A\x01\xD1\x06\x00\xE2" - это запись команды "FF FF 0A 01 D1 06 00 E2".


3. Создадим в движке правил новый файл с правилами

/etc/wb-rules/rs485_cmd.js

Файл можно редактировать с помощью vim, nano или mcedit в сеансе ssh на устройстве, либо залить его с помощью SCP.

root@wirenboard:~# mcedit  /etc/wb-rules/rs485_cmd.js


4. Описываем в файле виртуальный девайс

defineVirtualDevice("rs485_cmd", {
    title: "Send custom command to RS-485 port",
    cells: {
	enabled: {
	    type: "switch",
	    value: false
	},
    }
});

5. Перезапускаем wb-rules и проверяем работу

root@wirenboard:~# /etc/init.d/wb-rules restart
root@wirenboard:~# tail -f /var/log/messages

В логе не должно быть сообщений об ошибке (выход через control-c)


В веб-интерфейсе в разделе Devices должно появиться новое устройство "Send custom command to RS-485 port".


6. Добавим функцию для конфигурирования порта.


function setup_port() {
    runShellCommand("stty -F /dev/ttyNSC0 ospeed 9600 ispeed 9600 raw clocal -crtscts -parenb -echo cs8");
}


7. Опишем правила на вклчение и выключение переключателя

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");
  }
});

Обратите внимание на двойное экранирование.



7. Собираем всё вместе

Полное содержимое файла с правилами:

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");
  }
});