generated from homebridge/homebridge-plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
action_helper.ts
415 lines (344 loc) · 13.2 KB
/
action_helper.ts
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
export class SwitchActionMapping {
public serviceLabelIndex: number | undefined;
public extension: number | undefined;
public valueSinglePress: string | undefined;
public valueDoublePress: string | undefined;
public valueLongPress: string | undefined;
private _id: string | undefined;
get subType(): string | undefined {
if (this.extension === undefined || this.extension === 0) {
return this.identifier;
}
return `${this._id ?? ''}#ext${this.extension}`;
}
get identifier(): string | undefined {
return this._id;
}
set identifier(value: string | undefined) {
if (this._id !== undefined || this.serviceLabelIndex !== undefined || this.extension !== undefined) {
throw new Error('Can only set identifier once and before serviceLabelIndex has been set.');
}
this._id = value;
}
public merge(other: SwitchActionMapping): this {
if (this.subType !== other.subType) {
throw new Error(
'Can NOT merge SwitchActionMapping instances with different identifiers and/or extensions. ' +
`(got subtype ${this.subType} and ${other.subType})`
);
}
if (this.serviceLabelIndex === undefined) {
this.serviceLabelIndex = other.serviceLabelIndex;
} else {
this.compareValuesForMerge('service label index', this.serviceLabelIndex, other.serviceLabelIndex);
}
if (this.valueSinglePress === undefined) {
this.valueSinglePress = other.valueSinglePress;
} else {
this.compareValuesForMerge('single press', this.valueSinglePress, other.valueSinglePress);
}
if (this.valueDoublePress === undefined) {
this.valueDoublePress = other.valueDoublePress;
} else {
this.compareValuesForMerge('double press', this.valueDoublePress, other.valueDoublePress);
}
if (this.valueLongPress === undefined) {
this.valueLongPress = other.valueLongPress;
} else {
this.compareValuesForMerge('long press', this.valueLongPress, other.valueLongPress);
}
return this;
}
private compareValuesForMerge(description: string, local: string | number, other: string | number | undefined) {
if (other === undefined) {
return;
}
if (other !== local) {
throw new Error(
`Can NOT merge SwitchActionMapping instances that both have a different value for ${description}. ` + `(got ${local} and ${other})`
);
}
}
public hasValidValues(): boolean {
return this.valueSinglePress !== undefined || this.valueDoublePress !== undefined || this.valueLongPress !== undefined;
}
public isValidMapping(): boolean {
return this.serviceLabelIndex !== undefined && this.hasValidValues();
}
public toString(): string | undefined {
if (!this.isValidMapping()) {
return undefined;
}
let str = `Button ${this.serviceLabelIndex}`;
if (this.identifier !== undefined) {
str += ` (${this.identifier})`;
}
if (this.extension !== undefined && this.extension > 0) {
str += ` #ext${this.extension}`;
}
str += ':';
if (this.valueSinglePress !== undefined) {
str += `\n\t- SINGLE: ${this.valueSinglePress}`;
}
if (this.valueDoublePress !== undefined) {
str += `\n\t- DOUBLE: ${this.valueDoublePress}`;
}
if (this.valueLongPress !== undefined) {
str += `\n\t- LONG : ${this.valueLongPress}`;
}
return str;
}
}
export class SwitchActionHelper {
private static readonly singleAction: Set<string> = new Set(['single', 'click', 'press']);
private static readonly doubleAction: Set<string> = new Set(['double']);
private static readonly longAction: Set<string> = new Set(['hold', 'long']);
private static readonly tripleAction: Set<string> = new Set(['triple', 'tripple']);
private static readonly quadrupleAction: Set<string> = new Set(['quadruple']);
private static readonly ignoredAdditions: Set<string> = new Set(['release', 'hold-release']);
private static readonly separators: string[] = ['_', '-'];
private static readonly regex_number = /(\d{1,3})/;
private readonly regex_id_start: RegExp;
private readonly regex_id_end: RegExp;
private readonly regex_separator_count: RegExp;
private static instance: SwitchActionHelper;
private constructor() {
const additions = [
...SwitchActionHelper.ignoredAdditions,
...SwitchActionHelper.singleAction,
...SwitchActionHelper.doubleAction,
...SwitchActionHelper.longAction,
...SwitchActionHelper.tripleAction,
...SwitchActionHelper.quadrupleAction,
].join('|');
const separatorsString = SwitchActionHelper.separators.join('');
this.regex_id_start = new RegExp(`^(${additions})[${separatorsString}]`, 'i');
this.regex_id_end = new RegExp(`[${separatorsString}](${additions})$`, 'i');
this.regex_separator_count = new RegExp(`[${separatorsString}]+`, 'ig');
}
public static getInstance(): SwitchActionHelper {
if (SwitchActionHelper.instance === undefined) {
SwitchActionHelper.instance = new SwitchActionHelper();
}
return SwitchActionHelper.instance;
}
private firstNumberInString(input: string): number | undefined {
const numbers = SwitchActionHelper.regex_number.exec(input)?.map((x: string) => parseInt(x));
if (numbers !== undefined && numbers.length > 0 && !isNaN(numbers[0])) {
return numbers[0];
}
return undefined;
}
private numberOfSeparators(input: string): number {
return (input.match(this.regex_separator_count) || []).length;
}
private matchActionValues(mapping: SwitchActionMapping, value: string, type: string | undefined = undefined): boolean {
if (type === undefined) {
type = value;
}
type = type.toLowerCase();
if (SwitchActionHelper.singleAction.has(type)) {
mapping.valueSinglePress = value;
return true;
}
if (SwitchActionHelper.doubleAction.has(type)) {
mapping.valueDoublePress = value;
return true;
}
if (SwitchActionHelper.longAction.has(type)) {
mapping.valueLongPress = value;
return true;
}
// Extended actions
if (SwitchActionHelper.tripleAction.has(type)) {
mapping.extension = 1;
mapping.valueSinglePress = value;
return true;
}
if (SwitchActionHelper.quadrupleAction.has(type)) {
mapping.extension = 1;
mapping.valueDoublePress = value;
return true;
}
return false;
}
private valueToMapping(input: string): SwitchActionMapping {
// Devices that have a wildcard in the reported values can not be supported.
if (input.indexOf('*') >= 0) {
throw new Error('Device found with a wildcard in the exposed possible values for the action, which cannot be mapped: ' + input);
}
// Check exact matches first
const mapping = new SwitchActionMapping();
if (this.matchActionValues(mapping, input)) {
return mapping;
}
if (SwitchActionHelper.ignoredAdditions.has(input)) {
return mapping;
}
mapping.identifier = input.replace(this.regex_id_start, '').replace(this.regex_id_end, '');
// Check if identifier is equal to the input
// If so, consider it a single press action
if (input === mapping.identifier) {
mapping.valueSinglePress = input;
return mapping;
}
// Determine action for value
const startMatch = this.regex_id_start.exec(input);
if (startMatch && startMatch.length >= 2 && this.matchActionValues(mapping, input, startMatch[1])) {
return mapping;
}
const endMatch = this.regex_id_end.exec(input);
if (endMatch && endMatch.length >= 2 && this.matchActionValues(mapping, input, endMatch[1])) {
return mapping;
}
return mapping;
}
valuesToNumberedMappings(values: string[]): SwitchActionMapping[] {
// Convert and combine
const groupedMappings = new Map<string | undefined, SwitchActionMapping>();
for (const value of values) {
const mapping = this.valueToMapping(value);
const existingMapping = groupedMappings.get(mapping.subType);
if (existingMapping !== undefined) {
existingMapping.merge(mapping);
} else {
groupedMappings.set(mapping.subType, mapping);
}
}
// Filter out invalid mappings and sort them
const sortedMappings = this.sortMappingsByIdentifier([...groupedMappings.values()].filter((m) => m.hasValidValues()));
// Determine labels
this.labelSortedMappings(sortedMappings);
return sortedMappings;
}
private determineLabelStrategy(mappings: SwitchActionMapping[]): [number, boolean, number] {
// Check which single digit numbers are present in the identifiers and how often the same number is used
let maximumExtension = 0;
let maximumTimesNumberIsUsed = 0;
let highestNumber = 0;
const foundNumbers = new Map<number, number>();
for (const mapping of mappings) {
const numericId = this.firstNumberInString(mapping.identifier ?? '');
if ((mapping.extension ?? 0) > maximumExtension) {
maximumExtension = mapping.extension ?? 0;
}
if (numericId !== undefined && numericId <= 9) {
const newCount = (foundNumbers.get(numericId) ?? 0) + 1;
foundNumbers.set(numericId, newCount);
if (newCount > maximumTimesNumberIsUsed) {
maximumTimesNumberIsUsed = newCount;
}
if (numericId > highestNumber) {
highestNumber = numericId;
}
}
}
// Determine numbering strategy
const numericMultiplier = maximumTimesNumberIsUsed <= 1 ? 1 : 10;
const useIncrementalNumbers = maximumTimesNumberIsUsed > 10;
const startLabelForNonNumericIds = useIncrementalNumbers ? 1 : (highestNumber + 1) * numericMultiplier;
return [numericMultiplier, useIncrementalNumbers, startLabelForNonNumericIds];
}
private labelSortedMappings(mappings: SwitchActionMapping[]) {
// Determine strategy
const [numericMultiplier, useIncrementalNumbers, startLabelForNonNumericIds] = this.determineLabelStrategy(mappings);
// Apply service labels
const usedLabels = new Set<number>();
for (const mapping of mappings) {
// Use first numeric value in identifier (if present)
const foundNumber = this.firstNumberInString(mapping.identifier ?? '');
let maximumNumber = 255;
let numericId = startLabelForNonNumericIds;
if (!useIncrementalNumbers && foundNumber !== undefined && foundNumber > 0 && foundNumber <= 9) {
numericId = foundNumber * numericMultiplier;
maximumNumber = numericId + (numericMultiplier - 1);
}
// Find first available index
while (usedLabels.has(numericId)) {
++numericId;
if (numericId >= maximumNumber) {
throw new Error('Service Label Index going out of range!');
}
}
mapping.serviceLabelIndex = numericId;
// Store used label to prevent duplicates
usedLabels.add(numericId);
}
}
private sortByFirstNumber(x: string, y: string): number {
const X_GOES_FIRST = -1;
const Y_GOES_FIRST = 1;
const firstNumberInX = this.firstNumberInString(x);
const firstNumberInY = this.firstNumberInString(y);
if (firstNumberInX !== undefined) {
if (firstNumberInY === undefined) {
return X_GOES_FIRST;
}
// Both have numbers, compare first number in string
if (firstNumberInX === firstNumberInY) {
// No preference
return 0;
}
// Prefer any number over 0, as the service label in HomeKit can not be 0.
if (firstNumberInX === 0) {
return Y_GOES_FIRST;
}
if (firstNumberInY === 0) {
return X_GOES_FIRST;
}
// No zero values, just compare them then.
if (firstNumberInX < firstNumberInY) {
return X_GOES_FIRST;
}
if (firstNumberInX > firstNumberInY) {
return Y_GOES_FIRST;
}
} else if (firstNumberInY !== undefined) {
return Y_GOES_FIRST;
}
return 0;
}
private sortMappingsByIdentifier(ids: SwitchActionMapping[]): SwitchActionMapping[] {
return ids.sort((xMapping: SwitchActionMapping, yMapping: SwitchActionMapping): number => {
const X_GOES_FIRST = -1;
const Y_GOES_FIRST = 1;
const x = xMapping.identifier?.toLowerCase().trim() ?? '';
const y = yMapping.identifier?.toLowerCase().trim() ?? '';
if (x === y) {
const extensionX = xMapping.extension ?? 0;
const extensionY = yMapping.extension ?? 0;
return extensionX - extensionY;
}
// Prefer strings with a numeric sequence.
// If both have one, the lowest number is preferred.
const numberResult = this.sortByFirstNumber(x, y);
if (numberResult !== 0) {
return numberResult;
}
// Prefer empty string over other values
if (x === '') {
return X_GOES_FIRST;
}
if (y === '') {
return Y_GOES_FIRST;
}
// Prefer strings with fewer separators
const separatorCountX = this.numberOfSeparators(x);
const separatorCountY = this.numberOfSeparators(y);
if (separatorCountX < separatorCountY) {
return X_GOES_FIRST;
}
if (separatorCountX > separatorCountY) {
return Y_GOES_FIRST;
}
// Use normal alphabetic order
if (x < y) {
return X_GOES_FIRST;
}
if (x > y) {
return Y_GOES_FIRST;
}
return 0;
});
}
}