node-red-contrib-unifi-ultimate 0.1.0-beta.1
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/LICENSE +21 -0
- package/README.md +140 -0
- package/examples/unifi-protect-info.json +87 -0
- package/nodes/icons/unifi-protect-icon.svg +5 -0
- package/nodes/unifi-protect-config.html +52 -0
- package/nodes/unifi-protect-config.js +382 -0
- package/nodes/unifi-protect-device.html +715 -0
- package/nodes/unifi-protect-device.js +316 -0
- package/nodes/utils/unifi-protect-device-registry.js +1541 -0
- package/nodes/utils/unifi-protect-utils.js +153 -0
- package/package.json +41 -0
|
@@ -0,0 +1,715 @@
|
|
|
1
|
+
<script type="text/javascript">
|
|
2
|
+
RED.nodes.registerType("unifi-protect-device", {
|
|
3
|
+
category: "Unifi Ultimate",
|
|
4
|
+
color: "#7DB7E8",
|
|
5
|
+
defaults: {
|
|
6
|
+
name: { value: "" },
|
|
7
|
+
server: { value: "", type: "unifi-protect-config", required: true },
|
|
8
|
+
deviceType: { value: "", required: true },
|
|
9
|
+
deviceId: { value: "", required: true },
|
|
10
|
+
capability: { value: "observe", required: true },
|
|
11
|
+
capabilityConfig: { value: "{}" },
|
|
12
|
+
timeout: { value: 15000, required: true, validate: RED.validators.number() }
|
|
13
|
+
},
|
|
14
|
+
inputs: 1,
|
|
15
|
+
outputs: 2,
|
|
16
|
+
icon: "unifi-protect-icon.svg",
|
|
17
|
+
paletteLabel: "DEVICE",
|
|
18
|
+
outputLabels: ["state", "events"],
|
|
19
|
+
label: function() {
|
|
20
|
+
return this.name || this.deviceId || "Unifi Protect Device";
|
|
21
|
+
},
|
|
22
|
+
oneditsave: function() {
|
|
23
|
+
const $capabilityConfig = $("#node-input-capabilityConfig");
|
|
24
|
+
const capabilityConfig = {};
|
|
25
|
+
if ($capabilityConfig.length === 0) {
|
|
26
|
+
this.capabilityConfig = "{}";
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
$("#node-input-capabilityConfigContainer").find("[id^='node-input-capability-config-']").each(function() {
|
|
31
|
+
const $field = $(this);
|
|
32
|
+
const fieldId = String($field.attr("id") || "").replace("node-input-capability-config-", "");
|
|
33
|
+
if (!fieldId) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if ($field.is(":checkbox")) {
|
|
38
|
+
capabilityConfig[fieldId] = $field.is(":checked");
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if ($field.is("select[multiple]")) {
|
|
43
|
+
capabilityConfig[fieldId] = $field.val() || [];
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
capabilityConfig[fieldId] = $field.val();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const existingValue = String($capabilityConfig.val() || "").trim();
|
|
51
|
+
let parsedExistingValue = {};
|
|
52
|
+
if (existingValue) {
|
|
53
|
+
try {
|
|
54
|
+
const parsed = JSON.parse(existingValue);
|
|
55
|
+
parsedExistingValue = parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
|
|
56
|
+
} catch (error) {
|
|
57
|
+
parsedExistingValue = {};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
this.capabilityConfig = JSON.stringify({
|
|
62
|
+
...parsedExistingValue,
|
|
63
|
+
...capabilityConfig
|
|
64
|
+
});
|
|
65
|
+
},
|
|
66
|
+
oneditprepare: function() {
|
|
67
|
+
const node = this;
|
|
68
|
+
const $name = $("#node-input-name");
|
|
69
|
+
const $server = $("#node-input-server");
|
|
70
|
+
const $deviceType = $("#node-input-deviceType");
|
|
71
|
+
const $deviceId = $("#node-input-deviceId");
|
|
72
|
+
const $capability = $("#node-input-capability");
|
|
73
|
+
const $capabilityConfig = $("#node-input-capabilityConfig");
|
|
74
|
+
const $capabilityConfigRow = $("#node-row-capabilityConfig");
|
|
75
|
+
const $capabilityConfigContainer = $("#node-input-capabilityConfigContainer");
|
|
76
|
+
const $refresh = $("#node-input-refreshDevices");
|
|
77
|
+
let lastAutoName = "";
|
|
78
|
+
let capabilityConfig = parseCapabilityConfig(node.capabilityConfig);
|
|
79
|
+
let capabilityOptionsRequestId = 0;
|
|
80
|
+
let suspendChangeHandling = false;
|
|
81
|
+
let isInitializingEditor = true;
|
|
82
|
+
|
|
83
|
+
function parseCapabilityConfig(rawValue) {
|
|
84
|
+
if (rawValue && typeof rawValue === "object" && !Array.isArray(rawValue)) {
|
|
85
|
+
return rawValue;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (typeof rawValue !== "string" || rawValue.trim() === "") {
|
|
89
|
+
return {};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
try {
|
|
93
|
+
const parsed = JSON.parse(rawValue);
|
|
94
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
|
|
95
|
+
} catch (error) {
|
|
96
|
+
return {};
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function writeCapabilityConfig() {
|
|
101
|
+
$capabilityConfig.val(JSON.stringify(capabilityConfig || {}));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function withSuppressedChanges(callback) {
|
|
105
|
+
suspendChangeHandling = true;
|
|
106
|
+
try {
|
|
107
|
+
return callback();
|
|
108
|
+
} finally {
|
|
109
|
+
suspendChangeHandling = false;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function getCurrentOrNodeValue($input, nodeValue, fallbackValue) {
|
|
114
|
+
const currentValue = String($input.val() || "").trim();
|
|
115
|
+
if (currentValue !== "") {
|
|
116
|
+
return currentValue;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const persistedValue = String(nodeValue || "").trim();
|
|
120
|
+
if (persistedValue !== "") {
|
|
121
|
+
return persistedValue;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return String(fallbackValue || "").trim();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function requestJson(url) {
|
|
128
|
+
return new Promise(function(resolve, reject) {
|
|
129
|
+
$.getJSON(url, function(data) {
|
|
130
|
+
resolve(data);
|
|
131
|
+
}).fail(function(xhr) {
|
|
132
|
+
reject(xhr);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function syncCapabilityConfigFromInputs() {
|
|
138
|
+
$capabilityConfigContainer.find("[id^='node-input-capability-config-']").each(function() {
|
|
139
|
+
const $field = $(this);
|
|
140
|
+
const fieldId = String($field.attr("id") || "").replace("node-input-capability-config-", "");
|
|
141
|
+
if (!fieldId) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if ($field.is(":checkbox")) {
|
|
146
|
+
capabilityConfig[fieldId] = $field.is(":checked");
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if ($field.is("select[multiple]")) {
|
|
151
|
+
capabilityConfig[fieldId] = $field.val() || [];
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
capabilityConfig[fieldId] = $field.val();
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
writeCapabilityConfig();
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function getSelectedDeviceName() {
|
|
162
|
+
const $selected = $deviceId.find("option:selected");
|
|
163
|
+
return String($selected.data("deviceName") || "").trim();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function syncNameFromSelection() {
|
|
167
|
+
const selectedDeviceName = getSelectedDeviceName();
|
|
168
|
+
const currentName = String($name.val() || "").trim();
|
|
169
|
+
const shouldUpdate = currentName === "" || currentName === lastAutoName;
|
|
170
|
+
|
|
171
|
+
if (!selectedDeviceName || !shouldUpdate) {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
$name.val(selectedDeviceName);
|
|
176
|
+
lastAutoName = selectedDeviceName;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function renderDeviceTypes(items) {
|
|
180
|
+
const currentValue = getCurrentOrNodeValue($deviceType, node.deviceType, "");
|
|
181
|
+
withSuppressedChanges(function() {
|
|
182
|
+
$deviceType.empty();
|
|
183
|
+
$deviceType.append($("<option></option>").val("").text("Select a type"));
|
|
184
|
+
|
|
185
|
+
items.forEach(function(item) {
|
|
186
|
+
$deviceType.append(
|
|
187
|
+
$("<option></option>")
|
|
188
|
+
.val(item.type)
|
|
189
|
+
.text(item.label)
|
|
190
|
+
);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
if (currentValue && $deviceType.find("option[value='" + currentValue + "']").length > 0) {
|
|
194
|
+
$deviceType.val(currentValue);
|
|
195
|
+
} else {
|
|
196
|
+
$deviceType.val("");
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function renderCapabilities(items) {
|
|
202
|
+
const currentValue = getCurrentOrNodeValue($capability, node.capability, "observe");
|
|
203
|
+
withSuppressedChanges(function() {
|
|
204
|
+
$capability.empty();
|
|
205
|
+
|
|
206
|
+
items.forEach(function(item) {
|
|
207
|
+
$capability.append(
|
|
208
|
+
$("<option></option>")
|
|
209
|
+
.val(item.id)
|
|
210
|
+
.text(item.label)
|
|
211
|
+
);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
if (currentValue && $capability.find("option[value='" + currentValue + "']").length > 0) {
|
|
215
|
+
$capability.val(currentValue);
|
|
216
|
+
} else if ($capability.find("option[value='observe']").length > 0) {
|
|
217
|
+
$capability.val("observe");
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function normalizeFieldValue(field, rawValue) {
|
|
223
|
+
if (rawValue === undefined || rawValue === null) {
|
|
224
|
+
if (field.multiple) {
|
|
225
|
+
return [];
|
|
226
|
+
}
|
|
227
|
+
return field.defaultValue !== undefined ? field.defaultValue : "";
|
|
228
|
+
}
|
|
229
|
+
if (field.type === "checkbox") {
|
|
230
|
+
return rawValue === true || rawValue === "true";
|
|
231
|
+
}
|
|
232
|
+
if (field.multiple) {
|
|
233
|
+
return Array.isArray(rawValue) ? rawValue.map(String) : [String(rawValue)];
|
|
234
|
+
}
|
|
235
|
+
return String(rawValue).trim();
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function resolveSelectValue(field, normalizedValue) {
|
|
239
|
+
const optionValues = Array.isArray(field.options)
|
|
240
|
+
? field.options.map(function(option) {
|
|
241
|
+
return String(option.value);
|
|
242
|
+
})
|
|
243
|
+
: [];
|
|
244
|
+
|
|
245
|
+
if (field.multiple) {
|
|
246
|
+
const selectedValues = Array.isArray(normalizedValue) ? normalizedValue.map(String) : [];
|
|
247
|
+
const matchingValues = selectedValues.filter(function(value) {
|
|
248
|
+
return optionValues.indexOf(value) !== -1;
|
|
249
|
+
});
|
|
250
|
+
if (matchingValues.length > 0) {
|
|
251
|
+
return matchingValues;
|
|
252
|
+
}
|
|
253
|
+
if (Array.isArray(field.defaultValue)) {
|
|
254
|
+
return field.defaultValue.map(String).filter(function(value) {
|
|
255
|
+
return optionValues.indexOf(value) !== -1;
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
return [];
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (optionValues.indexOf(String(normalizedValue)) !== -1) {
|
|
262
|
+
return String(normalizedValue);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (field.defaultValue !== undefined && field.defaultValue !== null) {
|
|
266
|
+
const fallbackValue = String(field.defaultValue).trim();
|
|
267
|
+
if (optionValues.indexOf(fallbackValue) !== -1) {
|
|
268
|
+
return fallbackValue;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (field.allowEmpty) {
|
|
273
|
+
return "";
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return optionValues.length > 0 ? optionValues[0] : "";
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function applySelectValue($input, field, resolvedValue) {
|
|
280
|
+
const selectedValues = field.multiple
|
|
281
|
+
? (Array.isArray(resolvedValue) ? resolvedValue.map(String) : [])
|
|
282
|
+
: [String(resolvedValue)];
|
|
283
|
+
|
|
284
|
+
$input.find("option").each(function() {
|
|
285
|
+
const $option = $(this);
|
|
286
|
+
const optionValue = String($option.val());
|
|
287
|
+
$option.prop("selected", selectedValues.indexOf(optionValue) !== -1);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
if (field.multiple) {
|
|
291
|
+
$input.val(selectedValues);
|
|
292
|
+
} else {
|
|
293
|
+
$input.val(selectedValues.length > 0 ? selectedValues[0] : "");
|
|
294
|
+
const selectedIndex = $input.find("option").toArray().findIndex(function(option) {
|
|
295
|
+
return String(option.value) === String(selectedValues.length > 0 ? selectedValues[0] : "");
|
|
296
|
+
});
|
|
297
|
+
if (selectedIndex >= 0 && $input.get(0)) {
|
|
298
|
+
$input.get(0).selectedIndex = selectedIndex;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function restoreRenderedFieldValues(fields) {
|
|
304
|
+
const liveCapabilityConfig = parseCapabilityConfig($capabilityConfig.val());
|
|
305
|
+
|
|
306
|
+
(fields || []).forEach(function(field) {
|
|
307
|
+
if (field.type !== "select") {
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const $input = $("#node-input-capability-config-" + field.id);
|
|
312
|
+
if ($input.length === 0) {
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const currentValue = Object.prototype.hasOwnProperty.call(liveCapabilityConfig, field.id)
|
|
317
|
+
? liveCapabilityConfig[field.id]
|
|
318
|
+
: undefined;
|
|
319
|
+
const normalizedValue = normalizeFieldValue(field, currentValue);
|
|
320
|
+
const resolvedValue = resolveSelectValue(field, normalizedValue);
|
|
321
|
+
applySelectValue($input, field, resolvedValue);
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function renderCapabilityOptions(data, options) {
|
|
326
|
+
const renderOptions = options || {};
|
|
327
|
+
const fields = data && Array.isArray(data.fields) ? data.fields : [];
|
|
328
|
+
syncCapabilityConfigFromInputs();
|
|
329
|
+
$capabilityConfigContainer.empty();
|
|
330
|
+
|
|
331
|
+
if (fields.length === 0) {
|
|
332
|
+
$capabilityConfigRow.hide();
|
|
333
|
+
if (renderOptions.clearStoredConfig) {
|
|
334
|
+
capabilityConfig = {};
|
|
335
|
+
writeCapabilityConfig();
|
|
336
|
+
}
|
|
337
|
+
$capabilityConfigContainer.empty();
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (renderOptions.clearStoredConfig) {
|
|
342
|
+
capabilityConfig = {};
|
|
343
|
+
writeCapabilityConfig();
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
$capabilityConfigRow.show();
|
|
347
|
+
fields.forEach(function(field) {
|
|
348
|
+
const fieldId = "node-input-capability-config-" + field.id;
|
|
349
|
+
const $wrapper = $("<div></div>").css("margin-bottom", "8px");
|
|
350
|
+
const isHiddenField = field.type === "hidden";
|
|
351
|
+
const $label = $("<label></label>")
|
|
352
|
+
.attr("for", fieldId)
|
|
353
|
+
.css({ width: "140px", marginRight: "12px", display: "inline-block" })
|
|
354
|
+
.text(field.label || field.id);
|
|
355
|
+
let $input;
|
|
356
|
+
|
|
357
|
+
if (field.type === "select") {
|
|
358
|
+
$input = $("<select></select>").attr("id", fieldId).css("width", "calc(100% - 160px)");
|
|
359
|
+
if (field.multiple) {
|
|
360
|
+
$input.attr("multiple", "multiple").css("min-height", "88px");
|
|
361
|
+
}
|
|
362
|
+
if (field.placeholder && !field.allowEmpty) {
|
|
363
|
+
$input.append($("<option></option>").val("").text(field.placeholder));
|
|
364
|
+
}
|
|
365
|
+
(field.options || []).forEach(function(option) {
|
|
366
|
+
$input.append(
|
|
367
|
+
$("<option></option>")
|
|
368
|
+
.val(option.value)
|
|
369
|
+
.text(option.label)
|
|
370
|
+
);
|
|
371
|
+
});
|
|
372
|
+
} else if (field.type === "checkbox") {
|
|
373
|
+
$input = $("<input>").attr({ id: fieldId, type: "checkbox" });
|
|
374
|
+
} else if (field.type === "hidden") {
|
|
375
|
+
$input = $("<input>").attr({ id: fieldId, type: "hidden" });
|
|
376
|
+
} else {
|
|
377
|
+
$input = $("<input>").attr({
|
|
378
|
+
id: fieldId,
|
|
379
|
+
type: field.type === "number" ? "number" : "text",
|
|
380
|
+
placeholder: field.placeholder || ""
|
|
381
|
+
}).css("width", "calc(100% - 160px)");
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
const currentValue = Object.prototype.hasOwnProperty.call(capabilityConfig, field.id)
|
|
385
|
+
? capabilityConfig[field.id]
|
|
386
|
+
: undefined;
|
|
387
|
+
const normalizedValue = normalizeFieldValue(field, currentValue);
|
|
388
|
+
const resolvedValue = field.type === "select"
|
|
389
|
+
? resolveSelectValue(field, normalizedValue)
|
|
390
|
+
: normalizedValue;
|
|
391
|
+
if (!Object.prototype.hasOwnProperty.call(capabilityConfig, field.id) || field.type === "select") {
|
|
392
|
+
capabilityConfig[field.id] = resolvedValue;
|
|
393
|
+
}
|
|
394
|
+
if (field.type === "checkbox") {
|
|
395
|
+
$input.prop("checked", resolvedValue === true);
|
|
396
|
+
} else if (field.type === "hidden") {
|
|
397
|
+
$input.val(resolvedValue);
|
|
398
|
+
} else if (field.type === "select") {
|
|
399
|
+
applySelectValue($input, field, resolvedValue);
|
|
400
|
+
} else {
|
|
401
|
+
$input.val(resolvedValue);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
$input.on("change input", function() {
|
|
405
|
+
if (field.type === "checkbox") {
|
|
406
|
+
capabilityConfig[field.id] = $(this).is(":checked");
|
|
407
|
+
} else if (field.multiple) {
|
|
408
|
+
capabilityConfig[field.id] = $(this).val() || [];
|
|
409
|
+
} else {
|
|
410
|
+
capabilityConfig[field.id] = $(this).val();
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
if (field.reloadOnChange && field.id === "propertyPath") {
|
|
414
|
+
delete capabilityConfig.propertyValue;
|
|
415
|
+
delete capabilityConfig.valueType;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
writeCapabilityConfig();
|
|
419
|
+
|
|
420
|
+
if (field.reloadOnChange) {
|
|
421
|
+
syncCapabilityConfigFromInputs();
|
|
422
|
+
loadCapabilityOptions();
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
if (isHiddenField) {
|
|
427
|
+
$wrapper.append($input);
|
|
428
|
+
} else {
|
|
429
|
+
$wrapper.append($label).append($input);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
if (field.helpText) {
|
|
433
|
+
$wrapper.append(
|
|
434
|
+
$("<div></div>")
|
|
435
|
+
.addClass("form-tips")
|
|
436
|
+
.css({ marginLeft: "152px" })
|
|
437
|
+
.text(field.helpText)
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
$capabilityConfigContainer.append($wrapper);
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
writeCapabilityConfig();
|
|
445
|
+
restoreRenderedFieldValues(fields);
|
|
446
|
+
setTimeout(function() {
|
|
447
|
+
restoreRenderedFieldValues(fields);
|
|
448
|
+
}, 0);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function renderDevices(items) {
|
|
452
|
+
const currentValue = getCurrentOrNodeValue($deviceId, node.deviceId, "");
|
|
453
|
+
withSuppressedChanges(function() {
|
|
454
|
+
$deviceId.empty();
|
|
455
|
+
$deviceId.append($("<option></option>").val("").text("Select a device"));
|
|
456
|
+
|
|
457
|
+
items.forEach(function(item) {
|
|
458
|
+
const labelParts = [item.name || item.id];
|
|
459
|
+
if (item.state) {
|
|
460
|
+
labelParts.push("[" + item.state + "]");
|
|
461
|
+
}
|
|
462
|
+
$deviceId.append(
|
|
463
|
+
$("<option></option>")
|
|
464
|
+
.val(item.id)
|
|
465
|
+
.text(labelParts.join(" "))
|
|
466
|
+
.attr("data-device-name", item.name || item.id)
|
|
467
|
+
);
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
if (currentValue && $deviceId.find("option[value='" + currentValue + "']").length > 0) {
|
|
471
|
+
$deviceId.val(currentValue);
|
|
472
|
+
} else {
|
|
473
|
+
$deviceId.val("");
|
|
474
|
+
}
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
syncNameFromSelection();
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
function loadCapabilities() {
|
|
481
|
+
const selectedType = getCurrentOrNodeValue($deviceType, node.deviceType, "");
|
|
482
|
+
if (!selectedType) {
|
|
483
|
+
renderCapabilities([]);
|
|
484
|
+
renderCapabilityOptions({ fields: [] }, { clearStoredConfig: false });
|
|
485
|
+
return Promise.resolve();
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
return requestJson("unifiProtect/device-capabilities?deviceType=" + encodeURIComponent(selectedType))
|
|
489
|
+
.then(function(data) {
|
|
490
|
+
renderCapabilities(Array.isArray(data) ? data : []);
|
|
491
|
+
})
|
|
492
|
+
.catch(function(xhr) {
|
|
493
|
+
renderCapabilities([]);
|
|
494
|
+
renderCapabilityOptions({ fields: [] }, { clearStoredConfig: false });
|
|
495
|
+
const errorText = xhr && xhr.responseJSON && xhr.responseJSON.error ? xhr.responseJSON.error : "Unable to load capabilities";
|
|
496
|
+
RED.notify(errorText, "error");
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
function loadCapabilityOptions() {
|
|
501
|
+
const requestId = ++capabilityOptionsRequestId;
|
|
502
|
+
const serverId = $server.val();
|
|
503
|
+
const currentDeviceType = getCurrentOrNodeValue($deviceType, node.deviceType, "");
|
|
504
|
+
const currentDeviceId = getCurrentOrNodeValue($deviceId, node.deviceId, "");
|
|
505
|
+
const currentCapability = getCurrentOrNodeValue($capability, node.capability, "observe");
|
|
506
|
+
if (!serverId || serverId === "_ADD_" || !currentDeviceType || !currentCapability) {
|
|
507
|
+
renderCapabilityOptions({ fields: [] }, { clearStoredConfig: false });
|
|
508
|
+
return Promise.resolve();
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
return requestJson(
|
|
512
|
+
"unifiProtect/device-capability-options?serverId=" + encodeURIComponent(serverId) +
|
|
513
|
+
"&deviceType=" + encodeURIComponent(currentDeviceType) +
|
|
514
|
+
"&deviceId=" + encodeURIComponent(currentDeviceId || "") +
|
|
515
|
+
"&capability=" + encodeURIComponent(currentCapability) +
|
|
516
|
+
"&capabilityConfig=" + encodeURIComponent(JSON.stringify(capabilityConfig || {}))
|
|
517
|
+
).then(function(data) {
|
|
518
|
+
if (requestId !== capabilityOptionsRequestId) {
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
renderCapabilityOptions(data || { fields: [] });
|
|
522
|
+
}).catch(function(xhr) {
|
|
523
|
+
if (requestId !== capabilityOptionsRequestId) {
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
renderCapabilityOptions({ fields: [] }, { clearStoredConfig: false });
|
|
527
|
+
const errorText = xhr && xhr.responseJSON && xhr.responseJSON.error ? xhr.responseJSON.error : "Unable to load capability options";
|
|
528
|
+
RED.notify(errorText, "error");
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
function loadDevices() {
|
|
533
|
+
const serverId = $server.val();
|
|
534
|
+
const selectedType = getCurrentOrNodeValue($deviceType, node.deviceType, "");
|
|
535
|
+
if (!serverId || serverId === "_ADD_" || !selectedType) {
|
|
536
|
+
renderDevices([]);
|
|
537
|
+
renderCapabilityOptions({ fields: [] }, { clearStoredConfig: false });
|
|
538
|
+
return Promise.resolve();
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
$refresh.addClass("fa-spin");
|
|
542
|
+
return requestJson(
|
|
543
|
+
"unifiProtect/devices?serverId=" + encodeURIComponent(serverId) + "&deviceType=" + encodeURIComponent(selectedType)
|
|
544
|
+
).then(function(data) {
|
|
545
|
+
renderDevices(Array.isArray(data) ? data : []);
|
|
546
|
+
}).catch(function(xhr) {
|
|
547
|
+
renderDevices([]);
|
|
548
|
+
renderCapabilityOptions({ fields: [] }, { clearStoredConfig: false });
|
|
549
|
+
const errorText = xhr && xhr.responseJSON && xhr.responseJSON.error ? xhr.responseJSON.error : "Unable to load devices";
|
|
550
|
+
RED.notify(errorText, "error");
|
|
551
|
+
}).finally(function() {
|
|
552
|
+
$refresh.removeClass("fa-spin");
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
function loadDeviceTypes() {
|
|
557
|
+
return requestJson("unifiProtect/device-types")
|
|
558
|
+
.then(function(data) {
|
|
559
|
+
renderDeviceTypes(Array.isArray(data) ? data : []);
|
|
560
|
+
})
|
|
561
|
+
.catch(function(xhr) {
|
|
562
|
+
renderDeviceTypes([]);
|
|
563
|
+
renderCapabilities([]);
|
|
564
|
+
renderDevices([]);
|
|
565
|
+
renderCapabilityOptions({ fields: [] }, { clearStoredConfig: false });
|
|
566
|
+
const errorText = xhr && xhr.responseJSON && xhr.responseJSON.error ? xhr.responseJSON.error : "Unable to load device types";
|
|
567
|
+
RED.notify(errorText, "error");
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
function refreshEditor() {
|
|
572
|
+
return loadDeviceTypes()
|
|
573
|
+
.then(function() {
|
|
574
|
+
return loadCapabilities();
|
|
575
|
+
})
|
|
576
|
+
.then(function() {
|
|
577
|
+
return loadDevices();
|
|
578
|
+
})
|
|
579
|
+
.then(function() {
|
|
580
|
+
return loadCapabilityOptions();
|
|
581
|
+
})
|
|
582
|
+
.finally(function() {
|
|
583
|
+
isInitializingEditor = false;
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
function refreshAfterServerChange() {
|
|
588
|
+
return loadDevices()
|
|
589
|
+
.then(function() {
|
|
590
|
+
return loadCapabilityOptions();
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
function refreshAfterTypeChange() {
|
|
595
|
+
return loadCapabilities()
|
|
596
|
+
.then(function() {
|
|
597
|
+
return loadDevices();
|
|
598
|
+
})
|
|
599
|
+
.then(function() {
|
|
600
|
+
return loadCapabilityOptions();
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
function refreshAfterDeviceChange() {
|
|
605
|
+
return loadCapabilityOptions();
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
function refreshAfterCapabilityChange() {
|
|
609
|
+
return loadCapabilityOptions();
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
try {
|
|
613
|
+
RED.sidebar.show("help");
|
|
614
|
+
} catch (error) {
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
writeCapabilityConfig();
|
|
618
|
+
|
|
619
|
+
$server.on("change", function() {
|
|
620
|
+
if (suspendChangeHandling || isInitializingEditor) {
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
refreshAfterServerChange();
|
|
624
|
+
});
|
|
625
|
+
$deviceType.on("change", function() {
|
|
626
|
+
if (suspendChangeHandling || isInitializingEditor) {
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
capabilityConfig = {};
|
|
630
|
+
writeCapabilityConfig();
|
|
631
|
+
refreshAfterTypeChange();
|
|
632
|
+
});
|
|
633
|
+
$deviceId.on("change", function() {
|
|
634
|
+
if (suspendChangeHandling || isInitializingEditor) {
|
|
635
|
+
return;
|
|
636
|
+
}
|
|
637
|
+
syncNameFromSelection();
|
|
638
|
+
capabilityConfig = {};
|
|
639
|
+
writeCapabilityConfig();
|
|
640
|
+
refreshAfterDeviceChange();
|
|
641
|
+
});
|
|
642
|
+
$capability.on("change", function() {
|
|
643
|
+
if (suspendChangeHandling || isInitializingEditor) {
|
|
644
|
+
return;
|
|
645
|
+
}
|
|
646
|
+
capabilityConfig = {};
|
|
647
|
+
writeCapabilityConfig();
|
|
648
|
+
refreshAfterCapabilityChange();
|
|
649
|
+
});
|
|
650
|
+
$refresh.on("click", function(event) {
|
|
651
|
+
event.preventDefault();
|
|
652
|
+
refreshAfterServerChange();
|
|
653
|
+
});
|
|
654
|
+
|
|
655
|
+
refreshEditor();
|
|
656
|
+
}
|
|
657
|
+
});
|
|
658
|
+
</script>
|
|
659
|
+
|
|
660
|
+
<script type="text/html" data-template-name="unifi-protect-device">
|
|
661
|
+
<div class="form-row">
|
|
662
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
663
|
+
<input type="text" id="node-input-name" placeholder="Unifi Protect Device">
|
|
664
|
+
</div>
|
|
665
|
+
<div class="form-row">
|
|
666
|
+
<label for="node-input-server"><i class="fa fa-cog"></i> Config</label>
|
|
667
|
+
<input type="text" id="node-input-server">
|
|
668
|
+
</div>
|
|
669
|
+
<div class="form-row">
|
|
670
|
+
<label for="node-input-deviceType"><i class="fa fa-cubes"></i> Type</label>
|
|
671
|
+
<select id="node-input-deviceType"></select>
|
|
672
|
+
</div>
|
|
673
|
+
<div class="form-row">
|
|
674
|
+
<label for="node-input-deviceId"><i class="fa fa-dot-circle-o"></i> Device</label>
|
|
675
|
+
<select id="node-input-deviceId" style="width:70%;"></select>
|
|
676
|
+
<a href="#" id="node-input-refreshDevices" class="editor-button" style="margin-left:6px;">
|
|
677
|
+
<i class="fa fa-refresh"></i>
|
|
678
|
+
</a>
|
|
679
|
+
</div>
|
|
680
|
+
<div class="form-row">
|
|
681
|
+
<label for="node-input-capability"><i class="fa fa-tasks"></i> Capability</label>
|
|
682
|
+
<select id="node-input-capability"></select>
|
|
683
|
+
</div>
|
|
684
|
+
<div class="form-row" id="node-row-capabilityConfig" style="display:none;">
|
|
685
|
+
<label><i class="fa fa-sliders"></i> Options</label>
|
|
686
|
+
<div id="node-input-capabilityConfigContainer" style="width:70%; display:inline-block;"></div>
|
|
687
|
+
<input type="hidden" id="node-input-capabilityConfig">
|
|
688
|
+
</div>
|
|
689
|
+
<div class="form-row">
|
|
690
|
+
<label for="node-input-timeout"><i class="fa fa-clock-o"></i> Timeout</label>
|
|
691
|
+
<input type="text" id="node-input-timeout" placeholder="15000">
|
|
692
|
+
</div>
|
|
693
|
+
</script>
|
|
694
|
+
|
|
695
|
+
<script type="text/html" data-help-name="unifi-protect-device">
|
|
696
|
+
<p>Rappresenta un oggetto UniFi Protect generico. Il nodo usa discovery di tipo, istanze e capability supportate.</p>
|
|
697
|
+
|
|
698
|
+
<h3>Configurazione</h3>
|
|
699
|
+
<p>Seleziona un tipo di device, poi una specifica istanza e infine la capability da usare. La capability <code>Observe</code> effettua fetch iniziale e resta collegata agli stream Protect. Se la capability espone opzioni configurabili, il nodo le propone automaticamente. La capability <code>Set property</code> scopre le proprieta patchabili dal payload reale del device selezionato. Per le camera doorbell puoi anche usare <code>Set doorbell message</code> con messaggi predefiniti, custom o immagine. Per <code>Sensor + Observe</code>, <code>Camera + Observe</code> e <code>Light + Observe</code> puoi scegliere l'osservabile e il nodo emette <code>true/false</code> in <code>msg.payload</code>, lasciando il dettaglio completo in <code>msg.RAW</code>.</p>
|
|
700
|
+
|
|
701
|
+
<h3>Input</h3>
|
|
702
|
+
<ul>
|
|
703
|
+
<li><code>msg.deviceType</code>, <code>msg.deviceId</code>, <code>msg.capability</code> per override runtime della chiamata</li>
|
|
704
|
+
<li><code>msg.capabilityConfig</code> per override runtime delle opzioni selezionate nell'editor</li>
|
|
705
|
+
<li><code>msg.params</code> per override puntuali dei parametri path, quando necessario</li>
|
|
706
|
+
<li><code>msg.query</code>, <code>msg.headers</code> e <code>msg.payload</code> per la richiesta HTTP</li>
|
|
707
|
+
</ul>
|
|
708
|
+
|
|
709
|
+
<h3>Output</h3>
|
|
710
|
+
<ul>
|
|
711
|
+
<li>Output 1: stato o risposta principale del device</li>
|
|
712
|
+
<li>Output 2: eventi Protect correlati al device selezionato</li>
|
|
713
|
+
<li><code>msg.unifiProtect</code>: metadati standardizzati del device e della capability usata</li>
|
|
714
|
+
</ul>
|
|
715
|
+
</script>
|