243 lines
13 KiB
HTML
243 lines
13 KiB
HTML
<!--
|
|
Copyright 2018, Bart Butenaers
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
-->
|
|
<script type="text/x-red" data-template-name="calculator">
|
|
<div class="form-row">
|
|
<label for="node-input-typed-inputMsgField"><i class="fa fa-list"></i> Input field</label>
|
|
<input id="node-input-typed-inputMsgField" type="text" style="width: 70%">
|
|
<input id="node-input-inputMsgField" type="hidden">
|
|
</div>
|
|
<div class="form-row">
|
|
<label for="node-input-typed-outputMsgField"><i class="fa fa-list"></i> Output field</label>
|
|
<input id="node-input-typed-outputMsgField" type="text" style="width: 70%">
|
|
<input id="node-input-outputMsgField" type="hidden">
|
|
</div>
|
|
<div class="form-row">
|
|
<label for="node-input-operation"><i class="fa fa-calculator"></i> Operation</label>
|
|
<select id="node-input-operation">
|
|
|
|
</select>
|
|
</div>
|
|
<div class="form-row">
|
|
<!-- Set step to 'any' to allow not only integers but also decimals -->
|
|
<label for="node-input-typed-constant"><i class="fa fa-hashtag"></i> Constant</label>
|
|
<input id="node-input-typed-constant" type="number" step="any" style="width: 70%">
|
|
<input id="node-input-constant" type="hidden">
|
|
</div>
|
|
<div class="form-row">
|
|
<label> </label>
|
|
<input type="checkbox" id="node-input-round" placeholder="" style="display:inline-block; width:auto; vertical-align:baseline;">
|
|
<label for="node-input-round">Round result to </label>
|
|
<input type="text" id="node-input-decimals" style="width:80px">
|
|
<span for="node-input-decimals" style="margin-left:5px;">decimals</span>
|
|
</div>
|
|
|
|
<br>
|
|
<div class="form-row">
|
|
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
<input type="text" id="node-input-name" placeholder="Name">
|
|
</div>
|
|
</script>
|
|
|
|
<script type="text/javascript">
|
|
var operations = [
|
|
{v:"" ,t:""},
|
|
{v:"avg" ,t:"Average"},
|
|
{v:"max" ,t:"Maximum"},
|
|
{v:"min" ,t:"Minimum"},
|
|
{v:"inc" ,t:"Increment"},
|
|
{v:"dec" ,t:"Decrement"},
|
|
{v:"trunc" ,t:"Integer part"},
|
|
{v:"ceil" ,t:"Round upwards"},
|
|
{v:"floor" ,t:"Round downwards"},
|
|
{v:"round" ,t:"Nearest integer"},
|
|
{v:"rdec" ,t:"Round decimal places"},
|
|
{v:"sum" ,t:"Sum"},
|
|
{v:"sub" ,t:"Subtract"},
|
|
{v:"mult" ,t:"Multiply"},
|
|
{v:"div" ,t:"Divide"},
|
|
{v:"mod" ,t:"Modulus (remainder)"},
|
|
{v:"abs" ,t:"Absolute value"},
|
|
{v:"rand" ,t:"Random"},
|
|
{v:"randb" ,t:"Random between min and max"},
|
|
{v:"randa" ,t:"Random from array"},
|
|
{v:"len" ,t:"Length of array"},
|
|
{v:"sorta" ,t:"Sort ascending"},
|
|
{v:"sortd" ,t:"Sort descending"},
|
|
{v:"range" ,t:"Create range"},
|
|
{v:"dist" ,t:"Get distance"},
|
|
{v:"pow" ,t:"X to the power of y"},
|
|
{v:"exp" ,t:"E to power of x"},
|
|
{v:"cbrt" ,t:"Cubic root"},
|
|
{v:"log" ,t:"Natural logarithm (base E)"},
|
|
{v:"log10" ,t:"Logarithm (base 10)"},
|
|
{v:"acos" ,t:"Arccosine"},
|
|
{v:"acosh" ,t:"Hyperbolic arccosine"},
|
|
{v:"asin" ,t:"Arcsine"},
|
|
{v:"asinh" ,t:"Hyperbolic arcsine"},
|
|
{v:"atan" ,t:"Arctangent"},
|
|
{v:"atanh" ,t:"Hyperbolic arctangent"},
|
|
{v:"cos" ,t:"Cosine"},
|
|
{v:"cosh" ,t:"Hyperbolic cosine"},
|
|
{v:"sin" ,t:"Sine"},
|
|
{v:"sinh" ,t:"Hyperbolic sine"},
|
|
{v:"sqrt" ,t:"Square root"},
|
|
{v:"tan" ,t:"Tangent"},
|
|
{v:"tanh" ,t:"Hyperbolic tangent"}
|
|
];
|
|
|
|
RED.nodes.registerType('calculator', {
|
|
category: 'function',
|
|
color: '#fdd0a2',
|
|
defaults: {
|
|
name: {value: ""},
|
|
inputMsgField: {value:"payload", required:true},
|
|
outputMsgField: {value:"payload", required:true},
|
|
operation: {value: ""},
|
|
constant: {value: null},
|
|
round: {value: false},
|
|
decimals: {value: 0}
|
|
},
|
|
inputs: 1,
|
|
outputs: 1,
|
|
icon: "font-awesome/fa-calculator",
|
|
label: function() {
|
|
var operationText = "";
|
|
|
|
// Get the text of the current operation stored in this node
|
|
for (var i = 0; i < operations.length; i++) {
|
|
if (operations[i].v === this.operation) {
|
|
operationText = operations[i].t;
|
|
break;
|
|
}
|
|
}
|
|
|
|
return this.name || operationText || "Calculator";
|
|
},
|
|
oneditprepare: function() {
|
|
// Fill the dropdown with all available operations
|
|
for (var i = 0; i < operations.length; i++) {
|
|
var value = operations[i].v;
|
|
var text = operations[i].t;
|
|
$('#node-input-operation').append($("<option></option>").attr("value", value).text(text));
|
|
}
|
|
|
|
$('#node-input-operation').val(this.operation);
|
|
|
|
// Show the inputMsgField value in a typedinput element (dropdown with only 'msg')
|
|
var inputKeyValue = $("#node-input-inputMsgField").val() || 'payload';
|
|
$("#node-input-typed-inputMsgField").typedInput({types:['msg']});
|
|
$("#node-input-typed-inputMsgField").typedInput('type','msg');
|
|
$("#node-input-typed-inputMsgField").typedInput('value',inputKeyValue);
|
|
|
|
// Show the outputMsgField value in a typedinput element (dropdown with only 'msg')
|
|
var outputKeyValue = $("#node-input-outputMsgField").val() || 'payload';
|
|
$("#node-input-typed-outputMsgField").typedInput({types:['msg']});
|
|
$("#node-input-typed-outputMsgField").typedInput('type','msg');
|
|
$("#node-input-typed-outputMsgField").typedInput('value',outputKeyValue);
|
|
|
|
// Show the constant value in a typedinput element (dropdown with only 'number')
|
|
var constantValue = $("#node-input-constant").val();
|
|
$("#node-input-typed-constant").typedInput({types:['num']});
|
|
$("#node-input-typed-constant").typedInput('type','num');
|
|
$("#node-input-typed-constant").typedInput('value',constantValue);
|
|
|
|
$('#node-input-round').change(function() {
|
|
$('#node-input-decimals').prop('disabled', !this.checked);
|
|
|
|
// When rounding is disabled, the number of decimals should be reset to zero
|
|
if (!this.checked) {
|
|
$('#node-input-decimals').val(0);
|
|
}
|
|
});
|
|
$('#node-input-round').change();
|
|
},
|
|
oneditsave: function() {
|
|
// Copy the inputMsgField value from the typedinput element to the inputMsgField element
|
|
var inputKeyValue = $("#node-input-typed-inputMsgField").typedInput('value');
|
|
$("#node-input-inputMsgField").val(inputKeyValue);
|
|
|
|
// Copy the outputMsgField value from the typedinput element to the outputMsgField element
|
|
var outputKeyValue = $("#node-input-typed-outputMsgField").typedInput('value');
|
|
$("#node-input-outputMsgField").val(outputKeyValue);
|
|
|
|
// Copy the constantValue value from the typedinput element to the constantValue element
|
|
var constantValue = $("#node-input-typed-constant").typedInput('value');
|
|
if (constantValue) {
|
|
constantValue = parseFloat(constantValue);
|
|
}
|
|
$("#node-input-constant").val(constantValue);
|
|
}
|
|
});
|
|
</script>
|
|
|
|
<script type="text/x-red" data-help-name="calculator">
|
|
<p>A Node Red node to perform basic mathematical operations.</p>
|
|
<p>See <a target="_blank" href="https://github.com/bartbutenaers/node-red-contrib-calc">Github</a> for examples.</p>
|
|
<p><strong>Input field:</strong><br/>
|
|
The name of the field in the input message, that contains the input number (or array of input numbers).</p>
|
|
<p><strong>Output field:</strong><br/>
|
|
The name of the field in the output message, where the result will be stored (as number or array of input numbers).</p>
|
|
<p><strong>Operation:</strong><br/>
|
|
A series of operations are available, that can be executed on the input data:
|
|
<ul>
|
|
<li>Average (avg): average of all the numbers in the array</li>
|
|
<li>Maximum (max): get the number with the highest value from an array of numbers</li>
|
|
<li>Minimum (min): get the number with the lowest value from an array of numbers</li>
|
|
<li>Increment (inc): add 1 to the number</li>
|
|
<li>Decrement (dec): subtract 1 from the number</li>
|
|
<li>Integer part (trunc): truncate the number to the integer part</li>
|
|
<li>Round upwards (ceil): round the number upwards to the nearest integer</li>
|
|
<li>Round downwards (floor): round the number downwards to the nearest integer</li>
|
|
<li>Nearest integer (round): rounds the number to the nearest integer</li>
|
|
<li>Round decimal places (rdec): round the number at a specified number of decimal places</li>
|
|
<li>Sum (sum): sum of the all the numbers in the array</li>
|
|
<li>Subtract (sub): subtraction of the all the numbers in the array</li>
|
|
<li>Multiply (mult): multiply all the numbers in the array</li>
|
|
<li>Divide (div): division of all the numbers in the array</li>
|
|
<li>Modulus (mod): get the remainder of the division of the two numbers in the array</li>
|
|
<li>Absolute value (abs): absolute value of the number</li>
|
|
<li>Random (rand): a random number between 0 and 1</li>
|
|
<li>Random between min and max (randb): a random number between between the two numbers in the array)</li>
|
|
<li>Random from array (randa): a random number from an array of possible numbers</li>
|
|
<li>Length of array (len): the lenght of the array</li>
|
|
<li>Sort ascending (sorta): Sort the array ascending (i.e. from low to high)</li>
|
|
<li>Sort descending (sortd): Sort the array descending (i.e. from high to low)</li>
|
|
<li>Create range (range): Create an array of integer numbers, between the two numbers in the array</li>
|
|
<li>Get distance (dist): Get the distance (i.e. the range) between the numbers in the array</li>
|
|
<li>X to the power of y (pow): x<sup>y</sup> (from an array of two numbers [x, y])</li>
|
|
<li>E to the power of x (exp): value of E<sup>x</sup>, where E is Euler's number (approximately 2.7183)</li>
|
|
<li>Cubic root (cbrt): cubic root (x<sup>3</sup>) of the number</li>
|
|
<li>Natural logarithm (log): natural logarithm base E of the number</li>
|
|
<li>Logarithm (log10): logarithm base 10 of the number</li>
|
|
<li>Arccosine (acos): arccosine value of the number</li>
|
|
<li>Hyperbolic arccosine (acosh): hyperbolic arccosine of the number</li>
|
|
<li>Arcsine (asin): arcsine of the number in radians</li>
|
|
<li>Hyperbolic arcsine (asinh): hyperbolic arcsine of the number</li>
|
|
<li>Arctangent (atan): arctangent of the number, as a numeric value between -PI/2 and PI/2 radians</li>
|
|
<li>Hyperbolic arctangent (atanh): hyperbolic arctangent of the number</li>
|
|
<li>Cosine (cos): cosine of the number in radians</li>
|
|
<li>Hyperbolic cosine (cosh): hyperbolic cosine of the number</li>
|
|
<li>Sine (sin): sine of the number in radians</li>
|
|
<li>Hyperbolic sine (sinh): hyperbolic sine of the number</li>
|
|
<li>Square root (sqrt): square root of the number</li>
|
|
<li>Tangent (tan): tangent of an angle</li>
|
|
<li>Hyperbolic tangent (tanh): hyperbolic tangent of the number</li>
|
|
</ul>
|
|
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>
|
|
<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>
|
|
<p><strong>Constant field:</strong><br/>
|
|
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>
|
|
<p><strong>Round result to ... decimals:</strong><br/>
|
|
When selected, the output number(s) will be rounded to the specified number of decimals.</p>
|
|
</script>
|