UNPKG

homebridge-smartsystem

Version:

SmartServer (Proxy Websockets to TCP sockets, Smappee MQTT, Duotecno IP Nodes, Homekit interface)

106 lines 4.16 kB
"use strict"; /////////////////// // Global Config // /////////////////// Object.defineProperty(exports, "__esModule", { value: true }); exports.setAPIConfig = exports.getAPIConfig = exports.setSubConfig = exports.getSubConfig = exports.getConfig = exports.setConfig = exports.kConfigFiles = exports.isMac = exports.setToken = void 0; const api_1 = require("./api"); const types_1 = require("./types"); const logger_1 = require("./logger"); const fs = require("fs"); const os_1 = require("os"); let gToken = ""; function setToken(token) { gToken = token; } exports.setToken = setToken; exports.isMac = ((0, os_1.platform)() === "darwin"); exports.kConfigFiles = (exports.isMac) ? (__dirname + "/..") : ((0, os_1.homedir)()); if (!fs.existsSync(exports.kConfigFiles)) fs.mkdirSync(exports.kConfigFiles); // set up empty config let gPlatformConfig = types_1.Sanitizers.platform(null); add("smartapp"); // webserver interface add("settings"); // Pi settings add("system"); // Duotecno system add("server"); // web socket add("smappee"); // power-smappee add("p1"); // power-p1 // add("shelly"); // power-shelly function add(type, cfg = null) { gPlatformConfig[type] = types_1.Sanitizers[type] ? types_1.Sanitizers[type](cfg) : cfg; } // getter and setter function setConfig(cfg) { gPlatformConfig = types_1.Sanitizers.platform(cfg); } exports.setConfig = setConfig; function getConfig() { return gPlatformConfig; } exports.getConfig = getConfig; function getSubConfig(type) { const cfg = gPlatformConfig[type]; return types_1.Sanitizers[type] ? types_1.Sanitizers[type](cfg) : cfg; } exports.getSubConfig = getSubConfig; function setSubConfig(type, cfg) { gPlatformConfig[type] = types_1.Sanitizers[type] ? types_1.Sanitizers[type](cfg) : cfg; } exports.setSubConfig = setSubConfig; // we should get the config through the homebridge platform init call !! // no need to call this function. function getAPIConfig(done) { let config = null; if (gPlatformConfig.smartapp.apiHost) { const headers = { "Content-Type": "application/json" }; if (gToken) { headers["Authorization"] = "Bearer " + gToken; } (0, api_1.get)(gPlatformConfig.smartapp.apiHost, gPlatformConfig.smartapp.apiPort, "/api/config-editor/plugin/homebridge-smartsystem", headers).then((resp) => { (0, logger_1.debug)("smartapp", "OK: " + resp); const parsed = JSON.parse(resp); setConfig(parsed[0]); if (done) done(parsed[0]); }, (err) => { (0, logger_1.debug)("smartapp", "ERROR: " + JSON.stringify(err)); done(null); }); } else { const fn = exports.kConfigFiles + "/config.json"; try { const configBuf = fs.readFileSync(fn); const configStr = configBuf.toString(); config = JSON.parse(configStr); } catch (err) { (0, logger_1.log)("system", "Couldn't read my config file (" + fn + ") -- Creating empty config."); } // sanitize the config const sanitized = types_1.Sanitizers.platform(config); setConfig(sanitized); done(sanitized); } } exports.getAPIConfig = getAPIConfig; function setAPIConfig() { if (gPlatformConfig.smartapp.apiHost) { const headers = { "Content-Type": "application/json" }; if (gToken) { headers["Authorization"] = "Bearer " + gToken; } (0, api_1.post)(gPlatformConfig.smartapp.apiHost, gPlatformConfig.smartapp.apiPort, "/api/config-editor/plugin/homebridge-smartsystem", [gPlatformConfig], headers).then((resp) => (0, logger_1.debug)("smartapp", "OK: " + resp), (err) => (0, logger_1.debug)("smartapp", "ERROR: " + JSON.stringify(err))); } // still write the config files const fn = exports.kConfigFiles + "/config.json"; try { fs.writeFileSync(fn, JSON.stringify(gPlatformConfig, null, 2)); } catch (error) { (0, logger_1.err)("config", "Couldn't write config.json file (" + fn + ")"); } } exports.setAPIConfig = setAPIConfig; //# sourceMappingURL=config.js.map