@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,692 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type EndpointPath,
|
|
3
|
+
type ProviderBindingRequest,
|
|
4
|
+
getEndpointPathKey,
|
|
5
|
+
} from '@homelib/core';
|
|
6
|
+
import {type ProviderBindingComponentProps} from '@homelib/terminal';
|
|
7
|
+
import {Box, Text, useInput} from 'ink';
|
|
8
|
+
import {useCallback, useEffect, useRef, useState} from 'react';
|
|
9
|
+
|
|
10
|
+
import type {BackendDevice} from '../backend/index.js';
|
|
11
|
+
import {
|
|
12
|
+
type MiotBindingDeviceCandidate,
|
|
13
|
+
type MiotBindingDeviceProposal,
|
|
14
|
+
type MiotBindingDiscovery,
|
|
15
|
+
type MiotBindingEndpointCandidate,
|
|
16
|
+
type MiotBindingEndpointProposal,
|
|
17
|
+
discoverMiotBindingDevices,
|
|
18
|
+
resolveMiotBindingDeviceProposal,
|
|
19
|
+
} from '../binding.js';
|
|
20
|
+
import type {MiotProvider} from '../provider.js';
|
|
21
|
+
|
|
22
|
+
const PHYSICAL_DEVICE_PAGE_SIZE = 6;
|
|
23
|
+
|
|
24
|
+
export function MiotProviderBindings({
|
|
25
|
+
provider,
|
|
26
|
+
device,
|
|
27
|
+
providerBindings,
|
|
28
|
+
onBind,
|
|
29
|
+
onBack,
|
|
30
|
+
onComplete,
|
|
31
|
+
}: ProviderBindingComponentProps<MiotProvider>): React.JSX.Element {
|
|
32
|
+
const [state, setState] = useState<BindingState>({type: 'loading'});
|
|
33
|
+
const operationReference = useRef<object | undefined>(undefined);
|
|
34
|
+
const deviceReference = useRef(device);
|
|
35
|
+
deviceReference.current = device;
|
|
36
|
+
|
|
37
|
+
const load = useCallback(
|
|
38
|
+
(notice?: DevicesState['notice']): void => {
|
|
39
|
+
const operation = {};
|
|
40
|
+
|
|
41
|
+
operationReference.current = operation;
|
|
42
|
+
setState({type: 'loading'});
|
|
43
|
+
void discoverMiotBindingDevices(provider, deviceReference.current).then(
|
|
44
|
+
discovery => {
|
|
45
|
+
if (operationReference.current !== operation) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
operationReference.current = undefined;
|
|
50
|
+
setState({
|
|
51
|
+
type: 'devices',
|
|
52
|
+
discovery: {
|
|
53
|
+
...discovery,
|
|
54
|
+
devices: orderDevicesByLocation(discovery.devices),
|
|
55
|
+
},
|
|
56
|
+
cursor: 0,
|
|
57
|
+
notice,
|
|
58
|
+
});
|
|
59
|
+
},
|
|
60
|
+
error => {
|
|
61
|
+
if (operationReference.current !== operation) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
operationReference.current = undefined;
|
|
66
|
+
setState({type: 'load-error', message: getErrorMessage(error)});
|
|
67
|
+
},
|
|
68
|
+
);
|
|
69
|
+
},
|
|
70
|
+
[provider],
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
load();
|
|
75
|
+
|
|
76
|
+
return () => {
|
|
77
|
+
operationReference.current = undefined;
|
|
78
|
+
};
|
|
79
|
+
}, [load]);
|
|
80
|
+
|
|
81
|
+
const saveBindings = (
|
|
82
|
+
source: DeviceMatchState,
|
|
83
|
+
requests: readonly ProviderBindingRequest[],
|
|
84
|
+
): void => {
|
|
85
|
+
const operation = {};
|
|
86
|
+
|
|
87
|
+
operationReference.current = operation;
|
|
88
|
+
setState({type: 'saving', source, requests});
|
|
89
|
+
void onBind(requests).then(
|
|
90
|
+
() => {
|
|
91
|
+
if (operationReference.current !== operation) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
operationReference.current = undefined;
|
|
96
|
+
onComplete();
|
|
97
|
+
},
|
|
98
|
+
error => {
|
|
99
|
+
if (operationReference.current !== operation) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
operationReference.current = undefined;
|
|
104
|
+
setState({
|
|
105
|
+
type: 'save-error',
|
|
106
|
+
source,
|
|
107
|
+
requests,
|
|
108
|
+
message: getErrorMessage(error),
|
|
109
|
+
});
|
|
110
|
+
},
|
|
111
|
+
);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const prepareSave = (source: DeviceMatchState): void => {
|
|
115
|
+
const proposal = getProposal(source, providerBindings);
|
|
116
|
+
|
|
117
|
+
if (proposal.status === 'unavailable') {
|
|
118
|
+
return;
|
|
119
|
+
} else if (proposal.status === 'existing') {
|
|
120
|
+
onComplete();
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const requests = proposal.bindings.map(({endpoint, metadata}) => ({
|
|
125
|
+
endpoint,
|
|
126
|
+
metadata,
|
|
127
|
+
replaceExisting:
|
|
128
|
+
findCandidateEndpoint(proposal.device, endpoint)?.endpoint.binding !==
|
|
129
|
+
undefined && !hasProviderBinding(endpoint, providerBindings),
|
|
130
|
+
}));
|
|
131
|
+
|
|
132
|
+
if (requests.some(request => request.replaceExisting)) {
|
|
133
|
+
setState({type: 'confirm-save', source, requests});
|
|
134
|
+
} else {
|
|
135
|
+
saveBindings(source, requests);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
useInput((input, key) => {
|
|
140
|
+
if (state.type === 'loading') {
|
|
141
|
+
if (key.escape) {
|
|
142
|
+
operationReference.current = undefined;
|
|
143
|
+
onBack();
|
|
144
|
+
}
|
|
145
|
+
} else if (state.type === 'load-error') {
|
|
146
|
+
if (key.escape) {
|
|
147
|
+
onBack();
|
|
148
|
+
} else if (key.return || input === 'r') {
|
|
149
|
+
load();
|
|
150
|
+
}
|
|
151
|
+
} else if (state.type === 'devices') {
|
|
152
|
+
const deviceCount = state.discovery.devices.length;
|
|
153
|
+
const clearedState =
|
|
154
|
+
state.notice === undefined ? state : {...state, notice: undefined};
|
|
155
|
+
|
|
156
|
+
if (key.escape) {
|
|
157
|
+
onBack();
|
|
158
|
+
} else if (key.upArrow && deviceCount > 0) {
|
|
159
|
+
setState({
|
|
160
|
+
...clearedState,
|
|
161
|
+
cursor: wrapIndex(state.cursor - 1, deviceCount),
|
|
162
|
+
});
|
|
163
|
+
} else if (key.downArrow && deviceCount > 0) {
|
|
164
|
+
setState({
|
|
165
|
+
...clearedState,
|
|
166
|
+
cursor: wrapIndex(state.cursor + 1, deviceCount),
|
|
167
|
+
});
|
|
168
|
+
} else if (key.return) {
|
|
169
|
+
const selectedDevice = state.discovery.devices.at(state.cursor);
|
|
170
|
+
|
|
171
|
+
if (selectedDevice !== undefined) {
|
|
172
|
+
setState({
|
|
173
|
+
type: 'device-match',
|
|
174
|
+
discovery: state.discovery,
|
|
175
|
+
deviceKey: selectedDevice.key,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
} else if (input === 'r') {
|
|
179
|
+
load('reloaded');
|
|
180
|
+
} else if (state.notice !== undefined) {
|
|
181
|
+
setState(clearedState);
|
|
182
|
+
}
|
|
183
|
+
} else if (state.type === 'device-match') {
|
|
184
|
+
const proposal = getProposal(state, providerBindings);
|
|
185
|
+
|
|
186
|
+
if (key.escape) {
|
|
187
|
+
setState({
|
|
188
|
+
type: 'devices',
|
|
189
|
+
discovery: state.discovery,
|
|
190
|
+
cursor: getCandidateDeviceIndex(state.discovery, state.deviceKey),
|
|
191
|
+
});
|
|
192
|
+
} else if (key.return && proposal.status !== 'unavailable') {
|
|
193
|
+
prepareSave(state);
|
|
194
|
+
}
|
|
195
|
+
} else if (state.type === 'confirm-save') {
|
|
196
|
+
if (key.escape) {
|
|
197
|
+
setState(state.source);
|
|
198
|
+
} else if (input === 'y' || key.return) {
|
|
199
|
+
saveBindings(state.source, state.requests);
|
|
200
|
+
}
|
|
201
|
+
} else if (state.type === 'saving') {
|
|
202
|
+
return;
|
|
203
|
+
} else if (key.escape) {
|
|
204
|
+
setState(state.source);
|
|
205
|
+
} else if (key.return || input === 'r') {
|
|
206
|
+
saveBindings(state.source, state.requests);
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
return <BindingView providerBindings={providerBindings} state={state} />;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
type BindingViewProps = {
|
|
214
|
+
readonly state: BindingState;
|
|
215
|
+
readonly providerBindings: readonly {
|
|
216
|
+
readonly endpoint: EndpointPath;
|
|
217
|
+
readonly metadata: unknown;
|
|
218
|
+
}[];
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
function BindingView({
|
|
222
|
+
state,
|
|
223
|
+
providerBindings,
|
|
224
|
+
}: BindingViewProps): React.JSX.Element {
|
|
225
|
+
if (state.type === 'loading') {
|
|
226
|
+
return (
|
|
227
|
+
<Box flexDirection="column">
|
|
228
|
+
<Text>finding matching mi home devices…</Text>
|
|
229
|
+
<Hint>esc back</Hint>
|
|
230
|
+
</Box>
|
|
231
|
+
);
|
|
232
|
+
} else if (state.type === 'load-error') {
|
|
233
|
+
return (
|
|
234
|
+
<ErrorView message={state.message}>enter/r retry · esc back</ErrorView>
|
|
235
|
+
);
|
|
236
|
+
} else if (state.type === 'devices') {
|
|
237
|
+
return <DevicesView providerBindings={providerBindings} state={state} />;
|
|
238
|
+
} else if (state.type === 'device-match') {
|
|
239
|
+
return <DeviceMatchView proposal={getProposal(state, providerBindings)} />;
|
|
240
|
+
} else if (state.type === 'confirm-save') {
|
|
241
|
+
const replacementCount = state.requests.filter(
|
|
242
|
+
request => request.replaceExisting,
|
|
243
|
+
).length;
|
|
244
|
+
|
|
245
|
+
return (
|
|
246
|
+
<Box flexDirection="column">
|
|
247
|
+
<Text>
|
|
248
|
+
replace {replacementCount} existing{' '}
|
|
249
|
+
{replacementCount === 1 ? 'binding' : 'bindings'} and match this
|
|
250
|
+
device?
|
|
251
|
+
</Text>
|
|
252
|
+
<Hint>enter/y confirm · esc cancel</Hint>
|
|
253
|
+
</Box>
|
|
254
|
+
);
|
|
255
|
+
} else if (state.type === 'saving') {
|
|
256
|
+
return (
|
|
257
|
+
<Box flexDirection="column">
|
|
258
|
+
<Text>saving device match…</Text>
|
|
259
|
+
</Box>
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return (
|
|
264
|
+
<ErrorView message={state.message}>enter/r retry · esc back</ErrorView>
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function DevicesView({
|
|
269
|
+
state,
|
|
270
|
+
providerBindings,
|
|
271
|
+
}: {
|
|
272
|
+
readonly state: DevicesState;
|
|
273
|
+
readonly providerBindings: BindingViewProps['providerBindings'];
|
|
274
|
+
}): React.JSX.Element {
|
|
275
|
+
const {discovery} = state;
|
|
276
|
+
const visibleDevices = getVisibleItems(
|
|
277
|
+
discovery.devices,
|
|
278
|
+
state.cursor,
|
|
279
|
+
PHYSICAL_DEVICE_PAGE_SIZE,
|
|
280
|
+
);
|
|
281
|
+
const deviceGroups = groupDevicesByLocation(visibleDevices.items);
|
|
282
|
+
const hasSummary =
|
|
283
|
+
discovery.devices.length > PHYSICAL_DEVICE_PAGE_SIZE ||
|
|
284
|
+
discovery.failedDeviceCount > 0 ||
|
|
285
|
+
discovery.incompleteDeviceCount > 0 ||
|
|
286
|
+
state.notice === 'reloaded';
|
|
287
|
+
|
|
288
|
+
return (
|
|
289
|
+
<Box flexDirection="column">
|
|
290
|
+
<Text>choose a device</Text>
|
|
291
|
+
|
|
292
|
+
<Box flexDirection="column" marginTop={1}>
|
|
293
|
+
{discovery.devices.length === 0 ? (
|
|
294
|
+
<Text dimColor>no matching devices found.</Text>
|
|
295
|
+
) : (
|
|
296
|
+
deviceGroups.map((group, groupIndex) => (
|
|
297
|
+
<Box
|
|
298
|
+
key={group.location ?? ''}
|
|
299
|
+
flexDirection="column"
|
|
300
|
+
marginTop={groupIndex === 0 ? 0 : 1}
|
|
301
|
+
>
|
|
302
|
+
<Text bold dimColor>
|
|
303
|
+
{group.location ?? 'unknown location'}
|
|
304
|
+
</Text>
|
|
305
|
+
{group.devices.map(device => (
|
|
306
|
+
<ListItem
|
|
307
|
+
key={device.key}
|
|
308
|
+
label={getDeviceLabel(device.device)}
|
|
309
|
+
offline={device.device.online === false}
|
|
310
|
+
selected={
|
|
311
|
+
getCandidateDeviceIndex(discovery, device.key) ===
|
|
312
|
+
state.cursor
|
|
313
|
+
}
|
|
314
|
+
status={getDeviceMatchStatus(device, providerBindings)}
|
|
315
|
+
/>
|
|
316
|
+
))}
|
|
317
|
+
</Box>
|
|
318
|
+
))
|
|
319
|
+
)}
|
|
320
|
+
</Box>
|
|
321
|
+
|
|
322
|
+
{hasSummary ? (
|
|
323
|
+
<Box flexDirection="column" marginTop={1}>
|
|
324
|
+
{discovery.devices.length <= PHYSICAL_DEVICE_PAGE_SIZE ? null : (
|
|
325
|
+
<Text dimColor>
|
|
326
|
+
{visibleDevices.startIndex + 1}–
|
|
327
|
+
{visibleDevices.startIndex + visibleDevices.items.length} of{' '}
|
|
328
|
+
{discovery.devices.length}
|
|
329
|
+
</Text>
|
|
330
|
+
)}
|
|
331
|
+
|
|
332
|
+
{discovery.failedDeviceCount === 0 ? null : (
|
|
333
|
+
<Text color="yellow">
|
|
334
|
+
{discovery.failedDeviceCount} devices could not be checked.
|
|
335
|
+
</Text>
|
|
336
|
+
)}
|
|
337
|
+
|
|
338
|
+
{discovery.incompleteDeviceCount === 0 ? null : (
|
|
339
|
+
<Text dimColor>
|
|
340
|
+
{discovery.incompleteDeviceCount} devices do not expose enough
|
|
341
|
+
information.
|
|
342
|
+
</Text>
|
|
343
|
+
)}
|
|
344
|
+
|
|
345
|
+
{state.notice === 'reloaded' ? (
|
|
346
|
+
<Text color="green">devices reloaded.</Text>
|
|
347
|
+
) : null}
|
|
348
|
+
</Box>
|
|
349
|
+
) : null}
|
|
350
|
+
|
|
351
|
+
<Hint>
|
|
352
|
+
{discovery.devices.length === 0
|
|
353
|
+
? 'r reload · esc back'
|
|
354
|
+
: '↑↓ select · enter match device · r reload · esc back'}
|
|
355
|
+
</Hint>
|
|
356
|
+
</Box>
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function DeviceMatchView({
|
|
361
|
+
proposal,
|
|
362
|
+
}: {
|
|
363
|
+
readonly proposal: MiotBindingDeviceProposal;
|
|
364
|
+
}): React.JSX.Element {
|
|
365
|
+
return (
|
|
366
|
+
<Box flexDirection="column">
|
|
367
|
+
<Box>
|
|
368
|
+
<Text bold>{getDeviceLabel(proposal.device.device)}</Text>
|
|
369
|
+
<DeviceStatusBadges
|
|
370
|
+
offline={proposal.device.device.online === false}
|
|
371
|
+
status={proposal.status}
|
|
372
|
+
/>
|
|
373
|
+
</Box>
|
|
374
|
+
{getDeviceLocation(proposal.device.device) === undefined ? null : (
|
|
375
|
+
<Text dimColor>{getDeviceLocation(proposal.device.device)}</Text>
|
|
376
|
+
)}
|
|
377
|
+
|
|
378
|
+
<Box flexDirection="column" marginTop={1}>
|
|
379
|
+
<Text>device match</Text>
|
|
380
|
+
{proposal.endpoints.map(endpoint => (
|
|
381
|
+
<EndpointProposalRow
|
|
382
|
+
key={getEndpointPathKey(endpoint.endpoint.endpoint.path)}
|
|
383
|
+
proposal={endpoint}
|
|
384
|
+
/>
|
|
385
|
+
))}
|
|
386
|
+
</Box>
|
|
387
|
+
|
|
388
|
+
{proposal.status === 'unavailable' ? (
|
|
389
|
+
<Text color="yellow">
|
|
390
|
+
this device mapping uses resources already bound elsewhere.
|
|
391
|
+
</Text>
|
|
392
|
+
) : null}
|
|
393
|
+
|
|
394
|
+
<Box marginTop={1}>
|
|
395
|
+
{proposal.status === 'automatic' ? (
|
|
396
|
+
<Text bold color="cyan">
|
|
397
|
+
› bind device
|
|
398
|
+
</Text>
|
|
399
|
+
) : proposal.status === 'existing' ? (
|
|
400
|
+
<Text bold color="cyan">
|
|
401
|
+
› done
|
|
402
|
+
</Text>
|
|
403
|
+
) : (
|
|
404
|
+
<Text color="yellow">device match unavailable.</Text>
|
|
405
|
+
)}
|
|
406
|
+
</Box>
|
|
407
|
+
|
|
408
|
+
<Hint>
|
|
409
|
+
{proposal.status === 'unavailable' ? '' : 'enter confirm · '}esc choose
|
|
410
|
+
another device
|
|
411
|
+
</Hint>
|
|
412
|
+
</Box>
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
function EndpointProposalRow({
|
|
417
|
+
proposal,
|
|
418
|
+
}: {
|
|
419
|
+
readonly proposal: MiotBindingEndpointProposal;
|
|
420
|
+
}): React.JSX.Element {
|
|
421
|
+
const color = proposal.status === 'unavailable' ? 'red' : 'green';
|
|
422
|
+
const badge =
|
|
423
|
+
proposal.status === 'automatic'
|
|
424
|
+
? 'ready'
|
|
425
|
+
: proposal.status === 'existing'
|
|
426
|
+
? 'bound here'
|
|
427
|
+
: 'used elsewhere';
|
|
428
|
+
|
|
429
|
+
return (
|
|
430
|
+
<Box flexDirection="column">
|
|
431
|
+
<Text>
|
|
432
|
+
<Text color={color}>
|
|
433
|
+
{proposal.status === 'unavailable' ? '!' : '●'}{' '}
|
|
434
|
+
{getEndpointLabel(proposal.endpoint.endpoint)}
|
|
435
|
+
</Text>
|
|
436
|
+
<Text bold color={color}>
|
|
437
|
+
{' '}
|
|
438
|
+
[{badge}]
|
|
439
|
+
</Text>
|
|
440
|
+
</Text>
|
|
441
|
+
<Box marginLeft={2}>
|
|
442
|
+
<Text dimColor>{proposal.endpoint.label}</Text>
|
|
443
|
+
</Box>
|
|
444
|
+
</Box>
|
|
445
|
+
);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
function ListItem({
|
|
449
|
+
label,
|
|
450
|
+
offline,
|
|
451
|
+
selected,
|
|
452
|
+
status,
|
|
453
|
+
}: {
|
|
454
|
+
readonly label: string;
|
|
455
|
+
readonly offline: boolean;
|
|
456
|
+
readonly selected: boolean;
|
|
457
|
+
readonly status: MiotBindingDeviceProposal['status'];
|
|
458
|
+
}): React.JSX.Element {
|
|
459
|
+
return (
|
|
460
|
+
<Box>
|
|
461
|
+
<Text bold={selected} color={selected ? 'cyan' : undefined}>
|
|
462
|
+
{selected ? '› ' : ' '}
|
|
463
|
+
{label}
|
|
464
|
+
</Text>
|
|
465
|
+
<DeviceStatusBadges offline={offline} status={status} />
|
|
466
|
+
</Box>
|
|
467
|
+
);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function DeviceStatusBadges({
|
|
471
|
+
offline,
|
|
472
|
+
status,
|
|
473
|
+
}: {
|
|
474
|
+
readonly offline: boolean;
|
|
475
|
+
readonly status: MiotBindingDeviceProposal['status'];
|
|
476
|
+
}): React.JSX.Element {
|
|
477
|
+
return (
|
|
478
|
+
<>
|
|
479
|
+
{status === 'existing' ? (
|
|
480
|
+
<Text bold color="green">
|
|
481
|
+
{' '}
|
|
482
|
+
[bound here]
|
|
483
|
+
</Text>
|
|
484
|
+
) : status === 'unavailable' ? (
|
|
485
|
+
<Text bold color="red">
|
|
486
|
+
{' '}
|
|
487
|
+
[used elsewhere]
|
|
488
|
+
</Text>
|
|
489
|
+
) : null}
|
|
490
|
+
{offline ? <Text color="yellow"> [offline]</Text> : null}
|
|
491
|
+
</>
|
|
492
|
+
);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
function ErrorView({
|
|
496
|
+
message,
|
|
497
|
+
children,
|
|
498
|
+
}: {
|
|
499
|
+
readonly message: string;
|
|
500
|
+
readonly children: React.ReactNode;
|
|
501
|
+
}): React.JSX.Element {
|
|
502
|
+
return (
|
|
503
|
+
<Box flexDirection="column">
|
|
504
|
+
<Text color="red">{message}</Text>
|
|
505
|
+
<Hint>{children}</Hint>
|
|
506
|
+
</Box>
|
|
507
|
+
);
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
function Hint({
|
|
511
|
+
children,
|
|
512
|
+
}: {
|
|
513
|
+
readonly children: React.ReactNode;
|
|
514
|
+
}): React.JSX.Element {
|
|
515
|
+
return (
|
|
516
|
+
<Box marginTop={1}>
|
|
517
|
+
<Text dimColor>{children}</Text>
|
|
518
|
+
</Box>
|
|
519
|
+
);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
type BindingState =
|
|
523
|
+
| {readonly type: 'loading'}
|
|
524
|
+
| {readonly type: 'load-error'; readonly message: string}
|
|
525
|
+
| DevicesState
|
|
526
|
+
| DeviceMatchState
|
|
527
|
+
| ConfirmSaveState
|
|
528
|
+
| SavingState
|
|
529
|
+
| SaveErrorState;
|
|
530
|
+
|
|
531
|
+
type DevicesState = {
|
|
532
|
+
readonly type: 'devices';
|
|
533
|
+
readonly discovery: MiotBindingDiscovery;
|
|
534
|
+
readonly cursor: number;
|
|
535
|
+
readonly notice?: 'reloaded';
|
|
536
|
+
};
|
|
537
|
+
|
|
538
|
+
type DeviceMatchState = {
|
|
539
|
+
readonly type: 'device-match';
|
|
540
|
+
readonly discovery: MiotBindingDiscovery;
|
|
541
|
+
readonly deviceKey: string;
|
|
542
|
+
};
|
|
543
|
+
|
|
544
|
+
type ConfirmSaveState = {
|
|
545
|
+
readonly type: 'confirm-save';
|
|
546
|
+
readonly source: DeviceMatchState;
|
|
547
|
+
readonly requests: readonly ProviderBindingRequest[];
|
|
548
|
+
};
|
|
549
|
+
|
|
550
|
+
type SavingState = {
|
|
551
|
+
readonly type: 'saving';
|
|
552
|
+
readonly source: DeviceMatchState;
|
|
553
|
+
readonly requests: readonly ProviderBindingRequest[];
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
type SaveErrorState = {
|
|
557
|
+
readonly type: 'save-error';
|
|
558
|
+
readonly source: DeviceMatchState;
|
|
559
|
+
readonly requests: readonly ProviderBindingRequest[];
|
|
560
|
+
readonly message: string;
|
|
561
|
+
};
|
|
562
|
+
|
|
563
|
+
function getProposal(
|
|
564
|
+
state: DeviceMatchState,
|
|
565
|
+
providerBindings: BindingViewProps['providerBindings'],
|
|
566
|
+
): MiotBindingDeviceProposal {
|
|
567
|
+
const device = getCandidateDevice(state.discovery, state.deviceKey);
|
|
568
|
+
|
|
569
|
+
if (device === undefined) {
|
|
570
|
+
throw new TypeError('Unknown MIoT binding device.');
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
return resolveMiotBindingDeviceProposal(device, providerBindings);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
function getCandidateDevice(
|
|
577
|
+
discovery: MiotBindingDiscovery,
|
|
578
|
+
key: string,
|
|
579
|
+
): MiotBindingDeviceCandidate | undefined {
|
|
580
|
+
return discovery.devices.find(device => device.key === key);
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
function getCandidateDeviceIndex(
|
|
584
|
+
discovery: MiotBindingDiscovery,
|
|
585
|
+
key: string,
|
|
586
|
+
): number {
|
|
587
|
+
const index = discovery.devices.findIndex(device => device.key === key);
|
|
588
|
+
|
|
589
|
+
return index === -1 ? 0 : index;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
function findCandidateEndpoint(
|
|
593
|
+
device: MiotBindingDeviceCandidate,
|
|
594
|
+
endpointPath: EndpointPath,
|
|
595
|
+
): MiotBindingEndpointCandidate | undefined {
|
|
596
|
+
const endpointPathKey = getEndpointPathKey(endpointPath);
|
|
597
|
+
|
|
598
|
+
return device.endpoints.find(
|
|
599
|
+
endpoint => getEndpointPathKey(endpoint.endpoint.path) === endpointPathKey,
|
|
600
|
+
);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
function hasProviderBinding(
|
|
604
|
+
endpointPath: EndpointPath,
|
|
605
|
+
bindings: readonly {readonly endpoint: EndpointPath}[],
|
|
606
|
+
): boolean {
|
|
607
|
+
const endpointPathKey = getEndpointPathKey(endpointPath);
|
|
608
|
+
|
|
609
|
+
return bindings.some(
|
|
610
|
+
binding => getEndpointPathKey(binding.endpoint) === endpointPathKey,
|
|
611
|
+
);
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
function getDeviceLabel(device: BackendDevice): string {
|
|
615
|
+
return device.name ?? device.model ?? device.did;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
function getDeviceLocation(device: BackendDevice): string | undefined {
|
|
619
|
+
const location = [device.homeName, device.roomName]
|
|
620
|
+
.filter(value => value !== undefined)
|
|
621
|
+
.join(' › ');
|
|
622
|
+
|
|
623
|
+
return location === '' ? undefined : location;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
function getDeviceMatchStatus(
|
|
627
|
+
device: MiotBindingDeviceCandidate,
|
|
628
|
+
providerBindings: BindingViewProps['providerBindings'],
|
|
629
|
+
): MiotBindingDeviceProposal['status'] {
|
|
630
|
+
return resolveMiotBindingDeviceProposal(device, providerBindings).status;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
type DeviceGroup = {
|
|
634
|
+
readonly location: string | undefined;
|
|
635
|
+
readonly devices: readonly MiotBindingDeviceCandidate[];
|
|
636
|
+
};
|
|
637
|
+
|
|
638
|
+
function orderDevicesByLocation(
|
|
639
|
+
devices: readonly MiotBindingDeviceCandidate[],
|
|
640
|
+
): readonly MiotBindingDeviceCandidate[] {
|
|
641
|
+
return groupDevicesByLocation(devices).flatMap(group => group.devices);
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
function groupDevicesByLocation(
|
|
645
|
+
devices: readonly MiotBindingDeviceCandidate[],
|
|
646
|
+
): readonly DeviceGroup[] {
|
|
647
|
+
const groupMap = new Map<string | undefined, MiotBindingDeviceCandidate[]>();
|
|
648
|
+
|
|
649
|
+
for (const device of devices) {
|
|
650
|
+
const location = getDeviceLocation(device.device);
|
|
651
|
+
const group = groupMap.get(location);
|
|
652
|
+
|
|
653
|
+
if (group === undefined) {
|
|
654
|
+
groupMap.set(location, [device]);
|
|
655
|
+
} else {
|
|
656
|
+
group.push(device);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
return [...groupMap].map(([location, devices]) => ({location, devices}));
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
function getEndpointLabel(
|
|
664
|
+
endpoint: MiotBindingEndpointProposal['endpoint']['endpoint'],
|
|
665
|
+
): string {
|
|
666
|
+
return endpoint.endpoint.name === '' ? 'main' : endpoint.endpoint.name;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
function wrapIndex(index: number, length: number): number {
|
|
670
|
+
return (index + length) % length;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
function getVisibleItems<T>(
|
|
674
|
+
items: readonly T[],
|
|
675
|
+
selectedIndex: number,
|
|
676
|
+
pageSize: number,
|
|
677
|
+
): {readonly items: readonly T[]; readonly startIndex: number} {
|
|
678
|
+
const maximumStartIndex = Math.max(0, items.length - pageSize);
|
|
679
|
+
const startIndex = Math.min(
|
|
680
|
+
maximumStartIndex,
|
|
681
|
+
Math.max(0, selectedIndex - Math.floor(pageSize / 2)),
|
|
682
|
+
);
|
|
683
|
+
|
|
684
|
+
return {
|
|
685
|
+
items: items.slice(startIndex, startIndex + pageSize),
|
|
686
|
+
startIndex,
|
|
687
|
+
};
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
function getErrorMessage(error: unknown): string {
|
|
691
|
+
return error instanceof Error ? error.message : String(error);
|
|
692
|
+
}
|