Fix default alarm node details builder script. Update rule nodes UI with help resources.

This commit is contained in:
Igor Kulikov 2021-10-18 19:28:30 +03:00
parent 2426ef52eb
commit c88a442603
10 changed files with 473 additions and 16 deletions

View File

@ -21,15 +21,13 @@ import lombok.Data;
public abstract class TbAbstractAlarmNodeConfiguration { public abstract class TbAbstractAlarmNodeConfiguration {
static final String ALARM_DETAILS_BUILD_JS_TEMPLATE = "" + static final String ALARM_DETAILS_BUILD_JS_TEMPLATE = "" +
"//***DO NOT CHANGE THIS LINES***\n" +
"var details = {};\n" + "var details = {};\n" +
"if (metadata.prevAlarmDetails) {\n" + "if (metadata.prevAlarmDetails) {\n" +
" details = JSON.parse(metadata.prevAlarmDetails);\n" + " details = JSON.parse(metadata.prevAlarmDetails);\n" +
" //remove prevAlarmDetails from metadata\n" + " //remove prevAlarmDetails from metadata\n" +
" delete metadata.prevAlarmDetails;\n" + " delete metadata.prevAlarmDetails;\n" +
" //now metadata is the same as it comes IN this rule node" + " //now metadata is the same as it comes IN this rule node\n" +
"}\n" + "}\n" +
"//***PLACE YOUR CODE BELOW***\n" +
"\n" + "\n" +
"\n" + "\n" +
"return details;"; "return details;";

View File

@ -0,0 +1,68 @@
#### Clear alarm details builder function
<div class="divider"></div>
<br/>
*function Details(msg, metadata, msgType): any*
JavaScript function generating **Alarm Details** object to update existing one. Used for storing additional parameters inside Alarm.<br>
For example you can save attribute name/value pair from Original Message payload or Metadata.
**Parameters:**
{% include rulenode/common_node_script_args %}
**Returns:**
Should return the object presenting **Alarm Details**.
Current Alarm Details can be accessed via `metadata.prevAlarmDetails`.<br>
**Note** that `metadata.prevAlarmDetails` is a raw String field, and it needs to be converted into object using this construction:
```javascript
var details = {};
if (metadata.prevAlarmDetails) {
// remove prevAlarmDetails from metadata
delete metadata.prevAlarmDetails;
details = JSON.parse(metadata.prevAlarmDetails);
}
{:copy-code}
```
<div class="divider"></div>
##### Examples
<ul>
<li>
Take <code>count</code> property from previous Alarm and increment it.<br>
Also put <code>temperature</code> attribute from inbound Message payload into Alarm details:
</li>
</ul>
```javascript
var details = {temperature: msg.temperature, count: 1};
if (metadata.prevAlarmDetails) {
var prevDetails = JSON.parse(metadata.prevAlarmDetails);
// remove prevAlarmDetails from metadata
delete metadata.prevAlarmDetails;
if (prevDetails.count) {
details.count = prevDetails.count + 1;
}
}
return details;
{:copy-code}
```
<br>
More details about Alarms can be found in [this tutorial{:target="_blank"}](${baseUrl}/docs/user-guide/alarms/).
You can see the real life example, where this node is used, in the next tutorial:
- [Create and Clear Alarms{:target="_blank"}](${baseUrl}/docs/user-guide/rule-engine-2-0/tutorials/create-clear-alarms/)
<br>
<br>

View File

@ -0,0 +1,8 @@
<ul>
<li><b>msg:</b> <code>{[key: string]: any}</code> - is a Message payload key/value object.
</li>
<li><b>metadata:</b> <code>{[key: string]: string}</code> - is a Message metadata key/value object.
</li>
<li><b>msgType:</b> <code>string</code> - is a string Message type. See <a href="https://github.com/thingsboard/thingsboard/blob/ea039008b148453dfa166cf92bc40b26e487e660/ui-ngx/src/app/shared/models/rule-node.models.ts#L338" target="_blank">MessageType</a> enum for common used values.
</li>
</ul>

View File

@ -0,0 +1,69 @@
#### Create alarm details builder function
<div class="divider"></div>
<br/>
*function Details(msg, metadata, msgType): any*
JavaScript function generating **Alarm Details** object. Used for storing additional parameters inside Alarm.<br>
For example you can save attribute name/value pair from Original Message payload or Metadata.
**Parameters:**
{% include rulenode/common_node_script_args %}
**Returns:**
Should return the object presenting **Alarm Details**.
**Optional:** previous Alarm Details can be accessed via `metadata.prevAlarmDetails`.<br>
If previous Alarm does not exist, this field will not be present in Metadata. **Note** that `metadata.prevAlarmDetails`<br>
is a raw String field, and it needs to be converted into object using this construction:
```javascript
var details = {};
if (metadata.prevAlarmDetails) {
// remove prevAlarmDetails from metadata
delete metadata.prevAlarmDetails;
details = JSON.parse(metadata.prevAlarmDetails);
}
{:copy-code}
```
<div class="divider"></div>
##### Examples
<ul>
<li>
Take <code>count</code> property from previous Alarm and increment it.<br>
Also put <code>temperature</code> attribute from inbound Message payload into Alarm details:
</li>
</ul>
```javascript
var details = {temperature: msg.temperature, count: 1};
if (metadata.prevAlarmDetails) {
var prevDetails = JSON.parse(metadata.prevAlarmDetails);
// remove prevAlarmDetails from metadata
delete metadata.prevAlarmDetails;
if (prevDetails.count) {
details.count = prevDetails.count + 1;
}
}
return details;
{:copy-code}
```
<br>
More details about Alarms can be found in [this tutorial{:target="_blank"}](${baseUrl}/docs/user-guide/alarms/).
You can see the real life example, where this node is used, in the next tutorial:
- [Create and Clear Alarms{:target="_blank"}](${baseUrl}/docs/user-guide/rule-engine-2-0/tutorials/create-clear-alarms/)
<br>
<br>

View File

@ -0,0 +1,69 @@
#### Filter message function
<div class="divider"></div>
<br/>
*function Filter(msg, metadata, msgType): boolean*
JavaScript function evaluating **true/false** condition on incoming Message.
**Parameters:**
{% include rulenode/common_node_script_args %}
**Returns:**
Should return `boolean` value. If `true` - send Message via **True** chain, otherwise **False** chain is used.
<div class="divider"></div>
##### Examples
* Forward all messages with `temperature` value greater than `20` to the **True** chain and all other messages to the **False** chain:
```javascript
return msg.temperature > 20;
{:copy-code}
```
* Forward all messages with type `ATTRIBUTES_UPDATED` to the **True** chain and all other messages to the **False** chain:
```javascript
if (msgType === 'ATTRIBUTES_UPDATED') {
return true;
} else {
return false;
}
{:copy-code}
```
<ul>
<li>Send message to the <strong>True</strong> chain if the following conditions are met.<br>Message type is <code>POST_TELEMETRY_REQUEST</code> and<br>
(device type is <code>vehicle</code> and <code>humidity</code> value is greater than <code>50</code> or<br>
device type is <code>controller</code> and <code>temperature</code> value is greater than <code>20</code> and <code>humidity</code> value is greater than <code>60</code>).<br>
Otherwise send message to the <strong>False</strong> chain:
</li>
</ul>
```javascript
if (msgType === 'POST_TELEMETRY_REQUEST') {
if (metadata.deviceType === 'vehicle') {
return msg.humidity > 50;
} else if (metadata.deviceType === 'controller') {
return msg.temperature > 20 && msg.humidity > 60;
}
}
return false;
{:copy-code}
```
<br>
You can see real life example, how to use this node in those tutorials:
- [Create and Clear Alarms{:target="_blank"}](${baseUrl}/docs/user-guide/rule-engine-2-0/tutorials/create-clear-alarms/#node-a-filter-script)
- [Reply to RPC Calls{:target="_blank"}](${baseUrl}/docs/user-guide/rule-engine-2-0/tutorials/rpc-reply-tutorial#add-filter-script-node)
<br>
<br>

View File

@ -0,0 +1,118 @@
#### Message generator function
<div class="divider"></div>
<br/>
*function Generate(prevMsg, prevMetadata, prevMsgType): {msg: object, metadata: object, msgType: string}*
JavaScript function generating new message using previous Message payload, Metadata and Message type as input arguments.
**Parameters:**
<ul>
<li><b>prevMsg:</b> <code>{[key: string]: any}</code> - is a previously generated Message payload key/value object.
</li>
<li><b>prevMetadata:</b> <code>{[key: string]: string}</code> - is a previously generated Message metadata key/value object.
</li>
<li><b>prevMsgType:</b> <code>string</code> - is a previously generated string Message type. See <a href="https://github.com/thingsboard/thingsboard/blob/ea039008b148453dfa166cf92bc40b26e487e660/ui-ngx/src/app/shared/models/rule-node.models.ts#L338" target="_blank">MessageType</a> enum for common used values.
</li>
</ul>
**Returns:**
Should return the object with the following structure:
```javascript
{
msg?: {[key: string]: any},
metadata?: {[key: string]: string},
msgType?: string
}
```
All fields in resulting object are optional and will be taken from previously generated Message if not specified.
<div class="divider"></div>
##### Examples
* Generate message of type `POST_TELEMETRY_REQUEST` with random `temperature` value from `18` to `32`:
```javascript
var temperature = 18 + Math.random() * 14;
// Round to at most 2 decimal places (optional)
temperature = Math.round( temperature * 100 ) / 100;
var msg = { temperature: temperature };
var metadata = {};
var msgType = "POST_TELEMETRY_REQUEST";
return { msg: msg, metadata: metadata, msgType: msgType };
{:copy-code}
```
<ul>
<li>
Generate message of type <code>POST_TELEMETRY_REQUEST</code> with <code>temp</code> value <code>42</code>,
<code>humidity</code> value <code>77</code><br>
and <strong>metadata</strong> with field <code>data</code> having value <code>40</code>:
</li>
</ul>
```javascript
var msg = { temp: 42, humidity: 77 };
var metadata = { data: 40 };
var msgType = "POST_TELEMETRY_REQUEST";
return { msg: msg, metadata: metadata, msgType: msgType };
{:copy-code}
```
<ul>
<li>
Generate message of type <code>POST_TELEMETRY_REQUEST</code> with <code>temperature</code> value<br>
increasing and decreasing linearly in the range from <code>18</code> to <code>32</code>:
</li>
</ul>
```javascript
var lower = 18;
var upper = 32;
var isDecrement = 'false';
var temperature = lower;
// Get previous values
if (typeof prevMetadata !== 'undefined' &&
typeof prevMetadata.isDecrement !== 'undefined') {
isDecrement = prevMetadata.isDecrement;
}
if (typeof prevMsg !== 'undefined' &&
typeof prevMsg.temperature !== 'undefined') {
temperature = prevMsg.temperature;
}
if (isDecrement === 'true') {
temperature--;
if (temperature <= lower) {
isDecrement = 'false';
temperature = lower;
}
} else {
temperature++;
if (temperature >= upper) {
isDecrement = 'true';
temperature = upper;
}
}
var msg = { temperature: temperature };
var metadata = { isDecrement: isDecrement };
var msgType = "POST_TELEMETRY_REQUEST";
return { msg: msg, metadata: metadata, msgType: msgType };
{:copy-code}
```
<br>
<br>

View File

@ -0,0 +1,37 @@
#### Message to string function
<div class="divider"></div>
<br/>
*function toString(msg, metadata, msgType): string*
JavaScript function transforming incoming Message to String for further logging to the server log file.
**Parameters:**
{% include rulenode/common_node_script_args %}
**Returns:**
Should return `string` value used for logging to the server log file.
<div class="divider"></div>
##### Examples
* Create string message containing incoming message and incoming metadata values:
```javascript
return 'Incoming message:\n' + JSON.stringify(msg) +
'\nIncoming metadata:\n' + JSON.stringify(metadata);
{:copy-code}
```
<br>
You can see real life example, how to use this node in this tutorial:
- [Reply to RPC Calls{:target="_blank"}](${baseUrl}/docs/user-guide/rule-engine-2-0/tutorials/rpc-reply-tutorial#log-unknown-request)
<br>
<br>

View File

@ -0,0 +1,96 @@
#### Switch message function
<div class="divider"></div>
<br/>
*function Switch(msg, metadata, msgType): string[]*
JavaScript function computing **an array of next Relation names** for incoming Message.
**Parameters:**
{% include rulenode/common_node_script_args %}
**Returns:**
Should return an array of `string` values presenting **next Relation names** where Message should be routed.<br>
If returned array is empty - message will not be routed to any Node and discarded.
<div class="divider"></div>
##### Examples
<ul>
<li>
Forward all messages with <code>temperature</code> value greater than <code>30</code> to the <strong>'High temperature'</strong> chain,<br>
with <code>temperature</code> value lower than <code>20</code> to the <strong>'Low temperature'</strong> chain and all other messages<br>
to the <strong>'Normal temperature'</strong> chain:
</li>
</ul>
```javascript
if (msg.temperature > 30) {
return ['High temperature'];
} else if (msg.temperature < 20) {
return ['Low temperature'];
} else {
return ['Normal temperature'];
}
{:copy-code}
```
<ul>
<li>
For messages with type <code>POST_TELEMETRY_REQUEST</code>:
<ul>
<li>
if <code>temperature</code> value lower than <code>18</code> forward to the <strong>'Low temperature telemetry'</strong> chain,
</li>
<li>
otherwise to the <strong>'Normal temperature telemetry'</strong> chain.
</li>
</ul>
For messages with type <code>POST_ATTRIBUTES_REQUEST</code>:<br>
<ul>
<li>
if <code>currentState</code> value is <code>IDLE</code> forward to the <strong>'Idle State'</strong> and <strong>'Update State Attribute'</strong> chains,
</li>
<li>
if <code>currentState</code> value is <code>RUNNING</code> forward to the <strong>'Running State'</strong> and <strong>'Update State Attribute'</strong> chains,
</li>
<li>
otherwise to the <strong>'Unknown State'</strong> chain.
</li>
</ul>
For all other message types - discard the message (do not route to any Node).
</li>
</ul>
```javascript
if (msgType === 'POST_TELEMETRY_REQUEST') {
if (msg.temperature < 18) {
return ['Low Temperature Telemetry'];
} else {
return ['Normal Temperature Telemetry'];
}
} else if (msgType === 'POST_ATTRIBUTES_REQUEST') {
if (msg.currentState === 'IDLE') {
return ['Idle State', 'Update State Attribute'];
} else if (msg.currentState === 'RUNNING') {
return ['Running State', 'Update State Attribute'];
} else {
return ['Unknown State'];
}
}
return [];
{:copy-code}
```
<br>
You can see real life example, how to use this node in this tutorial:
- [Data function based on telemetry from 2 devices{:target="_blank"}](${baseUrl}/docs/user-guide/rule-engine-2-0/tutorials/function-based-on-telemetry-from-two-devices#delta-temperature-rule-chain)
<br>
<br>

View File

@ -3,20 +3,13 @@
<div class="divider"></div> <div class="divider"></div>
<br/> <br/>
*function (msg, metadata msgType): {msg: object, metadata: object, msgType: string}* *function Transform(msg, metadata, msgType): {msg: object, metadata: object, msgType: string}*
JavaScript function transforming input Message payload, Metadata or Message type. JavaScript function transforming input Message payload, Metadata or Message type.
**Parameters:** **Parameters:**
<ul> {% include rulenode/common_node_script_args %}
<li><b>msg:</b> <code>{[key: string]: any}</code> - is a Message payload key/value object.
</li>
<li><b>metadata:</b> <code>{[key: string]: string}</code> - is a Message metadata key/value object.
</li>
<li><b>msgType:</b> <code>string</code> - is a string Message type. See <a href="https://github.com/thingsboard/thingsboard/blob/ea039008b148453dfa166cf92bc40b26e487e660/ui-ngx/src/app/shared/models/rule-node.models.ts#L338" target="_blank">MessageType</a> enum for common used values.
</li>
</ul>
**Returns:** **Returns:**
@ -47,8 +40,6 @@ return { msgType: 'CUSTOM_REQUEST' };
<li>Change message type to <code>CUSTOM_UPDATE</code>,<br/>add additional attribute <strong><em>version</em></strong> into payload with value <strong><em>v1.1</em></strong>,<br/>change <strong><em>sensorType</em></strong> attribute value in Metadata to <strong><em>roomTemp</em></strong>:</li> <li>Change message type to <code>CUSTOM_UPDATE</code>,<br/>add additional attribute <strong><em>version</em></strong> into payload with value <strong><em>v1.1</em></strong>,<br/>change <strong><em>sensorType</em></strong> attribute value in Metadata to <strong><em>roomTemp</em></strong>:</li>
</ul> </ul>
The following transform function will perform all necessary modifications:
```javascript ```javascript
var newType = "CUSTOM_UPDATE"; var newType = "CUSTOM_UPDATE";
msg.version = "v1.1"; msg.version = "v1.1";
@ -57,9 +48,12 @@ return {msg: msg, metadata: metadata, msgType: newType};
{:copy-code} {:copy-code}
``` ```
<br>
You can see real life example, how to use this node in those tutorials: You can see real life example, how to use this node in those tutorials:
- [Transform incoming telemetry{:target="_blank"}](${baseUrl}/docs/user-guide/rule-engine-2-0/tutorials/transform-incoming-telemetry/) - [Transform incoming telemetry{:target="_blank"}](${baseUrl}/docs/user-guide/rule-engine-2-0/tutorials/transform-incoming-telemetry/)
- [Reply to RPC Calls{:target="_blank"}](${baseUrl}/docs/user-guide/rule-engine-2-0/tutorials/rpc-reply-tutorial#add-transform-script-node) - [Reply to RPC Calls{:target="_blank"}](${baseUrl}/docs/user-guide/rule-engine-2-0/tutorials/rpc-reply-tutorial#add-transform-script-node)
<br>
<br>