diff --git a/server/package-lock.json b/server/package-lock.json index 4e35cb4a308af9e08d988b33f585542ac881e054..73654863ecd4bd4f0745e1c438be56082276bf99 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -940,6 +940,19 @@ "has-flag": "^3.0.0" } }, + "swagger-ui-dist": { + "version": "3.23.10", + "resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-3.23.10.tgz", + "integrity": "sha512-lK9oNNb9HAz2HJIg6+cYZszbYC/0CTm0nWi0XX2AjEbVgu43smbW2/h5zn7yAjYstPJBaYXkt5g6ABRpO/Ncfg==" + }, + "swagger-ui-express": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/swagger-ui-express/-/swagger-ui-express-4.1.0.tgz", + "integrity": "sha512-sCP7SxIfWr8DAK46RZXaplJIe+1h0cOFIrUw+Jx8vSstlppe7sNR8mFOyDlBwVsqAx6K2BAhKnK88Hkkj28pqw==", + "requires": { + "swagger-ui-dist": "^3.18.1" + } + }, "toidentifier": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz", diff --git a/server/package.json b/server/package.json index 629b88711715d82700ebf05f015a460943ecd2a4..9de0253a97faf55ec370ebae38adab903656c0c2 100644 --- a/server/package.json +++ b/server/package.json @@ -32,7 +32,8 @@ "cors": "^2.8.4", "dotenv": "^6.0.0", "express": "^4.16.3", - "helmet": "^3.13.0" + "helmet": "^3.13.0", + "swagger-ui-express": "^4.1.0" }, "optionalDependencies": { "node-dht-sensor": "~0.0.36", diff --git a/server/src/controllers/actuator.controller.ts b/server/src/controllers/actuator.controller.ts index 04137f5170cb630fb072ada071a3b94cbbf21c6f..5d87985ef3506e13232a6d6f62b84ad625c63f20 100644 --- a/server/src/controllers/actuator.controller.ts +++ b/server/src/controllers/actuator.controller.ts @@ -1,5 +1,7 @@ import { Request, Response, NextFunction } from "express"; -import { setLEDservice, getLEDservice } from "../services/gpio.service"; +import { setLEDservice, getLEDservice, getAllLEDsservice } from "../services/gpio.service"; + +// const ledmapping = {"0" : "4", "1" : "2", "3": "22"} export function setLED(req: Request, res: Response, next: NextFunction) { @@ -8,12 +10,16 @@ export function setLED(req: Request, res: Response, next: NextFunction) { const id: string = req.params.id; const value = req.body["value"]; - - if(id != "4" || id != "2" || id != "22"){ - res.status(401).json({"msg" :"LED does not exist"}) + if (id == null) { + res.status(401).json({ "msg": "wrong id" }) + return + } + + if (id != "4" && id != "2" && id != "22") { + res.status(401).json({ "msg": "LED does not exist" }) } - if(value == null || !(value === 0 || value === 1)){ - res.status(401).json({"msg" : "invalid input"}) + if (value == null || !(value === 0 || value === 1)) { + res.status(401).json({ "msg": "invalid input" }) } setLEDservice(id, value); @@ -32,4 +38,8 @@ export function getLED(req: Request, res: Response, next: NextFunction) { else { res.status(200).json({ "led": "off" }); } +} + +export function getAllLEDs(req: Request, res: Response, next: NextFunction) { + res.status(200).json({ "leds": getAllLEDsservice() }) } \ No newline at end of file diff --git a/server/src/routes/actuator.route.ts b/server/src/routes/actuator.route.ts index f33cc880075edc2e0171c55fa76f274597d47ef0..c25b45ce1b9d890a2170c63bb7a0ef61a5f2fc48 100644 --- a/server/src/routes/actuator.route.ts +++ b/server/src/routes/actuator.route.ts @@ -1,8 +1,13 @@ import { Router } from "express"; -import { setLED,getLED } from "../controllers/actuator.controller"; +import { setLED,getLED, getAllLEDs } from "../controllers/actuator.controller"; const router = Router(); + +router + .route("/led") + .get(getAllLEDs) + router .route("/led/:id") .get(getLED) diff --git a/server/src/services/gpio.service.ts b/server/src/services/gpio.service.ts index d793e18eec2eb2a257798819e87e33d8569a15a3..69f539dc16771620d315c4207621d37728bebb35 100644 --- a/server/src/services/gpio.service.ts +++ b/server/src/services/gpio.service.ts @@ -10,7 +10,7 @@ try { } // The number of microseconds it takes sound to travel 1cm at 20 degrees celcius -const MICROSECDONDS_PER_CM = 1e6/34321; +const MICROSECDONDS_PER_CM = 1e6 / 34321; var trigger = null; var echo = null; var led = null; @@ -38,18 +38,18 @@ function isProduction() { // } // }); // }); - + // return promise; // }; export function getSingleHCSR04Value(): Promise<any> { const promise = new Promise((resolve, reject) => { - trigger = new Gpio(23, {mode: Gpio.OUTPUT}); - echo = new Gpio(24, {mode: Gpio.INPUT, alert: true}); + trigger = new Gpio(23, { mode: Gpio.OUTPUT }); + echo = new Gpio(24, { mode: Gpio.INPUT, alert: true }); trigger.digitalWrite(0); // Make sure trigger is low - + let startTick; - + echo.on('alert', (level, tick) => { if (level == 1) { startTick = tick; @@ -73,10 +73,10 @@ export function getSingleHCSR04Value(): Promise<any> { export function initialize() { if (isProduction()) { - sensorLib.initialize(11,12); + sensorLib.initialize(11, 12); - trigger = new Gpio(23, {mode: Gpio.OUTPUT}); - echo = new Gpio(24, {mode: Gpio.INPUT, alert: true}); + trigger = new Gpio(23, { mode: Gpio.OUTPUT }); + echo = new Gpio(24, { mode: Gpio.INPUT, alert: true }); trigger.digitalWrite(0); // Make sure trigger is low } @@ -86,11 +86,11 @@ export function retrieveDhtValue() { if (isProduction()) { return sensorLib.read(); } - return {"humidity":63,"temperature":22,"isValid":true,"errors":0}; + return { "humidity": 63, "temperature": 22, "isValid": true, "errors": 0 }; } export function retrieveUltrasonicValue(): Promise<any> { - if (isProduction()){ + if (isProduction()) { return getSingleHCSR04Value(); } return Promise.resolve(216.5); @@ -111,5 +111,14 @@ export function getLEDservice(lednr) { return led.digitalRead() } - return 0; + return 1; +} + +export function getAllLEDsservice() { + + let tmp = [{ "value": getLEDservice("2"), "id": "2" }, + { "value": getLEDservice("4"), "id": "4" }, + { "value": getLEDservice("22"), "id": "22" }] + + return tmp; } \ No newline at end of file