@mschaeffler/node-red-logic 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/math_reduce.html CHANGED
@@ -6,6 +6,7 @@
6
6
  name:{value:""},
7
7
  topic:{value:""},
8
8
  property:{value:"payload", required:true},
9
+ propertyType:{value:"msg"},
9
10
  minMean:{value:"1", required:true, validate:function(v){
10
11
  return v!=='' && !isNaN(v) && Number(v)>0;}
11
12
  },
@@ -44,8 +45,8 @@
44
45
  oneditprepare: function() {
45
46
  $("#node-input-property").typedInput({
46
47
  type:"msg",
47
- types:["msg"],
48
- typeField: "#node-input-property-type"
48
+ types:["msg","jsonata"],
49
+ typeField: "#node-input-propertyType"
49
50
  });
50
51
  }
51
52
  });
@@ -63,7 +64,7 @@
63
64
  <div class="form-row">
64
65
  <label for="node-input-input"><i class="fa fa-ellipsis-h"></i> Eigenschaft</label>
65
66
  <input type="text" id="node-input-property">
66
- <input type="hidden" id="node-input-property-type">
67
+ <input type="hidden" id="node-input-propertyType">
67
68
  </div>
68
69
  <div class="form-row">
69
70
  <label for="node-input-minMean"><i class="fa fa-hourglass-o"></i> min. Mean</label>
