File size: 2,421 Bytes
6ce4ca6
 
 
 
 
 
 
 
 
 
 
 
 
3cdf7b9
6ce4ca6
 
 
 
 
 
 
 
 
 
3cdf7b9
6ce4ca6
 
 
 
3cdf7b9
6ce4ca6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3cdf7b9
6ce4ca6
 
 
 
 
 
 
 
 
 
3cdf7b9
 
 
 
 
 
 
 
 
6ce4ca6
 
 
 
 
 
 
3cdf7b9
 
 
6ce4ca6
 
 
 
 
 
 
 
 
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
<script lang="ts">
	import { robotManager } from "$lib/elements/robot/RobotManager.svelte.js";
	import { onMount, onDestroy } from "svelte";
	import InputConnectionModal from "@/components/3d/elements/robot/modal/InputConnectionModal.svelte";
	import OutputConnectionModal from "@/components/3d/elements/robot/modal/OutputConnectionModal.svelte";
	import ManualControlSheet from "@/components/3d/elements/robot/modal/ManualControlSheet.svelte";
	import type { Robot } from "$lib/elements/robot/Robot.svelte.js";
	import { generateName } from "$lib/utils/generateName";
	import RobotGridItem from "@/components/3d/elements/robot/RobotGridItem.svelte";

	interface Props {
		workspaceId: string;
	}
	let { workspaceId }: Props = $props();

	let isInputModalOpen = $state(false);
	let isOutputModalOpen = $state(false);
	let isManualControlSheetOpen = $state(false);
	let selectedRobot = $state<Robot | null>(null);

	function onInputBoxClick(robot: Robot) {
		selectedRobot = robot;
		isInputModalOpen = true;
	}

	function onRobotBoxClick(robot: Robot) {
		selectedRobot = robot;
		isManualControlSheetOpen = true;
	}

	function onOutputBoxClick(robot: Robot) {
		selectedRobot = robot;
		isOutputModalOpen = true;
	}

	onMount(async () => {
		async function createRobot() {
			try {
				const robotId = generateName();
				await robotManager.createSO100Robot(robotId, {
					x: 0,
					y: 0,
					z: 0
				});
			} catch (error) {
				console.error("Failed to create robot:", error);
			}
		}

		if (robotManager.robots.length === 0) {
			await createRobot();
		}
	});

	onDestroy(() => {
		// Clean up robots and unlock servos for safety
		console.log("🧹 Cleaning up robots and unlocking servos...");
		robotManager
			.destroy()
			.then(() => {
				console.log("✅ Cleanup completed successfully");
			})
			.catch((error) => {
				console.error("❌ Error during cleanup:", error);
			});
	});
</script>

{#each robotManager.robots as robot (robot.id)}
	<RobotGridItem
		{robot}
		onCameraMove={() => {}}
		{onInputBoxClick}
		{onRobotBoxClick}
		{onOutputBoxClick}
	/>
{/each}

<!-- Connection Modals -->
{#if selectedRobot}
	<InputConnectionModal bind:open={isInputModalOpen} robot={selectedRobot} {workspaceId} />
	<OutputConnectionModal bind:open={isOutputModalOpen} robot={selectedRobot} {workspaceId} />
	<ManualControlSheet bind:open={isManualControlSheetOpen} robot={selectedRobot} {workspaceId} />
{/if}