-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.ts
147 lines (132 loc) · 4.11 KB
/
state.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { create } from "zustand";
import { useEffect, useRef, useState } from "react";
import type {
RobotClient,
StreamClient,
BaseClient,
SensorClient,
MovementSensorClient,
} from "@viamrobotics/sdk";
import {
RobotCredentials,
getRobotClient,
getStreamClient,
getGPSMovementSensorClient,
getBaseClient,
getOSStatsSensorClient,
} from "./client";
export type ClientStatus = "disconnected" | "loading" | "connected";
export interface Store {
status: ClientStatus;
client?: RobotClient;
streamClient?: StreamClient;
gpsMovementSensorClient?: MovementSensorClient;
osStatsSensorClient?: SensorClient;
baseClient?: BaseClient;
connectOrDisconnect: (credentials: RobotCredentials) => unknown;
}
export const useStore = create<Store>((set, get) => ({
status: "disconnected",
client: undefined,
streamClient: undefined,
baseClient: undefined,
gpsMovementSensorClient: undefined,
osStatsSensorClient: undefined,
connectOrDisconnect: (credentials: RobotCredentials) => {
const status = get().status;
if (status === "disconnected") {
console.log(`Currently disconnected, connecting...`);
console.log("Setting state to 'loading'...");
set({ status: "loading" });
console.log(
`Getting robot client for credentials ${JSON.stringify(
credentials,
null,
2
)}`
);
getRobotClient(credentials)
.then((client) => {
console.log(`Got client ${client}`);
console.log(`Getting stream client`);
const streamClient = getStreamClient(client);
console.log(
`Received stream client ${JSON.stringify(streamClient, null, 2)}`
);
console.log(`Getting base client`);
const baseClient = getBaseClient(client);
console.log(
`Received base client ${JSON.stringify(baseClient, null, 2)}`
);
const gpsMovementSensorClient = getGPSMovementSensorClient(client);
console.log(
`Received sensor client ${JSON.stringify(
gpsMovementSensorClient,
null,
2
)}`
);
const osStatsSensorClient = getOSStatsSensorClient(client);
console.log(
`Received sensor client ${JSON.stringify(
osStatsSensorClient,
null,
2
)}`
);
const stateUpdate = {
status: "connected",
client,
gpsMovementSensorClient,
osStatsSensorClient,
baseClient,
streamClient,
};
console.log(
`Setting state to connected: ${JSON.stringify(
stateUpdate,
null,
2
)}`
);
//@ts-ignore
set({ ...stateUpdate });
console.log("Set state to connected");
})
.catch((error: unknown) => set({ status: "disconnected" }));
} else if (status === "connected") {
console.log(`Currently connected, disconnecting...`);
set({ status: "loading" });
get()
?.client?.disconnect()
.then(() => set({ status: "disconnected" }))
.catch((error: unknown) => set({ status: "disconnected" }));
}
},
}));
export const useStream = (
streamClient: StreamClient | undefined,
cameraName: string
): MediaStream | undefined => {
const okToConnectRef = useRef(true);
const [stream, setStream] = useState<MediaStream | undefined>();
useEffect(() => {
if (streamClient && okToConnectRef.current) {
okToConnectRef.current = false;
streamClient
.getStream(cameraName)
.then((mediaStream) => setStream(mediaStream))
.catch((error: unknown) => {
console.warn(`Unable to connect to camera ${cameraName}`, error);
});
return () => {
okToConnectRef.current = true;
streamClient.remove(cameraName).catch((error: unknown) => {
console.warn(`Unable to disconnect to camera ${cameraName}`, error);
});
};
}
return undefined;
}, [streamClient, cameraName]);
return stream;
};