From 1ca8328e7f6ed81bac20b1a1d665ad90a8d28319 Mon Sep 17 00:00:00 2001 From: norman-andrians Date: Fri, 1 Mar 2024 19:28:20 +0700 Subject: [PATCH] Structured --- src/controller/led.ts | 61 +++++++++++++++++++++++++++++ src/index.ts | 89 ++++++------------------------------------- src/routes/api.ts | 12 ++++++ src/routes/client.ts | 0 src/routes/view.ts | 9 +++++ src/setup/index.ts | 13 +++++++ 6 files changed, 106 insertions(+), 78 deletions(-) delete mode 100644 src/routes/client.ts create mode 100644 src/routes/view.ts create mode 100644 src/setup/index.ts diff --git a/src/controller/led.ts b/src/controller/led.ts index e69de29..b7707b7 100644 --- a/src/controller/led.ts +++ b/src/controller/led.ts @@ -0,0 +1,61 @@ +import { Request, Response } from "express"; +import { board } from "../setup"; + +export async function readLed (req: Request, res: Response) { + const { p } = req.params; + const pin = Number.parseInt(p); + + if (Number.isNaN(pin)) { + return res.status(400).json({ + status: 400, + message: 'Invalid pin param, it should be integer' + }); + } + + const pinState = board.pins[pin].value == 1 ? 'ON' : 'OFF'; + + return res.status(200).json({ + status: 200, + message: `Pin ${pin} state ${pinState}` + }); +} + +export async function writeLed (req: Request, res: Response) { + const { p, a } = req.params; + const act = a.toLocaleLowerCase(); + const pin = Number.parseInt(p); + + try { + if (Number.isNaN(pin)) { + return res.status(400).json({ + status: 400, + message: 'Invalid pin param, it should be integer' + }); + } + + switch (act) { + case 'on': + board.digitalWrite(pin, board.HIGH); + console.log(`${req.hostname} | ${pin} | ${act.toLocaleUpperCase()}`); + break; + case 'off': + board.digitalWrite(pin, board.LOW); + console.log(`${req.hostname} | ${pin} | ${act.toLocaleUpperCase()}`); + break; + default: + console.log(`${req.hostname} | ${pin} | INVALID ACT`); + } + + res.status(200).json({ + status: 200, + message: `Success changed pin ${pin} to state ${act}` + }); + } + catch (err) { + console.error(err); + res.status(500).json({ + status: 500, + message: "Internal Server Error" + }); + } +}; \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 8da09a4..811615c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,91 +1,24 @@ import express, { Request, Response } from 'express'; -import Firmata from 'firmata'; -import promises from './promises'; -const app = express(); +import { board, comport } from './setup'; +import view from './routes/view'; +import api from './routes/api'; -const board = new Firmata('/dev/ttyUSB0'); +const app = express(); const host = 'localhost'; const port = 3000; -const PIN = { - servo: 0, - rgb_led: { - r: 10, - g: 9, - b: 8 - } -} - app.use(express.static('client')); -app.get('/', (req: Request, res: Response) => { - res.sendFile('./client/index.html', { root: __dirname }); -}) - -app.get('/api-arduino/led/:p', async (req: Request, res: Response) => { - const { p } = req.params; - const pin = Number.parseInt(p); - - if (Number.isNaN(pin)) { - return res.status(400).json({ - status: 400, - message: 'Invalid pin param, it should be integer' - }); - } - - const pinState = board.pins[pin].value; - - return res.status(200).json({ - status: 200, - message: `Pin ${pin} state ${pinState}` - }); -}) - -app.patch('/api-arduino/led/:p/:a', (req: Request, res: Response) => { - const { p, a } = req.params; - const act = a.toLocaleLowerCase(); - const pin = Number.parseInt(p); - - try { - if (Number.isNaN(pin)) { - return res.status(400).json({ - status: 400, - message: 'Invalid pin param, it should be integer' - }); - } - - switch (act) { - case 'on': - board.digitalWrite(pin, board.HIGH); - console.log(`${req.hostname} | ${pin} | ${act.toLocaleUpperCase()}`); - break; - case 'off': - board.digitalWrite(pin, board.LOW); - console.log(`${req.hostname} | ${pin} | ${act.toLocaleUpperCase()}`); - break; - default: - console.log(`${req.hostname} | ${pin} | INVALID ACT`); - } - - res.status(200).json({ - status: 200, - message: `Success changed pin ${pin} to state ${act}` - }); - } - catch (err) { - console.error(err); - res.status(500).json({ - status: 500, - message: "Internal Server Error" - }); - } -}); +app.use('/', view); +app.use('/api-arduino', api); app.listen(port, host, () => { - console.log(`Server is running in ${host} at port ${port}`); - console.log("Connecting Board"); + console.log(`Server is running in ${host} at port ${port} 🗣️🗣️🗣️`); + console.log(`URL: http://${host}:${port}/\n`); + console.log("Connecting to Board"); + board.on('ready', () => { - console.log(`Board ${board.ports[0]} Connected`); + console.log(`Board at port ${comport} Connected!! \⁠(⁠^⁠o⁠^⁠)⁠/`); }) }); \ No newline at end of file diff --git a/src/routes/api.ts b/src/routes/api.ts index b693c67..f35e2fa 100644 --- a/src/routes/api.ts +++ b/src/routes/api.ts @@ -1,2 +1,14 @@ +import { Response } from "express"; import { Router } from "express"; +import { readLed, writeLed } from "../controller/led"; +const router = Router(); + +router.get('/hello', (req, res: Response) => { + res.send("Hello"); +}) + +router.get('/led/:p', readLed); +router.patch('/led/:p/:a', writeLed); + +export default router; \ No newline at end of file diff --git a/src/routes/client.ts b/src/routes/client.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/routes/view.ts b/src/routes/view.ts new file mode 100644 index 0000000..db3e95f --- /dev/null +++ b/src/routes/view.ts @@ -0,0 +1,9 @@ +import { Request, Response, Router } from "express"; + +const router = Router(); + +router.get('/', (req: Request, res: Response) => { + res.sendFile('./client/index.html', { root: __dirname + "../../" }); +}) + +export default router; \ No newline at end of file diff --git a/src/setup/index.ts b/src/setup/index.ts new file mode 100644 index 0000000..62a597b --- /dev/null +++ b/src/setup/index.ts @@ -0,0 +1,13 @@ +import Firmata from 'firmata'; + +export const comport = '/dev/ttyUSB0'; +export const board = new Firmata(comport); + +export const PIN = { + servo: 0, + rgb_led: { + r: 10, + g: 9, + b: 8 + } +} \ No newline at end of file