smart-nodes 0.3.13 → 0.3.15

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/README.md CHANGED
@@ -167,7 +167,7 @@ This control parses a text and performs actions to the selected and matching sma
167
167
 
168
168
  [ text ] is optional
169
169
 
170
- - Turn on [the light in] the living room and int the kitchen off
170
+ - Turn on [the light in] the living room and the kitchen off
171
171
  - Open [the shutter in] the kitchen and turn studio off
172
172
  - Close [the shutter in the] sleeping room
173
173
  - Living room to 10 %
package/delay/delay.js CHANGED
@@ -103,7 +103,7 @@ module.exports = function (RED)
103
103
 
104
104
  if (delay_only_on_change)
105
105
  {
106
- if (timeout)
106
+ if (timeout != null)
107
107
  {
108
108
  // current delay runs already for the same payload, so don't start a new one.
109
109
  if (next_payload == msg.payload)
@@ -151,6 +151,7 @@ module.exports = function (RED)
151
151
  node.status({ fill: "yellow", shape: "ring", text: (new Date()).toLocaleString() + ": " + "Forward msg.topic = '" + (msg.topic ?? "null") + "' msg.payload = '" + (msg.payload ?? "null") + "' in " + helper.formatMsToStatus(delayMs, "at") });
152
152
  timeout = setTimeout(() =>
153
153
  {
154
+ timeout = null;
154
155
  node.status({ fill: "yellow", shape: "dot", text: (new Date()).toLocaleString() + ": " + "Sended msg.topic = '" + (msg.topic ?? "null") + "' msg.payload = '" + (msg.payload ?? "null") });
155
156
  nodeSettings.lastMessage = msg
156
157
 
@@ -88,6 +88,14 @@ module.exports = function (RED)
88
88
  node.status({ fill: "red", shape: "dot", text: (new Date()).toLocaleString() + ": Forwarding disabled" });
89
89
  }
90
90
 
91
+ if (config.save_state && config.resend_on_start && nodeSettings.lastMessage != null)
92
+ {
93
+ setTimeout(() =>
94
+ {
95
+ node.send(nodeSettings.lastMessage);
96
+ }, 10000);
97
+ }
98
+
91
99
  setStatus();
92
100
  }
93
101
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smart-nodes",
3
- "version": "0.3.13",
3
+ "version": "0.3.15",
4
4
  "description": "Smart Nodes",
5
5
  "keywords": [
6
6
  "node-red",
@@ -14,6 +14,16 @@
14
14
  <div class="form-row node-scheduler-schedules-row">
15
15
  <ol id="node-scheduler-schedules"></ol>
16
16
  </div>
17
+ <hr/>
18
+ <h4 style="margin: 0.5rem 0;">Systemstart</h4>
19
+ <div class="form-row">
20
+ <input type="checkbox" id="node-input-save_state" style="width: 20px;" />
21
+ <label for="node-input-save_state" style="width: calc(100% - 30px);">Zustand speichern</label>
22
+ </div>
23
+ <div class="form-row" id="resend_on_start_row">
24
+ <input type="checkbox" id="node-input-resend_on_start" style="width: 20px;" />
25
+ <label for="node-input-resend_on_start" style="width: calc(100% - 30px);">Letze Nachricht 10 Sekunden nach dem Start senden</label>
26
+ </div>
17
27
  </script>
18
28
 
19
29
  <script type="text/html" data-template-scheduler-row="">
@@ -117,6 +127,8 @@
117
127
  return true;
118
128
  },
119
129
  },
130
+ save_state: { value: true },
131
+ resend_on_start: { value: true }
120
132
  },
121
133
  inputs: 1,
122
134
  outputs: 1,
