Use a settings file instead of cfg.json. Cfg.json becomes hardcoded.

This commit is contained in:
mmcw-dev 2018-01-04 23:42:16 -05:00
parent 9997df1c99
commit 8bb1997694
3 changed files with 52 additions and 69 deletions

View File

@ -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"
}
}
}

View File

@ -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);
});
}

52
app/lib/settings/index.js Normal file
View File

@ -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