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

View File

@ -14,7 +14,8 @@ const actionTable = `CREATE TABLE IF NOT EXISTS actions (
counter INTEGER, counter INTEGER,
light TEXT, light TEXT,
dir INTEGER, dir INTEGER,
sequence INTEGER sequence INTEGER,
device TEXT
);` );`
var checkDir = function () { var checkDir = function () {

View File

@ -1,5 +1,3 @@
'use strict'
const os = require('os'); const os = require('os');
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
@ -11,46 +9,61 @@ settings.state = {
port : 1111, port : 1111,
enabled : true enabled : true
}, },
devices : [],
camera : {}, camera : {},
projector : {}, projector : {},
light : {} light : {}
} }
settings.checkDir = function () { settings.checkDir = function () {
const dir = path.join(os.homedir(), '.mcopy/') 'use strict'
const dir = path.join(os.homedir(), '.mcopy/');
if (!fs.existsSync(dir)) { if (!fs.existsSync(dir)) {
fs.mkdirSync(dir); fs.mkdirSync(dir);
} }
} }
settings.save = function () { settings.save = function () {
'use strict'
const str = JSON.stringify(settings.state, null, '\t'); const str = JSON.stringify(settings.state, null, '\t');
settings.checkDir() settings.checkDir();
fs.writeFile(settings.file, str, 'utf8', (err) => { fs.writeFile(settings.file, str, 'utf8', (err) => {
if (err) console.error(err); if (err) console.error(err);
}) })
} }
settings.update = function (key, val) { settings.update = function (key, val) {
settings.state[key] = val 'use strict'
settings.state[key] = val;
} }
settings.get = function (key) { settings.get = function (key) {
return settings.state[key] 'use strict'
return settings.state[key];
} }
settings.all = function () { settings.all = function () {
return settings.state 'use strict'
return settings.state;
} }
settings.restore = function () { settings.restore = function () {
let str 'use strict'
settings.checkDir() let str;
settings.checkDir();
if (fs.existsSync(settings.file)) { if (fs.existsSync(settings.file)) {
str = fs.readFileSync(settings.file, 'utf8') str = fs.readFileSync(settings.file, 'utf8')
settings.state = JSON.parse(str) settings.state = JSON.parse(str);
} else { } else {
settings.save() settings.save();
} }
} }
settings.reset = function () {
'use strict'
if (fs.existsSync(settings.file)) {
fs.unlinkSync(settings.file);
}
settings.restore();
};
module.exports = settings module.exports = settings

View File

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