Structured
This commit is contained in:
parent
9b30e597e9
commit
1ca8328e7f
@ -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"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
89
src/index.ts
89
src/index.ts
@ -1,91 +1,24 @@
|
|||||||
import express, { Request, Response } from 'express';
|
import express, { Request, Response } from 'express';
|
||||||
import Firmata from 'firmata';
|
import { board, comport } from './setup';
|
||||||
import promises from './promises';
|
import view from './routes/view';
|
||||||
const app = express();
|
import api from './routes/api';
|
||||||
|
|
||||||
const board = new Firmata('/dev/ttyUSB0');
|
const app = express();
|
||||||
|
|
||||||
const host = 'localhost';
|
const host = 'localhost';
|
||||||
const port = 3000;
|
const port = 3000;
|
||||||
|
|
||||||
const PIN = {
|
|
||||||
servo: 0,
|
|
||||||
rgb_led: {
|
|
||||||
r: 10,
|
|
||||||
g: 9,
|
|
||||||
b: 8
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
app.use(express.static('client'));
|
app.use(express.static('client'));
|
||||||
|
|
||||||
app.get('/', (req: Request, res: Response) => {
|
app.use('/', view);
|
||||||
res.sendFile('./client/index.html', { root: __dirname });
|
app.use('/api-arduino', api);
|
||||||
})
|
|
||||||
|
|
||||||
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.listen(port, host, () => {
|
app.listen(port, host, () => {
|
||||||
console.log(`Server is running in ${host} at port ${port}`);
|
console.log(`Server is running in ${host} at port ${port} 🗣️🗣️🗣️`);
|
||||||
console.log("Connecting Board");
|
console.log(`URL: http://${host}:${port}/\n`);
|
||||||
|
console.log("Connecting to Board");
|
||||||
|
|
||||||
board.on('ready', () => {
|
board.on('ready', () => {
|
||||||
console.log(`Board ${board.ports[0]} Connected`);
|
console.log(`Board at port ${comport} Connected!! \(^o^)/`);
|
||||||
})
|
})
|
||||||
});
|
});
|
@ -1,2 +1,14 @@
|
|||||||
|
import { Response } from "express";
|
||||||
import { Router } 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;
|
9
src/routes/view.ts
Normal file
9
src/routes/view.ts
Normal file
@ -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;
|
13
src/setup/index.ts
Normal file
13
src/setup/index.ts
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user