Node-Red configuration
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

math.html 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <!--
  2. Copyright 2018, Bart Butenaers
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. -->
  13. <script type="text/x-red" data-template-name="calculator">
  14. <div class="form-row">
  15. <label for="node-input-typed-inputMsgField"><i class="fa fa-list"></i> Input field</label>
  16. <input id="node-input-typed-inputMsgField" type="text" style="width: 70%">
  17. <input id="node-input-inputMsgField" type="hidden">
  18. </div>
  19. <div class="form-row">
  20. <label for="node-input-typed-outputMsgField"><i class="fa fa-list"></i> Output field</label>
  21. <input id="node-input-typed-outputMsgField" type="text" style="width: 70%">
  22. <input id="node-input-outputMsgField" type="hidden">
  23. </div>
  24. <div class="form-row">
  25. <label for="node-input-operation"><i class="fa fa-calculator"></i> Operation</label>
  26. <select id="node-input-operation">
  27. </select>
  28. </div>
  29. <div class="form-row">
  30. <!-- Set step to 'any' to allow not only integers but also decimals -->
  31. <label for="node-input-typed-constant"><i class="fa fa-hashtag"></i> Constant</label>
  32. <input id="node-input-typed-constant" type="number" step="any" style="width: 70%">
  33. <input id="node-input-constant" type="hidden">
  34. </div>
  35. <div class="form-row">
  36. <label>&nbsp;</label>
  37. <input type="checkbox" id="node-input-round" placeholder="" style="display:inline-block; width:auto; vertical-align:baseline;">
  38. <label for="node-input-round">Round result to </label>
  39. <input type="text" id="node-input-decimals" style="width:80px">
  40. <span for="node-input-decimals" style="margin-left:5px;">decimals</span>
  41. </div>
  42. <br>
  43. <div class="form-row">
  44. <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
  45. <input type="text" id="node-input-name" placeholder="Name">
  46. </div>
  47. </script>
  48. <script type="text/javascript">
  49. var operations = [
  50. {v:"" ,t:""},
  51. {v:"avg" ,t:"Average"},
  52. {v:"max" ,t:"Maximum"},
  53. {v:"min" ,t:"Minimum"},
  54. {v:"inc" ,t:"Increment"},
  55. {v:"dec" ,t:"Decrement"},
  56. {v:"trunc" ,t:"Integer part"},
  57. {v:"ceil" ,t:"Round upwards"},
  58. {v:"floor" ,t:"Round downwards"},
  59. {v:"round" ,t:"Nearest integer"},
  60. {v:"rdec" ,t:"Round decimal places"},
  61. {v:"sum" ,t:"Sum"},
  62. {v:"sub" ,t:"Subtract"},
  63. {v:"mult" ,t:"Multiply"},
  64. {v:"div" ,t:"Divide"},
  65. {v:"mod" ,t:"Modulus (remainder)"},
  66. {v:"abs" ,t:"Absolute value"},
  67. {v:"rand" ,t:"Random"},
  68. {v:"randb" ,t:"Random between min and max"},
  69. {v:"randa" ,t:"Random from array"},
  70. {v:"len" ,t:"Length of array"},
  71. {v:"sorta" ,t:"Sort ascending"},
  72. {v:"sortd" ,t:"Sort descending"},
  73. {v:"range" ,t:"Create range"},
  74. {v:"dist" ,t:"Get distance"},
  75. {v:"pow" ,t:"X to the power of y"},
  76. {v:"exp" ,t:"E to power of x"},
  77. {v:"cbrt" ,t:"Cubic root"},
  78. {v:"log" ,t:"Natural logarithm (base E)"},
  79. {v:"log10" ,t:"Logarithm (base 10)"},
  80. {v:"acos" ,t:"Arccosine"},
  81. {v:"acosh" ,t:"Hyperbolic arccosine"},
  82. {v:"asin" ,t:"Arcsine"},
  83. {v:"asinh" ,t:"Hyperbolic arcsine"},
  84. {v:"atan" ,t:"Arctangent"},
  85. {v:"atanh" ,t:"Hyperbolic arctangent"},
  86. {v:"cos" ,t:"Cosine"},
  87. {v:"cosh" ,t:"Hyperbolic cosine"},
  88. {v:"sin" ,t:"Sine"},
  89. {v:"sinh" ,t:"Hyperbolic sine"},
  90. {v:"sqrt" ,t:"Square root"},
  91. {v:"tan" ,t:"Tangent"},
  92. {v:"tanh" ,t:"Hyperbolic tangent"}
  93. ];
  94. RED.nodes.registerType('calculator', {
  95. category: 'function',
  96. color: '#fdd0a2',
  97. defaults: {
  98. name: {value: ""},
  99. inputMsgField: {value:"payload", required:true},
  100. outputMsgField: {value:"payload", required:true},
  101. operation: {value: ""},
  102. constant: {value: null},
  103. round: {value: false},
  104. decimals: {value: 0}
  105. },
  106. inputs: 1,
  107. outputs: 1,
  108. icon: "font-awesome/fa-calculator",
  109. label: function() {
  110. var operationText = "";
  111. // Get the text of the current operation stored in this node
  112. for (var i = 0; i < operations.length; i++) {
  113. if (operations[i].v === this.operation) {
  114. operationText = operations[i].t;
  115. break;
  116. }
  117. }
  118. return this.name || operationText || "Calculator";
  119. },
  120. oneditprepare: function() {
  121. // Fill the dropdown with all available operations
  122. for (var i = 0; i < operations.length; i++) {
  123. var value = operations[i].v;
  124. var text = operations[i].t;
  125. $('#node-input-operation').append($("<option></option>").attr("value", value).text(text));
  126. }
  127. $('#node-input-operation').val(this.operation);
  128. // Show the inputMsgField value in a typedinput element (dropdown with only 'msg')
  129. var inputKeyValue = $("#node-input-inputMsgField").val() || 'payload';
  130. $("#node-input-typed-inputMsgField").typedInput({types:['msg']});
  131. $("#node-input-typed-inputMsgField").typedInput('type','msg');
  132. $("#node-input-typed-inputMsgField").typedInput('value',inputKeyValue);
  133. // Show the outputMsgField value in a typedinput element (dropdown with only 'msg')
  134. var outputKeyValue = $("#node-input-outputMsgField").val() || 'payload';
  135. $("#node-input-typed-outputMsgField").typedInput({types:['msg']});
  136. $("#node-input-typed-outputMsgField").typedInput('type','msg');
  137. $("#node-input-typed-outputMsgField").typedInput('value',outputKeyValue);
  138. // Show the constant value in a typedinput element (dropdown with only 'number')
  139. var constantValue = $("#node-input-constant").val();
  140. $("#node-input-typed-constant").typedInput({types:['num']});
  141. $("#node-input-typed-constant").typedInput('type','num');
  142. $("#node-input-typed-constant").typedInput('value',constantValue);
  143. $('#node-input-round').change(function() {
  144. $('#node-input-decimals').prop('disabled', !this.checked);
  145. // When rounding is disabled, the number of decimals should be reset to zero
  146. if (!this.checked) {
  147. $('#node-input-decimals').val(0);
  148. }
  149. });
  150. $('#node-input-round').change();
  151. },
  152. oneditsave: function() {
  153. // Copy the inputMsgField value from the typedinput element to the inputMsgField element
  154. var inputKeyValue = $("#node-input-typed-inputMsgField").typedInput('value');
  155. $("#node-input-inputMsgField").val(inputKeyValue);
  156. // Copy the outputMsgField value from the typedinput element to the outputMsgField element
  157. var outputKeyValue = $("#node-input-typed-outputMsgField").typedInput('value');
  158. $("#node-input-outputMsgField").val(outputKeyValue);
  159. // Copy the constantValue value from the typedinput element to the constantValue element
  160. var constantValue = $("#node-input-typed-constant").typedInput('value');
  161. if (constantValue) {
  162. constantValue = parseFloat(constantValue);
  163. }
  164. $("#node-input-constant").val(constantValue);
  165. }
  166. });
  167. </script>
  168. <script type="text/x-red" data-help-name="calculator">
  169. <p>A Node Red node to perform basic mathematical operations.</p>
  170. <p>See <a target="_blank" href="https://github.com/bartbutenaers/node-red-contrib-calc">Github</a> for examples.</p>
  171. <p><strong>Input field:</strong><br/>
  172. The name of the field in the input message, that contains the input number (or array of input numbers).</p>
  173. <p><strong>Output field:</strong><br/>
  174. The name of the field in the output message, where the result will be stored (as number or array of input numbers).</p>
  175. <p><strong>Operation:</strong><br/>
  176. A series of operations are available, that can be executed on the input data:
  177. <ul>
  178. <li>Average (avg): average of all the numbers in the array</li>
  179. <li>Maximum (max): get the number with the highest value from an array of numbers</li>
  180. <li>Minimum (min): get the number with the lowest value from an array of numbers</li>
  181. <li>Increment (inc): add 1 to the number</li>
  182. <li>Decrement (dec): subtract 1 from the number</li>
  183. <li>Integer part (trunc): truncate the number to the integer part</li>
  184. <li>Round upwards (ceil): round the number upwards to the nearest integer</li>
  185. <li>Round downwards (floor): round the number downwards to the nearest integer</li>
  186. <li>Nearest integer (round): rounds the number to the nearest integer</li>
  187. <li>Round decimal places (rdec): round the number at a specified number of decimal places</li>
  188. <li>Sum (sum): sum of the all the numbers in the array</li>
  189. <li>Subtract (sub): subtraction of the all the numbers in the array</li>
  190. <li>Multiply (mult): multiply all the numbers in the array</li>
  191. <li>Divide (div): division of all the numbers in the array</li>
  192. <li>Modulus (mod): get the remainder of the division of the two numbers in the array</li>
  193. <li>Absolute value (abs): absolute value of the number</li>
  194. <li>Random (rand): a random number between 0 and 1</li>
  195. <li>Random between min and max (randb): a random number between between the two numbers in the array)</li>
  196. <li>Random from array (randa): a random number from an array of possible numbers</li>
  197. <li>Length of array (len): the lenght of the array</li>
  198. <li>Sort ascending (sorta): Sort the array ascending (i.e. from low to high)</li>
  199. <li>Sort descending (sortd): Sort the array descending (i.e. from high to low)</li>
  200. <li>Create range (range): Create an array of integer numbers, between the two numbers in the array</li>
  201. <li>Get distance (dist): Get the distance (i.e. the range) between the numbers in the array</li>
  202. <li>X to the power of y (pow): x<sup>y</sup> (from an array of two numbers [x, y])</li>
  203. <li>E to the power of x (exp): value of E<sup>x</sup>, where E is Euler's number (approximately 2.7183)</li>
  204. <li>Cubic root (cbrt): cubic root (x<sup>3</sup>) of the number</li>
  205. <li>Natural logarithm (log): natural logarithm base E of the number</li>
  206. <li>Logarithm (log10): logarithm base 10 of the number</li>
  207. <li>Arccosine (acos): arccosine value of the number</li>
  208. <li>Hyperbolic arccosine (acosh): hyperbolic arccosine of the number</li>
  209. <li>Arcsine (asin): arcsine of the number in radians</li>
  210. <li>Hyperbolic arcsine (asinh): hyperbolic arcsine of the number</li>
  211. <li>Arctangent (atan): arctangent of the number, as a numeric value between -PI/2 and PI/2 radians</li>
  212. <li>Hyperbolic arctangent (atanh): hyperbolic arctangent of the number</li>
  213. <li>Cosine (cos): cosine of the number in radians</li>
  214. <li>Hyperbolic cosine (cosh): hyperbolic cosine of the number</li>
  215. <li>Sine (sin): sine of the number in radians</li>
  216. <li>Hyperbolic sine (sinh): hyperbolic sine of the number</li>
  217. <li>Square root (sqrt): square root of the number</li>
  218. <li>Tangent (tan): tangent of an angle</li>
  219. <li>Hyperbolic tangent (tanh): hyperbolic tangent of the number</li>
  220. </ul>
  221. Note that - when no operation is selected on the config screen - an operation needs to be specified in the <code>msg.operation</code> field. The codes can be found between round brackets in the above list.</p>
  222. <p>When only a single number is needed for an operation, that number can be specified in the <code>msg.payload</code>. However it is also possible to put (for such operations) an array of numbers in the <code>msg.payload</code>, in which case the same operation will be executed on all numbers in the array. The result will also be an array then.</p>
  223. <p><strong>Constant field:</strong><br/>
  224. In this field an optional number can be entered, to have a constant value in the calculation. This constant value will automatically be added to the end of the input array.</p>
  225. <p><strong>Round result to ... decimals:</strong><br/>
  226. When selected, the output number(s) will be rounded to the specified number of decimals.</p>
  227. </script>