@homelib/xiaomi 0.0.0-wip.01265bf1cc90

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.
Files changed (183) hide show
  1. package/LICENSE +19 -0
  2. package/README.md +26 -0
  3. package/bld/library/backend/client.d.ts +73 -0
  4. package/bld/library/backend/client.js +490 -0
  5. package/bld/library/backend/client.js.map +1 -0
  6. package/bld/library/backend/constants.d.ts +7 -0
  7. package/bld/library/backend/constants.js +11 -0
  8. package/bld/library/backend/constants.js.map +1 -0
  9. package/bld/library/backend/index.d.ts +3 -0
  10. package/bld/library/backend/index.js +4 -0
  11. package/bld/library/backend/index.js.map +1 -0
  12. package/bld/library/backend/oauth.d.ts +24 -0
  13. package/bld/library/backend/oauth.js +121 -0
  14. package/bld/library/backend/oauth.js.map +1 -0
  15. package/bld/library/binding.d.ts +44 -0
  16. package/bld/library/binding.js +212 -0
  17. package/bld/library/binding.js.map +1 -0
  18. package/bld/library/cloud/client.d.ts +27 -0
  19. package/bld/library/cloud/client.js +109 -0
  20. package/bld/library/cloud/client.js.map +1 -0
  21. package/bld/library/cloud/constants.d.ts +9 -0
  22. package/bld/library/cloud/constants.js +11 -0
  23. package/bld/library/cloud/constants.js.map +1 -0
  24. package/bld/library/cloud/device.d.ts +84 -0
  25. package/bld/library/cloud/device.js +686 -0
  26. package/bld/library/cloud/device.js.map +1 -0
  27. package/bld/library/cloud/index.d.ts +5 -0
  28. package/bld/library/cloud/index.js +6 -0
  29. package/bld/library/cloud/index.js.map +1 -0
  30. package/bld/library/cloud/mqtt.d.ts +69 -0
  31. package/bld/library/cloud/mqtt.js +517 -0
  32. package/bld/library/cloud/mqtt.js.map +1 -0
  33. package/bld/library/cloud/transport.d.ts +8 -0
  34. package/bld/library/cloud/transport.js +34 -0
  35. package/bld/library/cloud/transport.js.map +1 -0
  36. package/bld/library/command.d.ts +6 -0
  37. package/bld/library/command.js +6 -0
  38. package/bld/library/command.js.map +1 -0
  39. package/bld/library/configuration.d.ts +62 -0
  40. package/bld/library/configuration.js +303 -0
  41. package/bld/library/configuration.js.map +1 -0
  42. package/bld/library/device.d.ts +69 -0
  43. package/bld/library/device.js +266 -0
  44. package/bld/library/device.js.map +1 -0
  45. package/bld/library/devices/air-conditioner.d.ts +48 -0
  46. package/bld/library/devices/air-conditioner.js +155 -0
  47. package/bld/library/devices/air-conditioner.js.map +1 -0
  48. package/bld/library/devices/command-effect.d.ts +35 -0
  49. package/bld/library/devices/command-effect.js +269 -0
  50. package/bld/library/devices/command-effect.js.map +1 -0
  51. package/bld/library/devices/dehumidifier.d.ts +41 -0
  52. package/bld/library/devices/dehumidifier.js +124 -0
  53. package/bld/library/devices/dehumidifier.js.map +1 -0
  54. package/bld/library/devices/fan.d.ts +37 -0
  55. package/bld/library/devices/fan.js +135 -0
  56. package/bld/library/devices/fan.js.map +1 -0
  57. package/bld/library/devices/index.d.ts +9 -0
  58. package/bld/library/devices/index.js +10 -0
  59. package/bld/library/devices/index.js.map +1 -0
  60. package/bld/library/devices/light.d.ts +22 -0
  61. package/bld/library/devices/light.js +112 -0
  62. package/bld/library/devices/light.js.map +1 -0
  63. package/bld/library/devices/pet-feeder.d.ts +36 -0
  64. package/bld/library/devices/pet-feeder.js +116 -0
  65. package/bld/library/devices/pet-feeder.js.map +1 -0
  66. package/bld/library/devices/placeholder.d.ts +3 -0
  67. package/bld/library/devices/placeholder.js +4 -0
  68. package/bld/library/devices/placeholder.js.map +1 -0
  69. package/bld/library/devices/temperature-humidity-sensor.d.ts +14 -0
  70. package/bld/library/devices/temperature-humidity-sensor.js +56 -0
  71. package/bld/library/devices/temperature-humidity-sensor.js.map +1 -0
  72. package/bld/library/devices/value-range.d.ts +2 -0
  73. package/bld/library/devices/value-range.js +6 -0
  74. package/bld/library/devices/value-range.js.map +1 -0
  75. package/bld/library/endpoint-connection.d.ts +168 -0
  76. package/bld/library/endpoint-connection.js +510 -0
  77. package/bld/library/endpoint-connection.js.map +1 -0
  78. package/bld/library/index.d.ts +26 -0
  79. package/bld/library/index.js +33 -0
  80. package/bld/library/index.js.map +1 -0
  81. package/bld/library/local/certificate.d.ts +31 -0
  82. package/bld/library/local/certificate.js +298 -0
  83. package/bld/library/local/certificate.js.map +1 -0
  84. package/bld/library/local/controller.d.ts +70 -0
  85. package/bld/library/local/controller.js +498 -0
  86. package/bld/library/local/controller.js.map +1 -0
  87. package/bld/library/local/discovery.d.ts +29 -0
  88. package/bld/library/local/discovery.js +321 -0
  89. package/bld/library/local/discovery.js.map +1 -0
  90. package/bld/library/local/index.d.ts +6 -0
  91. package/bld/library/local/index.js +7 -0
  92. package/bld/library/local/index.js.map +1 -0
  93. package/bld/library/local/message.d.ts +11 -0
  94. package/bld/library/local/message.js +128 -0
  95. package/bld/library/local/message.js.map +1 -0
  96. package/bld/library/local/mqtt.d.ts +113 -0
  97. package/bld/library/local/mqtt.js +1074 -0
  98. package/bld/library/local/mqtt.js.map +1 -0
  99. package/bld/library/local/routed-message-client.d.ts +49 -0
  100. package/bld/library/local/routed-message-client.js +326 -0
  101. package/bld/library/local/routed-message-client.js.map +1 -0
  102. package/bld/library/miot/index.d.ts +4 -0
  103. package/bld/library/miot/index.js +5 -0
  104. package/bld/library/miot/index.js.map +1 -0
  105. package/bld/library/miot/matcher.d.ts +84 -0
  106. package/bld/library/miot/matcher.js +326 -0
  107. package/bld/library/miot/matcher.js.map +1 -0
  108. package/bld/library/miot/request.d.ts +33 -0
  109. package/bld/library/miot/request.js +57 -0
  110. package/bld/library/miot/request.js.map +1 -0
  111. package/bld/library/miot/result.d.ts +8 -0
  112. package/bld/library/miot/result.js +5 -0
  113. package/bld/library/miot/result.js.map +1 -0
  114. package/bld/library/miot/spec.d.ts +96 -0
  115. package/bld/library/miot/spec.js +86 -0
  116. package/bld/library/miot/spec.js.map +1 -0
  117. package/bld/library/provider.d.ts +78 -0
  118. package/bld/library/provider.js +600 -0
  119. package/bld/library/provider.js.map +1 -0
  120. package/bld/library/session-manager.d.ts +34 -0
  121. package/bld/library/session-manager.js +177 -0
  122. package/bld/library/session-manager.js.map +1 -0
  123. package/bld/library/session.d.ts +36 -0
  124. package/bld/library/session.js +370 -0
  125. package/bld/library/session.js.map +1 -0
  126. package/bld/library/storage.d.ts +2 -0
  127. package/bld/library/storage.js +66 -0
  128. package/bld/library/storage.js.map +1 -0
  129. package/bld/library/terminal/bindings.d.ts +3 -0
  130. package/bld/library/terminal/bindings.js +306 -0
  131. package/bld/library/terminal/bindings.js.map +1 -0
  132. package/bld/library/terminal/index.d.ts +2 -0
  133. package/bld/library/terminal/index.js +3 -0
  134. package/bld/library/terminal/index.js.map +1 -0
  135. package/bld/library/terminal/provider-details.d.ts +3 -0
  136. package/bld/library/terminal/provider-details.js +477 -0
  137. package/bld/library/terminal/provider-details.js.map +1 -0
  138. package/package.json +29 -0
  139. package/src/library/backend/client.ts +737 -0
  140. package/src/library/backend/constants.ts +17 -0
  141. package/src/library/backend/index.ts +3 -0
  142. package/src/library/backend/oauth.ts +170 -0
  143. package/src/library/binding.ts +387 -0
  144. package/src/library/cloud/client.ts +155 -0
  145. package/src/library/cloud/constants.ts +19 -0
  146. package/src/library/cloud/device.ts +938 -0
  147. package/src/library/cloud/index.ts +5 -0
  148. package/src/library/cloud/mqtt.ts +671 -0
  149. package/src/library/cloud/transport.ts +46 -0
  150. package/src/library/command.ts +7 -0
  151. package/src/library/configuration.ts +480 -0
  152. package/src/library/device.ts +601 -0
  153. package/src/library/devices/air-conditioner.ts +171 -0
  154. package/src/library/devices/command-effect.ts +425 -0
  155. package/src/library/devices/dehumidifier.ts +131 -0
  156. package/src/library/devices/fan.ts +140 -0
  157. package/src/library/devices/index.ts +9 -0
  158. package/src/library/devices/light.ts +119 -0
  159. package/src/library/devices/pet-feeder.ts +129 -0
  160. package/src/library/devices/placeholder.ts +3 -0
  161. package/src/library/devices/temperature-humidity-sensor.ts +49 -0
  162. package/src/library/devices/value-range.ts +14 -0
  163. package/src/library/endpoint-connection.ts +841 -0
  164. package/src/library/index.ts +71 -0
  165. package/src/library/local/certificate.ts +401 -0
  166. package/src/library/local/controller.ts +693 -0
  167. package/src/library/local/discovery.ts +485 -0
  168. package/src/library/local/index.ts +6 -0
  169. package/src/library/local/message.ts +174 -0
  170. package/src/library/local/mqtt.ts +1555 -0
  171. package/src/library/local/routed-message-client.ts +377 -0
  172. package/src/library/miot/index.ts +4 -0
  173. package/src/library/miot/matcher.ts +683 -0
  174. package/src/library/miot/request.ts +70 -0
  175. package/src/library/miot/result.ts +14 -0
  176. package/src/library/miot/spec.ts +114 -0
  177. package/src/library/provider.ts +745 -0
  178. package/src/library/session-manager.ts +224 -0
  179. package/src/library/session.ts +528 -0
  180. package/src/library/storage.ts +83 -0
  181. package/src/library/terminal/bindings.tsx +692 -0
  182. package/src/library/terminal/index.ts +2 -0
  183. package/src/library/terminal/provider-details.tsx +871 -0
