Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

BooleanLogicUltimate.html 10KB

5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <script type="text/javascript">
  2. RED.nodes.registerType('BooleanLogicUltimate',{
  3. category: 'boolean logic ultimate',
  4. color: '#ff8080',
  5. defaults: {
  6. name: {
  7. value: ""
  8. },
  9. filtertrue: {value:"both"},
  10. persist: {value:true},
  11. sInitializeWith: {value:"WaitForPayload"},
  12. triggertopic: {
  13. value: "trigger",
  14. required: false
  15. },
  16. outputtriggeredby:{
  17. value:"all",
  18. required: false},
  19. inputCount: {
  20. value: 2,
  21. required: true,
  22. validate:
  23. function(v) {
  24. return !isNaN( parseInt( v ) ) && parseInt( v ) >= 2;
  25. }
  26. },
  27. topic: {
  28. value: "result",
  29. required: true,
  30. validate:
  31. function(v) {
  32. return v !== undefined && v.length > 0;
  33. }
  34. }
  35. },
  36. inputs:1,
  37. outputs:3,
  38. outputLabels: function(i) {
  39. var ret="";
  40. switch (i) {
  41. case 0:
  42. return "AND";
  43. break;
  44. case 1:
  45. return "OR";
  46. break;
  47. case 2
  48. :
  49. return "XOR";
  50. break;
  51. default:
  52. break;
  53. }
  54. },
  55. icon: "serial.png",
  56. label:
  57. function() {
  58. let label = "";
  59. let filtered=this.filtertrue=="both" ? "" : "f" ;
  60. let trigger="";
  61. if(typeof this.outputtriggeredby !== "undefined") trigger=this.outputtriggeredby=="all" ? "" : "t (" + this.triggertopic+")";
  62. label= this.inputCount + ((filtered+trigger)==""?" Boolean Logic":(filtered+trigger));
  63. if( this.name !== undefined && this.name.length > 0 ) {
  64. label += " (" + this.name + ")";
  65. }
  66. return label;
  67. },
  68. paletteLabel: function() {
  69. return "BooleanLogicUltimate";
  70. },
  71. oneditprepare: function () {
  72. // Add write and response as default for existing nodes like was default before
  73. if (this.outputtriggeredby === 'all') {
  74. $("#triggertopic").hide()
  75. }else
  76. {
  77. $("#triggertopic").show()
  78. }
  79. $("#node-input-outputtriggeredby").on('change',function() {
  80. if ($("#node-input-outputtriggeredby").val()==="all") {
  81. $("#triggertopic").hide()
  82. }else{
  83. $("#triggertopic").show()
  84. }
  85. })
  86. // default
  87. if(typeof this.outputtriggeredby === "undefined")
  88. {
  89. $("#node-input-outputtriggeredby").val("all");
  90. this.outputtriggeredby="all";
  91. }
  92. // default
  93. if(typeof this.sInitializeWith === "undefined")
  94. {
  95. $("#node-input-sInitializeWith").val("WaitForPayload");
  96. this.sInitializeWith="WaitForPayload";
  97. }
  98. },
  99. oneditsave: function () {
  100. // Delete persistent state file
  101. $.get( "stateoperation_delete?nodeid=" +this.id, function( data ) {});
  102. }
  103. });
  104. </script>
  105. <script type="text/x-red" data-template-name="BooleanLogicUltimate">
  106. <div class="form-row">
  107. <label for="node-input-inputCount"><i class="fa fa-indent"></i> Number of different topics to evaluate</label>
  108. <input type="text" id="node-input-inputCount" placeholder="Number of different topics to consider">
  109. </div>
  110. <div class="form-row">
  111. <label for="node-input-name"><i class="icon-tag"></i> Name</label>
  112. <input type="text" id="node-input-name" placeholder="Name">
  113. </div>
  114. <div class="form-row">
  115. <label for="node-input-topic"><i class="fa fa-tasks"></i> Node topic</label>
  116. <input type="text" id="node-input-topic" placeholder="Node's own topic">
  117. </div>
  118. <div class="form-row">
  119. <label for="node-input-filtertrue"><i class="fa fa-filter"></i> Filter output result</label>
  120. <select type="text" id="node-input-filtertrue" placeholder="Filter">
  121. <option value="both">Output both 'true' and 'false' results</option>
  122. <option value="onlytrue">Output only 'true' results</option>
  123. </select>
  124. </div>
  125. <div class="form-row">
  126. <label for="node-input-outputtriggeredby"><i class="fa fa-filter"></i> Trigger mode</label>
  127. <select type="text" id="node-input-outputtriggeredby" placeholder="Event">
  128. <option value="all">All topics</option>
  129. <option value="onlyonetopic">Single topic</option>
  130. </select>
  131. </div>
  132. <div class="form-row" id="triggertopic">
  133. <label for="node-input-triggertopic"><i class="fa fa-tasks"></i> Topic that start boolean logic evaluation</label>
  134. <input type="text" id="node-input-triggertopic" placeholder="Input topic">
  135. </div>
  136. <div class="form-row">
  137. <i class="fa fa-floppy-o"></i>
  138. <label style="width:auto" for="node-input-persist"> Remember latest input values after reboot</label> <input type="checkbox" id="node-input-persist" style="display:inline-block; width:auto; vertical-align:top;">
  139. <div id="helpallga"><i> If checked, the input values are retained <br /> after a node-red reboot.<br/>If you reboot your node-red, <br />you don't need to wait all inputs <br />to arrive and initialize the node, <br />before the node can output a payload.</div>
  140. </div>
  141. <div class="form-row">
  142. <label for="node-input-sInitializeWith"><i class="fa fa-home"></i> If input states are undefined</label>
  143. <select type="text" id="node-input-sInitializeWith" placeholder="">
  144. <option value="WaitForPayload">Leave undefined</option>
  145. <option value="false">Initialize all with False</option>
  146. <option value="true">Initialize all with True</option>
  147. </select>
  148. </div>
  149. </script>
  150. <script type="text/x-red" data-help-name="BooleanLogicUltimate">
  151. <p>
  152. <a href="https://www.paypal.me/techtoday" target="_blank"><img src='https://img.shields.io/badge/Donate-PayPal-blue.svg?style=flat-square' width='30%'></a>
  153. and
  154. <a href="http://eepurl.com/gJm095" target="_blank">Subscribe to my channel</a> for news about my nodes.
  155. </p>
  156. <p>The node performs Boolean logic on the incoming payloads.<br/>
  157. <a href="https://www.paypal.me/techtoday" target="_blank"><img src='https://img.shields.io/badge/Donate-PayPal-blue.svg?style=flat-square' width='30%'></a>
  158. <br/><br/>
  159. The node performs 3 checks (<b>AND,OR,XOR</b>) on the incoming boolean payloads and outputs the result at the same time, as follow:<br/>
  160. - Output "AND": true or false<br/>
  161. - Output "OR": true or false<br/>
  162. - Output "XOR": true or false<br/>
  163. <b>Number of different topics to evaluate</b><br />
  164. Set the number of different topics to be evaluated. The node will output a message to the flow, after this number of different topics arrives.<br/>
  165. <b> Remember: each input topic must be different. For example, if you set this field to 3, the node expects 3 different topics.</b><br/>
  166. <br/>
  167. Changing the topic is usually only needed when chaining multiple boolean nodes after each other becuse the topics will then all be the same when delivered to the nodes further down the chain.<br/>
  168. <br/>
  169. The node expects a fixed number of topics (configured in the settings) on which it will operate. It will only output a value
  170. when it has seen the expected number of topics. If it ever sees more than the configured number of topics it will log a message then reset its state and start over.<br/>
  171. <b> Remember: each input topic must be different. For example, if you set "Number of different topics to evaluate" to 3, the node expects 3 different topics.</b><br/>
  172. <br/><br/>
  173. <b>Filter output result</b><br />
  174. <ol>
  175. <li>Output both 'true' and 'false' results: Standard behaviour, the node will output <b>true</b> and <b>false</b> whenever it receives an input and calculate the boolean logics as output.</li>
  176. <li>Output only 'true' results: whenever the node receives an input, it outputs a payload <b>true</b> only if the result of the logic is true. <b>False</b> results are filtered out.</li>
  177. </ol>
  178. <b>filtered</b>: shown on label, means that the node will pass out only <code>true</code> values (Output only 'true' results). <br/>
  179. <br/><br/>
  180. <b>Trigger mode</b><br />
  181. The node can acts ad a standard boolean logic or as single topic triggered boolean logic.<br/>
  182. As single topic triggered boolean logic, the node will evaluate the inputs (and thus will output a payload) only if a specified topic input arrives.<br/>
  183. In a coding perspectives, it acts as follows:<br/>
  184. <code>
  185. if (msg.topic == specified topic)<br/>
  186. {<br/>
  187. If (all other inputs are true) -> outputs true otherwise false<br/>
  188. }<br/>
  189. </code>
  190. <ol>
  191. <li>All topics: standard behaviour, the node will output <b>true</b> and <b>false</b> by evaluating all inputs. Each input change will trigger the node output.</li>
  192. <li>Single topic + only eval others: <u>only whenever the node receives a msg input with the <b>specified topic</b> (having payload = true)</u>, it starts the evaluation of all other inputs as well and outputs the evaluated payload. If the node receives a msg input other than the <b>specified topic</b>), it only retains it's value for the boolean evaluation.</li>
  193. </ol>
  194. Example of trigger mode = Single topic + eval other inputs: <b>SEE THE README FOR AN EXAMPLE</b>
  195. <br/><br/>
  196. <b>Remember latest input values after reboot</b><br />
  197. If checked, the input values are retained after a node-red reboot. That means, that if you reboot your node-red, you don't need to wait all inputs to arrive and initialize the node, before the node can output a payload.<br/>
  198. Every time you modify the node's config, <b>the retained values are cleared</b>.<br/>
  199. <br/>
  200. <b> If input states are undefined</b><br />
  201. Every time you create a node or modify the node, all inputs are set to undefined. This means that the node will wait the arrive of all topics (for example 3 topics, if you've selected 3 topics in the option), before it can output a payload. This can be a problem if your logic must be operative as soon as you deploy the flow. To overcome this problem, you can "initialize" all the undefined inputs with True or False.
  202. <ol>
  203. <li>Leave undefined: Standard behaviour, the node will wait all the "undefined" topics to arrive, then starts a flow with the result.</li>
  204. <li>True or False: The node is immediately operative, by force the initialization of the "undefined" inputs with "true" or "false".</li>
  205. </ol>
  206. <br/>
  207. <i> How inputs are handled:</i><br />
  208. All incoming msg.payloads are converted into a boolean value according to the following rules (this applies to all boolean logic nodes):
  209. <ol>
  210. <li>Boolean values are taken as-is.</li>
  211. <li>For numbers, 0 evaluates to false, all other numbers evaluates to true.</li>
  212. <li>Strings are converted to numbers if they match the format of a decimal value, then the same rule as for numbers are applied. If it does not match, it evaluates to false. Also, the string "true" evaluates to true.</li>
  213. </ol>
  214. <br>
  215. The XOR operation operates in a one, and only one mode, i.e. (A ^ B) ^ C ... ^ n
  216. </p>
  217. </script>