Ready for wsocket
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
import { Request, Response } from "express";
|
||||
import { board } from "../setup";
|
||||
|
||||
export interface ColorChannel {
|
||||
r: string,
|
||||
g: string,
|
||||
b: string
|
||||
}
|
||||
|
||||
export async function readLed (req: Request, res: Response) {
|
||||
const { p } = req.params;
|
||||
const pin = Number.parseInt(p);
|
||||
@@ -36,14 +42,14 @@ export async function writeLed (req: Request, res: Response) {
|
||||
switch (act) {
|
||||
case 'on':
|
||||
board.digitalWrite(pin, board.HIGH);
|
||||
console.log(`${req.hostname} | ${pin} | ${act.toLocaleUpperCase()}`);
|
||||
console.log(`${req.hostname} | ${pin} | LED: ${act.toLocaleUpperCase()}`);
|
||||
break;
|
||||
case 'off':
|
||||
board.digitalWrite(pin, board.LOW);
|
||||
console.log(`${req.hostname} | ${pin} | ${act.toLocaleUpperCase()}`);
|
||||
console.log(`${req.hostname} | ${pin} | LED: ${act.toLocaleUpperCase()}`);
|
||||
break;
|
||||
default:
|
||||
console.log(`${req.hostname} | ${pin} | INVALID ACT`);
|
||||
console.log(`${req.hostname} | ${pin} | LED: INVALID ACT`);
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
|
||||
15
src/index.ts
15
src/index.ts
@@ -1,13 +1,21 @@
|
||||
import http from 'node:http';
|
||||
import express, { Request, Response } from 'express';
|
||||
import { board, comport } from './setup';
|
||||
import { Server } from 'socket.io';
|
||||
import chalk from 'chalk';
|
||||
|
||||
import { board, suBoard, comport } from './setup';
|
||||
|
||||
import view from './routes/view';
|
||||
import api from './routes/api';
|
||||
|
||||
const app = express();
|
||||
const server = http.createServer(app);
|
||||
const io = new Server(server); // I have no experience at WebSocket, so.. forgive me :)
|
||||
|
||||
const host = 'localhost';
|
||||
const port = 3000;
|
||||
|
||||
app.use(express.json());
|
||||
app.use(express.static('client'));
|
||||
|
||||
app.use('/', view);
|
||||
@@ -16,9 +24,10 @@ app.use('/api-arduino', api);
|
||||
app.listen(port, host, () => {
|
||||
console.log(`Server is running in ${host} at port ${port} 🗣️🗣️🗣️`);
|
||||
console.log(`URL: http://${host}:${port}/\n`);
|
||||
console.log("Connecting to Board");
|
||||
console.log(chalk.yellow(`Connecting to Board`));
|
||||
|
||||
board.on('ready', () => {
|
||||
console.log(`Board at port ${comport} Connected!! \(^o^)/`);
|
||||
console.log(chalk.green(`Board at port ${comport} Connected!! \(^o^)/`));
|
||||
suBoard.connected = true;
|
||||
})
|
||||
});
|
||||
14
src/middleware/connection.ts
Normal file
14
src/middleware/connection.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { suBoard } from "../setup";
|
||||
|
||||
export function isBoardConnected (req: Request, res: Response, next: NextFunction) {
|
||||
if (suBoard.connected) {
|
||||
next();
|
||||
}
|
||||
else {
|
||||
return res.status(500).json({
|
||||
status: 500,
|
||||
message: "Internal server error: No Boards connected"
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,15 @@
|
||||
import { Response } from "express";
|
||||
import { Router } from "express";
|
||||
import { readLed, writeLed } from "../controller/led";
|
||||
import { isBoardConnected } from "../middleware/connection";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get('/hello', (req, res: Response) => {
|
||||
res.send("Hello");
|
||||
res.status(200).send("Hello");
|
||||
})
|
||||
|
||||
router.get('/led/:p', readLed);
|
||||
router.patch('/led/:p/:a', writeLed);
|
||||
router.get('/led/:p', isBoardConnected, readLed);
|
||||
router.patch('/led/:p/:a', isBoardConnected, writeLed);
|
||||
|
||||
export default router;
|
||||
@@ -1,13 +1,16 @@
|
||||
import Firmata from 'firmata';
|
||||
|
||||
export const comport = '/dev/ttyUSB0';
|
||||
export const board = new Firmata(comport);
|
||||
export const comport: string = '/dev/ttyUSB0';
|
||||
export const board: Firmata = new Firmata(comport);
|
||||
|
||||
export const PIN = {
|
||||
servo: 0,
|
||||
rgb_led: {
|
||||
r: 10,
|
||||
g: 9,
|
||||
b: 8
|
||||
export const suBoard = {
|
||||
connected: false,
|
||||
PIN: {
|
||||
servo: 0,
|
||||
rgb_led: {
|
||||
r: 10,
|
||||
g: 9,
|
||||
b: 8
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user