您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

BooleanLogicUltimate.html 10KB

5 年前
5 年前
5 年前
5 年前
5 年前
5 年前
5 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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>The node performs Boolean logic on the incoming payloads.<br/>
  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. <br/><br/>
  154. 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/>
  155. - Output "AND": true or false<br/>
  156. - Output "OR": true or false<br/>
  157. - Output "XOR": true or false<br/>
  158. <b>Number of different topics to evaluate</b><br />
  159. 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/>
  160. <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/>
  161. <br/>
  162. 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/>
  163. <br/>
  164. The node expects a fixed number of topics (configured in the settings) on which it will operate. It will only output a value
  165. 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/>
  166. <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/>
  167. <br/><br/>
  168. <b>Filter output result</b><br />
  169. <ol>
  170. <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>
  171. <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>
  172. </ol>
  173. <b>filtered</b>: shown on label, means that the node will pass out only <code>true</code> values (Output only 'true' results). <br/>
  174. <br/><br/>
  175. <b>Trigger mode</b><br />
  176. The node can acts ad a standard boolean logic or as single topic triggered boolean logic.<br/>
  177. 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/>
  178. In a coding perspectives, it acts as follows:<br/>
  179. <code>
  180. if (msg.topic == specified topic)<br/>
  181. {<br/>
  182. If (all other inputs are true) -> outputs true otherwise false<br/>
  183. }<br/>
  184. </code>
  185. <ol>
  186. <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>
  187. <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>
  188. </ol>
  189. Example of trigger mode = Single topic + eval other inputs: <b>SEE THE README FOR AN EXAMPLE</b>
  190. <br/><br/>
  191. <b>Remember latest input values after reboot</b><br />
  192. 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/>
  193. Every time you modify the node's config, <b>the retained values are cleared</b>.<br/>
  194. <br/>
  195. <b> If input states are undefined</b><br />
  196. 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.
  197. <ol>
  198. <li>Leave undefined: Standard behaviour, the node will wait all the "undefined" topics to arrive, then starts a flow with the result.</li>
  199. <li>True or False: The node is immediately operative, by force the initialization of the "undefined" inputs with "true" or "false".</li>
  200. </ol>
  201. <br/>
  202. <i> How inputs are handled:</i><br />
  203. All incoming msg.payloads are converted into a boolean value according to the following rules (this applies to all boolean logic nodes):
  204. <ol>
  205. <li>Boolean values are taken as-is.</li>
  206. <li>For numbers, 0 evaluates to false, all other numbers evaluates to true.</li>
  207. <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>
  208. </ol>
  209. <br>
  210. The XOR operation operates in a one, and only one mode, i.e. (A ^ B) ^ C ... ^ n
  211. </p>
  212. </script>