From 8bb19976946ad53df6cc49d551e602c5d8481fbf Mon Sep 17 00:00:00 2001 From: mmcw-dev Date: Thu, 4 Jan 2018 23:42:16 -0500 Subject: [PATCH] Use a settings file instead of cfg.json. Cfg.json becomes hardcoded. --- app/data/cfg.json.default | 50 ------------------------------------- app/lib/settings-ui.js | 19 -------------- app/lib/settings/index.js | 52 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 69 deletions(-) delete mode 100644 app/data/cfg.json.default delete mode 100644 app/lib/settings-ui.js create mode 100644 app/lib/settings/index.js diff --git a/app/data/cfg.json.default b/app/data/cfg.json.default deleted file mode 100644 index a406ed0..0000000 --- a/app/data/cfg.json.default +++ /dev/null @@ -1,50 +0,0 @@ -{ - "version" : "2.0.0", - "ext_port" : 1111, - "arduino" : { - "baud" : 57600, - "board" : "uno", - "serialDelay" : 20, - "sequenceDelay" : 100, - "cam" : { - "time" : 750, - "delay" : 50, - "momentary" : 300 - }, - "proj" : { - "time" : 1300, - "delay" : 50, - "momentary" : 300 - }, - "black" : { - "before" : 250, - "after" : 250 - }, - "cmd" : { - "debug" : "d", - "connect": "i", - "light" : "l", - - "camera" : "c", - "projector" : "p", - "black" : "b", - - "cam_forward" : "e", - "cam_backward" : "f", - "cam_timed" : "n", - - "proj_forward" : "g", - "proj_backward" : "h", - - "mcopy_identifier" : "m", - - "proj_identifier" : "j", - "cam_identifier" : "k", - "light_identifier" : "o", - - "proj_light_identifier" : "q", - "proj_cam_light_identifier" : "r", - "proj_cam_identifier" : "s" - } - } -} \ No newline at end of file diff --git a/app/lib/settings-ui.js b/app/lib/settings-ui.js deleted file mode 100644 index 22f0e2e..0000000 --- a/app/lib/settings-ui.js +++ /dev/null @@ -1,19 +0,0 @@ -'use strict' - -const os = require('os'); -const fs = require('fs'); -const settings = {}; - -settings.file = `${os.homedir()}/.mcopy/settings.json`; -settings.state = { - camera : {}, - projector : {}, - light : {} -} - -settings.save = function () { - const str = JSON.stringify(settings.state, null, '\t'); - fs.writeFile(settings.file, str, 'utf8', (err) => { - if (err) console.error(err); - }); -} \ No newline at end of file diff --git a/app/lib/settings/index.js b/app/lib/settings/index.js new file mode 100644 index 0000000..0725ffd --- /dev/null +++ b/app/lib/settings/index.js @@ -0,0 +1,52 @@ +'use strict' + +const os = require('os'); +const path = require('path'); +const fs = require('fs'); +const settings = {}; + +settings.file = path.join(os.homedir(), `/.mcopy/settings.json`); +settings.state = { + server : { + port : 1111, + enabled : true + }, + camera : {}, + projector : {}, + light : {} +} + +settings.checkDir = function () { + const dir = path.join(os.homedir(), '.mcopy/') + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir); + } +} + +settings.save = function () { + const str = JSON.stringify(settings.state, null, '\t'); + settings.checkDir() + fs.writeFile(settings.file, str, 'utf8', (err) => { + if (err) console.error(err); + }) +} +settings.update = function (key, val) { + settings.state[key] = val +} + +settings.get = function (key) { + return settings.state[key] +} + +settings.restore = function () { + let str + settings.checkDir() + if (fs.existsSync(settings.file)) { + str = fs.readFileSync(settings.file, 'utf8') + settings.state = JSON.parse(str) + } else { + settings.save() + } +} + +module.exports = settings \ No newline at end of file