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

Материал из Wiren Board
(Новая страница: «400px|thumb|right|Editing rules in web interface You can write rules for the controller, for example: "If the sensor temperature is le…»)
(не показано 110 промежуточных версий 2 участников)
Строка 3: Строка 3:
  You can write rules for the controller, for example: "If the sensor temperature is less than 18°C, turn on the heater." Rules are created via [[Веб-интерфейс Wiren Board/en|web interface]] and written in a simple language similar to Javascript.
  You can write rules for the controller, for example: "If the sensor temperature is less than 18°C, turn on the heater." Rules are created via [[Веб-интерфейс Wiren Board/en|web interface]] and written in a simple language similar to Javascript.


The complete description of the rules engine: https://github.com/wirenboard/wb-rules
Самое полное описание движка правил: https://github.com/contactless/wb-rules






== How to create and edit rules ==
== Как создавать и редактировать правила ==


*The list of files with rules is on the ''Scripts'' page of the web interface.
*Список файлов с правилами находится на странице ''Scripts'' веб-интерфейса.
*Click the file name to open it for editing.
*Нажмите на название файла, чтобы открыть его для редактирования.
**To create a new file, click ''New...'' at the top, enter the name of the script (use only Latin letters and numbers for the name, specify as an extension ''.js''), in the main field, enter the text of the script, then click the ''Save button'' at the top.
**Чтобы создать новый файл, нажмите на пункт ''New...'', вверху введите название скрипта (используйте для названия только латинские буквы и цифры, в качестве расширения укажите ''.js''), в основное поле введите текст скрипта, затем нажмите кнопку ''Save'' вверху.
The rule starts automatically after you click ''Save'' if there are no errors (see below).
*Правило начинает работать автоматически после нажатия кнопки ''Save'', если в нём нет ошибок (смотрите ниже).
Notes:
Примечания:
#The rules files are stored on the controller as plain text files in the <code>/etc/wb-rules/</code> folder, so they [[Просмотр файлов контроллера с компьютера/en|can be edited and downloaded]] directly from your computer.
#Файлы с правилами хранятся на контроллере в виде обычных текстовых файлов в папке <code>/etc/wb-rules/</code>, поэтому [[Special:MyLanguage/Просмотр файлов контроллера с компьютера|их можно редактировать и загружать напрямую с компьютера]].
#The rules are executed by the ''wb-rules'' service, see the service's [https://github.com/wirenboard/wb-rules documentation on Github].
#Правила исполняются сервисом ''wb-rules'', документацию по нему смотрите [https://github.com/contactless/wb-rules странице сервиса в Github].






== Writing the first rule ==
== Пишем первое правило ==


[[File:Web-scripts-rule1.png|400px|thumb|The rule for the heater control, written via the web interface]]
[[File:Web-scripts-rule1.png|400px|thumb|Правило для управления обогревателем, записанное через веб-интерфейс]]


There are two types of rules :the rules themselves (start with defineRule) and virtual devices (start with defineVirtualDevice). Virtual devices are new switchers that appear in the web interface - for example, a toggle button that actually turns off two devices at the same time. It is not tied directly to any physical device, and the actions when you click it are determined by the script you wrote.
Правила бывают двух типов - непосредственно правила (начинаются со слов ''defineRule'') и виртуальные устройства (начинаются со слов ''defineVirtualDevice''). Виртуальные устройства - это появляющиеся в веб-интерфейсе новые элементы управления - например, кнопка-выключатель, которая на самом деле выключает два устройства одновременно. Она не привязана напрямую ни к какому физическому устройству, а действия при её нажатии определяются написанным вами скриптом.


Any number of different rules can be stored in a single file. Typically, rules responsible for close functions are stored in a single file.
Любое количество разных правил можно хранить в одном файле. Обычно в одном файле хранятся правила, отвечающие за близкие функции.






=== The first rule ===
=== Первое правило ===


To begin with, we will analyze a simple rule - when the temperature is exceeded, turn off the heater. The temperature is obtained from the  [[1-Wire/en|1-Wire]] sensor, the heater is connected to the Relay 1 of external  [[WB-MRM2/en|WB-MRM2]] relay module.
Для начала разберём простое правило - при превышении температуры выключи обогреватель. Температуру получаем с датчика [[Special:MyLanguage/1-Wire|1-Wire]], обогреватель подключён к Реле 1 внешнего релейного модуля [[Special:MyLanguage/WB-MRM2|WB-MRM2]].
<syntaxhighlight lang="ecmascript">
<syntaxhighlight lang="ecmascript">


defineRule("heater_control", { //rule name is arbitrary
defineRule("heater_control", { //название правила - "контроль обогревателя", может быть произвольным
   whenChanged: "wb-w1/28-0115a48fcfff", //when the state of 1-Wire sensor with ID 28-0115a48fcfff changes
   whenChanged: "wb-w1/28-0115a48fcfff", //при изменении состояния датчика 1-Wire с идентификатором 28-0115a48fcfff
   then: function (newValue, devName, cellName) { //execute
   then: function (newValue, devName, cellName) { //выполняй следующие действия
     if ( newValue > 30) { //if the sensor temperature value is more than 30 degrees
     if ( newValue > 30) { //если температура датчика больше 30 градусов
       dev["wb-mrm2_130"]["Relay 1"] = 0; //set the Relay 1 of WB-MRM2 module with address 130 in "off" state
       dev["wb-mrm2_130"]["Relay 1"] = 0; //установи Реле 1 модуля WB-MRM2 с адресом 130 в состояние "выключено"
     } else {
     } else {
       dev["wb-mrm2_130"]["Relay 1"] = 1; //set Relay 1 of WB-MRM2 module with address 130 in "on" state
       dev["wb-mrm2_130"]["Relay 1"] = 1; //установи Реле 1 модуля WB-MRM2 с адресом 130 в состояние "включено"
     }
     }
   }
   }
Строка 46: Строка 46:


</syntaxhighlight>
</syntaxhighlight>
*The first line is the code word ''defineRule'' and the name of the rule
*Первая строка - кодовое слово ''defineRule'' и название правила
*The second line is the code word for determining when the rule is executed-whenChanged - "when the parameter changes", then the name of the parameter, when changing which the rule will start - the temperature from the 1-Wire sensor. The parameter name is written as "Device/Control", where the device and Control names for each parameter can be found on the Settings page of the web interface in the ''MQTT Channels'' table.
*Вторая строка - кодовое слово для определения, когда выполняется правило, - ''whenChanged'' - "при изменении параметра", далее название параметра, при изменении которого запустится правило - температура с датчика 1-Wire. Название параметра записывается в виде "Device/Control", где названия ''Device'' и ''Control'' для каждого параметра можно найти на странице ''Settings'' веб-интерфейса в таблице ''MQTT Channels''.
*The third line is the beginning of the function to be executed
*Третья строка - начало функции, которая будет исполняться
*Then there is a condition - "if the temperature is greater than the threshold, then ...". The value of the parameter is written as dev[Device][Control] - note that it differs from the type of parameter recording, when changing which the rule is run, because there it is a parameter, and here - the value of the same parameter.
*Затем идёт условие - "если значение температуры больше порогового, то ...". Значение параметра записывается в виде ''dev[Device][Control]'' - заметьте, оно отличается от вида записи параметра, при изменении которого запускается правило, потому что там речь идёт о ''параметре'', а здесь - о ''значении'' того же параметра.
*Then we set the values for the relay in each case - 0 - "off", 1 - "on". The names of the Device and Control for the relay look all in the same table MQTT Channels on the ''Settings'' page of the web interface.
*Затем мы выставляем значения для реле в каждом случае - ''0'' - "выключено", ''1'' - "включено". Названия ''Device'' и ''Control'' для реле смотрим всё в той же таблице ''MQTT Channels'' на странице ''Settings'' веб-интерфейса.






=== The first rule with a virtual device ===
=== Первое правило с виртуальным устройством ===


Create a virtual switch, by clicking on which two relays are switched at once.
Создаём виртуальный переключатель, при нажатии на который переключаются сразу два реле.


<syntaxhighlight lang="ecmascript">
<syntaxhighlight lang="ecmascript">
Строка 80: Строка 80:




=== Write on complex rules ===
=== Пишем сложные правила ===


To start writing complex rules, you need to see examples of rules and complete documentation on the rules engine:
Чтобы начать писать сложные правила, нужно посмотреть примеры правил и полную документацию по движку правил:
#Examples of rules:
#Примеры правил:
#* on the same page below;
#* на этой же странице ниже;
#* in [http://forums.contactless.ru/t/dvizhok-pravil-primery-koda/483 a special topic on our forum(ru)].
#* в [http://forums.contactless.ru/t/dvizhok-pravil-primery-koda/483 специальной теме на нашем форуме ].
#[https://github.com/wirenboard/wb-rules Full description of the rules engine].
#[https://github.com/contactless/wb-rules Полное описание движка правил].






== Examples of rules ==
== Примеры правил ==




=== Control tracking ===
=== Слежение за контролом ===


This simple rule monitors the control and sets the other control in the same state.
Это простейшее правило следит за контролом и устанавливает другой контрол в такое же состояние.


For example, a rule can include a siren and a lamp if the motion sensor has noticed movement.  
Например правило может включать сирену и лампу, если датчик движения заметил движение.  


In the example, the motion sensor is connected to the input "dry contact", control type "switch". The siren is connected to the built-in relay Wiren Board, and the lamp - through the relay unit by Modbus. When the input type "dry contact" (output motion sensor) is closed, the lamp and the relay get "1", when off - "0".
В примере датчик движения подключен к входу "сухой контакт", контрол типа "switch". Сирена подключена к встроеному реле Wiren Board, а лампа - через релейный блок по Modbus. Когда вход типа "сухой контакт" (выход датчика движения) замкнут, то на лампу и реле подаётся "1", когда выключен - "0".




The rule is triggered every time the control value "D1_IN" of the device "wb-gpio" is changed. The new value of this control is passed to the rule code as a newValue variable.
Правило срабатывает каждый раз при изменении значения контрола "D1_IN" у устройства "wb-gpio". В код правила передаётся новое значение этого контрола в виде переменной newValue.


<syntaxhighlight lang="ecmascript">
<syntaxhighlight lang="ecmascript">
Строка 118: Строка 118:




The same, but with a virtual device as a source of events. Example of use: scenario button that turns on/off the siren and light bulb.
То же самое, но с виртуальным девайсом в качестве источника событий. Пример использования: сценарная кнопка, которая включает/выключает сирену и лампочку.


<syntaxhighlight lang="ecmascript">
<syntaxhighlight lang="ecmascript">
Строка 145: Строка 145:




=== Motion detection with timeout ===
=== Детектор движения c таймаутом ===


The motion detector with the "dry contact" output type is connected to the D2 input, which closes the D2 and GND when motion is detected. At the same time, the status "1" appears on the "wb-gpio/D2_IN" channel.
На вход D2 подключен детектор движения с выходом типа "сухой контакт", который замыкает D2 и GND при обнаружении движения.
При этом, на канале "wb-gpio/D2_IN" появляется статус "1".


The rule turns on the light when motion is detected and turns off the light 30 seconds after the motion sensor signal disappears.
Правило включает свет при обнаружении движения и выключает свет, спустя 30 секунд после пропадания сигнала с датчика движения.


Lighting is connected via built-in relay, wb-gpio/Relay_1 channel.
Освещение подключено через встроенное реле, канал wb-gpio/Relay_1.


<syntaxhighlight lang="ecmascript">
<syntaxhighlight lang="ecmascript">
Строка 180: Строка 181:




=== Creating similar rules ===
=== Создание однотипных правил ===


If you need several such motion detectors, you can wrap the creation of rules and variables into a function to avoid copying the code:
Если таких детекторов движения нужно несколько, то, чтобы не копировать код, можно обернуть создание правила и переменных в функцию:


<syntaxhighlight lang="ecmascript">
<syntaxhighlight lang="ecmascript">
Строка 212: Строка 213:




=== Activate a rule only at a specific time ===
=== Активация правила только в определённое время ===


The rule is as in the previous section, but runs only from 9:30 to 17:10 UTC.
Правило как в предыдущем разделе, но выполняется только с 9:30 до 17:10 по UTC.


<syntaxhighlight lang="ecmascript">
<syntaxhighlight lang="ecmascript">
Строка 257: Строка 258:




=== Rolling shutters ===
=== Роллеты ===


One relay includes the engine, raising the curtains, the second relay - includes the engine, lowering the curtains. The rule ensures that both relays are not switched on at the same time.
Одно реле включает двигатель, поднимающий шторы, второе реле - включает двигатель, опускающий шторы.
Правило следит за тем, чтобы оба реле не были включены одновременно.


In addition, the rule turns off the engines after a specified time after switching on.
Кроме этого, правило отключает двигатели спустя заданное время после включения.


<syntaxhighlight lang="ecmascript">
<syntaxhighlight lang="ecmascript">
Строка 336: Строка 338:




An older version of the same script demonstrates the use of aliases:
Более старая версия того же сценария демонстрирует использование alias-ов:
<syntaxhighlight lang="ecmascript">
<syntaxhighlight lang="ecmascript">


Строка 382: Строка 384:




=== System rules ===
=== Системные правила ===


Some rules come with the default rule system in the wb-rules-system package.  
Некоторые правила поставляются с системой правил по умолчанию в пакете wb-rules-system.  


Full list of rules is [https://github.com/wirenboard/wb-rules-system/tree/master/rules in the repository.].
Полный список правил [https://github.com/contactless/wb-rules-system/tree/master/rules в репозитории].


Some examples:
Некоторые примеры:






==== The rule for the buzzer====
==== Правило для пищалки ====


The [https://github.com/wirenboard/wb-rules-system/blob/master/rules/buzzer.js rule] creates a virtual buzzer device with sliders to adjust the volume and frequency, as well as a button to turn on the sound.
[https://github.com/contactless/wb-rules-system/blob/master/rules/buzzer.js Правило] создаёт виртуальное устройство buzzer с ползунками для регулировки громкости и частоты, а также кнопкой включения звука.




Строка 467: Строка 469:




==== Power status rule ====
==== Правило для статуса питания ====


[https://github.com/wirenboard/wb-rules-system/blob/master/rules/power_status.js The rule] creates a virtual device that reports the current power status. Two ADC channels are used as input data: battery voltage measurement and input voltage measurement.
[https://github.com/contactless/wb-rules-system/blob/master/rules/power_status.js Правило] создаёт виртуальное устройство, которое сообщает текущий статус питания. В качестве входных данных используется два канала АЦП: измерение напряжения на аккумуляторе и измерение входного напряжения.


The following logic is implemented:
Реализована следующая логика:


1. If the input voltage is less than the voltage on the battery, then the Board is powered by the battery. In this case, 0V is also displayed as the input voltage.
1. Если входное напряжение меньше напряжение на аккумуляторе, то значит плата питается от аккумулятора. В этом случае, также отображается 0V в качестве входного напряжения.


2. If the input voltage is greater than the battery voltage, the Board is powered by an external power supply. The measurement from the Vin channel is displayed as the input voltage.
2. Если входное напряжение  больше напряжения на аккумуляторе, то плата работает от внешнего источника питания. В качестве входонго напряжения отображается измерение с канала Vin.




To illustrate, the rules use two different ways of triggering: by changing the value of the control (rule _system_track_vin) and by changing the value of the expression (the other two).
Для иллюстрации правила используют два разных способа срабатывания: по изменению значения контрола (правило _system_track_vin) и по изменению значения выражения (два других).


<syntaxhighlight lang="ecmascript">
<syntaxhighlight lang="ecmascript">
Строка 537: Строка 539:




=== Sending commands via RS-485 ===
=== Отправка команд по RS-485 ===


For example, send a command to the device on the port /dev/ttys0 (corresponds to the hardware port RS-485-ISO on the [[Special:MyLanguage/Wiren Board 4|Wiren Board 4]]). To do this, we will use the rules engine and the ability to execute arbitrary shell commands. See [https://github.com/wirenboard/wb-rules#%D0%94%D1%80%D1%83%D0%B3%D0%B8%D0%B5-%D0%BF%D1%80%D0%B5%D0%B4%D0%BE%D0%BF%D1%80%D0%B5%D0%B4%D0%B5%D0%BB%D1%91%D0%BD%D0%BD%D1%8B%D0%B5-%D1%84%D1%83%D0%BD%D0%BA%D1%86%D0%B8%D0%B8-%D0%B8-%D0%BF%D0%B5%D1%80%D0%B5%D0%BC%D0%B5%D0%BD%D0%BD%D1%8B%D0%B5 documentation] for details.
Для примера отправим команду устройству на порт ''/dev/ttyNSC0'' (соответствует аппаратному порту RS-485-ISO на [[Special:MyLanguage/Wiren Board 4|Wiren Board 4]]).
Для этого будем использовать движок правил и возможность выполнения произвольных shell-команд. Подробнее см. [https://github.com/contactless/wb-rules#%D0%94%D1%80%D1%83%D0%B3%D0%B8%D0%B5-%D0%BF%D1%80%D0%B5%D0%B4%D0%BE%D0%BF%D1%80%D0%B5%D0%B4%D0%B5%D0%BB%D1%91%D0%BD%D0%BD%D1%8B%D0%B5-%D1%84%D1%83%D0%BD%D0%BA%D1%86%D0%B8%D0%B8-%D0%B8-%D0%BF%D0%B5%D1%80%D0%B5%D0%BC%D0%B5%D0%BD%D0%BD%D1%8B%D0%B5 в документации].


Create a virtual device with switch type control via rules engine.  
С помощью движка правил создадим виртуальное устройство с контролом типа switch (переключатель).  


When you turn on the switch a command will be sent: (Set Brightness ch. 00=0xff) for Uniel UCH-M141:
При включении переключателя будем отправлять команду (уст. Яркость кан. 00=0xff) для Uniel UCH-M141:
<pre>
<pre>
FF FF 0A 01 FF 00 00 0A
FF FF 0A 01 FF 00 00 0A
Строка 549: Строка 552:




When you turn off the switch  a command will be sent: (set channel brightness 00=0x00) for Uniel UCH-M141:
При выключении переключателя будем отправлять команду (уст. Яркость кан. 00=0x00) для Uniel UCH-M141:
<pre>
<pre>
FF FF 0A 01 00 00 00 0B
FF FF 0A 01 00 00 00 0B
Строка 558: Строка 561:




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


To configure the / dev/ttyNSC0 port to 9600 speed, run the following command
Для настройки порта /dev/ttyNSC0 на скорость 9600 надо выполнить следующую команду


<pre>
<pre>
Строка 566: Строка 569:
</pre>
</pre>


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


You can send data  by the following shell command:
Отправка данных делается следующей шелл-командой:


<pre>
<pre>
/usr/bin/printf '\xFF\xFF\x0A\x01\xD1\x06\x00\xE2' >/dev/ttyNSC0
/usr/bin/printf '\xFF\xFF\x0A\x01\xD1\x06\x00\xE2' >/dev/ttyNSC0
</pre>
</pre>
where "\xFF\xFF\x0A\x01\xD1\x06\x00\xE2" - is the entry of a "FF FF 0A 01 D1 06 00 E2" command.
где "\xFF\xFF\x0A\x01\xD1\x06\x00\xE2" - это запись команды "FF FF 0A 01 D1 06 00 E2".




3. Create the new rules file <code>/etc/wb-rules/rs485_cmd.js</code> in the rules engine
3. Создадим в движке правил новый файл с правилами <code>/etc/wb-rules/rs485_cmd.js</code>


The file can be edited with vim, nano, or mcedit in an ssh session on the device, or it can be downloaded with SCP.
Файл можно редактировать с помощью vim, nano или mcedit в сеансе ssh на устройстве, либо залить его с помощью SCP.


<pre>
<pre>
Строка 585: Строка 588:




4. Describe the virtual device in the file
4. Описываем в файле виртуальный девайс


<syntaxhighlight lang="ecmascript">
<syntaxhighlight lang="ecmascript">
Строка 600: Строка 603:




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


<pre>
<pre>
Строка 607: Строка 610:
</pre>
</pre>


There should be no error messages in the log (exit via control-c)
В логе не должно быть сообщений об ошибке (выход через control-c)




A new device "Send custom command to RS-485 port"should appear in the Devices section of the web interface.
В веб-интерфейсе в разделе Devices должно появиться новое устройство "Send custom command to RS-485 port".




6. Add a function to configure the port.
6. Добавим функцию для конфигурирования порта.




Строка 624: Строка 627:




7. Let's describe the rules for switching on and off the switch
7. Опишем правила на включение и выключение переключателя


<syntaxhighlight lang="ecmascript">
<syntaxhighlight lang="ecmascript">
Строка 648: Строка 651:




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








8. Putting it all together
7. Собираем всё вместе


Full contents of the rules file:
Полное содержимое файла с правилами:


<syntaxhighlight lang="ecmascript">
<syntaxhighlight lang="ecmascript">
Строка 691: Строка 694:
});
});


setTimeout(setup_port, 1000); // set setup_port() running 1 second after starting.
setTimeout(setup_port, 1000); // запланировать выполнение setup_port() через 1 секунду после старта правил.


</syntaxhighlight>
</syntaxhighlight>
Строка 697: Строка 700:




=== User fields in the interface ===
=== Пользовательские поля в интерфейсе ===


To enable the user to enter exact parameter values (setpoints) from the interface, you can use the [https://wirenboard.com/wiki/index.php/%D0%A1%D0%BE%D0%B7%D0%B4%D0%B0%D0%BD%D0%B8%D0%B5_%D1%80%D0%B5%D0%B4%D0%B0%D0%BA%D1%82%D0%B8%D1%80%D1%83%D0%B5%D0%BC%D1%8B%D1%85_%D0%BF%D0%BE%D0%BB%D1%8C%D0%B7%D0%BE%D0%B2%D0%B0%D1%82%D0%B5%D0%BB%D1%8C%D1%81%D0%BA%D0%B8%D1%85_%D0%BF%D0%BE%D0%BB%D0%B5%D0%B9_%D0%B2_%D0%B2%D0%B5%D0%B1-%D0%B8%D0%BD%D1%82%D0%B5%D1%80%D1%84%D0%B5%D0%B9%D1%81%D0%B5 instruction].
Чтобы дать пользователю возможность вводить точные значения параметров (уставки) из интерфейса, можно воспользоваться [https://wirenboard.com/wiki/index.php/%D0%A1%D0%BE%D0%B7%D0%B4%D0%B0%D0%BD%D0%B8%D0%B5_%D1%80%D0%B5%D0%B4%D0%B0%D0%BA%D1%82%D0%B8%D1%80%D1%83%D0%B5%D0%BC%D1%8B%D1%85_%D0%BF%D0%BE%D0%BB%D1%8C%D0%B7%D0%BE%D0%B2%D0%B0%D1%82%D0%B5%D0%BB%D1%8C%D1%81%D0%BA%D0%B8%D1%85_%D0%BF%D0%BE%D0%BB%D0%B5%D0%B9_%D0%B2_%D0%B2%D0%B5%D0%B1-%D0%B8%D0%BD%D1%82%D0%B5%D1%80%D1%84%D0%B5%D0%B9%D1%81%D0%B5 инструкцией].


More detailed and with example - in the topic on the technical support [https://support.wirenboard.com/t/kak-na-wb5-wb6-sozdat-pole-dlya-vvoda-ustavok-i-peredat-znachenie-v-pravila/2180 forum].
Более подробно и с примером - в [https://support.wirenboard.com/t/kak-na-wb5-wb6-sozdat-pole-dlya-vvoda-ustavok-i-peredat-znachenie-v-pravila/2180 теме на портале техподдержки].






=== Complex scheduled rules ===
=== Сложные правила с расписаниями ===


Object - grocery store. Various store systems are controlled by feedback from temperature sensors and taking into account the schedule of the store.
Объект - продуктовый магазин. Различные системы магазина управляются по обратной связи от датчиков температуры и с учётом расписания работы магазина.


Not cron-rules are used for schedules, but  the libschedule. The libschedule enables and disables rules, which, unlike cron rules, are executed continuously when enabled.
Для расписаний используются не cron-правила, а обёртка над ними. Обёртка включает и выключает правила, которые, в отличие от cron-правил, выполняются постоянно, будучи включенными.


For example, we want the lighting to be on from 10 to 17h. The libschedule will follow the "turn on the lights" rule once a minute from 10 am to 17 PM.
Например, мы хотим, чтобы освещение было включено с 10 до 17ч. Обёртка (libschedule) будет выполнять правило "включить освещение" раз в минуту с 10 утра до 17 вечера.


This means that even if the controller works intermittently and missed the transition time between schedules (10 am), the controller will still turn on the lighting as soon as possible.
Это значит, что даже если контроллер работает с перерывами и пропустил время перехода между расписаниями (10 утра), то контроллер всё равно включит освещение при первой возможности.




Строка 721: Строка 724:
global.__proto__.Schedules = {};
global.__proto__.Schedules = {};


(function(Schedules) { // closing
(function(Schedules) { // замыкание


   function todayAt(now, hours, minutes) {
   function todayAt(now, hours, minutes) {
Строка 861: Строка 864:
</syntaxhighlight>
</syntaxhighlight>


An example of a rule using Schedules:
Пример правил, с использованием Schedules:
<syntaxhighlight lang="ecmascript">
<syntaxhighlight lang="ecmascript">
(function() { // closing
(function() { // замыкание


   defineAlias("countersTemperature", "wb-msw2_30/Temperature");
   defineAlias("countersTemperature", "wb-msw2_30/Temperature");
Строка 915: Строка 918:
   Schedules.initSchedules();
   Schedules.initSchedules();


   // signboard and facade illumination
   // Вывеска и фасадное освещение
   defineRule("signboardOnOff", {
   defineRule("signboardOnOff", {
     when: function() {
     when: function() {
Строка 931: Строка 934:




   // sales area illumination
   // Освещение торгового зала
   defineRule("lightingFrontshopOnOff", {
   defineRule("lightingFrontshopOnOff", {
     when: function() {
     when: function() {
Строка 938: Строка 941:
     then: function (newValue, devName, cellName) {
     then: function (newValue, devName, cellName) {
       log("lightingFrontshopOnOff  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
       log("lightingFrontshopOnOff  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
       dev["wb-gpio/EXT1_R3A1"] = ! dev._schedules.frontshop_lighting; //inverted contactor
       dev["wb-gpio/EXT1_R3A1"] = ! dev._schedules.frontshop_lighting; //инвертированный контактор
     }
     }
   });
   });




   // backstoreroom ventilation
   // Вентиляция подсобного помещения
   defineRule("ventBackstoreOnOff", {
   defineRule("ventBackstoreOnOff", {
     when: function() {
     when: function() {
Строка 951: Строка 954:
       log("ventBackstoreOnOff  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
       log("ventBackstoreOnOff  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
       var on = dev._schedules.ext_working_hours_15m;
       var on = dev._schedules.ext_working_hours_15m;
       dev["wb-mr6c_81/K1"] = ! on; //inverted contactor
       dev["wb-mr6c_81/K1"] = ! on; //инвертированный контактор
       dev["wb-mr6c_81/K5"] = ! on; //inverted contactor
       dev["wb-mr6c_81/K5"] = ! on; //инвертированный контактор
     }
     }
   });
   });


   // Freezer showcase illumination
   // Освещение холодильных горок
   defineRule("lightingCoolingshelfsOnOff", {
   defineRule("lightingCoolingshelfsOnOff", {
     when: function() {
     when: function() {
Строка 965: Строка 968:
       var on = dev._schedules.working_hours_15m;
       var on = dev._schedules.working_hours_15m;


       //  
       // освещение в горках через нормально-закрытые реле (инвертировано)
the lighting in the freezer showcases via the normally-closed relays (inverted)
       dev["wb-mrm2-old_60/Relay 1"] = !on;
       dev["wb-mrm2-old_60/Relay 1"] = !on;
       dev["wb-mrm2-old_61/Relay 1"] = !on;
       dev["wb-mrm2-old_61/Relay 1"] = !on;
Строка 979: Строка 981:




   //Display fridges
   //Брендовые холодильники (пиво, лимонады)
   defineRule("powerBrandFridgesOnOff", {
   defineRule("powerBrandFridgesOnOff", {
     when: function() {
     when: function() {
Строка 988: Строка 990:
       var on = dev._schedules.working_hours;
       var on = dev._schedules.working_hours;
        
        
       dev["wb-gpio/EXT1_R3A5"] = !on; // inverted
       dev["wb-gpio/EXT1_R3A5"] = !on; // инвертировано
     }
     }
   });
   });




   // ========= Boilers and supply ventilation ===========
   // ========= Котлы и приточная вентиляция ТЗ  ===========
   // feedback by the temperature of the vegetable  zone
   // обратная связь по температуре овощной зоны


   // position controller works daily
   // днём работает позиционный регулятор
   defineRule("heatersDayOff", {
   defineRule("heatersDayOff", {
     when: function() {
     when: function() {
Строка 1003: Строка 1005:
     then: function (newValue, devName, cellName) {
     then: function (newValue, devName, cellName) {
       log("heatersDayOff  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
       log("heatersDayOff  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
       heater1EnableInverted = !false; // inverted
       heater1EnableInverted = !false; // инвертировано
     }
     }
   });
   });
Строка 1013: Строка 1015:
     then: function (newValue, devName, cellName) {
     then: function (newValue, devName, cellName) {
       log("heatersDayOn  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
       log("heatersDayOn  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
       heater1EnableInverted = !true; // inverted
       heater1EnableInverted = !true; // инвертировано
     }
     }
   });
   });


   // position controller works at night
   // ночью работает позиционный регулятор
   defineRule("heatersNightOff", {
   defineRule("heatersNightOff", {
     when: function() {
     when: function() {
Строка 1024: Строка 1026:
     then: function (newValue, devName, cellName) {
     then: function (newValue, devName, cellName) {
       log("heatersNightOff  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
       log("heatersNightOff  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
       heater1EnableInverted = !false; // inverted
       heater1EnableInverted = !false; // инвертировано
     }
     }
   });
   });
Строка 1034: Строка 1036:
     then: function (newValue, devName, cellName) {
     then: function (newValue, devName, cellName) {
       log("heatersNightOn  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
       log("heatersNightOn  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
       heater1EnableInverted = !true; // inverted
       heater1EnableInverted = !true; // инвертировано
     }
     }
   });
   });




   // supply and exhaust ventilation are forcibly switched off
   // приточная и вытяжная вентиляция принудительно выключены
    
    
   defineRule("ventFrontshopAlwaysOff", {
   defineRule("ventFrontshopAlwaysOff", {
Строка 1050: Строка 1052:
    
    
    
    
   // ==================  The cash register area =================
   // ==================  Кассовая зона =================


   // in the cash area during working hours the temperature is maintained by air conditioning (position controller)
   // в кассовой зоне в рабочее время температура поддерживается кондиционерами (позиционный регулятор)


   defineRule("countersACOn", {
   defineRule("countersACOn", {
Строка 1060: Строка 1062:
     then: function (newValue, devName, cellName) {
     then: function (newValue, devName, cellName) {
       log("countersACOn  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
       log("countersACOn  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
       dev["wb-mir_75/Play from ROM7"] = true; // air conditioning cash area for heating
       dev["wb-mir_75/Play from ROM7"] = true; // кондиционер кассовой зоны на нагрев
     }
     }
   });
   });


   // after working hours, the air conditioning is off
   // в нерабочее время кондиционер выключен
   defineRule("countersACOff", {
   defineRule("countersACOff", {
     when: function() {
     when: function() {
Строка 1071: Строка 1073:
     then: function (newValue, devName, cellName) {
     then: function (newValue, devName, cellName) {
       log("countersACOff  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
       log("countersACOff  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
       dev["wb-mir_75/Play from ROM2"] = true; // shut down air conditioning cash area
       dev["wb-mir_75/Play from ROM2"] = true; // кондиционер кассовой зоны выключить
     }
     }
   });
   });


   // =============== Vegetable zone ==============
   // =============== Овощная зона ==============
   // Cooling vegetables air-conditioned only when the air temperature is above 18.5 C  
   // Охлаждение овощей кондиционером только при температуре воздуха выше 18.5C  


   defineRule("acVegOn", {
   defineRule("acVegOn", {
Строка 1084: Строка 1086:
     then: function (newValue, devName, cellName) {
     then: function (newValue, devName, cellName) {
       log("acVegOn  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
       log("acVegOn  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
       dev["wb-mir_76/Play from ROM3"] = true; // Cooling +18
       dev["wb-mir_76/Play from ROM3"] = true; // Охлаждение +18
     }
     }
   });
   });
Строка 1094: Строка 1096:
     then: function (newValue, devName, cellName) {
     then: function (newValue, devName, cellName) {
       log("acVegOff  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
       log("acVegOff  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
       dev["wb-mir_76/Play from ROM2"] = true; // shutdown
       dev["wb-mir_76/Play from ROM2"] = true; // выключить
     }
     }
   });
   });
Строка 1102: Строка 1104:




== A full description of the capabilities of the rules engine ==
== Полное описание возможностей движка правил ==


The most complete description of the rules engine: https://github.com/wirenboard/wb-rules/blob/master/README.md
Самое полное описание движка правил: https://github.com/contactless/wb-rules/blob/master/README.md






== What's new in the latest versions ==
== Новые возможности последних версий ==


* [[Special:MyLanguage/Движок_правил_wb-rules_1.7/en|wb-rules 1.7 rules engine]]
* [[Special:MyLanguage/Движок_правил_wb-rules_1.7|Движок правил wb-rules 1.7]]






== under development ==
== В разработке ==


A description of the features of future versions of the rules engine can be found here:
Описание возможностей будущих версий движка правил можно прочесть здесь:
* [[Special:MyLanguage/Движок_правил_wb-rules_2.0/en|Rules engine wb-rules 2.0]]
* [[Special:MyLanguage/Движок_правил_wb-rules_2.0|Движок правил wb-rules 2.0]]

Версия 17:44, 3 июня 2019

Editing rules in web interface
You can write rules for the controller, for example: "If the sensor temperature is less than 18°C, turn on the heater." Rules are created via web interface and written in a simple language similar to Javascript.

Самое полное описание движка правил: https://github.com/contactless/wb-rules


Как создавать и редактировать правила

  • Список файлов с правилами находится на странице Scripts веб-интерфейса.
  • Нажмите на название файла, чтобы открыть его для редактирования.
    • Чтобы создать новый файл, нажмите на пункт New..., вверху введите название скрипта (используйте для названия только латинские буквы и цифры, в качестве расширения укажите .js), в основное поле введите текст скрипта, затем нажмите кнопку Save вверху.
  • Правило начинает работать автоматически после нажатия кнопки Save, если в нём нет ошибок (смотрите ниже).

Примечания:

  1. Файлы с правилами хранятся на контроллере в виде обычных текстовых файлов в папке /etc/wb-rules/, поэтому их можно редактировать и загружать напрямую с компьютера.
  2. Правила исполняются сервисом wb-rules, документацию по нему смотрите странице сервиса в Github.


Пишем первое правило

Правило для управления обогревателем, записанное через веб-интерфейс

Правила бывают двух типов - непосредственно правила (начинаются со слов defineRule) и виртуальные устройства (начинаются со слов defineVirtualDevice). Виртуальные устройства - это появляющиеся в веб-интерфейсе новые элементы управления - например, кнопка-выключатель, которая на самом деле выключает два устройства одновременно. Она не привязана напрямую ни к какому физическому устройству, а действия при её нажатии определяются написанным вами скриптом.

Любое количество разных правил можно хранить в одном файле. Обычно в одном файле хранятся правила, отвечающие за близкие функции.


Первое правило

Для начала разберём простое правило - при превышении температуры выключи обогреватель. Температуру получаем с датчика 1-Wire, обогреватель подключён к Реле 1 внешнего релейного модуля WB-MRM2.

defineRule("heater_control", { //название правила - "контроль обогревателя", может быть произвольным
  whenChanged: "wb-w1/28-0115a48fcfff", //при изменении состояния датчика 1-Wire с идентификатором 28-0115a48fcfff
  then: function (newValue, devName, cellName) { //выполняй следующие действия
    if ( newValue > 30) { //если температура датчика больше 30 градусов
      dev["wb-mrm2_130"]["Relay 1"] = 0; //установи Реле 1 модуля WB-MRM2 с адресом 130 в состояние "выключено"
    } else {
      dev["wb-mrm2_130"]["Relay 1"] = 1; //установи Реле 1 модуля WB-MRM2 с адресом 130 в состояние "включено"
    }
  }
});
  • Первая строка - кодовое слово defineRule и название правила
  • Вторая строка - кодовое слово для определения, когда выполняется правило, - whenChanged - "при изменении параметра", далее название параметра, при изменении которого запустится правило - температура с датчика 1-Wire. Название параметра записывается в виде "Device/Control", где названия Device и Control для каждого параметра можно найти на странице Settings веб-интерфейса в таблице MQTT Channels.
  • Третья строка - начало функции, которая будет исполняться
  • Затем идёт условие - "если значение температуры больше порогового, то ...". Значение параметра записывается в виде dev[Device][Control] - заметьте, оно отличается от вида записи параметра, при изменении которого запускается правило, потому что там речь идёт о параметре, а здесь - о значении того же параметра.
  • Затем мы выставляем значения для реле в каждом случае - 0 - "выключено", 1 - "включено". Названия Device и Control для реле смотрим всё в той же таблице MQTT Channels на странице Settings веб-интерфейса.


Первое правило с виртуальным устройством

Создаём виртуальный переключатель, при нажатии на который переключаются сразу два реле.

defineVirtualDevice("switch_both", {
    title: "Switch both relays",
    cells: {
	enabled: {
	    type: "switch",
	    value: false
	},
    }
});

defineRule("control_both", {
  whenChanged: "switch_both/enabled",
  then: function (newValue, devName, cellName)  {
	dev["wb-mrm2_130"]["Relay 1"] = newValue;
    dev["wb-mrm2_130"]["Relay 2"] = newValue;
  }
});


Пишем сложные правила

Чтобы начать писать сложные правила, нужно посмотреть примеры правил и полную документацию по движку правил:

  1. Примеры правил:
  2. Полное описание движка правил.


Примеры правил

Слежение за контролом

Это простейшее правило следит за контролом и устанавливает другой контрол в такое же состояние.

Например правило может включать сирену и лампу, если датчик движения заметил движение.

В примере датчик движения подключен к входу "сухой контакт", контрол типа "switch". Сирена подключена к встроеному реле Wiren Board, а лампа - через релейный блок по Modbus. Когда вход типа "сухой контакт" (выход датчика движения) замкнут, то на лампу и реле подаётся "1", когда выключен - "0".


Правило срабатывает каждый раз при изменении значения контрола "D1_IN" у устройства "wb-gpio". В код правила передаётся новое значение этого контрола в виде переменной newValue.

defineRule("motion_detector", {
  whenChanged: "wb-gpio/D1_IN",
  then: function (newValue, devName, cellName) {
	dev["wb-gpio"]["Relay_2"] = newValue;
	dev["wb-mrm2_6"]["Relay 1"] = newValue;

  }
});


То же самое, но с виртуальным девайсом в качестве источника событий. Пример использования: сценарная кнопка, которая включает/выключает сирену и лампочку.

defineVirtualDevice("simple_test", {
    title: "Simple switch",
    cells: {
	enabled: {
	    type: "switch",
	    value: false
	},
    }
});


defineRule("simple_switch", {
  whenChanged: "simple_test/enabled",
  then: function (newValue, devName, cellName) {
	dev["wb-gpio"]["Relay_2"] = newValue;
	dev["wb-mrm2_6"]["Relay 1"] = newValue;

  }
});


Детектор движения c таймаутом

На вход D2 подключен детектор движения с выходом типа "сухой контакт", который замыкает D2 и GND при обнаружении движения. При этом, на канале "wb-gpio/D2_IN" появляется статус "1".

Правило включает свет при обнаружении движения и выключает свет, спустя 30 секунд после пропадания сигнала с датчика движения.

Освещение подключено через встроенное реле, канал wb-gpio/Relay_1.

var motion_timer_1_timeout_ms = 30 * 1000;
var motion_timer_1_id = null;

defineRule("motion_detector_1", {
  whenChanged: "wb-gpio/D2_IN",
  then: function (newValue, devName, cellName) {
    if (newValue) {
        dev["wb-gpio"]["Relay_1"] = 1;

      	if (motion_timer_1_id) {
          clearTimeout(motion_timer_1_id);
       }
      
        motion_timer_1_id = setTimeout(function () {
   		   dev["wb-gpio"]["Relay_1"] = 0;         
                   motion_timer_1_id = null;   
       	}, motion_timer_1_timeout_ms);           	
    }
   }
});


Создание однотипных правил

Если таких детекторов движения нужно несколько, то, чтобы не копировать код, можно обернуть создание правила и переменных в функцию:

 function makeMotionDetector(name, timeout_ms, detector_control, relay_control) {
  var motion_timer_id = null;
  defineRule(name, {
      whenChanged: "wb-gpio/" + detector_control,
      then: function(newValue, devName, cellName) {
          if (!newValue) {
              dev["wb-gpio"][relay_control] = 1;
              if (motion_timer_id) {
                  clearTimeout(motion_timer_id);
              }

              motion_timer_id = setTimeout(function() {
                  dev["wb-gpio"][relay_control] = 0;
                  motion_timer_id = null;
              }, timeout_ms);
          }
      }
  });
}

makeMotionDetector("motion_detector_1", 20000, "EXT1_DR1", "EXT2_R3A1");
makeMotionDetector("motion_detector_2", 10000, "EXT1_DR2", "EXT2_R3A2");
makeMotionDetector("motion_detector_3", 10000, "EXT1_DR3", "EXT2_R3A3");


Активация правила только в определённое время

Правило как в предыдущем разделе, но выполняется только с 9:30 до 17:10 по UTC.

var motion_timer_1_timeout_ms = 5 * 1000;
var motion_timer_1_id = null;
 
defineRule("motion_detector_1", {
  whenChanged: "wb-gpio/A1_IN",
  then: function (newValue, devName, cellName) {
    var date = new Date();

    // time point marking the beginning of the interval
    // i.e. "today, at HH:MM". All dates are in UTC!
    var date_start = new Date(date);
    date_start.setHours(9);
    date_start.setMinutes(30);

    // time point marking the end of the interval
    var date_end = new Date(date);
    date_end.setHours(17);
    date_end.setMinutes(10);
    
    // if time is between 09:30 and 17:10 UTC
    if ((date > date_start) && (date < date_end)) {
      if (newValue) {
          dev["wb-gpio"]["EXT1_R3A1"] = 1;
   
          if (motion_timer_1_id) {
            clearTimeout(motion_timer_1_id);
         }
   
          motion_timer_1_id = setTimeout(function () {
             dev["wb-gpio"]["EXT1_R3A1"] = 0;            
             motion_timer_1_id = null;
          }, motion_timer_1_timeout_ms);              
      }
     }
   }
});


Роллеты

Одно реле включает двигатель, поднимающий шторы, второе реле - включает двигатель, опускающий шторы. Правило следит за тем, чтобы оба реле не были включены одновременно.

Кроме этого, правило отключает двигатели спустя заданное время после включения.

(function() { //don't touch this line
  
  var suffix = "1"; // must be different in different JS files
 
  var relay_up_device = "lc103_4";
  var relay_up_control = "Relay 1";

  var relay_down_device = "lc103_4";
  var relay_down_control = "Relay 2";

  var timeout_s = 15;
  
  // End of settings
  
  
  var relay_up_timer_id = null;
  var relay_down_timer_id = null;
  
  defineRule( "roller_shutter_up_on" + suffix, {
   asSoonAs: function() {
     return dev[relay_up_device][relay_up_control];
   },
    then: function () {
      if (relay_up_timer_id) {
        relay_up_timer_id = clearTimeout(relay_up_timer_id); 
      };

      relay_up_timer_id = setTimeout(function() {
        return dev[relay_up_device][relay_up_control] = 0;
      }, timeout_s * 1000);
    }
  });

  defineRule("roller_shutter_down_on" + suffix, {
    asSoonAs: function() {
      return dev[relay_down_device][relay_down_control];
    },
    then: function () {
      if (relay_down_timer_id) {
        relay_down_timer_id = clearTimeout(relay_down_timer_id); 
      };
      
      relay_down_timer_id = setTimeout(function() {
        dev[relay_down_device][relay_down_control] = 0;
      }, timeout_s * 1000);
    }
  });

  defineRule("roller_shutter_both_on" + suffix, {
    asSoonAs: function() {
      return dev[relay_up_device][relay_up_control] && dev[relay_down_device][relay_down_control];
    },
    then: function () {
      if (relay_up_timer_id) {
        relay_up_timer_id = clearTimeout(relay_up_timer_id); 
      };

      if (relay_down_timer_id) {
        relay_down_timer_id = clearTimeout(relay_down_timer_id); 
      };

      
      dev[relay_up_device][relay_up_control] = 0;
      dev[relay_down_device][relay_down_control] = 0;
      log("Both roller shutter relays on, switching them off");
    }
  });
})();


Более старая версия того же сценария демонстрирует использование alias-ов:

(function() {
  defineAlias("relay_up_1", "lc103_4/Relay 1");
  defineAlias("relay_down_1", "lc103_4/Relay 2");
  var timeout_s = 15;

  defineRule("roller_shutter_1_up_on", {
   asSoonAs: function() {
     return relay_up_1;
   },
    then: function () {
      setTimeout(function() {
        relay_up_1 = 0;
      }, timeout_s * 1000);
    }
  });

  defineRule("roller_shutter_1_down_on", {
    asSoonAs: function() {
      return relay_down_1;
    },
    then: function () {
      setTimeout(function() {
        relay_down_1 = 0;
      }, timeout_s * 1000);
    }
  });

  defineRule("roller_shutter_1_both_on", {
    asSoonAs: function() {
      return relay_up_1 && relay_down_1;
    },
    then: function () {
      relay_up_1 = 0;
      relay_down_1 = 0;
      log("Both roller shutter relays on, switching them off");
    }
  });
})();


Системные правила

Некоторые правила поставляются с системой правил по умолчанию в пакете wb-rules-system.

Полный список правил в репозитории.

Некоторые примеры:


Правило для пищалки

Правило создаёт виртуальное устройство buzzer с ползунками для регулировки громкости и частоты, а также кнопкой включения звука.


defineVirtualDevice("buzzer", {
  title: "Buzzer", //

  cells: {
    frequency : {
        type : "range",
        value : 3000,
        max : 7000,
    },
    volume : {
        type : "range",
        value : 10,
        max : 100,
    },
    enabled : {
        type : "switch",
        value : false,
    },
  }
});


// setup pwm2
runShellCommand("echo 2 > /sys/class/pwm/pwmchip0/export");



function _buzzer_set_params() {
        var period = parseInt(1.0 / dev.buzzer.frequency * 1E9);
        var duty_cycle = parseInt(dev.buzzer.volume  * 1.0  / 100 * period * 0.5);


        runShellCommand("echo " + period + " > /sys/class/pwm/pwmchip0/pwm2/period");
        runShellCommand("echo " + duty_cycle + " > /sys/class/pwm/pwmchip0/pwm2/duty_cycle");
};


defineRule("_system_buzzer_params", {
  whenChanged: [
    "buzzer/frequency",
    "buzzer/volume",
    ],

  then: function (newValue, devName, cellName) {
    if ( dev.buzzer.enabled) {
        _buzzer_set_params();
    }
  }
});


defineRule("_system_buzzer_onof", {
  whenChanged: "buzzer/enabled",
  then: function (newValue, devName, cellName) {
    if ( dev.buzzer.enabled) {
        _buzzer_set_params();
        runShellCommand("echo 1  > /sys/class/pwm/pwmchip0/pwm2/enable");
    } else {
        runShellCommand("echo 0  > /sys/class/pwm/pwmchip0/pwm2/enable");
    }
   }
});


Правило для статуса питания

Правило создаёт виртуальное устройство, которое сообщает текущий статус питания. В качестве входных данных используется два канала АЦП: измерение напряжения на аккумуляторе и измерение входного напряжения.

Реализована следующая логика:

1. Если входное напряжение меньше напряжение на аккумуляторе, то значит плата питается от аккумулятора. В этом случае, также отображается 0V в качестве входного напряжения.

2. Если входное напряжение больше напряжения на аккумуляторе, то плата работает от внешнего источника питания. В качестве входонго напряжения отображается измерение с канала Vin.


Для иллюстрации правила используют два разных способа срабатывания: по изменению значения контрола (правило _system_track_vin) и по изменению значения выражения (два других).

defineVirtualDevice("power_status", {
  title: "Power status", //

  cells: {
    'working on battery' : {
        type : "switch",
        value : false,
        readonly : true
    },
    'Vin' : {
        type : "voltage",
        value : 0
    }


  }
});



defineRule("_system_track_vin", {
    whenChanged: "wb-adc/Vin",
    then: function() {
        if (dev["wb-adc"]["Vin"] < dev["wb-adc"]["BAT"] ) {
            dev["power_status"]["Vin"] = 0;
        } else {
            dev["power_status"]["Vin"] = dev["wb-adc"]["Vin"] ;
        }
    }
});



defineRule("_system_dc_on", {
  asSoonAs: function () {
    return  dev["wb-adc"]["Vin"] > dev["wb-adc"]["BAT"];
  },
  then: function () {
    dev["power_status"]["working on battery"] = false;
  }
});

defineRule("_system_dc_off", {
  asSoonAs: function () {
    return  dev["wb-adc"]["Vin"] <= dev["wb-adc"]["BAT"];
  },
  then: function () {
    dev["power_status"]["working on battery"] = true;
  }
});


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

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

С помощью движка правил создадим виртуальное устройство с контролом типа 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. Собираем всё вместе

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

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

setTimeout(setup_port, 1000); // запланировать выполнение setup_port() через 1 секунду после старта правил.


Пользовательские поля в интерфейсе

Чтобы дать пользователю возможность вводить точные значения параметров (уставки) из интерфейса, можно воспользоваться инструкцией.

Более подробно и с примером - в теме на портале техподдержки.


Сложные правила с расписаниями

Объект - продуктовый магазин. Различные системы магазина управляются по обратной связи от датчиков температуры и с учётом расписания работы магазина.

Для расписаний используются не cron-правила, а обёртка над ними. Обёртка включает и выключает правила, которые, в отличие от cron-правил, выполняются постоянно, будучи включенными.

Например, мы хотим, чтобы освещение было включено с 10 до 17ч. Обёртка (libschedule) будет выполнять правило "включить освещение" раз в минуту с 10 утра до 17 вечера.

Это значит, что даже если контроллер работает с перерывами и пропустил время перехода между расписаниями (10 утра), то контроллер всё равно включит освещение при первой возможности.


lib_schedules.js:

global.__proto__.Schedules = {};

(function(Schedules) { // замыкание

  function todayAt(now, hours, minutes) {
    var date = new Date(now);
    // i.e. "today, at HH:MM". All dates are in UTC!
    date.setHours(hours);
    date.setMinutes(minutes);
    return date;
  }


  function checkScheduleInterval(now, start_time, end_time) {
    var start_date = todayAt(now, start_time[0], start_time[1]);
    var end_date = todayAt(now, end_time[0], end_time[1]);
    log("checkScheduleInterval {} {} {}".format(now, start_date, end_date));

    if (end_date >= start_date) {
      if ((now >= start_date) && (now < end_date)) {
        return true;
      }
    } else {
      // end date is less than start date, 
      // assuming they belong to a different days (e.g. today and tomorrow)

      // option 1: what if it's now the day of "end" date?
      // in this case the following is enough:
      if (now < end_date) {
        return true;
      }

      // well, that seems not to be the case. ok,
      // option 2: it's the day of "start" date:

      if (now >= start_date) {
        return true;
      }
    }
    return false;

  }

  function checkSchedule(schedule, now) {
    if (now == undefined) {
      now = new Date();
    }

    for (var i = 0; i < schedule.intervals.length; ++i) {
      var item = schedule.intervals[i];
      if (checkScheduleInterval(now, item[0], item[1])) {
        log("found matching schedule interval at {}".format(item));
        return true;
      }
    }
    return false;
  }
  
  function updateSingleScheduleDevStatus(schedule) {
    log("updateSingleScheduleDevStatus {}".format(schedule.name));
    dev["_schedules"][schedule.name] = checkSchedule(schedule);
  };

  function addScheduleDevCronTasks(schedule) {
    for (var i = 0; i < schedule.intervals.length; ++i) {
      var interval = schedule.intervals[i];
      for (var j = 0; j < 2; ++j) { // either start or end of the interval
        var hours = interval[j][0]; 
        var minutes = interval[j][1];
        log("cron at " + "0 " + minutes + " " + hours + " * * *");
        defineRule("_schedule_dev_{}_{}_{}".format(schedule.name, i, j), {
          when: cron("0 " + minutes + " " + hours + " * * *"),
          then: function () {
            log("_schedule_dev_ {}_{}_{}".format(schedule.name, i, j));
            updateSingleScheduleDevStatus(schedule);
          }
        });
      }
    }    
  }

  function addScheduleAutoUpdCronTask(schedule) {
    defineRule("_schedule_auto_upd_{}".format(schedule.name), {
      when: cron("@every " + schedule.autoUpdate),
      then: function() {
        dev._schedules[schedule.name] = dev._schedules[schedule.name];
      }
    });
  }

  var _schedules = {};

  Schedules.registerSchedule = function(schedule) {
    _schedules[schedule.name] = schedule;
  };

  Schedules.initSchedules = function() {
    var params = {
      title: "Schedule Status", 
      cells: {}
    };

    for (var schedule_name in _schedules) {
      if (_schedules.hasOwnProperty(schedule_name)) {
        var schedule = _schedules[schedule_name];
        params.cells[schedule_name] = {type: "switch", value: false, readonly: true};
      }
    };

    defineVirtualDevice("_schedules", params);



    for (var schedule_name in _schedules) {
      if (_schedules.hasOwnProperty(schedule_name)) {
        var schedule = _schedules[schedule_name];

        // setup cron tasks which updates the schedule dev status at schedule
        //   interval beginings and ends
        addScheduleDevCronTasks(schedule);

        // if needed, setup periodic task to trigger rules which use this schedule
        if (schedule.autoUpdate) {
          addScheduleAutoUpdCronTask(schedule);
        }

        // set schedule dev status as soon as possible at startup
        (function(schedule) {
          setTimeout(function() {
            updateSingleScheduleDevStatus(schedule);
          }, 1);
        })(schedule);

      };
    };

    
  };

})(Schedules);

Пример правил, с использованием Schedules:

(function() { // замыкание

  defineAlias("countersTemperature", "wb-msw2_30/Temperature");
  defineAlias("vegetablesTemperature", "wb-msw2_31/Temperature");

  defineAlias("heater1EnableInverted", "wb-mrm2-old_70/Relay 1");
  defineAlias("frontshopVentInverted", "wb-gpio/EXT1_R3A3");

  Schedules.registerSchedule({
    "name" : "signboard", // вывеска
    "autoUpdate" : "1m",
    "intervals" : [
      [ [12, 30], [20, 30] ],  // в UTC, 15:30 - 23:30 MSK
      [ [3, 30], [5, 20] ],  // в UTC, 6:30 - 8:20 MSK
    ]
  });
  Schedules.registerSchedule({
    "name" : "ext_working_hours_15m",
    "autoUpdate" : "1m",
    "intervals" : [
      [ [4, 45], [20, 15] ],  // всё ещё UTC, 7:45 - 23:15 MSK
    ]
  });
  Schedules.registerSchedule({
    "name" : "working_hours",
    "autoUpdate" : "1m",
    "intervals" : [
      [ [5, 0], [19, 0] ],  // всё ещё UTC, 8:00 - 22:00 MSK
    ]
  });
  Schedules.registerSchedule({
    "name" : "working_hours_15m",
    "autoUpdate" : "1m",
    "intervals" : [
      [ [4, 45], [19, 15] ],  // всё ещё UTC, 7:45 - 22:15 MSK
    ]
  });
  Schedules.registerSchedule({
    "name" : "frontshop_lighting",
    "autoUpdate" : "1m",
    "intervals" : [
      [ [4, 20], [20, 45] ],  // всё ещё UTC, 7:20 -23:45 MSK
    ]
  });
  Schedules.registerSchedule({
    "name" : "heaters_schedule",
    "intervals" : [
       [ [4, 0], [17, 0] ],  // всё ещё UTC, 07:00 - 20:00 MSK дневной режим
    ]
  });
  Schedules.initSchedules();

  // Вывеска и фасадное освещение
  defineRule("signboardOnOff", {
    when: function() {
      return dev._schedules.signboard || true;
    },
    then: function (newValue, devName, cellName) {
      log("signboardOnOff  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
      var on = dev._schedules.signboard; //
      
      dev["wb-mr6c_80/K2"] = !on;
      dev["wb-mr6c_80/K1"] = !on;
      dev["wb-mr6c_80/K3"] = !on;
    }
  });


  // Освещение торгового зала
  defineRule("lightingFrontshopOnOff", {
    when: function() {
      return dev._schedules.frontshop_lighting || true;
    },
    then: function (newValue, devName, cellName) {
      log("lightingFrontshopOnOff  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
      dev["wb-gpio/EXT1_R3A1"] = ! dev._schedules.frontshop_lighting; //инвертированный контактор
    }
  });


  // Вентиляция подсобного помещения
  defineRule("ventBackstoreOnOff", {
    when: function() {
      return dev._schedules.ext_working_hours_15m || true;
    },
    then: function (newValue, devName, cellName) {
      log("ventBackstoreOnOff  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
      var on = dev._schedules.ext_working_hours_15m;
      dev["wb-mr6c_81/K1"] = ! on; //инвертированный контактор
      dev["wb-mr6c_81/K5"] = ! on; //инвертированный контактор
    }
  });

  // Освещение холодильных горок
  defineRule("lightingCoolingshelfsOnOff", {
    when: function() {
      return dev._schedules.frontshop_lighting || true;
    },
    then: function (newValue, devName, cellName) {
      log("lightingCoolingshelfsOnOff  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
      var on = dev._schedules.working_hours_15m;

      // освещение в горках через нормально-закрытые реле (инвертировано)
      dev["wb-mrm2-old_60/Relay 1"] = !on;
      dev["wb-mrm2-old_61/Relay 1"] = !on;
      dev["wb-mrm2-old_62/Relay 1"] = !on;
      dev["wb-mrm2-old_63/Relay 1"] = !on;
      dev["wb-mrm2-old_64/Relay 1"] = !on;
      dev["wb-mrm2-old_65/Relay 1"] = !on;
      dev["wb-mrm2-old_66/Relay 1"] = !on;
      dev["wb-mrm2-old_67/Relay 1"] = !on;
    }
  });


  //Брендовые холодильники (пиво, лимонады)
  defineRule("powerBrandFridgesOnOff", {
    when: function() {
      return dev._schedules.working_hours || true;
    },
    then: function (newValue, devName, cellName) {
      log("powerBrandFridgesOnOff  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
      var on = dev._schedules.working_hours;
      
      dev["wb-gpio/EXT1_R3A5"] = !on; // инвертировано
    }
  });


  // ========= Котлы и приточная вентиляция ТЗ  ===========
  // обратная связь по температуре овощной зоны

  // днём работает позиционный регулятор
  defineRule("heatersDayOff", {
    when: function() {
      return (dev._schedules.heaters_schedule) && (vegetablesTemperature > 17.0);
    },
    then: function (newValue, devName, cellName) {
      log("heatersDayOff  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
      heater1EnableInverted = !false; // инвертировано
    }
  });

  defineRule("heatersDayOn", {
    when: function() {
      return (dev._schedules.heaters_schedule) && (vegetablesTemperature < 16.7);
    },
    then: function (newValue, devName, cellName) {
      log("heatersDayOn  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
      heater1EnableInverted = !true; // инвертировано
    }
  });

  // ночью работает позиционный регулятор
  defineRule("heatersNightOff", {
    when: function() {
      return (!dev._schedules.heaters_schedule) && (vegetablesTemperature > 11.6);
    },
    then: function (newValue, devName, cellName) {
      log("heatersNightOff  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
      heater1EnableInverted = !false; // инвертировано
    }
  });

  defineRule("heatersNightOn", {
    when: function() {
      return (!dev._schedules.heaters_schedule) && (vegetablesTemperature < 11.3);
    },
    then: function (newValue, devName, cellName) {
      log("heatersNightOn  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
      heater1EnableInverted = !true; // инвертировано
    }
  });


  // приточная и вытяжная вентиляция принудительно выключены
  
  defineRule("ventFrontshopAlwaysOff", {
    when: cron("@every 1m"),
    then: function() {
  		dev["wb-gpio/EXT1_R3A3"] = !false;
  		dev["wb-gpio/EXT1_R3A4"] = !false;
    }
  });
  
  
  // ==================  Кассовая зона =================

  // в кассовой зоне в рабочее время температура поддерживается кондиционерами (позиционный регулятор)

  defineRule("countersACOn", {
    when: function() {
      return (dev._schedules.working_hours_15m) && (countersTemperature < 17.7);
    },
    then: function (newValue, devName, cellName) {
      log("countersACOn  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
      dev["wb-mir_75/Play from ROM7"] = true; // кондиционер кассовой зоны на нагрев
    }
  });

  // в нерабочее время кондиционер выключен
  defineRule("countersACOff", {
    when: function() {
      return (!dev._schedules.working_hours_15m) || (countersTemperature > 18.0);
    },
    then: function (newValue, devName, cellName) {
      log("countersACOff  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
      dev["wb-mir_75/Play from ROM2"] = true; // кондиционер кассовой зоны выключить
    }
  });

  // =============== Овощная зона ==============
  // Охлаждение овощей кондиционером только при температуре воздуха выше 18.5C  

  defineRule("acVegOn", {
    when: function() {
      return vegetablesTemperature >= 18.5
    },
    then: function (newValue, devName, cellName) {
      log("acVegOn  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
      dev["wb-mir_76/Play from ROM3"] = true; // Охлаждение +18
    }
  });

  defineRule("acVegOff", {
    when: function() {
      return vegetablesTemperature < 17.8
    },
    then: function (newValue, devName, cellName) {
      log("acVegOff  newValue={}, devName={}, cellName={}", newValue, devName, cellName);
      dev["wb-mir_76/Play from ROM2"] = true; // выключить
    }
  });
})()


Полное описание возможностей движка правил

Самое полное описание движка правил: https://github.com/contactless/wb-rules/blob/master/README.md


Новые возможности последних версий


В разработке

Описание возможностей будущих версий движка правил можно прочесть здесь: