@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.
- package/LICENSE +19 -0
- package/README.md +26 -0
- package/bld/library/backend/client.d.ts +73 -0
- package/bld/library/backend/client.js +490 -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 +34 -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 +69 -0
- package/bld/library/device.js +266 -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 +9 -0
- package/bld/library/devices/index.js +10 -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/pet-feeder.d.ts +36 -0
- package/bld/library/devices/pet-feeder.js +116 -0
- package/bld/library/devices/pet-feeder.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/temperature-humidity-sensor.d.ts +14 -0
- package/bld/library/devices/temperature-humidity-sensor.js +56 -0
- package/bld/library/devices/temperature-humidity-sensor.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 +168 -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 +33 -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 +498 -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 +1074 -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 +84 -0
- package/bld/library/miot/matcher.js +326 -0
- package/bld/library/miot/matcher.js.map +1 -0
- package/bld/library/miot/request.d.ts +33 -0
- package/bld/library/miot/request.js +57 -0
- package/bld/library/miot/request.js.map +1 -0
- package/bld/library/miot/result.d.ts +8 -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 +96 -0
- package/bld/library/miot/spec.js +86 -0
- package/bld/library/miot/spec.js.map +1 -0
- package/bld/library/provider.d.ts +78 -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 +306 -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 +737 -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 +46 -0
- package/src/library/command.ts +7 -0
- package/src/library/configuration.ts +480 -0
- package/src/library/device.ts +601 -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 +9 -0
- package/src/library/devices/light.ts +119 -0
- package/src/library/devices/pet-feeder.ts +129 -0
- package/src/library/devices/placeholder.ts +3 -0
- package/src/library/devices/temperature-humidity-sensor.ts +49 -0
- package/src/library/devices/value-range.ts +14 -0
- package/src/library/endpoint-connection.ts +841 -0
- package/src/library/index.ts +71 -0
- package/src/library/local/certificate.ts +401 -0
- package/src/library/local/controller.ts +693 -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 +1555 -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 +683 -0
- package/src/library/miot/request.ts +70 -0
- package/src/library/miot/result.ts +14 -0
- package/src/library/miot/spec.ts +114 -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 +692 -0
- package/src/library/terminal/index.ts +2 -0
- package/src/library/terminal/provider-details.tsx +871 -0
|
@@ -0,0 +1,601 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Device,
|
|
3
|
+
type DeviceConstructor,
|
|
4
|
+
type Endpoint,
|
|
5
|
+
type EndpointConnection,
|
|
6
|
+
type EndpointConnectionBinding,
|
|
7
|
+
type EndpointReference,
|
|
8
|
+
createEndpointConnectionBinding,
|
|
9
|
+
} from '@homelib/core';
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
type MiotEndpointConnection,
|
|
13
|
+
MiotEndpointConnectionMetadata,
|
|
14
|
+
type MiotEndpointConnectionResolvedMetadata,
|
|
15
|
+
type MiotEndpointConnectionResolvedResource,
|
|
16
|
+
type MiotEndpointConnectionResource,
|
|
17
|
+
type MiotEndpointConnectionTransports,
|
|
18
|
+
createMiotEndpointConnectionResolvedMetadata,
|
|
19
|
+
} from './endpoint-connection.js';
|
|
20
|
+
import {
|
|
21
|
+
type MiotActionSchema,
|
|
22
|
+
type MiotPropertySchema,
|
|
23
|
+
type MiotSpecAction,
|
|
24
|
+
type MiotSpecMatchContext,
|
|
25
|
+
type MiotSpecProperty,
|
|
26
|
+
type MiotSpecService,
|
|
27
|
+
assertMiotActionSchema,
|
|
28
|
+
assertMiotPropertySchema,
|
|
29
|
+
matchesMiotActionSchema,
|
|
30
|
+
resolveMiotPropertySchema,
|
|
31
|
+
} from './miot/index.js';
|
|
32
|
+
import type {MiotProvider} from './provider.js';
|
|
33
|
+
|
|
34
|
+
type ErasedMiotEndpoint = EndpointReference & {
|
|
35
|
+
enqueueCommand(command: never): EndpointReference;
|
|
36
|
+
bindConnection(connection: EndpointConnection<never> | undefined): void;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type MiotEndpointConstructor<
|
|
40
|
+
TEndpoint extends ErasedMiotEndpoint = ErasedMiotEndpoint,
|
|
41
|
+
> = abstract new (name?: string) => TEndpoint;
|
|
42
|
+
|
|
43
|
+
type MiotEndpointCommand<TEndpoint extends ErasedMiotEndpoint> = Parameters<
|
|
44
|
+
TEndpoint['enqueueCommand']
|
|
45
|
+
>[0];
|
|
46
|
+
|
|
47
|
+
type MiotEndpointAcceptedConnection<TEndpoint extends ErasedMiotEndpoint> =
|
|
48
|
+
Exclude<Parameters<TEndpoint['bindConnection']>[0], undefined>;
|
|
49
|
+
|
|
50
|
+
export type MiotEndpointConnectionConstructor<
|
|
51
|
+
TEndpoint extends ErasedMiotEndpoint = ErasedMiotEndpoint,
|
|
52
|
+
> = {
|
|
53
|
+
readonly Endpoint: MiotEndpointConstructor<TEndpoint>;
|
|
54
|
+
readonly actions?: MiotActionSchema;
|
|
55
|
+
readonly properties: MiotPropertySchema;
|
|
56
|
+
new (
|
|
57
|
+
provider: MiotProvider,
|
|
58
|
+
metadata: MiotEndpointConnectionResolvedMetadata,
|
|
59
|
+
transports: MiotEndpointConnectionTransports,
|
|
60
|
+
): MiotEndpointConnection<MiotEndpointCommand<TEndpoint>> &
|
|
61
|
+
MiotEndpointAcceptedConnection<TEndpoint>;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
type ValidMiotEndpointConnectionConstructor<
|
|
65
|
+
TConnection extends MiotEndpointConnectionConstructor,
|
|
66
|
+
> =
|
|
67
|
+
TConnection extends MiotEndpointConnectionConstructor<
|
|
68
|
+
InstanceType<TConnection['Endpoint']>
|
|
69
|
+
>
|
|
70
|
+
? TConnection
|
|
71
|
+
: never;
|
|
72
|
+
|
|
73
|
+
type ValidMiotEndpointConnectionConstructors<
|
|
74
|
+
TConnections extends readonly MiotEndpointConnectionConstructor[],
|
|
75
|
+
> = {
|
|
76
|
+
readonly [
|
|
77
|
+
TIndex in keyof TConnections
|
|
78
|
+
]: ValidMiotEndpointConnectionConstructor<TConnections[TIndex]>;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
type MiotEndpointConnectionConstructorTuple = readonly [
|
|
82
|
+
MiotEndpointConnectionConstructor,
|
|
83
|
+
...MiotEndpointConnectionConstructor[],
|
|
84
|
+
];
|
|
85
|
+
|
|
86
|
+
export type MiotLogicalDeviceEndpoint = {
|
|
87
|
+
readonly endpoint: EndpointReference;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export type MiotLogicalDevice<
|
|
91
|
+
TEndpoint extends MiotLogicalDeviceEndpoint = MiotLogicalDeviceEndpoint,
|
|
92
|
+
> = {
|
|
93
|
+
readonly deviceConstructors: readonly DeviceConstructor<Device>[];
|
|
94
|
+
readonly endpoints: readonly TEndpoint[];
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export type MiotDeviceEndpointMatch<
|
|
98
|
+
TEndpoint extends MiotLogicalDeviceEndpoint = MiotLogicalDeviceEndpoint,
|
|
99
|
+
> = {
|
|
100
|
+
readonly endpoint: TEndpoint;
|
|
101
|
+
readonly Connection: MiotEndpointConnectionConstructor;
|
|
102
|
+
readonly resources: readonly [
|
|
103
|
+
MiotEndpointConnectionResolvedResource,
|
|
104
|
+
...MiotEndpointConnectionResolvedResource[],
|
|
105
|
+
];
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export type MiotDeviceMatch<
|
|
109
|
+
TEndpoint extends MiotLogicalDeviceEndpoint = MiotLogicalDeviceEndpoint,
|
|
110
|
+
> = {
|
|
111
|
+
readonly Device: DeviceConstructor<Device>;
|
|
112
|
+
readonly endpoints: readonly MiotDeviceEndpointMatch<TEndpoint>[];
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export type MiotPhysicalDevice = {
|
|
116
|
+
readonly did: string;
|
|
117
|
+
readonly model?: string;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export type MiotDeviceEndpointConnectionBinding = {
|
|
121
|
+
readonly connection: MiotEndpointConnection<never>;
|
|
122
|
+
readonly binding: EndpointConnectionBinding;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
export class MiotDeviceRegistry {
|
|
126
|
+
private readonly deviceConnectionMap = new Map<
|
|
127
|
+
DeviceConstructor<Device>,
|
|
128
|
+
readonly MiotEndpointConnectionConstructor[]
|
|
129
|
+
>();
|
|
130
|
+
|
|
131
|
+
register<const TConnections extends MiotEndpointConnectionConstructorTuple>(
|
|
132
|
+
DeviceConstructor: DeviceConstructor<Device>,
|
|
133
|
+
...Connections: TConnections &
|
|
134
|
+
ValidMiotEndpointConnectionConstructors<TConnections>
|
|
135
|
+
): void {
|
|
136
|
+
if (this.deviceConnectionMap.has(DeviceConstructor)) {
|
|
137
|
+
throw new TypeError('Duplicate MIoT device registration.');
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (Connections.length === 0) {
|
|
141
|
+
throw new TypeError(
|
|
142
|
+
'A MIoT device requires at least one endpoint connection.',
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const connectionSet = new Set<MiotEndpointConnectionConstructor>();
|
|
147
|
+
const endpointSet = new Set<MiotEndpointConstructor>();
|
|
148
|
+
|
|
149
|
+
for (const Connection of Connections) {
|
|
150
|
+
if (connectionSet.has(Connection)) {
|
|
151
|
+
throw new TypeError('Duplicate MIoT endpoint connection registration.');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
assertMiotPropertySchema(Connection.properties);
|
|
155
|
+
|
|
156
|
+
if (Connection.actions !== undefined) {
|
|
157
|
+
assertMiotActionSchema(Connection.actions);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (endpointSet.has(Connection.Endpoint)) {
|
|
161
|
+
throw new TypeError(
|
|
162
|
+
'A MIoT device endpoint is handled by multiple connections.',
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
connectionSet.add(Connection);
|
|
167
|
+
endpointSet.add(Connection.Endpoint);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const registeredConnections = [...Connections];
|
|
171
|
+
|
|
172
|
+
this.deviceConnectionMap.set(DeviceConstructor, registeredConnections);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
getEndpointConnection(
|
|
176
|
+
deviceConstructors: readonly DeviceConstructor<Device>[],
|
|
177
|
+
endpoint: EndpointReference,
|
|
178
|
+
): MiotEndpointConnectionConstructor | undefined {
|
|
179
|
+
const registeredDevice = this.getRegisteredDevice(deviceConstructors);
|
|
180
|
+
|
|
181
|
+
if (registeredDevice === undefined) {
|
|
182
|
+
return undefined;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return registeredDevice.Connections.find(
|
|
186
|
+
Connection => Connection.Endpoint === endpoint.constructor,
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
match<TEndpoint extends MiotLogicalDeviceEndpoint>(
|
|
191
|
+
device: MiotLogicalDevice<TEndpoint>,
|
|
192
|
+
spec: MiotSpecMatchContext,
|
|
193
|
+
): MiotDeviceMatch<TEndpoint> | undefined {
|
|
194
|
+
const registeredDevice = this.getRegisteredDevice(
|
|
195
|
+
device.deviceConstructors,
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
if (
|
|
199
|
+
registeredDevice === undefined ||
|
|
200
|
+
registeredDevice.Connections.length !== device.endpoints.length
|
|
201
|
+
) {
|
|
202
|
+
return undefined;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const endpointMap = new Map<Function, TEndpoint>();
|
|
206
|
+
|
|
207
|
+
for (const endpoint of device.endpoints) {
|
|
208
|
+
const Endpoint = endpoint.endpoint.constructor;
|
|
209
|
+
|
|
210
|
+
if (endpointMap.has(Endpoint)) {
|
|
211
|
+
return undefined;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
endpointMap.set(Endpoint, endpoint);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const matchMap = new Map<Function, MiotDeviceEndpointMatch<TEndpoint>>();
|
|
218
|
+
const serviceIidSet = new Set<number>();
|
|
219
|
+
|
|
220
|
+
for (const Connection of registeredDevice.Connections) {
|
|
221
|
+
const endpoint = endpointMap.get(Connection.Endpoint);
|
|
222
|
+
|
|
223
|
+
if (endpoint === undefined) {
|
|
224
|
+
return undefined;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const resolvedResources = resolveMiotEndpointConnectionResources(
|
|
228
|
+
Connection,
|
|
229
|
+
spec,
|
|
230
|
+
);
|
|
231
|
+
const [firstResource, ...remainingResources] = resolvedResources ?? [];
|
|
232
|
+
|
|
233
|
+
if (firstResource === undefined) {
|
|
234
|
+
return undefined;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const resources: [
|
|
238
|
+
MiotEndpointConnectionResolvedResource,
|
|
239
|
+
...MiotEndpointConnectionResolvedResource[],
|
|
240
|
+
] = [firstResource, ...remainingResources];
|
|
241
|
+
|
|
242
|
+
for (const {service} of resources) {
|
|
243
|
+
if (serviceIidSet.has(service.iid)) {
|
|
244
|
+
return undefined;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
serviceIidSet.add(service.iid);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
matchMap.set(Connection.Endpoint, {
|
|
251
|
+
endpoint,
|
|
252
|
+
Connection,
|
|
253
|
+
resources,
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const endpointMatches: MiotDeviceEndpointMatch<TEndpoint>[] = [];
|
|
258
|
+
|
|
259
|
+
for (const endpoint of device.endpoints) {
|
|
260
|
+
const match = matchMap.get(endpoint.endpoint.constructor);
|
|
261
|
+
|
|
262
|
+
if (match === undefined) {
|
|
263
|
+
return undefined;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
endpointMatches.push(match);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return {
|
|
270
|
+
Device: registeredDevice.Device,
|
|
271
|
+
endpoints: endpointMatches,
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
private getRegisteredDevice(
|
|
276
|
+
deviceConstructors: readonly DeviceConstructor<Device>[],
|
|
277
|
+
):
|
|
278
|
+
| {
|
|
279
|
+
readonly Device: DeviceConstructor<Device>;
|
|
280
|
+
readonly Connections: readonly MiotEndpointConnectionConstructor[];
|
|
281
|
+
}
|
|
282
|
+
| undefined {
|
|
283
|
+
const registeredDevices = deviceConstructors.flatMap(DeviceConstructor => {
|
|
284
|
+
const Connections = this.deviceConnectionMap.get(DeviceConstructor);
|
|
285
|
+
|
|
286
|
+
return Connections === undefined
|
|
287
|
+
? []
|
|
288
|
+
: [{Device: DeviceConstructor, Connections}];
|
|
289
|
+
});
|
|
290
|
+
const [registeredDevice] = registeredDevices;
|
|
291
|
+
|
|
292
|
+
return registeredDevices.length === 1 ? registeredDevice : undefined;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const MIOT_DEVICE_REGISTRY = new MiotDeviceRegistry();
|
|
297
|
+
|
|
298
|
+
export function registerMiotDevice<
|
|
299
|
+
const TConnections extends MiotEndpointConnectionConstructorTuple,
|
|
300
|
+
>(
|
|
301
|
+
DeviceConstructor: DeviceConstructor<Device>,
|
|
302
|
+
...Connections: TConnections &
|
|
303
|
+
ValidMiotEndpointConnectionConstructors<TConnections>
|
|
304
|
+
): void;
|
|
305
|
+
export function registerMiotDevice(
|
|
306
|
+
DeviceConstructor: DeviceConstructor<Device>,
|
|
307
|
+
...Connections: MiotEndpointConnectionConstructorTuple
|
|
308
|
+
): void {
|
|
309
|
+
MIOT_DEVICE_REGISTRY.register(DeviceConstructor, ...Connections);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export function matchMiotDevice<TEndpoint extends MiotLogicalDeviceEndpoint>(
|
|
313
|
+
device: MiotLogicalDevice<TEndpoint>,
|
|
314
|
+
spec: MiotSpecMatchContext,
|
|
315
|
+
): MiotDeviceMatch<TEndpoint> | undefined {
|
|
316
|
+
return MIOT_DEVICE_REGISTRY.match(device, spec);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export function getMiotEndpointConnectionConstructor(
|
|
320
|
+
deviceConstructors: readonly DeviceConstructor<Device>[],
|
|
321
|
+
endpoint: EndpointReference,
|
|
322
|
+
): MiotEndpointConnectionConstructor | undefined {
|
|
323
|
+
return MIOT_DEVICE_REGISTRY.getEndpointConnection(
|
|
324
|
+
deviceConstructors,
|
|
325
|
+
endpoint,
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
export function createMiotDeviceEndpointConnectionBinding(
|
|
330
|
+
Connection: MiotEndpointConnectionConstructor,
|
|
331
|
+
provider: MiotProvider,
|
|
332
|
+
endpoint: EndpointReference,
|
|
333
|
+
metadata: MiotEndpointConnectionResolvedMetadata,
|
|
334
|
+
transports: MiotEndpointConnectionTransports,
|
|
335
|
+
disposeConnection?: (
|
|
336
|
+
connection: MiotEndpointConnection<never>,
|
|
337
|
+
) => void | PromiseLike<void>,
|
|
338
|
+
): MiotDeviceEndpointConnectionBinding {
|
|
339
|
+
if (endpoint.constructor !== Connection.Endpoint) {
|
|
340
|
+
throw new TypeError('Endpoint does not match its MIoT connection.');
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const connection = new Connection(provider, metadata, transports);
|
|
344
|
+
|
|
345
|
+
return {
|
|
346
|
+
connection,
|
|
347
|
+
binding: createEndpointConnectionBinding(
|
|
348
|
+
endpoint as Endpoint<never, MiotEndpointConnection<never>>,
|
|
349
|
+
connection,
|
|
350
|
+
() => disposeConnection?.(connection),
|
|
351
|
+
),
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
export function resolveMiotEndpointConnectionResources(
|
|
356
|
+
Connection: MiotEndpointConnectionConstructor,
|
|
357
|
+
spec: MiotSpecMatchContext,
|
|
358
|
+
): readonly MiotEndpointConnectionResolvedResource[] | undefined {
|
|
359
|
+
const resources = resolveMiotPropertySchema(spec, Connection.properties);
|
|
360
|
+
|
|
361
|
+
return resources !== undefined &&
|
|
362
|
+
(Connection.actions === undefined ||
|
|
363
|
+
matchesMiotActionSchema(resources, Connection.actions))
|
|
364
|
+
? resources
|
|
365
|
+
: undefined;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
export function resolvePersistedMiotEndpointConnectionResources(
|
|
369
|
+
Connection: MiotEndpointConnectionConstructor,
|
|
370
|
+
spec: MiotSpecMatchContext,
|
|
371
|
+
): readonly MiotEndpointConnectionResolvedResource[] | undefined {
|
|
372
|
+
const resources = resolveMiotPropertySchema(spec, Connection.properties, {
|
|
373
|
+
allowMultipleOptionalServices: true,
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
return resources !== undefined &&
|
|
377
|
+
(Connection.actions === undefined ||
|
|
378
|
+
matchesMiotActionSchema(resources, Connection.actions))
|
|
379
|
+
? resources
|
|
380
|
+
: undefined;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
export function resolveMiotEndpointConnectionMetadata(
|
|
384
|
+
Connection: MiotEndpointConnectionConstructor,
|
|
385
|
+
metadata: MiotEndpointConnectionMetadata,
|
|
386
|
+
): MiotEndpointConnectionResolvedMetadata {
|
|
387
|
+
const resources = resolvePersistedMiotEndpointConnectionResources(
|
|
388
|
+
Connection,
|
|
389
|
+
{
|
|
390
|
+
type: metadata.device.urn,
|
|
391
|
+
services: metadata.resources.map(resource => resource.service),
|
|
392
|
+
},
|
|
393
|
+
);
|
|
394
|
+
|
|
395
|
+
if (
|
|
396
|
+
resources === undefined ||
|
|
397
|
+
resources.length === 0 ||
|
|
398
|
+
!resolvedResourcesMatchMetadata(resources, metadata.resources)
|
|
399
|
+
) {
|
|
400
|
+
throw new TypeError('Invalid MIoT endpoint metadata.');
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
return createMiotEndpointConnectionResolvedMetadata(metadata, resources);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
export function createMiotEndpointConnectionMetadata(
|
|
407
|
+
device: MiotPhysicalDevice,
|
|
408
|
+
urn: string,
|
|
409
|
+
resources: readonly MiotEndpointConnectionResolvedResource[],
|
|
410
|
+
): MiotEndpointConnectionMetadata {
|
|
411
|
+
return MiotEndpointConnectionMetadata.satisfies({
|
|
412
|
+
device: {did: device.did, model: device.model, urn},
|
|
413
|
+
resources: resources
|
|
414
|
+
.toSorted(compareResources)
|
|
415
|
+
.map(resource => ({service: resource.service})),
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
export function miotEndpointConnectionMetadataEqual(
|
|
420
|
+
left: MiotEndpointConnectionMetadata,
|
|
421
|
+
right: MiotEndpointConnectionMetadata,
|
|
422
|
+
): boolean {
|
|
423
|
+
return (
|
|
424
|
+
left.device.did === right.device.did &&
|
|
425
|
+
left.device.model === right.device.model &&
|
|
426
|
+
left.device.urn === right.device.urn &&
|
|
427
|
+
endpointResourcesEqual(left.resources, right.resources)
|
|
428
|
+
);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
function resolvedResourcesMatchMetadata(
|
|
432
|
+
expected: readonly MiotEndpointConnectionResolvedResource[],
|
|
433
|
+
actual: readonly MiotEndpointConnectionResource[],
|
|
434
|
+
): boolean {
|
|
435
|
+
const actualResourceMap = new Map(
|
|
436
|
+
actual.map(resource => [resource.service.iid, resource]),
|
|
437
|
+
);
|
|
438
|
+
|
|
439
|
+
return (
|
|
440
|
+
expected.length === actual.length &&
|
|
441
|
+
expected.every(expectedResource => {
|
|
442
|
+
const actualResource = actualResourceMap.get(
|
|
443
|
+
expectedResource.service.iid,
|
|
444
|
+
);
|
|
445
|
+
|
|
446
|
+
return (
|
|
447
|
+
actualResource !== undefined &&
|
|
448
|
+
serviceEqual(expectedResource.service, actualResource.service)
|
|
449
|
+
);
|
|
450
|
+
})
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
function endpointResourcesEqual(
|
|
455
|
+
left: readonly MiotEndpointConnectionResource[],
|
|
456
|
+
right: readonly MiotEndpointConnectionResource[],
|
|
457
|
+
): boolean {
|
|
458
|
+
return unorderedArraysEqual(left, right, (leftResource, rightResource) => {
|
|
459
|
+
return serviceEqual(leftResource.service, rightResource.service);
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
function serviceEqual(left: MiotSpecService, right: MiotSpecService): boolean {
|
|
464
|
+
return (
|
|
465
|
+
left.iid === right.iid &&
|
|
466
|
+
left.type === right.type &&
|
|
467
|
+
left.description === right.description &&
|
|
468
|
+
optionalPropertyArraysEqual(left.properties, right.properties) &&
|
|
469
|
+
optionalActionArraysEqual(left.actions, right.actions)
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
function actionEqual(left: MiotSpecAction, right: MiotSpecAction): boolean {
|
|
474
|
+
return (
|
|
475
|
+
left.iid === right.iid &&
|
|
476
|
+
left.type === right.type &&
|
|
477
|
+
left.description === right.description &&
|
|
478
|
+
numberArraysEqual(left.in, right.in) &&
|
|
479
|
+
numberArraysEqual(left.out, right.out)
|
|
480
|
+
);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
function propertyEqual(
|
|
484
|
+
left: MiotSpecProperty,
|
|
485
|
+
right: MiotSpecProperty,
|
|
486
|
+
): boolean {
|
|
487
|
+
return (
|
|
488
|
+
left.iid === right.iid &&
|
|
489
|
+
left.type === right.type &&
|
|
490
|
+
left.description === right.description &&
|
|
491
|
+
left.format === right.format &&
|
|
492
|
+
left.unit === right.unit &&
|
|
493
|
+
valueRangesEqual(left['value-range'], right['value-range']) &&
|
|
494
|
+
valueListsEqual(left['value-list'], right['value-list']) &&
|
|
495
|
+
stringSetsEqual(left.access, right.access)
|
|
496
|
+
);
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
function optionalPropertyArraysEqual(
|
|
500
|
+
left: readonly MiotSpecProperty[] | undefined,
|
|
501
|
+
right: readonly MiotSpecProperty[] | undefined,
|
|
502
|
+
): boolean {
|
|
503
|
+
if (left === undefined || right === undefined) {
|
|
504
|
+
return left === right;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
return unorderedArraysEqual(left, right, propertyEqual);
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
function optionalActionArraysEqual(
|
|
511
|
+
left: readonly MiotSpecAction[] | undefined,
|
|
512
|
+
right: readonly MiotSpecAction[] | undefined,
|
|
513
|
+
): boolean {
|
|
514
|
+
if (left === undefined || right === undefined) {
|
|
515
|
+
return left === right;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
return unorderedArraysEqual(left, right, actionEqual);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
function numberArraysEqual(
|
|
522
|
+
left: readonly number[],
|
|
523
|
+
right: readonly number[],
|
|
524
|
+
): boolean {
|
|
525
|
+
return (
|
|
526
|
+
left.length === right.length &&
|
|
527
|
+
left.every((value, index) => value === right[index])
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
function valueRangesEqual(
|
|
532
|
+
left: MiotSpecProperty['value-range'],
|
|
533
|
+
right: MiotSpecProperty['value-range'],
|
|
534
|
+
): boolean {
|
|
535
|
+
if (left === undefined || right === undefined) {
|
|
536
|
+
return left === right;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
return left.every((value, index) => value === right[index]);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
function valueListsEqual(
|
|
543
|
+
left: MiotSpecProperty['value-list'],
|
|
544
|
+
right: MiotSpecProperty['value-list'],
|
|
545
|
+
): boolean {
|
|
546
|
+
if (left === undefined || right === undefined) {
|
|
547
|
+
return left === right;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
return unorderedArraysEqual(left, right, (first, second) => {
|
|
551
|
+
return (
|
|
552
|
+
first.value === second.value && first.description === second.description
|
|
553
|
+
);
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
function stringSetsEqual(
|
|
558
|
+
left: readonly string[],
|
|
559
|
+
right: readonly string[],
|
|
560
|
+
): boolean {
|
|
561
|
+
const leftSet = new Set(left);
|
|
562
|
+
const rightSet = new Set(right);
|
|
563
|
+
|
|
564
|
+
return (
|
|
565
|
+
leftSet.size === rightSet.size &&
|
|
566
|
+
[...leftSet].every(value => rightSet.has(value))
|
|
567
|
+
);
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
function unorderedArraysEqual<T>(
|
|
571
|
+
left: readonly T[],
|
|
572
|
+
right: readonly T[],
|
|
573
|
+
valuesEqual: (left: T, right: T) => boolean,
|
|
574
|
+
): boolean {
|
|
575
|
+
if (left.length !== right.length) {
|
|
576
|
+
return false;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
const matchedIndexSet = new Set<number>();
|
|
580
|
+
|
|
581
|
+
for (const leftValue of left) {
|
|
582
|
+
const matchedIndex = right.findIndex((rightValue, index) => {
|
|
583
|
+
return !matchedIndexSet.has(index) && valuesEqual(leftValue, rightValue);
|
|
584
|
+
});
|
|
585
|
+
|
|
586
|
+
if (matchedIndex === -1) {
|
|
587
|
+
return false;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
matchedIndexSet.add(matchedIndex);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
return true;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
function compareResources(
|
|
597
|
+
left: MiotEndpointConnectionResolvedResource,
|
|
598
|
+
right: MiotEndpointConnectionResolvedResource,
|
|
599
|
+
): number {
|
|
600
|
+
return left.service.iid - right.service.iid;
|
|
601
|
+
}
|