package/math_reduce.js CHANGED
@@ -7,6 +7,7 @@ module.exports = function(RED) {
7
7
  var context = this.context();
8
8
  this.topic = config.topic ?? "";
9
9
  this.property = config.property || "payload";
10
+ this.propertyType = config.propertyType ?? "msg";
10
11
  this.minMean = Number( config.minMean ?? 1 );
11
12
  this.maxMean = Number( config.maxMean ?? 1 );
12
13
  this.minData = Number( config.minData ?? 1 );
@@ -14,6 +15,16 @@ module.exports = function(RED) {
14
15
  this.showState= Boolean( config.showState );
15
16
  this.filter = Boolean( config.filter );
16
17
  this.last = null;
18
+ if( this.propertyType === "jsonata" )
19
+ {
20
+ try {
21
+ this.propertyPrepared = RED.util.prepareJSONataExpression( this.property, this );
22
+ }
23
+ catch (e) {
24
+ node.error(RED._("debug.invalid-exp", {error: this.property}));
25
+ return;
26
+ }
27
+ }
17
28
  node.status( "" );
18
29
 
19
30
  node.on('input', function(msg,send,done) {
@@ -29,91 +40,115 @@ module.exports = function(RED) {
29
40
  }
30
41
  else
31
42
  {
32
- const payload = Number( RED.util.getMessageProperty( msg, node.property ) );
33
- let status = { shape:"dot" };
34
- if( ! isNaN( payload ) )
43
+ function getPayload(callback)
35
44
  {
36
- let data = context.get( "data" ) ?? {};
37
- let item = data[msg.topic] ?? [];
38
- item.push( payload )
39
- data[msg.topic] = item;
40
-
41
- msg.topic = node.topic;
42
- msg.count = 0;
43
- msg.data = data;
44
- switch( node.algo )
45
- {
46
- case "add":
47
- case "mean": msg.payload = 0; break;
48
- case "prod": msg.payload = 1; break;
49
- case "min": msg.payload = Number.MAX_SAFE_INTEGER; break;
50
- case "max": msg.payload = Number.MIN_SAFE_INTEGER; break;
51
- default: done( "invalid algo: "+node.algo ); return;
52
- }
53
- for( const key in data )
45
+ if( node.propertyPrepared )
54
46
  {
55
- if( data[key].length >= node.minMean )
47
+ RED.util.evaluateJSONataExpression( node.propertyPrepared, msg, function(err, value)
56
48
  {
57
- msg.count++;
58
- while( data[key].length > node.maxMean )
49
+ if( err )
59
50
  {
60
- data[key].shift();
51
+ done( RED._("debug.invalid-exp", {error: editExpression}) );
61
52
  }
62
- const help = data[key].reduce( (accumulator,value)=>accumulator+value ) / data[key].length;
63
- switch( node.algo )
53
+ else
64
54
  {
65
- case "add":
66
- case "mean": msg.payload += help; break;
67
- case "prod": msg.payload *= help; break;
68
- case "min": msg.payload = Math.min( msg.payload, help ); break;
69
- case "max": msg.payload = Math.max( msg.payload, help ); break;
55
+ callback( value );
70
56
  }
71
- }
57
+ } );
72
58
  }
73
- context.set( "data", data );
74
-
75
- if( msg.count >= node.minData )
59
+ else
60
+ {
61
+ callback( RED.util.getMessageProperty( msg, node.property ) );
62
+ }
63
+ }
64
+ getPayload( function(value)
65
+ {
66
+ const payload = Number( value );
67
+ let status = { shape:"dot" };
68
+ if( ! isNaN( payload ) )
76
69
  {
70
+ let data = context.get( "data" ) ?? {};
71
+ let item = data[msg.topic] ?? [];
72
+ item.push( payload )
73
+ data[msg.topic] = item;
74
+
75
+ msg.topic = node.topic;
76
+ msg.count = 0;
77
+ msg.data = data;
77
78
  switch( node.algo )
78
79
  {
79
- case "mean": msg.payload /= msg.count; break;
80
+ case "add":
81
+ case "mean": msg.payload = 0; break;
82
+ case "prod": msg.payload = 1; break;
83
+ case "min": msg.payload = Number.MAX_SAFE_INTEGER; break;
84
+ case "max": msg.payload = Number.MIN_SAFE_INTEGER; break;
85
+ default: done( "invalid algo: "+node.algo ); return;
80
86
  }
81
- status.text = msg.payload;
82
- if( node.filter )
87
+ for( const key in data )
83
88
  {
84
- if( msg.payload !== node.last )
89
+ if( data[key].length >= node.minMean )
85
90
  {
86
- node.last = msg.payload;
87
- status.fill = "green";
88
- send( msg );
91
+ msg.count++;
92
+ while( data[key].length > node.maxMean )
93
+ {
94
+ data[key].shift();
95
+ }
96
+ const help = data[key].reduce( (accumulator,value)=>accumulator+value ) / data[key].length;
97
+ switch( node.algo )
98
+ {
99
+ case "add":
100
+ case "mean": msg.payload += help; break;
101
+ case "prod": msg.payload *= help; break;
102
+ case "min": msg.payload = Math.min( msg.payload, help ); break;
103
+ case "max": msg.payload = Math.max( msg.payload, help ); break;
104
+ }
105
+ }
106
+ }
107
+ context.set( "data", data );
108
+
109
+ if( msg.count >= node.minData )
110
+ {
111
+ switch( node.algo )
112
+ {
113
+ case "mean": msg.payload /= msg.count; break;
114
+ }
115
+ status.text = msg.payload;
116
+ if( node.filter )
117
+ {
118
+ if( msg.payload !== node.last )
119
+ {
120
+ node.last = msg.payload;
121
+ status.fill = "green";
122
+ send( msg );
123
+ }
124
+ else
125
+ {
126
+ status.fill = "gray";
127
+ }
89
128
  }
90
129
  else
91
130
  {
92
- status.fill = "gray";
131
+ status.fill = "green";
132
+ send( msg );
93
133
  }
94
134
  }
95
135
  else
96
136
  {
97
- status.fill = "green";
98
- send( msg );
137
+ status.fill = "gray";
138
+ status.text = "to less data";
99
139
  }
100
140
  }
101
141
  else
102
142
  {
103
- status.fill = "gray";
104
- status.text = "to less data";
143
+ status.fill = "red";
144
+ status.text = "payload is NaN";
105
145
  }
106
- }
107
- else
108
- {
109
- status.fill = "red";
110
- status.text = "payload is NaN";
111
- }
112
- if( node.showState )
113
- {
114
- node.status( status );
115
- }
116
- done();
146
+ if( node.showState )
147
+ {
148
+ node.status( status );
149
+ }
150
+ done();
151
+ } );
117
152
  }
118
153
  });
119
154
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mschaeffler/node-red-logic",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "nodes for simple operations.",
5
5
  "license": "LGPL-2.1",
6
6
  "homepage": "https://github.com/m-schaeffler/node-red-my-nodes/tree/main/node-red-logic",