From dba64d5bd79cff286f81da6b9e1091f5230b3f77 Mon Sep 17 00:00:00 2001 From: mmcwilliams Date: Sun, 21 Jan 2018 18:53:15 -0500 Subject: [PATCH] Settings, database and main.js --- app/lib/database/index.js | 3 ++- app/lib/settings/index.js | 35 ++++++++++++++++++++++++----------- app/main.js | 8 ++++---- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/app/lib/database/index.js b/app/lib/database/index.js index cd5135a..6e8a29e 100644 --- a/app/lib/database/index.js +++ b/app/lib/database/index.js @@ -14,7 +14,8 @@ const actionTable = `CREATE TABLE IF NOT EXISTS actions ( counter INTEGER, light TEXT, dir INTEGER, - sequence INTEGER + sequence INTEGER, + device TEXT );` var checkDir = function () { diff --git a/app/lib/settings/index.js b/app/lib/settings/index.js index 5e3b06e..78b1256 100644 --- a/app/lib/settings/index.js +++ b/app/lib/settings/index.js @@ -1,5 +1,3 @@ -'use strict' - const os = require('os'); const path = require('path'); const fs = require('fs'); @@ -11,46 +9,61 @@ settings.state = { port : 1111, enabled : true }, + devices : [], camera : {}, projector : {}, light : {} } settings.checkDir = function () { - const dir = path.join(os.homedir(), '.mcopy/') + 'use strict' + const dir = path.join(os.homedir(), '.mcopy/'); if (!fs.existsSync(dir)) { fs.mkdirSync(dir); } } settings.save = function () { + 'use strict' const str = JSON.stringify(settings.state, null, '\t'); - settings.checkDir() + settings.checkDir(); fs.writeFile(settings.file, str, 'utf8', (err) => { if (err) console.error(err); }) } settings.update = function (key, val) { - settings.state[key] = val + 'use strict' + settings.state[key] = val; } settings.get = function (key) { - return settings.state[key] + 'use strict' + return settings.state[key]; } settings.all = function () { - return settings.state + 'use strict' + return settings.state; } settings.restore = function () { - let str - settings.checkDir() + 'use strict' + let str; + settings.checkDir(); if (fs.existsSync(settings.file)) { str = fs.readFileSync(settings.file, 'utf8') - settings.state = JSON.parse(str) + settings.state = JSON.parse(str); } else { - settings.save() + settings.save(); } } +settings.reset = function () { + 'use strict' + if (fs.existsSync(settings.file)) { + fs.unlinkSync(settings.file); + } + settings.restore(); +}; + module.exports = settings \ No newline at end of file diff --git a/app/main.js b/app/main.js index 9518221..14fa319 100644 --- a/app/main.js +++ b/app/main.js @@ -31,9 +31,8 @@ let log = {} //console.log(process.version) -//cfg is now hardcoded, should only be modified by developers -//settings is now the source of user editable variables mcopy.cfg = require('./data/cfg.json') +mcopy.settings = {} var enumerateDevices = function (err, devices) { if (err) { @@ -426,8 +425,6 @@ transfer.listen = function () { } var init = function () { - - settings.restore() createWindow() //createMenu() @@ -441,7 +438,10 @@ var init = function () { arduino = require('./lib/arduino')(mcopy.cfg, ee) mscript = require('./lib/mscript') + + settings.restore() + mcopy.settings = settings.all() setTimeout( () => { arduino.enumerate(enumerateDevices)