@@ -0,0 +1,683 @@
1
+ import type {
2
+ MiotSpecAction,
3
+ MiotSpecProperty,
4
+ MiotSpecService,
5
+ MiotSpecValueList,
6
+ MiotSpecValueRange,
7
+ } from './spec.js';
8
+
9
+ const MIOT_INTEGER_FORMAT_RANGES: Readonly<
10
+ Record<string, readonly [minimum: number, maximum: number]>
11
+ > = {
12
+ int8: [-128, 127],
13
+ int16: [-32_768, 32_767],
14
+ int32: [-2_147_483_648, 2_147_483_647],
15
+ uint8: [0, 255],
16
+ uint16: [0, 65_535],
17
+ uint32: [0, 4_294_967_295],
18
+ };
19
+
20
+ /**
21
+ * One or more comma-separated full-URN prefix patterns. `*` matches one or
22
+ * more non-colon characters, and each successful match must end at a URN
23
+ * segment boundary.
24
+ */
25
+ export type MiotUrnPattern = string;
26
+
27
+ /** Maps HomeLib domain names to MIoT wire values. */
28
+ export type MiotEnumValueMapping = Readonly<Record<string, number>>;
29
+
30
+ /** Selects an enum value mapping from the complete device URN. */
31
+ export type MiotEnumMapping = Readonly<
32
+ Record<MiotUrnPattern, MiotEnumValueMapping>
33
+ >;
34
+
35
+ /** Selects a physical property IID from the complete device URN. */
36
+ export type MiotPropertyIidMapping = Readonly<Record<MiotUrnPattern, number>>;
37
+
38
+ export type MiotPropertyMapping =
39
+ | string
40
+ | {
41
+ readonly name: string;
42
+ readonly enum?: MiotEnumMapping;
43
+ readonly iid?: MiotPropertyIidMapping;
44
+ readonly optional?: true;
45
+ };
46
+
47
+ export type MiotPropertySchema = Readonly<
48
+ Record<string, Readonly<Record<string, MiotPropertyMapping>>>
49
+ >;
50
+
51
+ export type MiotActionMapping = {
52
+ readonly in: readonly MiotUrnPattern[];
53
+ readonly out?: readonly MiotUrnPattern[];
54
+ };
55
+
56
+ /** Required actions grouped by their owning service URN pattern. */
57
+ export type MiotActionSchema = Readonly<
58
+ Record<MiotUrnPattern, Readonly<Record<MiotUrnPattern, MiotActionMapping>>>
59
+ >;
60
+
61
+ export type MiotActionSchemaMatch = {
62
+ readonly service: MiotSpecService;
63
+ readonly action: MiotSpecAction;
64
+ readonly in: readonly MiotSpecProperty[];
65
+ readonly out: readonly MiotSpecProperty[] | undefined;
66
+ };
67
+
68
+ export type MiotSpecMatchContext = {
69
+ readonly type: string;
70
+ readonly services: readonly MiotSpecService[];
71
+ };
72
+
73
+ export type MiotResolvedSpecProperty = MiotSpecProperty & {
74
+ readonly enum?: MiotEnumValueMapping;
75
+ };
76
+
77
+ export type MiotPropertySchemaResource = {
78
+ readonly service: MiotSpecService;
79
+ readonly properties: Readonly<Record<string, MiotResolvedSpecProperty>>;
80
+ };
81
+
82
+ type MiotPropertySchemaMapping<TSchema extends MiotPropertySchema> = {
83
+ readonly [TService in keyof TSchema]: TSchema[TService] extends Readonly<
84
+ Record<string, MiotPropertyMapping>
85
+ >
86
+ ? TSchema[TService][keyof TSchema[TService]]
87
+ : never;
88
+ }[keyof TSchema];
89
+
90
+ type MiotPropertyMappingName<TMapping> = TMapping extends string
91
+ ? TMapping
92
+ : TMapping extends {readonly name: infer TName extends string}
93
+ ? TName
94
+ : never;
95
+
96
+ type MiotRequiredPropertyName<TMapping> = TMapping extends {
97
+ readonly optional: true;
98
+ }
99
+ ? never
100
+ : MiotPropertyMappingName<TMapping>;
101
+
102
+ type MiotOptionalPropertyName<TMapping> = TMapping extends {
103
+ readonly optional: true;
104
+ }
105
+ ? MiotPropertyMappingName<TMapping>
106
+ : never;
107
+
108
+ type MiotPropertyMappingForName<TMapping, TName> =
109
+ TMapping extends MiotPropertyMapping
110
+ ? MiotPropertyMappingName<TMapping> extends TName
111
+ ? TMapping
112
+ : never
113
+ : never;
114
+
115
+ type MiotResolvedPropertyForName<
116
+ TSchema extends MiotPropertySchema,
117
+ TName,
118
+ > = Omit<MiotResolvedSpecProperty, 'enum'> & {
119
+ readonly name: Extract<TName, string>;
120
+ } & (MiotPropertyMappingForName<
121
+ MiotPropertySchemaMapping<TSchema>,
122
+ TName
123
+ > extends infer TMapping
124
+ ? TMapping extends {readonly enum: infer TEnum extends MiotEnumMapping}
125
+ ? {readonly enum: TEnum[keyof TEnum]}
126
+ : {readonly enum?: undefined}
127
+ : never);
128
+
129
+ export type MiotPropertySchemaProperties<TSchema extends MiotPropertySchema> =
130
+ Readonly<
131
+ {
132
+ readonly [
133
+ TName in MiotRequiredPropertyName<MiotPropertySchemaMapping<TSchema>>
134
+ ]: MiotResolvedPropertyForName<TSchema, TName>;
135
+ } & {
136
+ readonly [
137
+ TName in MiotOptionalPropertyName<MiotPropertySchemaMapping<TSchema>>
138
+ ]?: MiotResolvedPropertyForName<TSchema, TName>;
139
+ }
140
+ >;
141
+
142
+ export function assertMiotPropertySchema(schema: MiotPropertySchema): void {
143
+ const services = Object.entries(schema);
144
+
145
+ if (services.length === 0) {
146
+ throw new TypeError('A MIoT property schema requires a service.');
147
+ }
148
+
149
+ const nameSet = new Set<string>();
150
+
151
+ for (const [serviceType, properties] of services) {
152
+ if (
153
+ !isValidMiotUrnPattern(serviceType) ||
154
+ Object.keys(properties).length === 0
155
+ ) {
156
+ throw new TypeError('Invalid MIoT property schema service.');
157
+ }
158
+
159
+ for (const [propertyType, mapping] of Object.entries(properties)) {
160
+ if (!isValidMiotUrnPattern(propertyType)) {
161
+ throw new TypeError('Invalid MIoT property schema property.');
162
+ }
163
+
164
+ const {
165
+ name,
166
+ enum: enumMapping,
167
+ iid: iidMapping,
168
+ } = getPropertyMapping(mapping);
169
+
170
+ if (iidMapping !== undefined && !isValidIidMapping(iidMapping)) {
171
+ throw new TypeError('Invalid MIoT property schema IID.');
172
+ }
173
+
174
+ if (name.length === 0 || name === '__proto__' || nameSet.has(name)) {
175
+ throw new TypeError('Invalid MIoT property schema name.');
176
+ }
177
+
178
+ nameSet.add(name);
179
+
180
+ if (enumMapping !== undefined && !isValidEnumMapping(enumMapping)) {
181
+ throw new TypeError('Invalid MIoT property schema enum.');
182
+ }
183
+ }
184
+ }
185
+ }
186
+
187
+ export function assertMiotActionSchema(schema: MiotActionSchema): void {
188
+ const services = Object.entries(schema);
189
+
190
+ if (services.length === 0) {
191
+ throw new TypeError('A MIoT action schema requires a service.');
192
+ }
193
+
194
+ for (const [serviceType, actions] of services) {
195
+ if (
196
+ !isValidMiotUrnPattern(serviceType) ||
197
+ Object.keys(actions).length === 0
198
+ ) {
199
+ throw new TypeError('Invalid MIoT action schema service.');
200
+ }
201
+
202
+ for (const [actionType, mapping] of Object.entries(actions)) {
203
+ if (
204
+ !isValidMiotUrnPattern(actionType) ||
205
+ !mapping.in.every(isValidMiotUrnPattern) ||
206
+ (mapping.out !== undefined && !mapping.out.every(isValidMiotUrnPattern))
207
+ ) {
208
+ throw new TypeError('Invalid MIoT action schema action.');
209
+ }
210
+ }
211
+ }
212
+ }
213
+
214
+ export function matchesMiotActionSchema(
215
+ resources: readonly MiotPropertySchemaResource[],
216
+ schema: MiotActionSchema,
217
+ ): boolean {
218
+ return resolveMiotActionSchema(resources, schema) !== undefined;
219
+ }
220
+
221
+ export function resolveMiotActionSchema(
222
+ resources: readonly MiotPropertySchemaResource[],
223
+ schema: MiotActionSchema,
224
+ ): readonly MiotActionSchemaMatch[] | undefined {
225
+ assertMiotActionSchema(schema);
226
+ const matches: MiotActionSchemaMatch[] = [];
227
+
228
+ for (const [serviceType, actionSchema] of Object.entries(schema)) {
229
+ const matchingResources = resources.filter(resource =>
230
+ matchesMiotUrnPattern(resource.service.type, serviceType),
231
+ );
232
+ const [resource] = matchingResources;
233
+
234
+ if (matchingResources.length !== 1 || resource === undefined) {
235
+ return undefined;
236
+ }
237
+
238
+ for (const [actionType, mapping] of Object.entries(actionSchema)) {
239
+ const matchingActions = (resource.service.actions ?? []).filter(action =>
240
+ matchesMiotUrnPattern(action.type, actionType),
241
+ );
242
+ const [matchedAction] = matchingActions;
243
+ const inputProperties =
244
+ matchedAction === undefined
245
+ ? undefined
246
+ : resolveActionProperties(
247
+ resource.service,
248
+ matchedAction.in,
249
+ mapping.in,
250
+ );
251
+ const outputProperties =
252
+ matchedAction === undefined || mapping.out === undefined
253
+ ? undefined
254
+ : resolveActionProperties(
255
+ resource.service,
256
+ matchedAction.out,
257
+ mapping.out,
258
+ );
259
+
260
+ if (
261
+ matchingActions.length !== 1 ||
262
+ matchedAction === undefined ||
263
+ inputProperties === undefined ||
264
+ (mapping.out !== undefined && outputProperties === undefined)
265
+ ) {
266
+ return undefined;
267
+ }
268
+
269
+ matches.push({
270
+ service: resource.service,
271
+ action: matchedAction,
272
+ in: inputProperties,
273
+ out: outputProperties,
274
+ });
275
+ }
276
+ }
277
+
278
+ return matches;
279
+ }
280
+
281
+ export function resolveMiotPropertySchema(
282
+ spec: MiotSpecMatchContext,
283
+ schema: MiotPropertySchema,
284
+ options: {readonly allowMultipleOptionalServices?: boolean} = {},
285
+ ): readonly MiotPropertySchemaResource[] | undefined {
286
+ assertMiotPropertySchema(schema);
287
+
288
+ const {services, type: deviceType} = spec;
289
+ const resources: MiotPropertySchemaResource[] = [];
290
+ const serviceIidSet = new Set<number>();
291
+
292
+ for (const [serviceType, propertySchema] of Object.entries(schema)) {
293
+ const required = Object.values(propertySchema).some(
294
+ mapping => !getPropertyMapping(mapping).optional,
295
+ );
296
+ const matches = services.flatMap(service => {
297
+ if (!matchesMiotUrnPattern(service.type, serviceType)) {
298
+ return [];
299
+ }
300
+
301
+ const properties = resolveServiceProperties(
302
+ deviceType,
303
+ service,
304
+ propertySchema,
305
+ );
306
+
307
+ return properties === undefined ||
308
+ (!required && Object.keys(properties).length === 0)
309
+ ? []
310
+ : [{service, properties}];
311
+ });
312
+
313
+ if (
314
+ (required && matches.length !== 1) ||
315
+ (!required &&
316
+ !options.allowMultipleOptionalServices &&
317
+ matches.length > 1)
318
+ ) {
319
+ return undefined;
320
+ }
321
+
322
+ for (const match of matches) {
323
+ if (serviceIidSet.has(match.service.iid)) {
324
+ return undefined;
325
+ }
326
+
327
+ serviceIidSet.add(match.service.iid);
328
+ resources.push(match);
329
+ }
330
+ }
331
+
332
+ return resources;
333
+ }
334
+
335
+ export function isValidMiotSpecValueList(
336
+ valueList: MiotSpecValueList | undefined,
337
+ ): valueList is MiotSpecValueList {
338
+ if (valueList === undefined || valueList.length === 0) {
339
+ return false;
340
+ }
341
+
342
+ return isValidUniqueNumberSet(valueList.map(entry => entry.value));
343
+ }
344
+
345
+ export function isValidMiotSpecValueRange(
346
+ valueRange: MiotSpecValueRange | undefined,
347
+ format?: string,
348
+ ): valueRange is MiotSpecValueRange {
349
+ if (valueRange === undefined) {
350
+ return false;
351
+ }
352
+
353
+ const [minimum, maximum, step] = valueRange;
354
+
355
+ if (
356
+ !Number.isFinite(minimum) ||
357
+ !Number.isFinite(maximum) ||
358
+ !Number.isFinite(step) ||
359
+ minimum >= maximum ||
360
+ step <= 0
361
+ ) {
362
+ return false;
363
+ }
364
+
365
+ const stepCount = (maximum - minimum) / step;
366
+
367
+ if (!isApproximatelyInteger(stepCount)) {
368
+ return false;
369
+ }
370
+
371
+ const formatRange =
372
+ format === undefined ? undefined : MIOT_INTEGER_FORMAT_RANGES[format];
373
+
374
+ if (formatRange === undefined) {
375
+ return true;
376
+ }
377
+
378
+ const [formatMinimum, formatMaximum] = formatRange;
379
+
380
+ return (
381
+ Number.isInteger(minimum) &&
382
+ Number.isInteger(maximum) &&
383
+ Number.isInteger(step) &&
384
+ minimum >= formatMinimum &&
385
+ maximum <= formatMaximum
386
+ );
387
+ }
388
+
389
+ function resolveServiceProperties(
390
+ deviceType: string,
391
+ service: MiotSpecService,
392
+ schema: Readonly<Record<string, MiotPropertyMapping>>,
393
+ ): Readonly<Record<string, MiotResolvedSpecProperty>> | undefined {
394
+ const properties: Record<string, MiotResolvedSpecProperty> = {};
395
+ const usedPropertyIids = new Set<number>();
396
+ const optionalCandidates: Array<
397
+ readonly [name: string, property: MiotResolvedSpecProperty]
398
+ > = [];
399
+ const optionalPropertyUseCount = new Map<number, number>();
400
+
401
+ for (const [propertyType, configuredMapping] of Object.entries(schema)) {
402
+ const mapping = getPropertyMapping(configuredMapping);
403
+ const enumMapping =
404
+ mapping.enum === undefined
405
+ ? undefined
406
+ : selectMiotUrnPatternValue(deviceType, mapping.enum);
407
+ const iid =
408
+ mapping.iid === undefined
409
+ ? undefined
410
+ : selectMiotUrnPatternValue(deviceType, mapping.iid);
411
+ const candidates =
412
+ mapping.iid !== undefined && iid === undefined
413
+ ? []
414
+ : findPropertyCandidates(
415
+ service,
416
+ propertyType,
417
+ enumMapping,
418
+ mapping.enum !== undefined,
419
+ iid,
420
+ );
421
+ const [property] = candidates;
422
+
423
+ if (!mapping.optional) {
424
+ if (
425
+ candidates.length !== 1 ||
426
+ property === undefined ||
427
+ usedPropertyIids.has(property.iid)
428
+ ) {
429
+ return undefined;
430
+ }
431
+
432
+ properties[mapping.name] = resolveProperty(property, enumMapping);
433
+ usedPropertyIids.add(property.iid);
434
+ continue;
435
+ }
436
+
437
+ if (
438
+ candidates.length !== 1 ||
439
+ property === undefined ||
440
+ usedPropertyIids.has(property.iid)
441
+ ) {
442
+ continue;
443
+ }
444
+
445
+ optionalCandidates.push([
446
+ mapping.name,
447
+ resolveProperty(property, enumMapping),
448
+ ]);
449
+ optionalPropertyUseCount.set(
450
+ property.iid,
451
+ (optionalPropertyUseCount.get(property.iid) ?? 0) + 1,
452
+ );
453
+ }
454
+
455
+ for (const [name, property] of optionalCandidates) {
456
+ if (
457
+ optionalPropertyUseCount.get(property.iid) === 1 &&
458
+ !usedPropertyIids.has(property.iid)
459
+ ) {
460
+ properties[name] = property;
461
+ }
462
+ }
463
+
464
+ return properties;
465
+ }
466
+
467
+ function findPropertyCandidates(
468
+ service: MiotSpecService,
469
+ propertyType: string,
470
+ enumMapping: MiotEnumValueMapping | undefined,
471
+ requiresEnumMapping: boolean,
472
+ iid: number | undefined,
473
+ ): readonly MiotSpecProperty[] {
474
+ return (service.properties ?? []).filter(property => {
475
+ if (
476
+ !matchesMiotUrnPattern(property.type, propertyType) ||
477
+ (iid !== undefined && property.iid !== iid)
478
+ ) {
479
+ return false;
480
+ }
481
+
482
+ // Vendor instances can declare different access modes for the same base
483
+ // property type.
484
+ // State-backed aliases must remain observable between commands so that
485
+ // acknowledged effects can be reconciled with manual device changes.
486
+ if (
487
+ !property.access.includes('read') ||
488
+ !property.access.includes('notify')
489
+ ) {
490
+ return false;
491
+ }
492
+
493
+ if (enumMapping === undefined) {
494
+ return !requiresEnumMapping;
495
+ }
496
+
497
+ const valueList = property['value-list'];
498
+
499
+ return (
500
+ isValidMiotSpecValueList(valueList) &&
501
+ Object.values(enumMapping).some(value =>
502
+ valueList.some(entry => entry.value === value),
503
+ )
504
+ );
505
+ });
506
+ }
507
+
508
+ function resolveActionProperties(
509
+ service: MiotSpecService,
510
+ propertyIids: readonly number[],
511
+ propertyTypes: readonly MiotUrnPattern[],
512
+ ): readonly MiotSpecProperty[] | undefined {
513
+ if (propertyIids.length !== propertyTypes.length) {
514
+ return undefined;
515
+ }
516
+
517
+ const properties: MiotSpecProperty[] = [];
518
+
519
+ for (const [index, iid] of propertyIids.entries()) {
520
+ const propertyType = propertyTypes[index];
521
+ const candidates = (service.properties ?? []).filter(
522
+ property => property.iid === iid,
523
+ );
524
+ const [property] = candidates;
525
+
526
+ if (
527
+ candidates.length !== 1 ||
528
+ property === undefined ||
529
+ propertyType === undefined ||
530
+ !matchesMiotUrnPattern(property.type, propertyType)
531
+ ) {
532
+ return undefined;
533
+ }
534
+
535
+ properties.push(property);
536
+ }
537
+
538
+ return properties;
539
+ }
540
+
541
+ function resolveProperty(
542
+ property: MiotSpecProperty,
543
+ enumMapping: MiotEnumValueMapping | undefined,
544
+ ): MiotResolvedSpecProperty {
545
+ return enumMapping === undefined
546
+ ? property
547
+ : Object.assign({}, property, {enum: enumMapping});
548
+ }
549
+
550
+ function getPropertyMapping(mapping: MiotPropertyMapping): {
551
+ readonly name: string;
552
+ readonly enum?: MiotEnumMapping;
553
+ readonly iid?: MiotPropertyIidMapping;
554
+ readonly optional: boolean;
555
+ } {
556
+ return typeof mapping === 'string'
557
+ ? {name: mapping, optional: false}
558
+ : {
559
+ name: mapping.name,
560
+ enum: mapping.enum,
561
+ iid: mapping.iid,
562
+ optional: mapping.optional === true,
563
+ };
564
+ }
565
+
566
+ function isValidEnumMapping(mapping: MiotEnumMapping): boolean {
567
+ const entries = Object.entries(mapping);
568
+
569
+ return (
570
+ entries.length > 0 &&
571
+ entries.every(
572
+ ([pattern, valueMapping]) =>
573
+ isValidMiotUrnPattern(pattern) && isValidEnumValueMapping(valueMapping),
574
+ )
575
+ );
576
+ }
577
+
578
+ function isValidIidMapping(mapping: MiotPropertyIidMapping): boolean {
579
+ const entries = Object.entries(mapping);
580
+
581
+ return (
582
+ entries.length > 0 &&
583
+ entries.every(
584
+ ([pattern, iid]) =>
585
+ isValidMiotUrnPattern(pattern) && Number.isInteger(iid) && iid > 0,
586
+ )
587
+ );
588
+ }
589
+
590
+ function isValidEnumValueMapping(mapping: MiotEnumValueMapping): boolean {
591
+ const entries = Object.entries(mapping);
592
+
593
+ return (
594
+ entries.length > 0 &&
595
+ entries.every(([key, value]) => key.length > 0 && Number.isFinite(value)) &&
596
+ new Set(entries.map(([, value]) => value)).size === entries.length
597
+ );
598
+ }
599
+
600
+ /** Selects a matching pattern that is nested within every other match. */
601
+ export function selectMiotUrnPatternValue<T>(
602
+ urn: string,
603
+ values: Readonly<Record<MiotUrnPattern, T>>,
604
+ ): T | undefined {
605
+ const matches = Object.entries(values).filter(([pattern]) =>
606
+ matchesMiotUrnPattern(urn, pattern),
607
+ );
608
+ const nestedMatches = matches.filter(([pattern]) =>
609
+ matches.every(
610
+ ([otherPattern]) =>
611
+ pattern === otherPattern ||
612
+ isMiotUrnPatternNestedWithin(pattern, otherPattern),
613
+ ),
614
+ );
615
+
616
+ return nestedMatches.length === 1 ? nestedMatches[0]?.[1] : undefined;
617
+ }
618
+
619
+ /** Matches a pattern against the beginning of a complete MIoT URN. */
620
+ export function matchesMiotUrnPattern(
621
+ urn: string,
622
+ pattern: MiotUrnPattern,
623
+ ): boolean {
624
+ return getMiotUrnPatternAlternatives(pattern).some(alternative =>
625
+ matchesSingleMiotUrnPattern(urn, alternative),
626
+ );
627
+ }
628
+
629
+ function isValidMiotUrnPattern(pattern: MiotUrnPattern): boolean {
630
+ const alternatives = getMiotUrnPatternAlternatives(pattern);
631
+
632
+ return (
633
+ alternatives.length > 0 &&
634
+ alternatives.every(alternative => alternative.length > 0)
635
+ );
636
+ }
637
+
638
+ function getMiotUrnPatternAlternatives(
639
+ pattern: MiotUrnPattern,
640
+ ): readonly string[] {
641
+ return pattern.split(',').map(alternative => alternative.trim());
642
+ }
643
+
644
+ function isMiotUrnPatternNestedWithin(
645
+ pattern: MiotUrnPattern,
646
+ otherPattern: MiotUrnPattern,
647
+ ): boolean {
648
+ const alternatives = getMiotUrnPatternAlternatives(pattern);
649
+ const otherAlternatives = getMiotUrnPatternAlternatives(otherPattern);
650
+
651
+ return alternatives.every(alternative =>
652
+ otherAlternatives.some(otherAlternative =>
653
+ matchesSingleMiotUrnPattern(alternative, otherAlternative),
654
+ ),
655
+ );
656
+ }
657
+
658
+ function matchesSingleMiotUrnPattern(urn: string, pattern: string): boolean {
659
+ const source = pattern
660
+ .split('*')
661
+ .map(part => part.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&'))
662
+ .join('[^:]+');
663
+
664
+ return new RegExp(`^${source}(?=:|$)`).test(urn);
665
+ }
666
+
667
+ function isValidUniqueNumberSet(values: readonly number[]): boolean {
668
+ return (
669
+ values.every(value => Number.isFinite(value)) &&
670
+ new Set(values).size === values.length
671
+ );
672
+ }
673
+
674
+ function isApproximatelyInteger(value: number): boolean {
675
+ if (!Number.isFinite(value)) {
676
+ return false;
677
+ }
678
+
679
+ const nearestInteger = Math.round(value);
680
+ const tolerance = Number.EPSILON * Math.max(1, Math.abs(value)) * 8;
681
+
682
+ return Math.abs(value - nearestInteger) <= tolerance;
683
+ }