UNPKG

twokeys-server

Version:

Server for 2Keys

136 lines (132 loc) 4.99 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /** Copyright 2018 Kishan Sambhi This file is part of 2Keys. 2Keys is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 2Keys is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with 2Keys. If not, see <https://www.gnu.org/licenses/>. */ /** * @overview Main routes of 2Keys server */ const express_1 = require("express"); const fs_1 = require("fs"); const path_1 = require("path"); const yaml_1 = __importDefault(require("yaml")); const config_1 = require("../util/config"); const logger_1 = __importDefault(require("../util/logger")); const interfaces_1 = require("../util/interfaces"); const ahk_1 = require("../util/ahk"); const constants_1 = require("../util/constants"); const logger = new logger_1.default({ name: "api", }); const router = express_1.Router(); /** * Returns the config for the 2Keys project */ router.get("/get/config", (req, res, next) => { logger.debug("Sending a config copy as JSON..."); fs_1.readFile(path_1.join(process.cwd(), "config.yml"), (err, data) => { if (err) { return next(err); } const data_to_send = JSON.stringify(yaml_1.default.parse(data.toString())); res.setHeader("Content-Type", "application/json"); res.statusCode = 200; res.send(data_to_send); }); }); /** * Trigger a hotkey * Info to send: * - keyboard: The keyboard name that has been pressed * - hotkey: set of keys that have been pressed */ router.post("/post/trigger", async (req, res, next) => { /** * 1: Get hotkey function from config * 2: Execute C++ bindings with #Include <root of keyboard>; function() */ // Get vars const keyboard = req.body.keyboard; const hotkey_code = req.body.hotkey; const value = req.body.hasOwnProperty("value") ? req.body.value : interfaces_1.EvDevValues.Down; logger.debug(`Got keyboard ${keyboard} and hotkey ${hotkey_code}, with value ${value}`); // Parse config try { const fetched_hotkey = await ahk_1.fetch_hotkey(keyboard, hotkey_code); // Gets hotkey let func_to_run; // Use the value arg to select if (typeof fetched_hotkey.func === "object") { // Is an object logger.debug("Got a multi event hotkey."); // Select which function to run if (value === interfaces_1.EvDevValues.Down) { func_to_run = fetched_hotkey.func.down; } else if (value === interfaces_1.EvDevValues.Up) { func_to_run = fetched_hotkey.func.up; } else { // Stop exec as and error was encountered return next(new TypeError(`The request keyboard event value of ${value} is invalid. Valid event values are: 0 (Up) & 1 (Down)`)); } // Validate a function actually exists if (typeof func_to_run === "undefined") { // Ignore logger.warn(`Ignoring hotkey ${hotkey_code} of value ${value}, as no function to run exists`); res.statusCode = 404; res.send("Hotkey function not found"); return; } } else { func_to_run = fetched_hotkey.func; } // Execute ahk_1.run_hotkey(fetched_hotkey.file, func_to_run); res.statusCode = 200; res.send("OK"); } catch (err) { next(err); // Hand off to error handler } }); /** * Handles keyboard path update */ router.post("/post/update-keyboard-path", (req, res, next) => { const { keyboard, path } = req.body; logger.info(`Got update for ${keyboard}, path ${path}`); config_1.config_loader() .then((config) => { // Make changes config.keyboards[keyboard].path = path; // Write logger.debug("Writing config..."); fs_1.writeFile(constants_1.CONFIG_FILE, yaml_1.default.stringify(config), (err) => { if (err) { return next(err); } else { res.statusCode = 200; res.send("OK"); } res.end(); }); }); }); exports.default = router; //# sourceMappingURL=api.js.map