Add rooms as 'parallel' in socket handler and add digital analog api
This commit is contained in:
parent
23e35cdd2c
commit
3d1b9bf3ac
47
src/controller/basic/analog.ts
Normal file
47
src/controller/basic/analog.ts
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
import { Request, Response } from "express";
|
||||||
|
import { board } from "../../setup";
|
||||||
|
import * as Promises from "../../promises";
|
||||||
|
|
||||||
|
interface AnalogState {
|
||||||
|
pin: number | string,
|
||||||
|
value: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export function analogWrite (req: Request, res: Response): Response<string | any> {
|
||||||
|
const { pin, value }: AnalogState = req.body;
|
||||||
|
|
||||||
|
try {
|
||||||
|
board.analogWrite(pin, value);
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
status: 200,
|
||||||
|
analog_state: {
|
||||||
|
[pin]: value
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
res.sendStatus(500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function analogRead (req: Request, res: Response) {
|
||||||
|
let { pin } = req.params;
|
||||||
|
pin = pin[0] == "A" ? pin.slice(1, pin.length) : pin;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const value = await Promises.analogRead(board, pin);
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
status: 200,
|
||||||
|
analog_state: {
|
||||||
|
[pin]: value
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
res.sendStatus(500);
|
||||||
|
}
|
||||||
|
}
|
54
src/controller/basic/digital.ts
Normal file
54
src/controller/basic/digital.ts
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
import { Request, Response } from "express";
|
||||||
|
import { board } from "../../setup";
|
||||||
|
|
||||||
|
interface DigitalState {
|
||||||
|
pin: number | string,
|
||||||
|
state: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export function digitalWrite (req: Request, res: Response): Response<string | any> {
|
||||||
|
const { pin, state }: DigitalState = req.body;
|
||||||
|
let value: number;
|
||||||
|
|
||||||
|
try {
|
||||||
|
switch (state) {
|
||||||
|
case 'LOW' || 0: value = 0; break;
|
||||||
|
case 'HIGH' || 1: value = 1; break;
|
||||||
|
default:
|
||||||
|
return res.status(400).json({
|
||||||
|
status: 400,
|
||||||
|
message: `Invalid state ${state}, read the documentation`
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const val = value == 1 ? "HIGH" : "LOW";
|
||||||
|
board.digitalWrite(pin, value);
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
status: 200,
|
||||||
|
pin_state: {
|
||||||
|
[pin]: val
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
res.sendStatus(500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function digitalRead (req: Request, res: Response): Response<string | any> {
|
||||||
|
try {
|
||||||
|
const { pin } = req.params;
|
||||||
|
const value = board.pins[pin].value == 1 ? 'HIGH' : 'LOW';
|
||||||
|
return res.status(200).json({
|
||||||
|
pin_state: {
|
||||||
|
[pin]: value
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
res.sendStatus(500);
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import { board, suBoard } from "../setup";
|
import { board, suBoard } from "../../setup";
|
||||||
import { sPinModes } from ".";
|
import { sPinModes } from "..";
|
||||||
import { Pin } from "johnny-five";
|
import { Pin } from "johnny-five";
|
||||||
|
|
||||||
export function readPin (req: Request, res: Response) {
|
export function readPin (req: Request, res: Response) {
|
@ -1,6 +1,6 @@
|
|||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import { board } from "../setup";
|
import { board } from "../../setup";
|
||||||
import { ChannelPins, digitalValue, voltage } from ".";
|
import { ChannelPins, digitalValue, voltage } from "..";
|
||||||
import { Led } from "johnny-five";
|
import { Led } from "johnny-five";
|
||||||
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
|||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import { board } from "../setup";
|
import { board } from "../../setup";
|
||||||
import { analogRead } from "../promises";
|
import { analogRead } from "../../promises";
|
||||||
|
|
||||||
export async function readResistor (req: Request, res: Response): Promise<Response<string, any>> {
|
export async function readResistor (req: Request, res: Response): Promise<Response<string, any>> {
|
||||||
const { p } = req.params;
|
const { p } = req.params;
|
@ -1,6 +1,6 @@
|
|||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import { Piezo } from "johnny-five";
|
import { Piezo } from "johnny-five";
|
||||||
import { Pitch } from "../melodies";
|
import { Pitch } from "../../melodies";
|
||||||
|
|
||||||
export function piezoTone (req: Request, res: Response): Response {
|
export function piezoTone (req: Request, res: Response): Response {
|
||||||
const pin: number = Number.parseInt(req.params.p);
|
const pin: number = Number.parseInt(req.params.p);
|
@ -1,5 +1,5 @@
|
|||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import { board } from "../setup";
|
import { board } from "../../setup";
|
||||||
|
|
||||||
export function rotateServo (req: Request, res: Response): Response<string, any> {
|
export function rotateServo (req: Request, res: Response): Response<string, any> {
|
||||||
const { p } = req.params;
|
const { p } = req.params;
|
@ -1,23 +1,55 @@
|
|||||||
import { Socket } from "socket.io";
|
import { Socket } from "socket.io";
|
||||||
import { Pin } from "johnny-five";
|
import { Pin, Sensor } from "johnny-five";
|
||||||
import { board } from "../setup";
|
import { board } from "../setup";
|
||||||
|
|
||||||
export default (socket: Socket) => {
|
export default (socket: Socket) => {
|
||||||
console.log(`${socket.id} | ${socket.client.request.headers.host} | Joined`);
|
console.log(`${socket.id} | ${socket.client.request.headers.host} | Joined`);
|
||||||
|
|
||||||
socket.on("servo", (p: string, ang: string) => {
|
socket.on("servo", (p: string, ang: string, cb?: (msg?: string) => void) => {
|
||||||
const pin = Number.parseInt(p);
|
const pin: number = Number.parseInt(p);
|
||||||
const angle = Number.parseInt(ang);
|
const angle: number = Number.parseInt(ang);
|
||||||
|
|
||||||
board.pinMode(pin, Pin.SERVO);
|
board.pinMode(pin, Pin.SERVO);
|
||||||
board.servoWrite(pin, angle);
|
board.servoWrite(pin, angle);
|
||||||
|
|
||||||
|
if (cb) cb(`Set servo pin ${p} to ${angle} degrees`);
|
||||||
console.log(socket.id, pin, angle);
|
console.log(socket.id, pin, angle);
|
||||||
})
|
})
|
||||||
|
|
||||||
socket.on("set-photoresistor", (pin: string) => {
|
socket.on("set-photoresistor", (pin: string, cb?: (msg?: string) => void) => {
|
||||||
board.analogRead(pin, (value) => {
|
const room = `resistor-${pin}`;
|
||||||
socket.emit("photoresistor", value);
|
|
||||||
console.log(socket.id, "A"+pin, value);
|
if (!socket.rooms.has(room)) {
|
||||||
});
|
socket.join(room);
|
||||||
|
|
||||||
|
const sensor = new Sensor({
|
||||||
|
pin: pin,
|
||||||
|
board: board,
|
||||||
|
type: "analog",
|
||||||
|
freq: 250
|
||||||
|
});
|
||||||
|
|
||||||
|
sensor.on("change", () => {
|
||||||
|
socket.to(room).emit("photoresistor", sensor.value);
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(`New room: ${room}`);
|
||||||
|
console.log(`${socket.id} joined room ${room}`);
|
||||||
|
|
||||||
|
if (cb) cb(`Set pin resistor to pin ${pin}`);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (cb) cb(`Resistor pin ${pin} is already used, try to lisen to "photoresistor" room: ${room}`);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
socket.on("join-photoresistor", (pin) => {
|
||||||
|
const room = `resistor-${pin}`;
|
||||||
|
|
||||||
|
if (!socket.rooms.has(room)) {
|
||||||
|
socket.join(room);
|
||||||
|
console.log(socket.rooms);
|
||||||
|
console.log(`${socket.id} Joined room ${room}`);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
@ -1,17 +1,19 @@
|
|||||||
import { Response } from "express";
|
import { Response } from "express";
|
||||||
import { Router } from "express";
|
import { Router } from "express";
|
||||||
|
|
||||||
import { readLed, readRgbLed, writeLed, writeRgbLed } from "../controller/led";
|
import { readPin, setPin } from "../controller/basic/pin";
|
||||||
import { readPin, setPin } from "../controller/pin";
|
import { digitalRead, digitalWrite } from "../controller/basic/digital";
|
||||||
import { piezoNoTone, piezoNote, piezoPlayNotes, piezoTone } from "../controller/piezo";
|
import { analogRead, analogWrite } from "../controller/basic/analog";
|
||||||
import { rotateServo } from "../controller/servo";
|
|
||||||
import { readResistor } from "../controller/photoresistor";
|
import { readLed, readRgbLed, writeLed, writeRgbLed } from "../controller/components/led";
|
||||||
|
import { piezoNoTone, piezoNote, piezoPlayNotes, piezoTone } from "../controller/components/piezo";
|
||||||
|
import { rotateServo } from "../controller/components/servo";
|
||||||
|
import { readResistor } from "../controller/components/photoresistor";
|
||||||
|
|
||||||
import { isPinNumeric } from "../middleware/pin";
|
import { isPinNumeric } from "../middleware/pin";
|
||||||
|
|
||||||
const router: Router = Router();
|
const router: Router = Router();
|
||||||
|
|
||||||
// Client page
|
|
||||||
router.get('/hello', (req, res: Response): Response<string> => {
|
router.get('/hello', (req, res: Response): Response<string> => {
|
||||||
return res.status(200).send("Hello");
|
return res.status(200).send("Hello");
|
||||||
})
|
})
|
||||||
@ -20,6 +22,15 @@ router.get('/hello', (req, res: Response): Response<string> => {
|
|||||||
router.get('/pin/:p', readPin);
|
router.get('/pin/:p', readPin);
|
||||||
router.patch('/pin/:p/:m', setPin);
|
router.patch('/pin/:p/:m', setPin);
|
||||||
|
|
||||||
|
// Digital read/write
|
||||||
|
router.get('/digital/:pin', digitalRead);
|
||||||
|
router.patch('/digital', digitalWrite);
|
||||||
|
|
||||||
|
// Analog read/write
|
||||||
|
router.get('/analog/:pin', analogRead);
|
||||||
|
router.patch('/analog', analogWrite);
|
||||||
|
|
||||||
|
|
||||||
// LED
|
// LED
|
||||||
router.get('/led/:p', isPinNumeric, readLed);
|
router.get('/led/:p', isPinNumeric, readLed);
|
||||||
router.patch('/led/:p/:a', isPinNumeric, writeLed);
|
router.patch('/led/:p/:a', isPinNumeric, writeLed);
|
||||||
@ -35,7 +46,7 @@ router.patch('/piezo/music/', piezoPlayNotes);
|
|||||||
router.patch('/piezo/stop/', piezoNoTone);
|
router.patch('/piezo/stop/', piezoNoTone);
|
||||||
|
|
||||||
// for real-time communication is deprecated and not recommended
|
// for real-time communication is deprecated and not recommended
|
||||||
// use other protocol like websocket instead
|
// use other protocol like websocket instead, we're using socket.io
|
||||||
router.patch('/servo/:p/:m', rotateServo);
|
router.patch('/servo/:p/:m', rotateServo);
|
||||||
router.get('/photoresistor/:p', readResistor);
|
router.get('/photoresistor/:p', readResistor);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user