-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.tsx
177 lines (161 loc) · 6.25 KB
/
index.tsx
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import React from 'react'
import { createRoot, Root } from 'react-dom/client';
import { WebRTCConnection } from 'shared/webrtcconnections';
import { WebRTCMessage, RemoteStream } from 'shared/util';
import { RemoteRobot } from 'shared/remoterobot';
import { cmd } from 'shared/commands';
import { Operator } from './Operator';
import { DEFAULT_VELOCITY_SCALE } from './static_components/SpeedControl';
import { StorageHandler } from './storage_handler/StorageHandler';
import { FirebaseStorageHandler } from './storage_handler/FirebaseStorageHandler';
import { LocalStorageHandler } from './storage_handler/LocalStorageHandler';
import { FirebaseOptions } from "firebase/app"
import { ButtonFunctionProvider } from './function_providers/ButtonFunctionProvider';
import { FunctionProvider } from './function_providers/FunctionProvider';
import { PredictiveDisplayFunctionProvider } from './function_providers/PredictiveDisplayFunctionProvider';
import { UnderVideoFunctionProvider } from './function_providers/UnderVideoFunctionProvider';
import { VoiceFunctionProvider } from './function_providers/VoiceFunctionProvider';
import "operator/css/index.css"
let allRemoteStreams: Map<string, RemoteStream> = new Map<string, RemoteStream>()
let remoteRobot: RemoteRobot;
let connection: WebRTCConnection;
let root: Root;
// Create the function providers. These abstract the logic between the React
// components and remote robot.
export var buttonFunctionProvider = new ButtonFunctionProvider();
export var voiceFunctionProvider = new VoiceFunctionProvider();
export var predicitiveDisplayFunctionProvider = new PredictiveDisplayFunctionProvider();
export var underVideoFunctionProvider = new UnderVideoFunctionProvider()
// Create the WebRTC connection and connect the operator room
connection = new WebRTCConnection({
peerRole: "operator",
polite: true,
onMessage: handleWebRTCMessage,
onTrackAdded: handleRemoteTrackAdded,
onMessageChannelOpen: initializeOperator,
onConnectionEnd: disconnectFromRobot
});
connection.joinOperatorRoom()
// Check if the WebRTC connection is resolved. Reload every 4 seconds until resolved.
setTimeout(() => {
let isResolved = connection.connectionState() == 'connected' ? true : false
console.log("connection state: ", isResolved)
if (isResolved) {
console.log('WebRTC connection is resolved.');
} else {
window.location.reload()
}
}, 4000);
// Create root once when index is loaded
const container = document.getElementById('root');
root = createRoot(container!);
/** Handle when the WebRTC connection adds a new track on a camera video stream. */
function handleRemoteTrackAdded(event: RTCTrackEvent) {
console.log('Remote track added.');
const track = event.track;
const stream = event.streams[0];
console.log('got track id=' + track.id, track);
if (stream) {
console.log('stream id=' + stream.id, stream);
}
console.log('OPERATOR: adding remote tracks');
let streamName = connection.cameraInfo[stream.id]
allRemoteStreams.set(streamName, { 'track': track, 'stream': stream });
}
/**
* Callback to handle a new WebRTC message from the robot browser.
* @param message the {@link WebRTCMessage} or an array of messages.
*/
function handleWebRTCMessage(message: WebRTCMessage | WebRTCMessage[]) {
if (message instanceof Array) {
for (const subMessage of message) {
// Recursive call to handle each message in the array
handleWebRTCMessage(subMessage)
}
return
}
switch (message.type) {
case 'validJointState':
remoteRobot.sensors.checkValidJointState(
message.jointsInLimits,
message.jointsInCollision
);
break;
default:
throw Error(`unhandled WebRTC message type ${message.type}`)
}
}
/**
* Sets up remote robot, creates the storage handler,
* and renders the operator browser.
*/
function initializeOperator() {
configureRemoteRobot();
let storageHandler: StorageHandler;
const storageHandlerReadyCallback = () => {
renderOperator(storageHandler);
}
storageHandler = createStorageHandler(storageHandlerReadyCallback);
// renderOperator(storageHandler);
}
/**
* Configures the remote robot, which connects with the robot browser over the
* WebRTC connection.
*/
function configureRemoteRobot() {
remoteRobot = new RemoteRobot({
robotChannel: (message: cmd) => connection.sendData(message),
});
remoteRobot.setRobotMode("navigation");
remoteRobot.sensors.setFunctionProviderCallback(buttonFunctionProvider.updateJointStates);
}
/**
* Creates a storage handler based on the `storage` property in the process
* environment.
* @param storageHandlerReadyCallback callback when the storage handler is ready
* @returns the storage handler
*/
function createStorageHandler(storageHandlerReadyCallback: () => void) {
switch (process.env.storage) {
case ('firebase'):
const config: FirebaseOptions = {
apiKey: process.env.apiKey,
authDomain: process.env.authDomain,
projectId: process.env.projectId,
storageBucket: process.env.storageBucket,
messagingSenderId: process.env.messagingSenderId,
appId: process.env.appId,
measurementId: process.env.measurementId
}
return new FirebaseStorageHandler(
storageHandlerReadyCallback,
config
);
default:
// if (process.env.storage == 'localstorage')
return new LocalStorageHandler(storageHandlerReadyCallback);
}
}
/**
* Renders the operator browser.
*
* @param storageHandler the storage handler
*/
function renderOperator(storageHandler: StorageHandler) {
const layout = storageHandler.loadCurrentLayoutOrDefault();
FunctionProvider.initialize(DEFAULT_VELOCITY_SCALE, layout.actionMode);
FunctionProvider.addRemoteRobot(remoteRobot);
root.render(
<Operator
remoteStreams={allRemoteStreams}
layout={layout}
storageHandler={storageHandler}
/>
);
}
function disconnectFromRobot() {
connection.hangup()
}
window.onbeforeunload = () => {
connection.hangup()
};