/
event-filter.html
283 lines (240 loc) · 8.8 KB
/
event-filter.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<script type="text/javascript">
let Changed = false;
let ActiveFilter = undefined;
let IndexMap = undefined;
RED.nodes.registerType('event-filter', {
category: 'ZWave JS',
color: 'rgb(46,145,205)',
defaults: {
name: { value: 'ZWave Event Filter' },
filters: { value: [] },
outputs: { value: 0 },
changeDate: { value: undefined },
showStatus: { value: true }
},
inputs: 1,
outputs: 0,
icon: 'rbe.png',
label: function () {
return this.name;
},
oneditprepare: SortUI,
oneditsave: DoSave,
oneditcancel: function () {
Changed = false;
ActiveFilter = undefined;
},
outputLabels: function (index) {
return this.filters[index].name;
},
paletteLabel: 'Event Filter',
oneditresize: function (size) {
$('#filtersets').editableList('height', size.height - 70);
}
});
function compare(a, b) {
if (a.index < b.index) {
return -1;
}
if (a.index > b.index) {
return 1;
}
return 0;
}
function SortUI() {
IndexMap = {};
$('#filtersets').editableList({
header: $('<div>').append(
'<div>Filter Sets - order defines outputs</div>'
),
sortable: true,
removable: true,
addButton: 'Add Filter Set',
addItem: AddItem,
sortItems: SortItems,
removeItem: RemoveItem
});
this.filters.sort(compare).forEach((Item) => {
$('#filtersets').editableList('addItem', Item);
});
}
function SortItems(Items) {
for (let i = 0; i < Items.length; i++) {
IndexMap[Items[i].data('data').originalIndex] = i;
Items[i].data('data').index = i;
}
}
function RemoveItem(Item) {
IndexMap[Item.originalIndex] = -1;
const Items = $('#filtersets').editableList('items');
for (let i = 0; i < Items.length; i++) {
IndexMap[Items[i].data('data').originalIndex] = i;
Items[i].data('data').index = i;
}
}
function AddItem(container, index, data) {
if (Object.keys(data).length === 0) {
data.index = index;
data.name = 'Filter Name';
data.valueIds = [];
data.events = ['VALUE_UPDATED'];
data.strict = false;
data.id = Math.floor((0x99999 - 0x10000) * Math.random()).toString();
}
data.originalIndex = index;
IndexMap[data.originalIndex] = index;
$(container).data('data', data);
$(container).click(SetActiveFilter);
$(container).css({ minWidth: '400px' });
$(container).parent().css({ height: '45px', overflow: 'hidden' });
const HTML = `
<table style="width:100%">
<tr>
<td>Name</td>
<td><input class="data-name" value="${data.name}" type="text"></td>
</tr>
<tr>
<td>ValueIDs <span class="data-vid-count">${data.valueIds.length}</span> </td>
<td><input class="data-valueid-editor" value="" type="text"></td>
</tr>
<tr>
<td>Strict (Match Endpoint)</td>
<td><input class="data-event-Endpoint" type="checkbox"></td>
</tr>
<tr>
<td>Events</td>
<td>
<input class="data-event-VALUE_UPDATED" type="checkbox"> VALUE_UPDATED<br />
<input class="data-event-VALUE_NOTIFICATION" type="checkbox"> VALUE_NOTIFICATION<br />
<input class="data-event-NOTIFICATION" type="checkbox"> NOTIFICATION<br />
<input class="data-event-GET_VALUE_RESPONSE" type="checkbox"> GET_VALUE_RESPONSE
</td>
</tr>
`;
$(container).html(HTML);
$('<button>')
.addClass('ui-button')
.addClass('ui-corner-all')
.addClass('ui-widget')
.addClass('rightButton')
.html('↻')
.click(() => {
SyncEdits();
})
.insertAfter($(container).find('.data-valueid-editor'));
$(container)
.find('.data-valueid-editor')
.typedInput({
types: ['json'],
default: 'json'
});
$(container)
.find('.data-valueid-editor')
.typedInput('value', JSON.stringify(data.valueIds));
SortChecbox($(container));
}
function SyncEdits() {
const JSONs = ActiveFilter.find('.data-valueid-editor').typedInput('value');
const NewObject = JSON.parse(JSONs);
ActiveFilter.data('data').valueIds = NewObject;
const Count = NewObject.length;
ActiveFilter.find('.data-vid-count').html(Count);
Changed = true;
}
function SetActiveFilter() {
if (
ActiveFilter === undefined ||
ActiveFilter.data('data').id !== $(this).data('data').id
) {
const Items = $('#filtersets').editableList('items');
for (let i = 0; i < Items.length; i++) {
Items[i].parent().animate({ height: '45px' }, 200);
}
ActiveFilter = $(this);
$(this).parent().animate({ height: '200px' }, 200);
}
}
function SortChecbox(El) {
const Data = El.data('data');
Data.events.forEach((EV) => {
El.find('.data-event-' + EV).prop('checked', true);
});
if (Data.strict) {
El.find('.data-event-Endpoint').prop('checked', true);
}
}
function DoSave() {
if (Changed) {
this.changeDate = new Date();
Changed = false;
}
const Items = $('#filtersets').editableList('items').sort(compare);
const Events = [
'VALUE_UPDATED',
'VALUE_NOTIFICATION',
'NOTIFICATION',
'GET_VALUE_RESPONSE'
];
this.filters = [];
for (let i = 0; i < Items.length; i++) {
const El = $(Items[i]);
const ItemData = Items[i].data('data');
ItemData.events = [];
Events.forEach((EV) => {
if (El.find('.data-event-' + EV).prop('checked')) {
ItemData.events.push(EV);
}
});
ItemData.name = El.find('.data-name').val();
ItemData.strict = El.find('.data-event-Endpoint').prop('checked');
delete ItemData.originalIndex;
this.filters.push(ItemData);
}
ActiveFilter = undefined;
$('#node-input-outputs').val(JSON.stringify(IndexMap));
}
function AddValueIDToFilter(ValueID) {
if (ActiveFilter !== undefined) {
const VIDs = ActiveFilter.data('data').valueIds;
VIDs.push(ValueID);
const Count = VIDs.length;
ActiveFilter.find('.data-vid-count').html(Count);
ActiveFilter.find('.data-valueid-editor').typedInput(
'value',
JSON.stringify(VIDs)
);
Changed = true;
return true;
} else {
return false;
}
}
</script>
<script type="text/x-red" data-template-name="event-filter">
<input type="hidden" id="node-input-outputs">
<div class="form-row">
<label for="node-input-name" style="width:130px"><i class="fa fa-pencil"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Filter Name" style="width: calc(100% - 135px)">
</div>
<div class="form-row">
<label for="node-input-showStatus" style="width:130px"><i class="fa fa-pencil"></i> Show Status</label>
<input type="checkbox" id="node-input-showStatus" />
</div>
<div>
<ol id="filtersets" style="min-height:450px;min-width:400px"> </ol>
</div>
</script>
<!-- prettier-ignore -->
<script type="text/markdown" data-help-name="event-filter">
<p>A Z-Wave event filter.</p>
This node allows advanced filtration of the various value events that a node may send. It further allows filtering the type of data that was updated i.e. a Motion Event, Air Temp changes, Door Sensors, etc.
The UI panel on the right can be used to provide the filter values, removing the need to write them yourself, but of course you can use manual entries if you prefer. Please visit our [event filter](https://github.com/zwave-js/node-red-contrib-zwave-js/wiki/Event-Filter-Node) wiki page for a detailed walkthrough on how to set up your filters.
Essentially this node is a `switch` node with specific sorting for the values found in Z-Wave messages. The Input of this node should be from a `ZWave Controller` node or a `ZWave Device` node.
### Message Handling
Inputs are unfiltered Z-Wave messages. The filter settings are used to decide if the message will be output from the node. The message will be output on the pin corresponding to the matched filter set. If there is a match, the node will stop searching for additional matches. If there is no match, the input message will stop with this node and nothing will be output.
### Filter Options
**Name** - is a name for your filter. The output pin will match this name, and will be included in the output as `msg.filter.name`.
**ValueIDs** - can include zero or more Value IDs for this filter set. Value IDs may be added using the UI or manually. See our [event filter](https://github.com/zwave-js/node-red-contrib-zwave-js/wiki/Event-Filter-Node) wiki page for more details.
**Strict** - if checked it will require the `Endpoint` property of the Input message to match the selected ValueIDs.
**Events** - the four event types which may be output from a Z-Wave Device. At least one must be selected for any message to be output. See details about these events on [this](https://github.com/zwave-js/node-red-contrib-zwave-js/wiki/Payload-Messaging-Format) wiki page.
</script>