@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,155 @@
|
|
|
1
|
+
import type {BackendClient} from '../backend/index.js';
|
|
2
|
+
import type {MiotProperty} from '../miot/index.js';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
CloudDeviceChannel,
|
|
6
|
+
type CloudDeviceListener,
|
|
7
|
+
type CloudDeviceMessageSource,
|
|
8
|
+
type CloudDeviceSubscription,
|
|
9
|
+
type CloudPropertySnapshot,
|
|
10
|
+
} from './device.js';
|
|
11
|
+
import {
|
|
12
|
+
CloudMqttClient,
|
|
13
|
+
type CloudMqttConnectionStateListener,
|
|
14
|
+
} from './mqtt.js';
|
|
15
|
+
|
|
16
|
+
export class CloudClient {
|
|
17
|
+
private readonly backendClient: BackendClient;
|
|
18
|
+
|
|
19
|
+
private readonly messageClient: CloudDeviceMessageClient;
|
|
20
|
+
|
|
21
|
+
private readonly deviceChannelMap = new Map<string, CloudDeviceChannel>();
|
|
22
|
+
|
|
23
|
+
constructor(
|
|
24
|
+
backendClient: BackendClient,
|
|
25
|
+
messageClient: CloudDeviceMessageClient = new CloudMqttClient({
|
|
26
|
+
uuid: backendClient.uuid,
|
|
27
|
+
accessToken: backendClient.accessToken,
|
|
28
|
+
cloudServer: backendClient.cloudServer,
|
|
29
|
+
}),
|
|
30
|
+
private readonly preferredStateReader?: CloudDeviceStateReader,
|
|
31
|
+
) {
|
|
32
|
+
this.backendClient = backendClient;
|
|
33
|
+
this.messageClient = messageClient;
|
|
34
|
+
this.messageClient.addConnectionStateListener(connected => {
|
|
35
|
+
for (const channel of this.deviceChannelMap.values()) {
|
|
36
|
+
channel.handleConnectionState(connected);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
updateAccessToken(accessToken: string): void {
|
|
42
|
+
this.backendClient.updateAccessToken(accessToken);
|
|
43
|
+
this.messageClient.updateAccessToken(accessToken);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async connect(): Promise<void> {
|
|
47
|
+
await this.messageClient.connect();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async disconnect(): Promise<void> {
|
|
51
|
+
await this.messageClient.disconnect();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async subscribeDevice(
|
|
55
|
+
did: string,
|
|
56
|
+
properties: readonly MiotProperty[],
|
|
57
|
+
listener: CloudDeviceListener,
|
|
58
|
+
): Promise<CloudDeviceSubscription> {
|
|
59
|
+
let channel = this.deviceChannelMap.get(did);
|
|
60
|
+
|
|
61
|
+
if (channel === undefined) {
|
|
62
|
+
const newChannel = new CloudDeviceChannel(
|
|
63
|
+
did,
|
|
64
|
+
this.messageClient,
|
|
65
|
+
requestedProperties => this.readProperties(requestedProperties),
|
|
66
|
+
() => this.readOnline(did),
|
|
67
|
+
() => {
|
|
68
|
+
if (this.deviceChannelMap.get(did) === newChannel) {
|
|
69
|
+
this.deviceChannelMap.delete(did);
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
);
|
|
73
|
+
channel = newChannel;
|
|
74
|
+
this.deviceChannelMap.set(did, channel);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return channel.subscribe(properties, listener);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private async readProperties(
|
|
81
|
+
properties: readonly MiotProperty[],
|
|
82
|
+
): Promise<readonly CloudPropertySnapshot[]> {
|
|
83
|
+
if (this.preferredStateReader !== undefined) {
|
|
84
|
+
try {
|
|
85
|
+
const snapshot =
|
|
86
|
+
await this.preferredStateReader.getProperties(properties);
|
|
87
|
+
assertCompletePropertySnapshot(properties, snapshot);
|
|
88
|
+
return snapshot;
|
|
89
|
+
} catch {
|
|
90
|
+
// The preferred local route is optional; cloud remains the fallback.
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return this.backendClient.getProperties(properties);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
private async readOnline(did: string): Promise<boolean> {
|
|
98
|
+
if (this.preferredStateReader !== undefined) {
|
|
99
|
+
try {
|
|
100
|
+
return await this.preferredStateReader.getDeviceOnline(did);
|
|
101
|
+
} catch {
|
|
102
|
+
// The preferred local route is optional; cloud remains the fallback.
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return this.backendClient.getDeviceOnline(did);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export type CloudDeviceMessageClient = CloudDeviceMessageSource & {
|
|
111
|
+
updateAccessToken(accessToken: string): void;
|
|
112
|
+
connect(): Promise<void>;
|
|
113
|
+
disconnect(): Promise<void>;
|
|
114
|
+
addConnectionStateListener(
|
|
115
|
+
listener: CloudMqttConnectionStateListener,
|
|
116
|
+
): () => void;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
export type CloudDeviceStateReader = {
|
|
120
|
+
getProperties(
|
|
121
|
+
properties: readonly MiotProperty[],
|
|
122
|
+
): Promise<readonly CloudPropertySnapshot[]>;
|
|
123
|
+
getDeviceOnline(did: string): Promise<boolean>;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
function assertCompletePropertySnapshot(
|
|
127
|
+
properties: readonly MiotProperty[],
|
|
128
|
+
snapshot: readonly CloudPropertySnapshot[],
|
|
129
|
+
): void {
|
|
130
|
+
const expectedKeySet = new Set(properties.map(getPropertyKey));
|
|
131
|
+
const actualKeySet = new Set<string>();
|
|
132
|
+
|
|
133
|
+
for (const result of snapshot) {
|
|
134
|
+
const key = getPropertyKey(result);
|
|
135
|
+
|
|
136
|
+
if (
|
|
137
|
+
!expectedKeySet.has(key) ||
|
|
138
|
+
actualKeySet.has(key) ||
|
|
139
|
+
(result.code !== 0 && result.code !== 1) ||
|
|
140
|
+
!Object.hasOwn(result, 'value')
|
|
141
|
+
) {
|
|
142
|
+
throw new Error('Preferred MIoT property snapshot is incomplete.');
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
actualKeySet.add(key);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (actualKeySet.size !== expectedKeySet.size) {
|
|
149
|
+
throw new Error('Preferred MIoT property snapshot is incomplete.');
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function getPropertyKey(property: MiotProperty): string {
|
|
154
|
+
return `${property.did}.${property.siid}.${property.piid}`;
|
|
155
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type {CloudServer} from '../backend/index.js';
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_CLOUD_MQTT_HOST = 'ha.mqtt.io.mi.com';
|
|
4
|
+
|
|
5
|
+
export const CLOUD_MQTT_PORT = 8883;
|
|
6
|
+
|
|
7
|
+
export const CLOUD_MQTT_KEEPALIVE = 60;
|
|
8
|
+
|
|
9
|
+
export const CLOUD_MQTT_CONNECT_TIMEOUT = 30_000;
|
|
10
|
+
|
|
11
|
+
export const CLOUD_MQTT_RECONNECT_INTERVAL = 10_000;
|
|
12
|
+
|
|
13
|
+
export const CLOUD_MQTT_SUBSCRIPTION_BATCH_SIZE = 300;
|
|
14
|
+
|
|
15
|
+
export const CLOUD_MQTT_SUBSCRIPTION_BATCH_INTERVAL = 1_000;
|
|
16
|
+
|
|
17
|
+
export function getCloudMqttHost(cloudServer: CloudServer): string {
|
|
18
|
+
return `${cloudServer}-${DEFAULT_CLOUD_MQTT_HOST}`;
|
|
19
|
+
}
|