Valetudo/backend/lib/webserver/capabilityRouters/SimpleToggleCapabilityRoute...

38 lines
1.1 KiB
JavaScript

const CapabilityRouter = require("./CapabilityRouter");
class SimpleToggleCapabilityRouter extends CapabilityRouter {
initRoutes() {
this.router.get("/", async (req, res) => {
try {
res.json({
enabled: await this.capability.isEnabled()
});
} catch (e) {
this.sendErrorResponse(req, res, e);
}
});
this.router.put("/", this.validator, async (req, res) => {
try {
switch (req.body.action) {
case "enable":
await this.capability.enable();
break;
case "disable":
await this.capability.disable();
break;
default:
// noinspection ExceptionCaughtLocallyJS
throw new Error("Invalid action");
}
res.sendStatus(200);
} catch (e) {
this.sendErrorResponse(req, res, e);
}
});
}
}
module.exports = SimpleToggleCapabilityRouter;