UNPKG

homebridge-smartsystem

Version:

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

126 lines (99 loc) 3.48 kB
/////////////////// // Global Config // /////////////////// import { get, post } from "./api"; import { PlatformConfig, Sanitizers } from "./types"; import { debug, err, log } from "./logger"; import * as fs from "fs"; import { homedir, platform } from "os"; let gToken = ""; export function setToken(token: string) { gToken = token; } export const isMac = (platform() === "darwin"); export const kConfigFiles = (isMac) ? (__dirname+"/..") : (homedir()); if (! fs.existsSync(kConfigFiles)) fs.mkdirSync(kConfigFiles); // set up empty config let gPlatformConfig: PlatformConfig = 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: string, cfg = null) { gPlatformConfig[type] = Sanitizers[type] ? Sanitizers[type](cfg) : cfg; } // getter and setter export function setConfig(cfg: PlatformConfig) { gPlatformConfig = Sanitizers.platform(cfg); } export function getConfig(): PlatformConfig { return gPlatformConfig; } export function getSubConfig(type: string): object { const cfg = gPlatformConfig[type]; return Sanitizers[type] ? Sanitizers[type](cfg) : cfg; } export function setSubConfig(type: string, cfg: object) { gPlatformConfig[type] = Sanitizers[type] ? Sanitizers[type](cfg) : cfg; } // we should get the config through the homebridge platform init call !! // no need to call this function. export function getAPIConfig(done) { let config = null; if (gPlatformConfig.smartapp.apiHost) { const headers = {"Content-Type": "application/json"}; if (gToken) { headers["Authorization"] = "Bearer "+ gToken; } get(gPlatformConfig.smartapp.apiHost, gPlatformConfig.smartapp.apiPort, "/api/config-editor/plugin/homebridge-smartsystem", headers).then( (resp) => { debug("smartapp", "OK: " + resp); const parsed = JSON.parse(resp); setConfig(parsed[0]); if (done) done(parsed[0]); }, (err) => { debug("smartapp", "ERROR: " + JSON.stringify(err)); done(null); } ); } else { const fn = kConfigFiles + "/config.json"; try { const configBuf = fs.readFileSync(fn); const configStr = configBuf.toString(); config = JSON.parse(configStr); } catch(err) { log("system", "Couldn't read my config file (" + fn + ") -- Creating empty config.") } // sanitize the config const sanitized = Sanitizers.platform(config); setConfig(sanitized); done(sanitized); } } export function setAPIConfig() { if (gPlatformConfig.smartapp.apiHost) { const headers = {"Content-Type": "application/json"}; if (gToken) { headers["Authorization"] = "Bearer "+ gToken; } post(gPlatformConfig.smartapp.apiHost, gPlatformConfig.smartapp.apiPort, "/api/config-editor/plugin/homebridge-smartsystem", [gPlatformConfig], headers).then( (resp) => debug("smartapp", "OK: " + resp), (err) => debug("smartapp", "ERROR: " + JSON.stringify(err)) ); } // still write the config files const fn = kConfigFiles + "/config.json"; try { fs.writeFileSync(fn, JSON.stringify(gPlatformConfig, null, 2)); } catch(error) { err("config", "Couldn't write config.json file (" + fn + ")"); } }