Skip to content
Snippets Groups Projects
actuator.controller.ts 1.3 KiB
Newer Older
larsH's avatar
larsH committed
import { Request, Response, NextFunction } from "express";
larsH's avatar
larsH committed
import { setLEDservice, getLEDservice, getAllLEDsservice } from "../services/gpio.service";

// const ledmapping = {"0" : "4", "1" : "2", "3": "22"}
larsH's avatar
larsH committed


export function setLED(req: Request, res: Response, next: NextFunction) {

    //Get wanted LED and wished value
    const id: string = req.params.id;
kasperlauge's avatar
kasperlauge committed
    const newValue = req.body["value"];
larsH's avatar
larsH committed

larsH's avatar
larsH committed
    if (id == null) {
kasperlauge's avatar
kasperlauge committed
        res.status(401).json({ "msg": "wrong id" });
        return;
larsH's avatar
larsH committed
    }

    if (id != "4" && id != "2" && id != "22") {
kasperlauge's avatar
kasperlauge committed
        res.status(401).json({ "msg": "LED does not exist" });
        return;
larsH's avatar
larsH committed
    }
kasperlauge's avatar
kasperlauge committed
    if (newValue == null || !(newValue === 0 || newValue === 1)) {
        res.status(401).json({ "msg": "invalid input" });
        return;
larsH's avatar
larsH committed
    }

kasperlauge's avatar
kasperlauge committed
    setLEDservice(id, newValue);
    const value = getLEDservice(id);
    res.status(201).json({id,value});
larsH's avatar
larsH committed
}

export function getLED(req: Request, res: Response, next: NextFunction) {

larsH's avatar
larsH committed
    const id: Number = parseInt(req.params.id);
larsH's avatar
larsH committed

    const value = getLEDservice(id);
    if (value) {
        res.status(200).json({ "led": "on" })
    }
    else {
        res.status(200).json({ "led": "off" });
    }
larsH's avatar
larsH committed
}

export function getAllLEDs(req: Request, res: Response, next: NextFunction) {
    res.status(200).json({ "leds": getAllLEDsservice() })
larsH's avatar
larsH committed
}