Spaces:
Running
Running
File size: 14,985 Bytes
130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 1a7b22d 130ae50 |
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
import { useState, useEffect } from "react";
import { Button } from "./ui/button";
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "./ui/card";
import { Alert, AlertDescription } from "./ui/alert";
import { Badge } from "./ui/badge";
import { findPort, isWebSerialSupported } from "@lerobot/web";
import type { RobotConnection } from "@lerobot/web";
interface PortManagerProps {
connectedRobots: RobotConnection[];
onConnectedRobotsChange: (robots: RobotConnection[]) => void;
onCalibrate?: (port: any) => void; // Let library handle port type
onTeleoperate?: (robot: RobotConnection) => void;
}
export function PortManager({
connectedRobots,
onConnectedRobotsChange,
onCalibrate,
onTeleoperate,
}: PortManagerProps) {
const [isFindingPorts, setIsFindingPorts] = useState(false);
const [findPortsLog, setFindPortsLog] = useState<string[]>([]);
const [error, setError] = useState<string | null>(null);
// Load saved robots on mount by calling findPort with saved data
useEffect(() => {
loadSavedRobots();
}, []);
const loadSavedRobots = async () => {
try {
console.log("๐ Loading saved robots from localStorage...");
// Load saved robot configs for auto-connect mode
const robotConfigs: any[] = [];
const { getUnifiedRobotData } = await import("../lib/unified-storage");
// Check localStorage for saved robot data
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (key && key.startsWith("lerobotjs-")) {
const serialNumber = key.replace("lerobotjs-", "");
const robotData = getUnifiedRobotData(serialNumber);
if (robotData) {
console.log(
`โ
Found saved robot: ${robotData.device_info.robotId}`
);
// Create robot config for auto-connect mode
robotConfigs.push({
robotType: robotData.device_info.robotType,
robotId: robotData.device_info.robotId,
serialNumber: serialNumber,
});
}
}
}
if (robotConfigs.length > 0) {
console.log(
`๐ Auto-connecting to ${robotConfigs.length} saved robots...`
);
// Use auto-connect mode - NO DIALOG will be shown!
const findPortProcess = await findPort({
robotConfigs,
onMessage: (message) => {
console.log(`Auto-connect: ${message}`);
},
});
const reconnectedRobots = await findPortProcess.result;
console.log(
`โ
Auto-connected to ${
reconnectedRobots.filter((r) => r.isConnected).length
}/${robotConfigs.length} saved robots`
);
onConnectedRobotsChange(reconnectedRobots);
} else {
console.log("No saved robots found in localStorage");
}
} catch (error) {
console.error("Failed to load saved robots:", error);
}
};
const handleFindPorts = async () => {
if (!isWebSerialSupported()) {
setError("Web Serial API is not supported in this browser");
return;
}
try {
setIsFindingPorts(true);
setFindPortsLog([]);
setError(null);
// Use clean library API - library handles everything!
const findPortProcess = await findPort({
onMessage: (message) => {
setFindPortsLog((prev) => [...prev, message]);
},
});
const robotConnections = await findPortProcess.result;
// Add new robots to the list (avoid duplicates)
const newRobots = robotConnections.filter(
(newRobot) =>
!connectedRobots.some(
(existing) => existing.serialNumber === newRobot.serialNumber
)
);
onConnectedRobotsChange([...connectedRobots, ...newRobots]);
setFindPortsLog((prev) => [
...prev,
`โ
Found ${newRobots.length} new robots`,
]);
} catch (error) {
if (
error instanceof Error &&
(error.message.includes("cancelled") ||
error.name === "NotAllowedError")
) {
console.log("Port discovery cancelled by user");
return;
}
setError(error instanceof Error ? error.message : "Failed to find ports");
} finally {
setIsFindingPorts(false);
}
};
const handleDisconnect = (index: number) => {
const updatedRobots = connectedRobots.filter((_, i) => i !== index);
onConnectedRobotsChange(updatedRobots);
};
const handleCalibrate = (robot: RobotConnection) => {
if (!robot.robotType || !robot.robotId) {
setError("Please configure robot type and ID first");
return;
}
if (onCalibrate) {
onCalibrate(robot.port);
}
};
const handleTeleoperate = (robot: RobotConnection) => {
if (!robot.robotType || !robot.robotId) {
setError("Please configure robot type and ID first");
return;
}
if (!robot.isConnected || !robot.port) {
setError(
"Robot is not connected. Please use 'Find & Connect Robots' first."
);
return;
}
// Robot is connected, proceed with teleoperation
if (onTeleoperate) {
onTeleoperate(robot);
}
};
return (
<Card>
<CardHeader>
<CardTitle>๐ Robot Connection Manager</CardTitle>
<CardDescription>
Find and connect to your robot devices
</CardDescription>
</CardHeader>
<CardContent>
<div className="space-y-6">
{/* Error Display */}
{error && (
<Alert variant="destructive">
<AlertDescription>{error}</AlertDescription>
</Alert>
)}
{/* Find Ports Button */}
<Button
onClick={handleFindPorts}
disabled={isFindingPorts || !isWebSerialSupported()}
className="w-full"
>
{isFindingPorts ? "Finding Robots..." : "๐ Find & Connect Robots"}
</Button>
{/* Find Ports Log */}
{findPortsLog.length > 0 && (
<div className="bg-gray-50 p-3 rounded-md text-sm space-y-1 max-h-32 overflow-y-auto">
{findPortsLog.map((log, index) => (
<div key={index} className="text-gray-700">
{log}
</div>
))}
</div>
)}
{/* Connected Robots */}
<div>
<h4 className="font-semibold mb-3">
Connected Robots ({connectedRobots.length})
</h4>
{connectedRobots.length === 0 ? (
<div className="text-center py-8 text-gray-500">
<div className="text-2xl mb-2">๐ค</div>
<p>No robots found</p>
<p className="text-xs">
Click "Find & Connect Robots" to discover devices
</p>
</div>
) : (
<div className="space-y-4">
{connectedRobots.map((robot, index) => (
<RobotCard
key={robot.serialNumber || index}
robot={robot}
onDisconnect={() => handleDisconnect(index)}
onCalibrate={() => handleCalibrate(robot)}
onTeleoperate={() => handleTeleoperate(robot)}
/>
))}
</div>
)}
</div>
</div>
</CardContent>
</Card>
);
}
interface RobotCardProps {
robot: RobotConnection;
onDisconnect: () => void;
onCalibrate: () => void;
onTeleoperate: () => void;
}
function RobotCard({
robot,
onDisconnect,
onCalibrate,
onTeleoperate,
}: RobotCardProps) {
const [calibrationStatus, setCalibrationStatus] = useState<{
timestamp: string;
readCount: number;
} | null>(null);
const [isEditing, setIsEditing] = useState(false);
const [editRobotType, setEditRobotType] = useState<
"so100_follower" | "so100_leader"
>(robot.robotType || "so100_follower");
const [editRobotId, setEditRobotId] = useState(robot.robotId || "");
const isConfigured = robot.robotType && robot.robotId;
// Check calibration status using unified storage
useEffect(() => {
const checkCalibrationStatus = async () => {
if (!robot.serialNumber) return;
try {
const { getCalibrationStatus } = await import("../lib/unified-storage");
const status = getCalibrationStatus(robot.serialNumber);
setCalibrationStatus(status);
} catch (error) {
console.warn("Failed to check calibration status:", error);
}
};
checkCalibrationStatus();
}, [robot.serialNumber]);
const handleSaveConfig = async () => {
if (!editRobotId.trim() || !robot.serialNumber) return;
try {
const { saveRobotConfig } = await import("../lib/unified-storage");
saveRobotConfig(
robot.serialNumber,
editRobotType,
editRobotId.trim(),
robot.usbMetadata
);
// Update the robot object (this should trigger a re-render)
robot.robotType = editRobotType;
robot.robotId = editRobotId.trim();
setIsEditing(false);
console.log("โ
Robot configuration saved");
} catch (error) {
console.error("Failed to save robot configuration:", error);
}
};
const handleCancelEdit = () => {
setEditRobotType(robot.robotType || "so100_follower");
setEditRobotId(robot.robotId || "");
setIsEditing(false);
};
return (
<div className="border rounded-lg p-4 space-y-3">
{/* Header */}
<div className="flex items-center justify-between">
<div className="flex items-center space-x-2">
<div className="flex flex-col">
<span className="font-medium">
{robot.robotId || robot.name || "Unnamed Robot"}
</span>
<span className="text-xs text-gray-500">
{robot.robotType?.replace("_", " ") || "Not configured"}
</span>
{robot.serialNumber && (
<span className="text-xs text-gray-400 font-mono">
{robot.serialNumber.length > 20
? robot.serialNumber.substring(0, 20) + "..."
: robot.serialNumber}
</span>
)}
</div>
<div className="flex flex-col gap-1">
<Badge variant={robot.isConnected ? "default" : "outline"}>
{robot.isConnected ? "Connected" : "Available"}
</Badge>
{calibrationStatus && (
<Badge variant="default" className="bg-green-100 text-green-800">
โ
Calibrated
</Badge>
)}
</div>
</div>
<Button variant="destructive" size="sm" onClick={onDisconnect}>
Remove
</Button>
</div>
{/* Robot Configuration Display (when not editing) */}
{!isEditing && isConfigured && (
<div className="flex items-center justify-between p-3 bg-gray-50 rounded-lg">
<div className="flex items-center space-x-3">
<div>
<div className="font-medium text-sm">{robot.robotId}</div>
<div className="text-xs text-gray-600">
{robot.robotType?.replace("_", " ")}
</div>
</div>
</div>
<Button
variant="outline"
size="sm"
onClick={() => setIsEditing(true)}
>
Edit
</Button>
</div>
)}
{/* Configuration Prompt for unconfigured robots */}
{!isEditing && !isConfigured && (
<div className="flex items-center justify-between p-3 bg-blue-50 rounded-lg">
<div className="text-sm text-blue-800">
Robot needs configuration before use
</div>
<Button
variant="outline"
size="sm"
onClick={() => setIsEditing(true)}
>
Configure
</Button>
</div>
)}
{/* Robot Configuration Form (when editing) */}
{isEditing && (
<div className="space-y-3 p-3 bg-gray-50 rounded-lg">
<div className="grid grid-cols-2 gap-3">
<div>
<label className="text-sm font-medium block mb-1">
Robot Type
</label>
<select
value={editRobotType}
onChange={(e) =>
setEditRobotType(
e.target.value as "so100_follower" | "so100_leader"
)
}
className="w-full px-2 py-1 border rounded text-sm"
>
<option value="so100_follower">SO-100 Follower</option>
<option value="so100_leader">SO-100 Leader</option>
</select>
</div>
<div>
<label className="text-sm font-medium block mb-1">Robot ID</label>
<input
type="text"
value={editRobotId}
onChange={(e) => setEditRobotId(e.target.value)}
placeholder="e.g., my_robot"
className="w-full px-2 py-1 border rounded text-sm"
/>
</div>
</div>
<div className="flex gap-2">
<Button
size="sm"
onClick={handleSaveConfig}
disabled={!editRobotId.trim()}
>
Save
</Button>
<Button size="sm" variant="outline" onClick={handleCancelEdit}>
Cancel
</Button>
</div>
</div>
)}
{/* Calibration Status */}
{isConfigured && !isEditing && (
<div className="text-sm text-gray-600">
{calibrationStatus ? (
<span>
Last calibrated:{" "}
{new Date(calibrationStatus.timestamp).toLocaleDateString()}
<span className="text-xs ml-1">
({calibrationStatus.readCount} readings)
</span>
</span>
) : (
<span>Not calibrated yet</span>
)}
</div>
)}
{/* Actions */}
{isConfigured && !isEditing && (
<div className="flex gap-2">
<Button
size="sm"
variant={calibrationStatus ? "outline" : "default"}
onClick={onCalibrate}
>
{calibrationStatus ? "๐ Re-calibrate" : "๐ Calibrate"}
</Button>
<Button
size="sm"
variant="outline"
onClick={onTeleoperate}
disabled={!robot.isConnected}
title={
!robot.isConnected
? "Use 'Find & Connect Robots' first"
: undefined
}
>
๐ฎ Teleoperate
</Button>
</div>
)}
</div>
);
}
|