robo starter

This commit is contained in:
miqlangelo 2025-05-07 09:29:33 +02:00
parent ff88494585
commit 47efcf207a
6 changed files with 86 additions and 0 deletions

View File

@ -0,0 +1,11 @@
{
"tasks": {
"start": "deno run --allow-net src/server.ts",
"robo": "deno run --allow-net src/cli.ts",
"light-on": "deno run --allow-net src/cli.ts light on",
"move": "deno run --allow-net src/cli.ts move forward 50 3"
},
"imports": {
"@std/assert": "jsr:@std/assert@1"
}
}

View File

@ -0,0 +1,12 @@
{
"version": "4",
"remote": {
"https://deno.land/std@0.224.0/async/delay.ts": "f90dd685b97c2f142b8069082993e437b1602b8e2561134827eeb7c12b95c499",
"https://deno.land/std@0.224.0/http/server.ts": "f9313804bf6467a1704f45f76cb6cd0a3396a3b31c316035e6a4c2035d1ea514"
},
"workspace": {
"dependencies": [
"jsr:@std/assert@1"
]
}
}

View File

@ -0,0 +1,21 @@
const args = Deno.args;
const command = args[0];
async function sendRequest(
endpoint: string,
method: string = 'GET',
body?: object
) {
return await Promise.resolve();
}
if (command === 'horn') sendRequest('/horn/beep');
else if (command === 'light')
sendRequest('/light', 'POST', { led_0: { red: 0, green: 255, blue: 0 } });
else if (command === 'move')
sendRequest('/move', 'POST', {
motorLeft: { direction: args[1], speed: args[2] },
motorRight: { direction: args[1], speed: args[2] },
durationInSeconds: args[3],
});
else console.log('Unknown command');

View File

@ -0,0 +1,19 @@
export function handleBeep(): Response {
console.log('Beep!');
return new Response('Beep!', { status: 200 });
}
export function handleStatusLight(action: string): Response {
console.log(`Status light action: ${action}`);
return new Response(`Status light ${action}`, { status: 200 });
}
export function handleLight(body: any): Response {
console.log('Set light: ', body);
return new Response('Light set', { status: 200 });
}
export function handleMove(body: any): Response {
console.log('Move command: ', body);
return new Response('Moving', { status: 200 });
}

View File

@ -0,0 +1,19 @@
import {
handleBeep,
handleLight,
handleMove,
handleStatusLight,
} from './robot.ts';
export async function handleRequest(req: Request): Promise<Response> {
const url = new URL(req.url);
if (req.method === 'GET') {
if (url.pathname === '/horn/beep') return handleBeep();
if (url.pathname.startsWith('/statuslight'))
return handleStatusLight(url.pathname);
} else if (req.method === 'POST') {
if (url.pathname === '/light') return handleLight(await req.json());
if (url.pathname === '/move') return handleMove(await req.json());
}
return new Response('Not Found', { status: 404 });
}

View File

@ -0,0 +1,4 @@
import { handleRequest } from './routes.ts';
console.log('Server running on http://localhost:8000');
Deno.serve({ port: 8000 }, handleRequest);