@homelib/xiaomi 0.0.0-wip.ba4eafa051eb
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 +19 -0
- package/README.md +22 -0
- package/bld/library/backend/client.d.ts +68 -0
- package/bld/library/backend/client.js +477 -0
- package/bld/library/backend/client.js.map +1 -0
- package/bld/library/backend/constants.d.ts +7 -0
- package/bld/library/backend/constants.js +11 -0
- package/bld/library/backend/constants.js.map +1 -0
- package/bld/library/backend/index.d.ts +3 -0
- package/bld/library/backend/index.js +4 -0
- package/bld/library/backend/index.js.map +1 -0
- package/bld/library/backend/oauth.d.ts +24 -0
- package/bld/library/backend/oauth.js +121 -0
- package/bld/library/backend/oauth.js.map +1 -0
- package/bld/library/binding.d.ts +44 -0
- package/bld/library/binding.js +212 -0
- package/bld/library/binding.js.map +1 -0
- package/bld/library/cloud/client.d.ts +27 -0
- package/bld/library/cloud/client.js +109 -0
- package/bld/library/cloud/client.js.map +1 -0
- package/bld/library/cloud/constants.d.ts +9 -0
- package/bld/library/cloud/constants.js +11 -0
- package/bld/library/cloud/constants.js.map +1 -0
- package/bld/library/cloud/device.d.ts +84 -0
- package/bld/library/cloud/device.js +686 -0
- package/bld/library/cloud/device.js.map +1 -0
- package/bld/library/cloud/index.d.ts +5 -0
- package/bld/library/cloud/index.js +6 -0
- package/bld/library/cloud/index.js.map +1 -0
- package/bld/library/cloud/mqtt.d.ts +69 -0
- package/bld/library/cloud/mqtt.js +517 -0
- package/bld/library/cloud/mqtt.js.map +1 -0
- package/bld/library/cloud/transport.d.ts +8 -0
- package/bld/library/cloud/transport.js +30 -0
- package/bld/library/cloud/transport.js.map +1 -0
- package/bld/library/command.d.ts +6 -0
- package/bld/library/command.js +6 -0
- package/bld/library/command.js.map +1 -0
- package/bld/library/configuration.d.ts +62 -0
- package/bld/library/configuration.js +303 -0
- package/bld/library/configuration.js.map +1 -0
- package/bld/library/device.d.ts +68 -0
- package/bld/library/device.js +235 -0
- package/bld/library/device.js.map +1 -0
- package/bld/library/devices/air-conditioner.d.ts +48 -0
- package/bld/library/devices/air-conditioner.js +155 -0
- package/bld/library/devices/air-conditioner.js.map +1 -0
- package/bld/library/devices/command-effect.d.ts +35 -0
- package/bld/library/devices/command-effect.js +269 -0
- package/bld/library/devices/command-effect.js.map +1 -0
- package/bld/library/devices/dehumidifier.d.ts +41 -0
- package/bld/library/devices/dehumidifier.js +124 -0
- package/bld/library/devices/dehumidifier.js.map +1 -0
- package/bld/library/devices/fan.d.ts +37 -0
- package/bld/library/devices/fan.js +135 -0
- package/bld/library/devices/fan.js.map +1 -0
- package/bld/library/devices/index.d.ts +7 -0
- package/bld/library/devices/index.js +8 -0
- package/bld/library/devices/index.js.map +1 -0
- package/bld/library/devices/light.d.ts +22 -0
- package/bld/library/devices/light.js +112 -0
- package/bld/library/devices/light.js.map +1 -0
- package/bld/library/devices/placeholder.d.ts +3 -0
- package/bld/library/devices/placeholder.js +4 -0
- package/bld/library/devices/placeholder.js.map +1 -0
- package/bld/library/devices/value-range.d.ts +2 -0
- package/bld/library/devices/value-range.js +6 -0
- package/bld/library/devices/value-range.js.map +1 -0
- package/bld/library/endpoint-connection.d.ts +154 -0
- package/bld/library/endpoint-connection.js +510 -0
- package/bld/library/endpoint-connection.js.map +1 -0
- package/bld/library/index.d.ts +26 -0
- package/bld/library/index.js +31 -0
- package/bld/library/index.js.map +1 -0
- package/bld/library/local/certificate.d.ts +31 -0
- package/bld/library/local/certificate.js +298 -0
- package/bld/library/local/certificate.js.map +1 -0
- package/bld/library/local/controller.d.ts +70 -0
- package/bld/library/local/controller.js +497 -0
- package/bld/library/local/controller.js.map +1 -0
- package/bld/library/local/discovery.d.ts +29 -0
- package/bld/library/local/discovery.js +321 -0
- package/bld/library/local/discovery.js.map +1 -0
- package/bld/library/local/index.d.ts +6 -0
- package/bld/library/local/index.js +7 -0
- package/bld/library/local/index.js.map +1 -0
- package/bld/library/local/message.d.ts +11 -0
- package/bld/library/local/message.js +128 -0
- package/bld/library/local/message.js.map +1 -0
- package/bld/library/local/mqtt.d.ts +113 -0
- package/bld/library/local/mqtt.js +1044 -0
- package/bld/library/local/mqtt.js.map +1 -0
- package/bld/library/local/routed-message-client.d.ts +49 -0
- package/bld/library/local/routed-message-client.js +326 -0
- package/bld/library/local/routed-message-client.js.map +1 -0
- package/bld/library/miot/index.d.ts +4 -0
- package/bld/library/miot/index.js +5 -0
- package/bld/library/miot/index.js.map +1 -0
- package/bld/library/miot/matcher.d.ts +65 -0
- package/bld/library/miot/matcher.js +217 -0
- package/bld/library/miot/matcher.js.map +1 -0
- package/bld/library/miot/request.d.ts +16 -0
- package/bld/library/miot/request.js +27 -0
- package/bld/library/miot/request.js.map +1 -0
- package/bld/library/miot/result.d.ts +4 -0
- package/bld/library/miot/result.js +5 -0
- package/bld/library/miot/result.js.map +1 -0
- package/bld/library/miot/spec.d.ts +74 -0
- package/bld/library/miot/spec.js +78 -0
- package/bld/library/miot/spec.js.map +1 -0
- package/bld/library/provider.d.ts +71 -0
- package/bld/library/provider.js +600 -0
- package/bld/library/provider.js.map +1 -0
- package/bld/library/session-manager.d.ts +34 -0
- package/bld/library/session-manager.js +177 -0
- package/bld/library/session-manager.js.map +1 -0
- package/bld/library/session.d.ts +36 -0
- package/bld/library/session.js +370 -0
- package/bld/library/session.js.map +1 -0
- package/bld/library/storage.d.ts +2 -0
- package/bld/library/storage.js +66 -0
- package/bld/library/storage.js.map +1 -0
- package/bld/library/terminal/bindings.d.ts +3 -0
- package/bld/library/terminal/bindings.js +280 -0
- package/bld/library/terminal/bindings.js.map +1 -0
- package/bld/library/terminal/index.d.ts +2 -0
- package/bld/library/terminal/index.js +3 -0
- package/bld/library/terminal/index.js.map +1 -0
- package/bld/library/terminal/provider-details.d.ts +3 -0
- package/bld/library/terminal/provider-details.js +477 -0
- package/bld/library/terminal/provider-details.js.map +1 -0
- package/package.json +29 -0
- package/src/library/backend/client.ts +713 -0
- package/src/library/backend/constants.ts +17 -0
- package/src/library/backend/index.ts +3 -0
- package/src/library/backend/oauth.ts +170 -0
- package/src/library/binding.ts +387 -0
- package/src/library/cloud/client.ts +155 -0
- package/src/library/cloud/constants.ts +19 -0
- package/src/library/cloud/device.ts +938 -0
- package/src/library/cloud/index.ts +5 -0
- package/src/library/cloud/mqtt.ts +671 -0
- package/src/library/cloud/transport.ts +38 -0
- package/src/library/command.ts +7 -0
- package/src/library/configuration.ts +480 -0
- package/src/library/device.ts +548 -0
- package/src/library/devices/air-conditioner.ts +171 -0
- package/src/library/devices/command-effect.ts +425 -0
- package/src/library/devices/dehumidifier.ts +131 -0
- package/src/library/devices/fan.ts +140 -0
- package/src/library/devices/index.ts +7 -0
- package/src/library/devices/light.ts +119 -0
- package/src/library/devices/placeholder.ts +3 -0
- package/src/library/devices/value-range.ts +14 -0
- package/src/library/endpoint-connection.ts +841 -0
- package/src/library/index.ts +62 -0
- package/src/library/local/certificate.ts +401 -0
- package/src/library/local/controller.ts +692 -0
- package/src/library/local/discovery.ts +485 -0
- package/src/library/local/index.ts +6 -0
- package/src/library/local/message.ts +174 -0
- package/src/library/local/mqtt.ts +1493 -0
- package/src/library/local/routed-message-client.ts +377 -0
- package/src/library/miot/index.ts +4 -0
- package/src/library/miot/matcher.ts +461 -0
- package/src/library/miot/request.ts +30 -0
- package/src/library/miot/result.ts +10 -0
- package/src/library/miot/spec.ts +103 -0
- package/src/library/provider.ts +745 -0
- package/src/library/session-manager.ts +224 -0
- package/src/library/session.ts +528 -0
- package/src/library/storage.ts +83 -0
- package/src/library/terminal/bindings.tsx +612 -0
- package/src/library/terminal/index.ts +2 -0
- package/src/library/terminal/provider-details.tsx +871 -0
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
MiotSpecProperty,
|
|
3
|
+
MiotSpecService,
|
|
4
|
+
MiotSpecValueList,
|
|
5
|
+
MiotSpecValueRange,
|
|
6
|
+
} from './spec.js';
|
|
7
|
+
|
|
8
|
+
const MIOT_INTEGER_FORMAT_RANGES: Readonly<
|
|
9
|
+
Record<string, readonly [minimum: number, maximum: number]>
|
|
10
|
+
> = {
|
|
11
|
+
int8: [-128, 127],
|
|
12
|
+
int16: [-32_768, 32_767],
|
|
13
|
+
int32: [-2_147_483_648, 2_147_483_647],
|
|
14
|
+
uint8: [0, 255],
|
|
15
|
+
uint16: [0, 65_535],
|
|
16
|
+
uint32: [0, 4_294_967_295],
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A full-URN prefix pattern. `*` matches one or more non-colon characters,
|
|
21
|
+
* and a successful match must end at a URN segment boundary.
|
|
22
|
+
*/
|
|
23
|
+
export type MiotUrnPattern = string;
|
|
24
|
+
|
|
25
|
+
/** Maps HomeLib domain names to MIoT wire values. */
|
|
26
|
+
export type MiotEnumValueMapping = Readonly<Record<string, number>>;
|
|
27
|
+
|
|
28
|
+
/** Selects an enum value mapping from the complete device URN. */
|
|
29
|
+
export type MiotEnumMapping = Readonly<
|
|
30
|
+
Record<MiotUrnPattern, MiotEnumValueMapping>
|
|
31
|
+
>;
|
|
32
|
+
|
|
33
|
+
export type MiotPropertyMapping =
|
|
34
|
+
| string
|
|
35
|
+
| {
|
|
36
|
+
readonly name: string;
|
|
37
|
+
readonly enum?: MiotEnumMapping;
|
|
38
|
+
readonly optional?: true;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export type MiotPropertySchema = Readonly<
|
|
42
|
+
Record<string, Readonly<Record<string, MiotPropertyMapping>>>
|
|
43
|
+
>;
|
|
44
|
+
|
|
45
|
+
export type MiotSpecMatchContext = {
|
|
46
|
+
readonly type: string;
|
|
47
|
+
readonly services: readonly MiotSpecService[];
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export type MiotResolvedSpecProperty = MiotSpecProperty & {
|
|
51
|
+
readonly enum?: MiotEnumValueMapping;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export type MiotPropertySchemaResource = {
|
|
55
|
+
readonly service: MiotSpecService;
|
|
56
|
+
readonly properties: Readonly<Record<string, MiotResolvedSpecProperty>>;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
type MiotPropertySchemaMapping<TSchema extends MiotPropertySchema> = {
|
|
60
|
+
readonly [TService in keyof TSchema]: TSchema[TService] extends Readonly<
|
|
61
|
+
Record<string, MiotPropertyMapping>
|
|
62
|
+
>
|
|
63
|
+
? TSchema[TService][keyof TSchema[TService]]
|
|
64
|
+
: never;
|
|
65
|
+
}[keyof TSchema];
|
|
66
|
+
|
|
67
|
+
type MiotPropertyMappingName<TMapping> = TMapping extends string
|
|
68
|
+
? TMapping
|
|
69
|
+
: TMapping extends {readonly name: infer TName extends string}
|
|
70
|
+
? TName
|
|
71
|
+
: never;
|
|
72
|
+
|
|
73
|
+
type MiotRequiredPropertyName<TMapping> = TMapping extends {
|
|
74
|
+
readonly optional: true;
|
|
75
|
+
}
|
|
76
|
+
? never
|
|
77
|
+
: MiotPropertyMappingName<TMapping>;
|
|
78
|
+
|
|
79
|
+
type MiotOptionalPropertyName<TMapping> = TMapping extends {
|
|
80
|
+
readonly optional: true;
|
|
81
|
+
}
|
|
82
|
+
? MiotPropertyMappingName<TMapping>
|
|
83
|
+
: never;
|
|
84
|
+
|
|
85
|
+
type MiotPropertyMappingForName<TMapping, TName> =
|
|
86
|
+
TMapping extends MiotPropertyMapping
|
|
87
|
+
? MiotPropertyMappingName<TMapping> extends TName
|
|
88
|
+
? TMapping
|
|
89
|
+
: never
|
|
90
|
+
: never;
|
|
91
|
+
|
|
92
|
+
type MiotResolvedPropertyForName<
|
|
93
|
+
TSchema extends MiotPropertySchema,
|
|
94
|
+
TName,
|
|
95
|
+
> = Omit<MiotResolvedSpecProperty, 'enum'> & {
|
|
96
|
+
readonly name: Extract<TName, string>;
|
|
97
|
+
} & (MiotPropertyMappingForName<
|
|
98
|
+
MiotPropertySchemaMapping<TSchema>,
|
|
99
|
+
TName
|
|
100
|
+
> extends infer TMapping
|
|
101
|
+
? TMapping extends {readonly enum: infer TEnum extends MiotEnumMapping}
|
|
102
|
+
? {readonly enum: TEnum[keyof TEnum]}
|
|
103
|
+
: {readonly enum?: undefined}
|
|
104
|
+
: never);
|
|
105
|
+
|
|
106
|
+
export type MiotPropertySchemaProperties<TSchema extends MiotPropertySchema> =
|
|
107
|
+
Readonly<
|
|
108
|
+
{
|
|
109
|
+
readonly [
|
|
110
|
+
TName in MiotRequiredPropertyName<MiotPropertySchemaMapping<TSchema>>
|
|
111
|
+
]: MiotResolvedPropertyForName<TSchema, TName>;
|
|
112
|
+
} & {
|
|
113
|
+
readonly [
|
|
114
|
+
TName in MiotOptionalPropertyName<MiotPropertySchemaMapping<TSchema>>
|
|
115
|
+
]?: MiotResolvedPropertyForName<TSchema, TName>;
|
|
116
|
+
}
|
|
117
|
+
>;
|
|
118
|
+
|
|
119
|
+
export function assertMiotPropertySchema(schema: MiotPropertySchema): void {
|
|
120
|
+
const services = Object.entries(schema);
|
|
121
|
+
|
|
122
|
+
if (services.length === 0) {
|
|
123
|
+
throw new TypeError('A MIoT property schema requires a service.');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const nameSet = new Set<string>();
|
|
127
|
+
|
|
128
|
+
for (const [serviceType, properties] of services) {
|
|
129
|
+
if (serviceType.length === 0 || Object.keys(properties).length === 0) {
|
|
130
|
+
throw new TypeError('Invalid MIoT property schema service.');
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
for (const [propertyType, mapping] of Object.entries(properties)) {
|
|
134
|
+
if (propertyType.length === 0) {
|
|
135
|
+
throw new TypeError('Invalid MIoT property schema property.');
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const {name, enum: enumMapping} = getPropertyMapping(mapping);
|
|
139
|
+
|
|
140
|
+
if (name.length === 0 || name === '__proto__' || nameSet.has(name)) {
|
|
141
|
+
throw new TypeError('Invalid MIoT property schema name.');
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
nameSet.add(name);
|
|
145
|
+
|
|
146
|
+
if (enumMapping !== undefined && !isValidEnumMapping(enumMapping)) {
|
|
147
|
+
throw new TypeError('Invalid MIoT property schema enum.');
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function resolveMiotPropertySchema(
|
|
154
|
+
spec: MiotSpecMatchContext,
|
|
155
|
+
schema: MiotPropertySchema,
|
|
156
|
+
options: {readonly allowMultipleOptionalServices?: boolean} = {},
|
|
157
|
+
): readonly MiotPropertySchemaResource[] | undefined {
|
|
158
|
+
assertMiotPropertySchema(schema);
|
|
159
|
+
|
|
160
|
+
const {services, type: deviceType} = spec;
|
|
161
|
+
const resources: MiotPropertySchemaResource[] = [];
|
|
162
|
+
const serviceIidSet = new Set<number>();
|
|
163
|
+
|
|
164
|
+
for (const [serviceType, propertySchema] of Object.entries(schema)) {
|
|
165
|
+
const required = Object.values(propertySchema).some(
|
|
166
|
+
mapping => !getPropertyMapping(mapping).optional,
|
|
167
|
+
);
|
|
168
|
+
const matches = services.flatMap(service => {
|
|
169
|
+
if (!matchesMiotUrnPattern(service.type, serviceType)) {
|
|
170
|
+
return [];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const properties = resolveServiceProperties(
|
|
174
|
+
deviceType,
|
|
175
|
+
service,
|
|
176
|
+
propertySchema,
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
return properties === undefined ||
|
|
180
|
+
(!required && Object.keys(properties).length === 0)
|
|
181
|
+
? []
|
|
182
|
+
: [{service, properties}];
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
if (
|
|
186
|
+
(required && matches.length !== 1) ||
|
|
187
|
+
(!required &&
|
|
188
|
+
!options.allowMultipleOptionalServices &&
|
|
189
|
+
matches.length > 1)
|
|
190
|
+
) {
|
|
191
|
+
return undefined;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
for (const match of matches) {
|
|
195
|
+
if (serviceIidSet.has(match.service.iid)) {
|
|
196
|
+
return undefined;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
serviceIidSet.add(match.service.iid);
|
|
200
|
+
resources.push(match);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return resources;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export function isValidMiotSpecValueList(
|
|
208
|
+
valueList: MiotSpecValueList | undefined,
|
|
209
|
+
): valueList is MiotSpecValueList {
|
|
210
|
+
if (valueList === undefined || valueList.length === 0) {
|
|
211
|
+
return false;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return isValidUniqueNumberSet(valueList.map(entry => entry.value));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export function isValidMiotSpecValueRange(
|
|
218
|
+
valueRange: MiotSpecValueRange | undefined,
|
|
219
|
+
format?: string,
|
|
220
|
+
): valueRange is MiotSpecValueRange {
|
|
221
|
+
if (valueRange === undefined) {
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const [minimum, maximum, step] = valueRange;
|
|
226
|
+
|
|
227
|
+
if (
|
|
228
|
+
!Number.isFinite(minimum) ||
|
|
229
|
+
!Number.isFinite(maximum) ||
|
|
230
|
+
!Number.isFinite(step) ||
|
|
231
|
+
minimum >= maximum ||
|
|
232
|
+
step <= 0
|
|
233
|
+
) {
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const stepCount = (maximum - minimum) / step;
|
|
238
|
+
|
|
239
|
+
if (!isApproximatelyInteger(stepCount)) {
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const formatRange =
|
|
244
|
+
format === undefined ? undefined : MIOT_INTEGER_FORMAT_RANGES[format];
|
|
245
|
+
|
|
246
|
+
if (formatRange === undefined) {
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const [formatMinimum, formatMaximum] = formatRange;
|
|
251
|
+
|
|
252
|
+
return (
|
|
253
|
+
Number.isInteger(minimum) &&
|
|
254
|
+
Number.isInteger(maximum) &&
|
|
255
|
+
Number.isInteger(step) &&
|
|
256
|
+
minimum >= formatMinimum &&
|
|
257
|
+
maximum <= formatMaximum
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function resolveServiceProperties(
|
|
262
|
+
deviceType: string,
|
|
263
|
+
service: MiotSpecService,
|
|
264
|
+
schema: Readonly<Record<string, MiotPropertyMapping>>,
|
|
265
|
+
): Readonly<Record<string, MiotResolvedSpecProperty>> | undefined {
|
|
266
|
+
const properties: Record<string, MiotResolvedSpecProperty> = {};
|
|
267
|
+
const usedPropertyIids = new Set<number>();
|
|
268
|
+
const optionalCandidates: Array<
|
|
269
|
+
readonly [name: string, property: MiotResolvedSpecProperty]
|
|
270
|
+
> = [];
|
|
271
|
+
const optionalPropertyUseCount = new Map<number, number>();
|
|
272
|
+
|
|
273
|
+
for (const [propertyType, configuredMapping] of Object.entries(schema)) {
|
|
274
|
+
const mapping = getPropertyMapping(configuredMapping);
|
|
275
|
+
const enumMapping =
|
|
276
|
+
mapping.enum === undefined
|
|
277
|
+
? undefined
|
|
278
|
+
: selectMiotUrnPatternValue(deviceType, mapping.enum);
|
|
279
|
+
const candidates = findPropertyCandidates(
|
|
280
|
+
service,
|
|
281
|
+
propertyType,
|
|
282
|
+
enumMapping,
|
|
283
|
+
mapping.enum !== undefined,
|
|
284
|
+
);
|
|
285
|
+
const [property] = candidates;
|
|
286
|
+
|
|
287
|
+
if (!mapping.optional) {
|
|
288
|
+
if (
|
|
289
|
+
candidates.length !== 1 ||
|
|
290
|
+
property === undefined ||
|
|
291
|
+
usedPropertyIids.has(property.iid)
|
|
292
|
+
) {
|
|
293
|
+
return undefined;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
properties[mapping.name] = resolveProperty(property, enumMapping);
|
|
297
|
+
usedPropertyIids.add(property.iid);
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (
|
|
302
|
+
candidates.length !== 1 ||
|
|
303
|
+
property === undefined ||
|
|
304
|
+
usedPropertyIids.has(property.iid)
|
|
305
|
+
) {
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
optionalCandidates.push([
|
|
310
|
+
mapping.name,
|
|
311
|
+
resolveProperty(property, enumMapping),
|
|
312
|
+
]);
|
|
313
|
+
optionalPropertyUseCount.set(
|
|
314
|
+
property.iid,
|
|
315
|
+
(optionalPropertyUseCount.get(property.iid) ?? 0) + 1,
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
for (const [name, property] of optionalCandidates) {
|
|
320
|
+
if (
|
|
321
|
+
optionalPropertyUseCount.get(property.iid) === 1 &&
|
|
322
|
+
!usedPropertyIids.has(property.iid)
|
|
323
|
+
) {
|
|
324
|
+
properties[name] = property;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return properties;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function findPropertyCandidates(
|
|
332
|
+
service: MiotSpecService,
|
|
333
|
+
propertyType: string,
|
|
334
|
+
enumMapping: MiotEnumValueMapping | undefined,
|
|
335
|
+
requiresEnumMapping: boolean,
|
|
336
|
+
): readonly MiotSpecProperty[] {
|
|
337
|
+
return (service.properties ?? []).filter(property => {
|
|
338
|
+
if (!matchesMiotUrnPattern(property.type, propertyType)) {
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// Vendor instances can declare different access modes for the same base
|
|
343
|
+
// property type.
|
|
344
|
+
// State-backed aliases must remain observable between commands so that
|
|
345
|
+
// acknowledged effects can be reconciled with manual device changes.
|
|
346
|
+
if (
|
|
347
|
+
!property.access.includes('read') ||
|
|
348
|
+
!property.access.includes('notify')
|
|
349
|
+
) {
|
|
350
|
+
return false;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (enumMapping === undefined) {
|
|
354
|
+
return !requiresEnumMapping;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const valueList = property['value-list'];
|
|
358
|
+
|
|
359
|
+
return (
|
|
360
|
+
isValidMiotSpecValueList(valueList) &&
|
|
361
|
+
Object.values(enumMapping).some(value =>
|
|
362
|
+
valueList.some(entry => entry.value === value),
|
|
363
|
+
)
|
|
364
|
+
);
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function resolveProperty(
|
|
369
|
+
property: MiotSpecProperty,
|
|
370
|
+
enumMapping: MiotEnumValueMapping | undefined,
|
|
371
|
+
): MiotResolvedSpecProperty {
|
|
372
|
+
return enumMapping === undefined
|
|
373
|
+
? property
|
|
374
|
+
: Object.assign({}, property, {enum: enumMapping});
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function getPropertyMapping(mapping: MiotPropertyMapping): {
|
|
378
|
+
readonly name: string;
|
|
379
|
+
readonly enum?: MiotEnumMapping;
|
|
380
|
+
readonly optional: boolean;
|
|
381
|
+
} {
|
|
382
|
+
return typeof mapping === 'string'
|
|
383
|
+
? {name: mapping, optional: false}
|
|
384
|
+
: {
|
|
385
|
+
name: mapping.name,
|
|
386
|
+
enum: mapping.enum,
|
|
387
|
+
optional: mapping.optional === true,
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function isValidEnumMapping(mapping: MiotEnumMapping): boolean {
|
|
392
|
+
const entries = Object.entries(mapping);
|
|
393
|
+
|
|
394
|
+
return (
|
|
395
|
+
entries.length > 0 &&
|
|
396
|
+
entries.every(
|
|
397
|
+
([pattern, valueMapping]) =>
|
|
398
|
+
pattern.length > 0 && isValidEnumValueMapping(valueMapping),
|
|
399
|
+
)
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function isValidEnumValueMapping(mapping: MiotEnumValueMapping): boolean {
|
|
404
|
+
const entries = Object.entries(mapping);
|
|
405
|
+
|
|
406
|
+
return (
|
|
407
|
+
entries.length > 0 &&
|
|
408
|
+
entries.every(([key, value]) => key.length > 0 && Number.isFinite(value)) &&
|
|
409
|
+
new Set(entries.map(([, value]) => value)).size === entries.length
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/** Selects a matching pattern that is nested within every other match. */
|
|
414
|
+
export function selectMiotUrnPatternValue<T>(
|
|
415
|
+
urn: string,
|
|
416
|
+
values: Readonly<Record<MiotUrnPattern, T>>,
|
|
417
|
+
): T | undefined {
|
|
418
|
+
const matches = Object.entries(values).filter(([pattern]) =>
|
|
419
|
+
matchesMiotUrnPattern(urn, pattern),
|
|
420
|
+
);
|
|
421
|
+
const nestedMatches = matches.filter(([pattern]) =>
|
|
422
|
+
matches.every(
|
|
423
|
+
([otherPattern]) =>
|
|
424
|
+
pattern === otherPattern ||
|
|
425
|
+
matchesMiotUrnPattern(pattern, otherPattern),
|
|
426
|
+
),
|
|
427
|
+
);
|
|
428
|
+
|
|
429
|
+
return nestedMatches.length === 1 ? nestedMatches[0]?.[1] : undefined;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/** Matches a pattern against the beginning of a complete MIoT URN. */
|
|
433
|
+
export function matchesMiotUrnPattern(
|
|
434
|
+
urn: string,
|
|
435
|
+
pattern: MiotUrnPattern,
|
|
436
|
+
): boolean {
|
|
437
|
+
const source = pattern
|
|
438
|
+
.split('*')
|
|
439
|
+
.map(part => part.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&'))
|
|
440
|
+
.join('[^:]+');
|
|
441
|
+
|
|
442
|
+
return new RegExp(`^${source}(?=:|$)`).test(urn);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
function isValidUniqueNumberSet(values: readonly number[]): boolean {
|
|
446
|
+
return (
|
|
447
|
+
values.every(value => Number.isFinite(value)) &&
|
|
448
|
+
new Set(values).size === values.length
|
|
449
|
+
);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
function isApproximatelyInteger(value: number): boolean {
|
|
453
|
+
if (!Number.isFinite(value)) {
|
|
454
|
+
return false;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
const nearestInteger = Math.round(value);
|
|
458
|
+
const tolerance = Number.EPSILON * Math.max(1, Math.abs(value)) * 8;
|
|
459
|
+
|
|
460
|
+
return Math.abs(value - nearestInteger) <= tolerance;
|
|
461
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export abstract class MiotRequest {
|
|
2
|
+
declare private readonly requestBrand: void;
|
|
3
|
+
|
|
4
|
+
toLogString(): string {
|
|
5
|
+
return this.constructor.name;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export class MiotSetPropertyRequest extends MiotRequest {
|
|
10
|
+
constructor(
|
|
11
|
+
readonly property: MiotProperty,
|
|
12
|
+
readonly value: unknown,
|
|
13
|
+
) {
|
|
14
|
+
super();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
override toLogString(): string {
|
|
18
|
+
const {did, siid, piid} = this.property;
|
|
19
|
+
|
|
20
|
+
return `miot set did=${did} siid=${siid} piid=${piid}`;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type MiotExecutionRequest = MiotSetPropertyRequest;
|
|
25
|
+
|
|
26
|
+
export type MiotProperty = {
|
|
27
|
+
readonly did: string;
|
|
28
|
+
readonly siid: number;
|
|
29
|
+
readonly piid: number;
|
|
30
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Xiaomi's official HA client treats both codes as successful execution results.
|
|
2
|
+
export function isSuccessfulMiotExecutionResult(
|
|
3
|
+
result: MiotExecutionResult,
|
|
4
|
+
): boolean {
|
|
5
|
+
return result.code === 0 || result.code === 1;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type MiotExecutionResult = {
|
|
9
|
+
readonly code: number;
|
|
10
|
+
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import * as x from 'x-value';
|
|
2
|
+
|
|
3
|
+
const MIOT_SPEC_INSTANCE_URL = 'https://miot-spec.org/miot-spec-v2/instance';
|
|
4
|
+
const MIOT_SPEC_REQUEST_TIMEOUT = 30_000;
|
|
5
|
+
|
|
6
|
+
export class MiotSpecClient {
|
|
7
|
+
private readonly instancePromiseMap = new Map<
|
|
8
|
+
string,
|
|
9
|
+
Promise<MiotSpecInstance>
|
|
10
|
+
>();
|
|
11
|
+
|
|
12
|
+
getInstance(urn: string): Promise<MiotSpecInstance> {
|
|
13
|
+
const cachedInstancePromise = this.instancePromiseMap.get(urn);
|
|
14
|
+
|
|
15
|
+
if (cachedInstancePromise !== undefined) {
|
|
16
|
+
return cachedInstancePromise;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const instancePromise = withRequestTimeout(this.requestInstance(urn));
|
|
20
|
+
|
|
21
|
+
this.instancePromiseMap.set(urn, instancePromise);
|
|
22
|
+
void instancePromise.catch(() => {
|
|
23
|
+
if (this.instancePromiseMap.get(urn) === instancePromise) {
|
|
24
|
+
this.instancePromiseMap.delete(urn);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return instancePromise;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private async requestInstance(urn: string): Promise<MiotSpecInstance> {
|
|
32
|
+
const url = new URL(MIOT_SPEC_INSTANCE_URL);
|
|
33
|
+
url.searchParams.set('type', urn);
|
|
34
|
+
const response = await fetch(url);
|
|
35
|
+
|
|
36
|
+
if (!response.ok) {
|
|
37
|
+
throw new Error(`Failed to get MIoT spec instance: ${response.status}.`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return MiotSpecInstance.satisfies(await response.json());
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const MiotSpecValueRange = x.tuple([x.number, x.number, x.number]);
|
|
45
|
+
|
|
46
|
+
export type MiotSpecValueRange = x.TypeOf<typeof MiotSpecValueRange>;
|
|
47
|
+
|
|
48
|
+
export const MiotSpecValueListEntry = x.object({
|
|
49
|
+
value: x.number,
|
|
50
|
+
description: x.string,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
export type MiotSpecValueListEntry = x.TypeOf<typeof MiotSpecValueListEntry>;
|
|
54
|
+
|
|
55
|
+
export const MiotSpecValueList = x.array(MiotSpecValueListEntry);
|
|
56
|
+
|
|
57
|
+
export type MiotSpecValueList = x.TypeOf<typeof MiotSpecValueList>;
|
|
58
|
+
|
|
59
|
+
export const MiotSpecProperty = x.object({
|
|
60
|
+
iid: x.number,
|
|
61
|
+
type: x.string,
|
|
62
|
+
description: x.string,
|
|
63
|
+
format: x.string,
|
|
64
|
+
access: x.array(x.string),
|
|
65
|
+
unit: x.string.optional(),
|
|
66
|
+
'value-range': MiotSpecValueRange.optional(),
|
|
67
|
+
'value-list': MiotSpecValueList.optional(),
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
export type MiotSpecProperty = x.TypeOf<typeof MiotSpecProperty>;
|
|
71
|
+
|
|
72
|
+
export const MiotSpecService = x.object({
|
|
73
|
+
iid: x.number,
|
|
74
|
+
type: x.string,
|
|
75
|
+
description: x.string,
|
|
76
|
+
properties: x.array(MiotSpecProperty).optional(),
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
export type MiotSpecService = x.TypeOf<typeof MiotSpecService>;
|
|
80
|
+
|
|
81
|
+
export const MiotSpecInstance = x.object({
|
|
82
|
+
type: x.string,
|
|
83
|
+
description: x.string,
|
|
84
|
+
services: x.array(MiotSpecService),
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
export type MiotSpecInstance = x.TypeOf<typeof MiotSpecInstance>;
|
|
88
|
+
|
|
89
|
+
function withRequestTimeout<T>(request: Promise<T>): Promise<T> {
|
|
90
|
+
let timeout: ReturnType<typeof setTimeout> | undefined;
|
|
91
|
+
const timeoutPromise = new Promise<never>((_resolve, reject) => {
|
|
92
|
+
timeout = setTimeout(() => {
|
|
93
|
+
reject(new Error('MIoT spec request timed out.'));
|
|
94
|
+
}, MIOT_SPEC_REQUEST_TIMEOUT);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// This bounds the cached request's wait; the underlying fetch keeps running.
|
|
98
|
+
return Promise.race([request, timeoutPromise]).finally(() => {
|
|
99
|
+
if (timeout !== undefined) {
|
|
100
|
+
clearTimeout(timeout);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|