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

Нет описания правки
(Новая страница: «For example, we want the lighting to be on from 10 to 17h. The libschedule will follow the rule of "turn on the lights" once a minute from 10 am to 17 PM.»)
 
(не показаны 34 промежуточные версии 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/contactless/wb-ruleshttps://github.com/contactless/wb-rules
The complete description of the rules engine: https://github.com/wirenboard/wb-rules




Строка 15: Строка 15:
Notes:
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.
#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.
#The rules are executed by the ''wb-rules'' service, see the service's [https://github.com/contactless/wb-rules documentation on Github].
#The rules are executed by the ''wb-rules'' service, see the service's [https://github.com/wirenboard/wb-rules documentation on Github].






== Writing the first rule==
== 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|The rule for the heater control, written via the web interface]]
Строка 86: Строка 86:
#* on the same page below;
#* on the same page below;
#* in [http://forums.contactless.ru/t/dvizhok-pravil-primery-koda/483  a special topic on our forum(ru)].
#* in [http://forums.contactless.ru/t/dvizhok-pravil-primery-koda/483  a special topic on our forum(ru)].
#[https://github.com/contactless/wb-rules Full description of the rules engine].
#[https://github.com/wirenboard/wb-rules Full description of the rules engine].




Строка 386: Строка 386:
Some rules come with the default rule system in the wb-rules-system package.  
Some rules come with the default rule system in the wb-rules-system package.  


Full list of rules is[https://github.com/contactless/wb-rules-system/tree/master/rules in the repository.].
Full list of rules is [https://github.com/wirenboard/wb-rules-system/tree/master/rules in the repository.].


Some examples:
Some examples:
Строка 394: Строка 394:
==== The rule for the buzzer====
==== The rule for the buzzer====


[https://github.com/contactless/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.
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.




Строка 469: Строка 469:
==== Power status rule ====
==== Power status rule ====


[https://github.com/contactless/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/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.


The following logic is implemented:
The following logic is implemented:
Строка 537: Строка 537:




=== Sending commands viaRS-485 ===
=== Sending commands via 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/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 documentation] for details.
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.


Create a virtual device with switch type control via rules engine.  
Create a virtual device with switch type control via rules engine.  
Строка 701: Строка 701:
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].
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].


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 portal].
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].




Строка 711: Строка 711:
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.
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.


For example, we want the lighting to be on from 10 to 17h. The libschedule will follow the rule of "turn on the lights" once a minute from 10 am to 17 PM.
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 утра), то контроллер всё равно включит освещение при первой возможности.
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.




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


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


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


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


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


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




   // Освещение торгового зала
   // sales area illumination
   defineRule("lightingFrontshopOnOff", {
   defineRule("lightingFrontshopOnOff", {
     when: function() {
     when: function() {
Строка 938: Строка 938:
     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; //инвертированный контактор
       dev["wb-gpio/EXT1_R3A1"] = ! dev._schedules.frontshop_lighting; //inverted contactor
     }
     }
   });
   });




   // Вентиляция подсобного помещения
   // backstoreroom ventilation
   defineRule("ventBackstoreOnOff", {
   defineRule("ventBackstoreOnOff", {
     when: function() {
     when: function() {
Строка 951: Строка 951:
       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; //инвертированный контактор
       dev["wb-mr6c_81/K1"] = ! on; //inverted contactor
       dev["wb-mr6c_81/K5"] = ! on; //инвертированный контактор
       dev["wb-mr6c_81/K5"] = ! on; //inverted contactor
     }
     }
   });
   });


   // Освещение холодильных горок
   // Freezer showcase illumination
   defineRule("lightingCoolingshelfsOnOff", {
   defineRule("lightingCoolingshelfsOnOff", {
     when: function() {
     when: function() {
Строка 965: Строка 965:
       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;
Строка 978: Строка 979:




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




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


   // днём работает позиционный регулятор
   // position controller works daily
   defineRule("heatersDayOff", {
   defineRule("heatersDayOff", {
     when: function() {
     when: function() {
Строка 1002: Строка 1003:
     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; // инвертировано
       heater1EnableInverted = !false; // inverted
     }
     }
   });
   });
Строка 1012: Строка 1013:
     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; // инвертировано
       heater1EnableInverted = !true; // inverted
     }
     }
   });
   });


   // ночью работает позиционный регулятор
   // position controller works at night
   defineRule("heatersNightOff", {
   defineRule("heatersNightOff", {
     when: function() {
     when: function() {
Строка 1023: Строка 1024:
     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; // инвертировано
       heater1EnableInverted = !false; // inverted
     }
     }
   });
   });
Строка 1033: Строка 1034:
     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; // инвертировано
       heater1EnableInverted = !true; // inverted
     }
     }
   });
   });




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


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


   defineRule("countersACOn", {
   defineRule("countersACOn", {
Строка 1059: Строка 1060:
     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; // кондиционер кассовой зоны на нагрев
       dev["wb-mir_75/Play from ROM7"] = true; // air conditioning cash area for heating
     }
     }
   });
   });


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


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


   defineRule("acVegOn", {
   defineRule("acVegOn", {
Строка 1083: Строка 1084:
     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; // Охлаждение +18
       dev["wb-mir_76/Play from ROM3"] = true; // Cooling +18
     }
     }
   });
   });
Строка 1093: Строка 1094:
     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; // выключить
       dev["wb-mir_76/Play from ROM2"] = true; // shutdown
     }
     }
   });
   });
Строка 1101: Строка 1102:




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


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






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


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






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


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

правки