diff --git a/src/robo-project/backend/.vscode/settings.json b/src/robo-project/backend/.vscode/settings.json new file mode 100644 index 0000000..974a2b4 --- /dev/null +++ b/src/robo-project/backend/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "deno.enable": false +} diff --git a/src/robo-project/backend/package.json b/src/robo-project/backend/package.json new file mode 100644 index 0000000..284d3cb --- /dev/null +++ b/src/robo-project/backend/package.json @@ -0,0 +1,19 @@ +{ + "name": "@demofera/robo-backend", + "version": "1.0.0", + "description": "", + "main": "index.ts", + "scripts": { + "dev": "tsx watch src/index.ts" + }, + "keywords": [], + "author": "miqlangelo", + "dependencies": { + "@hono/node-server": "^1.14.0", + "hono": "^4.7.5" + }, + "devDependencies": { + "tsx": "^4.19.3", + "typescript": "^5.8.2" + } +} diff --git a/src/robo-project/backend/src/index.ts b/src/robo-project/backend/src/index.ts new file mode 100644 index 0000000..20bb85a --- /dev/null +++ b/src/robo-project/backend/src/index.ts @@ -0,0 +1,68 @@ +import { serve } from '@hono/node-server'; +import { Hono } from 'hono'; + +const app = new Hono(); + +let statuslight = 'unknown'; +const hornMessages = [ + 'möp möp', + 'honk honk!', + 'tuuut tuuut!', + '🚗 möööööp!', + 'achtung, hupe aktiviert!', + '🎺 ich bin ein Roboter, der hupen kann!', +]; + +function getRandomHornMessage() { + return hornMessages[Math.floor(Math.random() * hornMessages.length)]; +} + +// GET +app.get('/horn/beep', (c) => + c.json({ status: 200, message: getRandomHornMessage() }) +); +app.get('/statuslight/on', (c) => { + statuslight = 'on'; + return c.json({ status: 200, message: 'Statuslicht wurde eingeschaltet.' }); +}); +app.get('/statuslight/off', (c) => { + statuslight = 'off'; + return c.json({ status: 200, message: 'Statuslicht wurde ausgeschaltet.' }); +}); +app.get('/statuslight/state', (c) => { + return c.json({ + status: 200, + state: statuslight, + message: `Statuslicht ist aktuell: ${statuslight}`, + }); +}); + +// POST +app.post('/light', async (c) => { + const body = await c.req.json(); + return c.json({ + status: 200, + message: 'Lichter aktualisiert.', + received: body, + }); +}); + +app.post('/move', async (c) => { + const body = await c.req.json(); + const left = body.motorLeft?.direction || '?'; + const right = body.motorRight?.direction || '?'; + const duration = body.durationInSeconds || 0; + return c.json({ + status: 200, + message: `Fahre ${left} links und ${right} rechts für ${duration} Sek. 🛞`, + received: body, + }); +}); + +// Fallback +app.all('*', (c) => + c.json({ status: 404, message: 'Endpoint nicht gefunden' }, 404) +); + +serve({ fetch: app.fetch, port: 3000 }); +console.log('🚀 Mockserver läuft auf http://localhost:3000'); diff --git a/src/robo-project/backend/test.http b/src/robo-project/backend/test.http new file mode 100644 index 0000000..bc11cd7 --- /dev/null +++ b/src/robo-project/backend/test.http @@ -0,0 +1 @@ +GET http://localhost:3000/horn/beep \ No newline at end of file diff --git a/src/robo-project/backend/tsconfig.json b/src/robo-project/backend/tsconfig.json new file mode 100644 index 0000000..a776052 --- /dev/null +++ b/src/robo-project/backend/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "target": "ES2022", + "module": "ESNext", + "moduleResolution": "Node", + "strict": true, + "esModuleInterop": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true + }, + "include": ["src"] +} diff --git a/src/robo-project/test.http b/src/robo-project/test.http index f3941d1..a3ab1a8 100644 --- a/src/robo-project/test.http +++ b/src/robo-project/test.http @@ -6,6 +6,12 @@ GET http://robo-backend.innofera.com/horn/beep ### Get Status Light GET http://172.20.10.2/statuslight/state +### Set Status Light on +GET http://172.20.10.2/statuslight/on + +### Set Status Light off +GET http://172.20.10.2/statuslight/off + ### POST POST http://172.20.10.2/move