From 47efcf207a60eaa3b5c5ead9f3a9421c5d03032d Mon Sep 17 00:00:00 2001 From: miqlangelo Date: Wed, 7 May 2025 09:29:33 +0200 Subject: [PATCH] robo starter --- src/robo-project/robot-api-starter/deno.json | 11 ++++++++++ src/robo-project/robot-api-starter/deno.lock | 12 +++++++++++ src/robo-project/robot-api-starter/src/cli.ts | 21 +++++++++++++++++++ .../robot-api-starter/src/robot.ts | 19 +++++++++++++++++ .../robot-api-starter/src/routes.ts | 19 +++++++++++++++++ .../robot-api-starter/src/server.ts | 4 ++++ 6 files changed, 86 insertions(+) create mode 100644 src/robo-project/robot-api-starter/deno.json create mode 100644 src/robo-project/robot-api-starter/deno.lock create mode 100644 src/robo-project/robot-api-starter/src/cli.ts create mode 100644 src/robo-project/robot-api-starter/src/robot.ts create mode 100644 src/robo-project/robot-api-starter/src/routes.ts create mode 100644 src/robo-project/robot-api-starter/src/server.ts diff --git a/src/robo-project/robot-api-starter/deno.json b/src/robo-project/robot-api-starter/deno.json new file mode 100644 index 0000000..e84e570 --- /dev/null +++ b/src/robo-project/robot-api-starter/deno.json @@ -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" + } +} \ No newline at end of file diff --git a/src/robo-project/robot-api-starter/deno.lock b/src/robo-project/robot-api-starter/deno.lock new file mode 100644 index 0000000..22a1aa6 --- /dev/null +++ b/src/robo-project/robot-api-starter/deno.lock @@ -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" + ] + } +} diff --git a/src/robo-project/robot-api-starter/src/cli.ts b/src/robo-project/robot-api-starter/src/cli.ts new file mode 100644 index 0000000..d4ff396 --- /dev/null +++ b/src/robo-project/robot-api-starter/src/cli.ts @@ -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'); diff --git a/src/robo-project/robot-api-starter/src/robot.ts b/src/robo-project/robot-api-starter/src/robot.ts new file mode 100644 index 0000000..38592c6 --- /dev/null +++ b/src/robo-project/robot-api-starter/src/robot.ts @@ -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 }); +} diff --git a/src/robo-project/robot-api-starter/src/routes.ts b/src/robo-project/robot-api-starter/src/routes.ts new file mode 100644 index 0000000..223ce90 --- /dev/null +++ b/src/robo-project/robot-api-starter/src/routes.ts @@ -0,0 +1,19 @@ +import { + handleBeep, + handleLight, + handleMove, + handleStatusLight, +} from './robot.ts'; + +export async function handleRequest(req: Request): Promise { + 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 }); +} diff --git a/src/robo-project/robot-api-starter/src/server.ts b/src/robo-project/robot-api-starter/src/server.ts new file mode 100644 index 0000000..c284c5d --- /dev/null +++ b/src/robo-project/robot-api-starter/src/server.ts @@ -0,0 +1,4 @@ +import { handleRequest } from './routes.ts'; + +console.log('Server running on http://localhost:8000'); +Deno.serve({ port: 8000 }, handleRequest);