Settings, database and main.js

This commit is contained in:
mmcwilliams 2018-01-21 18:53:15 -05:00
parent b2e7578cf9
commit dba64d5bd7
3 changed files with 30 additions and 16 deletions
app
lib
database
settings
main.js

View File

@ -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 () {

View File

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

View File

@ -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) {
@ -427,8 +426,6 @@ transfer.listen = function () {
var init = function () {
settings.restore()
createWindow()
//createMenu()
log.init()
@ -443,6 +440,9 @@ var init = function () {
mscript = require('./lib/mscript')
settings.restore()
mcopy.settings = settings.all()
setTimeout( () => {
arduino.enumerate(enumerateDevices)
}, 1000)