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