@@ -129,6 +141,15 @@
129
141
  {
130
142
  let node = this;
131
143
 
144
+ $("#node-input-save_state").on("change", ev =>
145
+ {
146
+ if (ev.target.checked)
147
+ $("#resend_on_start_row").show();
148
+ else
149
+ $("#resend_on_start_row").hide();
150
+ });
151
+ $("#node-input-save_state").trigger("change");
152
+
132
153
  /**
133
154
  * prepare schedule
134
155
  */
@@ -8,13 +8,26 @@ module.exports = function (RED)
8
8
  const smartContext = require("../persistence.js")(RED);
9
9
  const helper = require("../smart_helper.js");
10
10
 
11
- var nodeSettings = Object.assign({}, {
11
+ var nodeSettings = {
12
12
  enabled: config.enabled,
13
- }, smartContext.get(node.id));
13
+ lastMessage: null,
14
+ };
14
15
 
15
- let timeouts = [];
16
- let nextEvents = [];
16
+ if (config.save_state)
17
+ {
18
+ // load old saved values
19
+ nodeSettings = Object.assign(nodeSettings, smartContext.get(node.id));
20
+ }
21
+ else
22
+ {
23
+ // delete old saved values
24
+ smartContext.del(node.id);
25
+ }
26
+
27
+ let timeout = null;
28
+ let nextEvent = null;
17
29
 
30
+ // prepare schedules
18
31
  setTimeout(() =>
19
32
  {
20
33
  for (let i = 0; i < config.schedules.length; i++)
@@ -26,7 +39,7 @@ module.exports = function (RED)
26
39
  }
27
40
 
28
41
  if (nodeSettings.enabled)
29
- initTimeouts();
42
+ initNextTimeout();
30
43
 
31
44
  setStatus();
32
45
  }, 1000);
@@ -41,6 +54,8 @@ module.exports = function (RED)
41
54
  return;
42
55
 
43
56
  nodeSettings.enabled = true;
57
+ if (config.save_state)
58
+ smartContext.set(node.id, nodeSettings);
44
59
  break;
45
60
 
46
61
  case "disable":
@@ -48,6 +63,8 @@ module.exports = function (RED)
48
63
  return;
49
64
 
50
65
  nodeSettings.enabled = false;
66
+ if (config.save_state)
67
+ smartContext.set(node.id, nodeSettings);
51
68
  break;
52
69
 
53
70
  case "set_state":
@@ -55,6 +72,8 @@ module.exports = function (RED)
55
72
  return;
56
73
 
57
74
  nodeSettings.enabled = !!msg.payload;
75
+ if (config.save_state)
76
+ smartContext.set(node.id, nodeSettings);
58
77
  break;
59
78
 
60
79
  default:
@@ -72,57 +91,42 @@ module.exports = function (RED)
72
91
 
73
92
  node.on("close", function ()
74
93
  {
75
- clearTimeouts();
94
+ if (timeout != null)
95
+ {
96
+ clearTimeout(timeout);
97
+ timeout = null;
98
+ }
76
99
  });
77
100
 
78
- let clearTimeouts = () =>
101
+ let initNextTimeout = () =>
79
102
  {
80
- for (let i = 0; i < timeouts.length; i++)
103
+ let minIndex = null;
104
+ for (let i = 0; i < config.schedules.length; i++)
81
105
  {
82
- if (timeouts[i] != 0)
106
+ config.schedules[i].nextEvent = calcNextEvent(i);
107
+ if (config.schedules[i].nextEvent != null)
83
108
  {
84
- clearTimeout(timeouts[i]);
85
- timeouts[i] = 0;
109
+ if (minIndex == null || config.schedules[i].nextEvent < config.schedules[minIndex].nextEvent)
110
+ minIndex = i;
86
111
  }
87
112
  }
88
- }
89
113
 
90
- let initTimeouts = () =>
91
- {
92
- for (let i = 0; i < config.schedules.length; i++)
114
+ // No events defined
115
+ if (minIndex == null)
93
116
  {
94
- initTimeout(i, config.schedules[i]);
95
- }
96
- }
97
-
98
- let initTimeout = (i, schedule) =>
99
- {
100
- if (!nodeSettings.enabled)
117
+ nextEvent = null;
101
118
  return;
102
-
103
- let waitTime = getWaitInMs(i, schedule);
104
- if (waitTime != null)
105
- {
106
- if (timeouts[i])
107
- clearTimeout(timeouts[i]);
108
-
109
- timeouts[i] = setTimeout(raiseEvent, waitTime, i, schedule);
110
119
  }
111
- }
112
120
 
