@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,480 @@
|
|
|
1
|
+
import {readFile} from 'node:fs/promises';
|
|
2
|
+
import {join} from 'node:path';
|
|
3
|
+
|
|
4
|
+
import * as x from 'x-value';
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
type BackendDevice,
|
|
8
|
+
type BackendHome,
|
|
9
|
+
type BackendHomeSource,
|
|
10
|
+
CLOUD_SERVERS,
|
|
11
|
+
type CloudServer,
|
|
12
|
+
} from './backend/index.js';
|
|
13
|
+
import {writePrivateJsonFile} from './storage.js';
|
|
14
|
+
|
|
15
|
+
const HomeSourceValue = x.union([
|
|
16
|
+
x.literal('owned'),
|
|
17
|
+
x.literal('shared-home'),
|
|
18
|
+
x.literal('shared-device'),
|
|
19
|
+
]);
|
|
20
|
+
|
|
21
|
+
const CloudServerValue = x.string.refined<never, CloudServer>(value => {
|
|
22
|
+
if (!isCloudServer(value)) {
|
|
23
|
+
throw new TypeError(`Unknown Xiaomi cloud server: ${value}.`);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return value;
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const AccountValue = x
|
|
30
|
+
.object({
|
|
31
|
+
cloudServer: CloudServerValue,
|
|
32
|
+
userId: x.union([x.string, x.null]),
|
|
33
|
+
})
|
|
34
|
+
.exact();
|
|
35
|
+
|
|
36
|
+
const HomeReferenceValue = x
|
|
37
|
+
.object({
|
|
38
|
+
source: HomeSourceValue,
|
|
39
|
+
id: x.string,
|
|
40
|
+
})
|
|
41
|
+
.exact();
|
|
42
|
+
|
|
43
|
+
const ConfigurationFileValue = x
|
|
44
|
+
.object({
|
|
45
|
+
version: x.literal(0),
|
|
46
|
+
account: AccountValue,
|
|
47
|
+
includedHomes: x.array(HomeReferenceValue),
|
|
48
|
+
})
|
|
49
|
+
.exact();
|
|
50
|
+
|
|
51
|
+
const BackendRoomValue = x.object({
|
|
52
|
+
id: x.string,
|
|
53
|
+
name: x.string,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const BackendHomeValue = x.object({
|
|
57
|
+
id: x.string,
|
|
58
|
+
name: x.string,
|
|
59
|
+
source: HomeSourceValue,
|
|
60
|
+
userId: x.string.optional(),
|
|
61
|
+
rooms: x.array(BackendRoomValue),
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const BackendDeviceValue = x.object({
|
|
65
|
+
did: x.string,
|
|
66
|
+
name: x.string.optional(),
|
|
67
|
+
model: x.string.optional(),
|
|
68
|
+
specType: x.string.optional(),
|
|
69
|
+
online: x.boolean.optional(),
|
|
70
|
+
parentDid: x.string.optional(),
|
|
71
|
+
firmwareVersion: x.string.optional(),
|
|
72
|
+
source: HomeSourceValue.optional(),
|
|
73
|
+
homeId: x.string.optional(),
|
|
74
|
+
homeName: x.string.optional(),
|
|
75
|
+
roomId: x.string.optional(),
|
|
76
|
+
roomName: x.string.optional(),
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
export class MiotProviderConfiguration {
|
|
80
|
+
private readonly path: string;
|
|
81
|
+
|
|
82
|
+
private readonly dependencies: MiotProviderConfigurationDependencies;
|
|
83
|
+
|
|
84
|
+
private completeDiscoveryPromise:
|
|
85
|
+
Promise<MiotProviderConfigurationDiscovery | undefined> | undefined;
|
|
86
|
+
|
|
87
|
+
constructor(options: MiotProviderConfigurationOptions) {
|
|
88
|
+
const {providerName, environmentDirectory, dependencies} = options;
|
|
89
|
+
|
|
90
|
+
assertProviderName(providerName);
|
|
91
|
+
this.path = join(
|
|
92
|
+
environmentDirectory,
|
|
93
|
+
'providers',
|
|
94
|
+
'miot',
|
|
95
|
+
'config',
|
|
96
|
+
`${providerName}.json`,
|
|
97
|
+
);
|
|
98
|
+
this.dependencies = dependencies;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async load(): Promise<MiotProviderConfigurationSnapshot | undefined> {
|
|
102
|
+
const state = await this.loadState();
|
|
103
|
+
|
|
104
|
+
if (state === undefined) {
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const {account, homes} = state.discovery;
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
account,
|
|
112
|
+
selectionSource: state.selectionSource,
|
|
113
|
+
homes: homes.map(home => ({
|
|
114
|
+
source: home.source,
|
|
115
|
+
id: home.id,
|
|
116
|
+
name: home.name,
|
|
117
|
+
included:
|
|
118
|
+
state.includedHomeKeySet === undefined ||
|
|
119
|
+
state.includedHomeKeySet.has(getHomeKey(home)),
|
|
120
|
+
})),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async discoverDevices(): Promise<MiotProviderFilteredDiscovery | undefined> {
|
|
125
|
+
const state = await this.loadState();
|
|
126
|
+
|
|
127
|
+
if (state === undefined) {
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const {account, homes, devices} = state.discovery;
|
|
132
|
+
const {includedHomeKeySet} = state;
|
|
133
|
+
|
|
134
|
+
if (includedHomeKeySet === undefined) {
|
|
135
|
+
return {account, homes, devices};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
account,
|
|
140
|
+
homes: homes.filter(home => includedHomeKeySet.has(getHomeKey(home))),
|
|
141
|
+
devices: devices.filter(device => {
|
|
142
|
+
if (device.source === undefined || device.homeId === undefined) {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return includedHomeKeySet.has(
|
|
147
|
+
getHomeKey({source: device.source, id: device.homeId}),
|
|
148
|
+
);
|
|
149
|
+
}),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async beginAuthorization(
|
|
154
|
+
cloudServer: CloudServer,
|
|
155
|
+
): Promise<MiotProviderConfigurationAuthorization> {
|
|
156
|
+
const validatedCloudServer = CloudServerValue.satisfies(cloudServer);
|
|
157
|
+
const authorization =
|
|
158
|
+
await this.dependencies.beginAuthorization(validatedCloudServer);
|
|
159
|
+
const url = x.string.satisfies(authorization.url);
|
|
160
|
+
let completion: Promise<void> | undefined;
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
url,
|
|
164
|
+
wait: () => {
|
|
165
|
+
completion ??= authorization.wait().then(() => {
|
|
166
|
+
this.completeDiscoveryPromise = undefined;
|
|
167
|
+
});
|
|
168
|
+
return completion;
|
|
169
|
+
},
|
|
170
|
+
submitCallbackUrl: callbackUrl =>
|
|
171
|
+
authorization.submitCallbackUrl(x.string.satisfies(callbackUrl)),
|
|
172
|
+
cancel: () => authorization.cancel(),
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async forgetAuthorization(): Promise<void> {
|
|
177
|
+
this.completeDiscoveryPromise = undefined;
|
|
178
|
+
|
|
179
|
+
try {
|
|
180
|
+
await this.dependencies.forgetAuthorization();
|
|
181
|
+
} finally {
|
|
182
|
+
this.completeDiscoveryPromise = undefined;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async saveIncludedHomes(
|
|
187
|
+
account: MiotProviderAccount,
|
|
188
|
+
includedHomes: readonly MiotProviderHomeReference[],
|
|
189
|
+
): Promise<void> {
|
|
190
|
+
const validatedAccount = AccountValue.satisfies(account);
|
|
191
|
+
const validatedHomes = includedHomes.map(home =>
|
|
192
|
+
HomeReferenceValue.satisfies(home),
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
if (validatedAccount.userId === null) {
|
|
196
|
+
throw new Error(
|
|
197
|
+
'Cannot save included homes without a stable Xiaomi account identifier.',
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
assertUniqueHomeReferences(validatedHomes, 'included home');
|
|
202
|
+
validatedHomes.sort(compareHomeReferences);
|
|
203
|
+
|
|
204
|
+
await writePrivateJsonFile(this.path, {
|
|
205
|
+
version: 0,
|
|
206
|
+
account: validatedAccount,
|
|
207
|
+
includedHomes: validatedHomes,
|
|
208
|
+
} satisfies ConfigurationFile);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
private async loadState(): Promise<LoadedState | undefined> {
|
|
212
|
+
const configuration = await this.readConfiguration();
|
|
213
|
+
const discovery = await this.getCompleteDiscovery();
|
|
214
|
+
|
|
215
|
+
if (discovery === undefined) {
|
|
216
|
+
return undefined;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (configuration === undefined) {
|
|
220
|
+
return {
|
|
221
|
+
discovery,
|
|
222
|
+
selectionSource: 'default',
|
|
223
|
+
includedHomeKeySet: undefined,
|
|
224
|
+
};
|
|
225
|
+
} else if (!accountsEqual(configuration.account, discovery.account)) {
|
|
226
|
+
return {
|
|
227
|
+
discovery,
|
|
228
|
+
selectionSource: 'account-mismatch',
|
|
229
|
+
includedHomeKeySet: undefined,
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return {
|
|
234
|
+
discovery,
|
|
235
|
+
selectionSource: 'saved',
|
|
236
|
+
includedHomeKeySet: new Set(configuration.includedHomes.map(getHomeKey)),
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
private getCompleteDiscovery(): Promise<
|
|
241
|
+
MiotProviderConfigurationDiscovery | undefined
|
|
242
|
+
> {
|
|
243
|
+
let discoveryPromise = this.completeDiscoveryPromise;
|
|
244
|
+
|
|
245
|
+
if (discoveryPromise === undefined) {
|
|
246
|
+
discoveryPromise = this.fetchCompleteDiscovery();
|
|
247
|
+
this.completeDiscoveryPromise = discoveryPromise;
|
|
248
|
+
void discoveryPromise.then(
|
|
249
|
+
discovery => {
|
|
250
|
+
if (
|
|
251
|
+
discovery === undefined &&
|
|
252
|
+
this.completeDiscoveryPromise === discoveryPromise
|
|
253
|
+
) {
|
|
254
|
+
this.completeDiscoveryPromise = undefined;
|
|
255
|
+
}
|
|
256
|
+
},
|
|
257
|
+
() => {
|
|
258
|
+
if (this.completeDiscoveryPromise === discoveryPromise) {
|
|
259
|
+
this.completeDiscoveryPromise = undefined;
|
|
260
|
+
}
|
|
261
|
+
},
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return discoveryPromise;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
private async fetchCompleteDiscovery(): Promise<
|
|
269
|
+
MiotProviderConfigurationDiscovery | undefined
|
|
270
|
+
> {
|
|
271
|
+
const discovery = await this.dependencies.discoverDevices();
|
|
272
|
+
return discovery === undefined ? undefined : sanitizeDiscovery(discovery);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
private async readConfiguration(): Promise<ConfigurationFile | undefined> {
|
|
276
|
+
let source: string;
|
|
277
|
+
|
|
278
|
+
try {
|
|
279
|
+
source = await readFile(this.path, 'utf8');
|
|
280
|
+
} catch (error) {
|
|
281
|
+
if (isFileNotFoundError(error)) {
|
|
282
|
+
return undefined;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
throw error;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const configuration = ConfigurationFileValue.satisfies(
|
|
289
|
+
JSON.parse(source) as unknown,
|
|
290
|
+
);
|
|
291
|
+
|
|
292
|
+
assertUniqueHomeReferences(configuration.includedHomes, 'configured home');
|
|
293
|
+
return configuration;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
export type MiotProviderConfigurationOptions = {
|
|
298
|
+
readonly providerName: string;
|
|
299
|
+
readonly environmentDirectory: string;
|
|
300
|
+
readonly dependencies: MiotProviderConfigurationDependencies;
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
export type MiotProviderConfigurationDependencies = {
|
|
304
|
+
discoverDevices(): Promise<MiotProviderConfigurationDiscovery | undefined>;
|
|
305
|
+
beginAuthorization(
|
|
306
|
+
cloudServer: CloudServer,
|
|
307
|
+
): Promise<MiotProviderConfigurationAuthorizationDependency>;
|
|
308
|
+
forgetAuthorization(): Promise<void>;
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
export type MiotProviderConfigurationDiscovery = {
|
|
312
|
+
readonly account: MiotProviderAccount;
|
|
313
|
+
readonly homes: readonly BackendHome[];
|
|
314
|
+
readonly devices: readonly BackendDevice[];
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
export type MiotProviderFilteredDiscovery = MiotProviderConfigurationDiscovery;
|
|
318
|
+
|
|
319
|
+
export type MiotProviderConfigurationAuthorizationDependency = {
|
|
320
|
+
readonly url: string;
|
|
321
|
+
wait(): Promise<void>;
|
|
322
|
+
submitCallbackUrl(callbackUrl: string): Promise<void>;
|
|
323
|
+
cancel(): Promise<void>;
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
export type MiotProviderConfigurationAuthorization = {
|
|
327
|
+
readonly url: string;
|
|
328
|
+
wait(): Promise<void>;
|
|
329
|
+
submitCallbackUrl(callbackUrl: string): Promise<void>;
|
|
330
|
+
cancel(): Promise<void>;
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
export type MiotProviderConfigurationSnapshot = {
|
|
334
|
+
readonly account: MiotProviderAccount;
|
|
335
|
+
readonly selectionSource: MiotProviderHomeSelectionSource;
|
|
336
|
+
readonly homes: readonly MiotProviderHomeItem[];
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
export type MiotProviderHomeSelectionSource =
|
|
340
|
+
'default' | 'saved' | 'account-mismatch';
|
|
341
|
+
|
|
342
|
+
export type MiotProviderAccount = {
|
|
343
|
+
readonly cloudServer: CloudServer;
|
|
344
|
+
readonly userId: string | null;
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
export type MiotProviderHomeReference = {
|
|
348
|
+
readonly source: BackendHomeSource;
|
|
349
|
+
readonly id: string;
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
export type MiotProviderHomeItem = MiotProviderHomeReference & {
|
|
353
|
+
readonly name: string;
|
|
354
|
+
readonly included: boolean;
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
type ConfigurationFile = {
|
|
358
|
+
readonly version: 0;
|
|
359
|
+
readonly account: MiotProviderAccount;
|
|
360
|
+
readonly includedHomes: readonly MiotProviderHomeReference[];
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
type LoadedState = {
|
|
364
|
+
readonly discovery: MiotProviderConfigurationDiscovery;
|
|
365
|
+
readonly selectionSource: MiotProviderHomeSelectionSource;
|
|
366
|
+
readonly includedHomeKeySet: ReadonlySet<string> | undefined;
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
function sanitizeDiscovery(
|
|
370
|
+
discovery: MiotProviderConfigurationDiscovery,
|
|
371
|
+
): MiotProviderConfigurationDiscovery {
|
|
372
|
+
const account = AccountValue.sanitize(discovery.account);
|
|
373
|
+
const homes = discovery.homes.map(home => BackendHomeValue.sanitize(home));
|
|
374
|
+
const devices = discovery.devices.map(device =>
|
|
375
|
+
BackendDeviceValue.sanitize(device),
|
|
376
|
+
);
|
|
377
|
+
|
|
378
|
+
assertUniqueHomeReferences(homes, 'discovered home');
|
|
379
|
+
assertUniqueDevices(devices);
|
|
380
|
+
|
|
381
|
+
return {account, homes, devices};
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
function assertUniqueHomeReferences(
|
|
385
|
+
homes: readonly MiotProviderHomeReference[],
|
|
386
|
+
description: string,
|
|
387
|
+
): void {
|
|
388
|
+
const keySet = new Set<string>();
|
|
389
|
+
|
|
390
|
+
for (const home of homes) {
|
|
391
|
+
const key = getHomeKey(home);
|
|
392
|
+
|
|
393
|
+
if (keySet.has(key)) {
|
|
394
|
+
throw new TypeError(
|
|
395
|
+
`Duplicate ${description}: ${home.source}/${home.id}.`,
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
keySet.add(key);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function assertUniqueDevices(devices: readonly BackendDevice[]): void {
|
|
404
|
+
const didSet = new Set<string>();
|
|
405
|
+
|
|
406
|
+
for (const device of devices) {
|
|
407
|
+
if (didSet.has(device.did)) {
|
|
408
|
+
throw new TypeError(`Duplicate discovered device: ${device.did}.`);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
didSet.add(device.did);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
function getHomeKey(home: MiotProviderHomeReference): string {
|
|
416
|
+
return JSON.stringify([home.source, home.id]);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
function compareHomeReferences(
|
|
420
|
+
left: MiotProviderHomeReference,
|
|
421
|
+
right: MiotProviderHomeReference,
|
|
422
|
+
): number {
|
|
423
|
+
return (
|
|
424
|
+
HOME_SOURCE_ORDER.indexOf(left.source) -
|
|
425
|
+
HOME_SOURCE_ORDER.indexOf(right.source) ||
|
|
426
|
+
compareStrings(left.id, right.id)
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function compareStrings(left: string, right: string): number {
|
|
431
|
+
if (left < right) {
|
|
432
|
+
return -1;
|
|
433
|
+
} else if (left > right) {
|
|
434
|
+
return 1;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
return 0;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function accountsEqual(
|
|
441
|
+
left: MiotProviderAccount,
|
|
442
|
+
right: MiotProviderAccount,
|
|
443
|
+
): boolean {
|
|
444
|
+
if (left.userId === null || right.userId === null) {
|
|
445
|
+
return false;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
return left.cloudServer === right.cloudServer && left.userId === right.userId;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function assertProviderName(name: string): void {
|
|
452
|
+
if (
|
|
453
|
+
name === '' ||
|
|
454
|
+
name === '.' ||
|
|
455
|
+
name === '..' ||
|
|
456
|
+
name.includes('/') ||
|
|
457
|
+
name.includes('\\') ||
|
|
458
|
+
name.includes('\0')
|
|
459
|
+
) {
|
|
460
|
+
throw new TypeError(`Invalid MIoT provider name: ${name}.`);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
function isCloudServer(value: string): value is CloudServer {
|
|
465
|
+
return CLOUD_SERVERS.some(cloudServer => cloudServer === value);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
function isFileNotFoundError(error: unknown): boolean {
|
|
469
|
+
if (typeof error !== 'object' || error === null || !('code' in error)) {
|
|
470
|
+
return false;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
return error.code === 'ENOENT';
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
const HOME_SOURCE_ORDER = [
|
|
477
|
+
'owned',
|
|
478
|
+
'shared-home',
|
|
479
|
+
'shared-device',
|
|
480
|
+
] as const satisfies readonly BackendHomeSource[];
|