113
- let raiseEvent = (i, schedule) =>
114
- {
115
- if (!nodeSettings.enabled)
116
- return;
117
-
118
- timeouts[i] = 0;
119
- node.send(schedule.message);
120
- initTimeout(i, schedule);
121
- setStatus();
121
+ nextEvent = config.schedules[minIndex].nextEvent;
122
+ let waitTime = nextEvent.getTime() - (new Date()).getTime();
123
+ timeout = setTimeout(raiseEvent, waitTime, minIndex);
122
124
  }
123
125
 
124
- let getWaitInMs = (i, schedule) =>
126
+ let calcNextEvent = i =>
125
127
  {
128
+ const schedule = config.schedules[i];
129
+
126
130
  // If no day is checked then we cannot it is never raised
127
131
  if (!schedule.days || schedule.days.length == 0)
128
132
  return null;
@@ -148,26 +152,51 @@ module.exports = function (RED)
148
152
  }
149
153
 
150
154
  // find next day when the event should be raised
151
- let possibleDays = schedule.days.filter(d => findNextDay ? d > now.getDay() : d >= now.getDay());
152
- if (possibleDays.length == 0)
153
- possibleDays = Math.min(...schedule.days);
155
+ let possibleDay = schedule.days.filter(d => findNextDay ? d > now.getDay() : d >= now.getDay());
156
+ if (possibleDay.length == 0)
157
+ possibleDay = Math.min(...schedule.days);
154
158
  else
155
- possibleDays = Math.min(...possibleDays);
159
+ possibleDay = Math.min(...possibleDay);
156
160
 
157
161
  let nextEvent = new Date(
158
162
  now.getFullYear(),
159
163
  now.getMonth(),
160
164
  now.getDate() + (
161
- possibleDays < now.getDay() ? 7 - now.getDay() + possibleDays : possibleDays - now.getDay()
165
+ findNextDay ?
166
+ possibleDay <= now.getDay() ? 7 - now.getDay() + possibleDay : possibleDay - now.getDay()
167
+ :
168
+ possibleDay < now.getDay() ? 7 - now.getDay() + possibleDay : possibleDay - now.getDay()
162
169
  ),
163
170
  schedule.hour,
164
171
  schedule.minute,
165
172
  schedule.second
166
173
  );
167
174
 
168
- nextEvents[i] = nextEvent;
175
+ // console.log({
176
+ // i,
177
+ // findNextDay,
178
+ // nextEvent
179
+ // });
169
180
 
170
- return nextEvent.getTime() - now.getTime();
181
+ return nextEvent;
182
+ }
183
+
184
+ let raiseEvent = i =>
185
+ {
186
+ const schedule = config.schedules[i];
187
+
188
+ if (!nodeSettings.enabled)
189
+ return;
190
+
191
+ timeout = null;
192
+ node.send(schedule.message);
193
+ nodeSettings.lastMessage = schedule.message;
194
+
195
+ if (config.save_state)
196
+ smartContext.set(node.id, nodeSettings);
197
+
198
+ initNextTimeout();
199
+ setStatus();
171
200
  }
172
201
 
173
202
  let setStatus = () =>
@@ -180,7 +209,7 @@ module.exports = function (RED)
180
209
  text: (new Date()).toLocaleString() + ": Scheduler disabled"
181
210
  });
182
211
  }
183
- else if (nextEvents.filter(d => d).lenght == 0)
212
+ else if (nextEvent == null)
184
213
  {
185
214
  node.status({
186
215
  fill: "red",
@@ -191,7 +220,6 @@ module.exports = function (RED)
191
220
  else
192
221
  {
193
222
  // filter out empty values
194
- let nextEvent = new Date(Math.min(...nextEvents.filter(d => d)));
195
223
  let time = nextEvent.getTime() - (new Date()).getTime();
196
224
  time = Math.ceil(time / 1000) * 1000;
197
225
 
@@ -203,6 +231,13 @@ module.exports = function (RED)
203
231
  }
204
232
  }
205
233
 
234
+ if (config.save_state && config.resend_on_start && nodeSettings.lastMessage != null)
235
+ {
236
+ setTimeout(() =>
237
+ {
238
+ node.send(nodeSettings.lastMessage);
239
+ }, 10000);
240
+ }
206
241
  }
207
242
 
208
243
  RED.nodes.registerType("smart_scheduler", SchedulerNode);