From 0382a49a7181f9651e3b86737c3c2115f876a1c0 Mon Sep 17 00:00:00 2001 From: William Viker Date: Tue, 5 Jun 2018 23:05:57 +0200 Subject: [PATCH] Implemented userconfig, and the ability to flip up/down counting direction of pages. Closing #29 and #56 --- app.js | 31 +++++++++++++------------- lib/action.js | 1 - lib/db.js | 19 ++++++++++++++++ lib/device.js | 10 +++++++-- lib/graphics.js | 21 ++++++++++++++++++ lib/userconfig.js | 51 ++++++++++++++++++++++++++++++++++++++++++ public/index.html | 43 +++++++++++++++++++++++++++++++---- public/userconfig.js | 53 ++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 207 insertions(+), 22 deletions(-) create mode 100644 lib/userconfig.js create mode 100644 public/userconfig.js diff --git a/app.js b/app.js index a7733f3869..eaf54a55f8 100644 --- a/app.js +++ b/app.js @@ -65,21 +65,22 @@ system.on('skeleton-bind-port', function(port) { system.on('skeleton-ready', function() { - var http = require('./lib/http')(system); - var io = require('./lib/io')(system, http); - var log = require('./lib/log')(system,io); - var db = require('./lib/db')(system,cfgDir); - var appRoot = require('app-root-path'); - var express = require('express'); - var bank = require('./lib/bank')(system); - var elgatoDM = require('./lib/elgato_dm')(system); - var preview = require('./lib/preview')(system); - var action = require('./lib/action')(system); - var instance = require('./lib/instance')(system); - var variable = require('./lib/variable')(system); - var osc = require('./lib/osc')(system); - var rest = require('./lib/rest')(system); - var udp = require('./lib/udp')(system); + var http = require('./lib/http')(system); + var io = require('./lib/io')(system, http); + var log = require('./lib/log')(system,io); + var db = require('./lib/db')(system,cfgDir); + var userconfig = require('./lib/userconfig')(system) + var appRoot = require('app-root-path'); + var express = require('express'); + var bank = require('./lib/bank')(system); + var elgatoDM = require('./lib/elgato_dm')(system); + var preview = require('./lib/preview')(system); + var action = require('./lib/action')(system); + var instance = require('./lib/instance')(system); + var variable = require('./lib/variable')(system); + var osc = require('./lib/osc')(system); + var rest = require('./lib/rest')(system); + var udp = require('./lib/udp')(system); system.on('exit', function() { elgatoDM.quit(); diff --git a/lib/action.js b/lib/action.js index 9682f36396..1cf4c146c2 100644 --- a/lib/action.js +++ b/lib/action.js @@ -18,7 +18,6 @@ function action(system) { self.system.on('action_save', function() { self.system.emit('db_set', 'bank_actions', self.bank_actions); self.system.emit('db_save'); - debug('saving'); }); diff --git a/lib/db.js b/lib/db.js index dbc8c86af2..6d1d677026 100644 --- a/lib/db.js +++ b/lib/db.js @@ -34,8 +34,27 @@ function db(system,cfgDir) { self.db = JSON.parse(data); debug('db loaded'); + + var changed_after_load = false; + // db defaults + if (self.db.userconfig === undefined) { + self.db.userconfig = {}; + changed_after_load = true; + } + + // is page up 1->2 or 2->1? + if (self.db.userconfig.page_direction_flipped === undefined) { + self.db.userconfig.page_direction_flipped = false; + changed_after_load = true; + } + system.emit('db_loaded', self.db); + if (changed_after_load === true) { + debug('config changed by default values after load, saving.'); + system.emit('db_save'); + } + } catch (err) { if (err.code == 'ENOENT') { diff --git a/lib/device.js b/lib/device.js index f11e79c5aa..792b410363 100644 --- a/lib/device.js +++ b/lib/device.js @@ -22,6 +22,12 @@ function device(_system, panel) { self.devicepath = panel.devicepath; self.page = 1; self.config = {}; + self.userconfig = {}; + + // get userconfig object + system.emit('get_userconfig', function(userconfig) { + self.userconfig = userconfig; + }); graphics = new require('./graphics')(system); @@ -53,7 +59,7 @@ function device(_system, panel) { if (state == true) { - if (key == 0) { + if (key == (self.userconfig.page_direction_flipped === true ? 10 : 0)) { self.page++; if (self.page == 100) { self.page = 1; } self.updatePagedevice(); @@ -65,7 +71,7 @@ function device(_system, panel) { }, 400); } - if (key == 10) { + if (key == (self.userconfig.page_direction_flipped === true ? 0 : 10)) { self.page--; if (self.page == 0) { self.page = 99; } self.updatePagedevice(); diff --git a/lib/graphics.js b/lib/graphics.js index 231a89b341..381395b19f 100644 --- a/lib/graphics.js +++ b/lib/graphics.js @@ -13,10 +13,23 @@ function graphics(_system) { system = _system; self.pushed = {}; + self.userconfig = {}; system.on('graphics_invalidate_bank', self.invalidateBank.bind(self)); system.on('graphics_indicate_push', self.indicatePush.bind(self)); + // get userconfig object + system.emit('get_userconfig', function(userconfig) { + self.userconfig = userconfig; + }); + + // if settings are changed, draw new up/down buttons + system.on('set_userconfig_key', function(key,val) { + if (key == 'page_direction_flipped') { + self.drawControls(); + } + }); + self.drawControls(); system.once('bank-update', function(config) { @@ -136,6 +149,10 @@ graphics.prototype.drawPage = function(page) { }; +// (self.userconfig.page_direction_flipped should maybe flip up/down as well?! +// just need some clarification from #29. drawControls() gets called again when +// it gets flipped in config. + graphics.prototype.drawControls = function() { var self = this; @@ -150,6 +167,10 @@ graphics.prototype.drawControls = function() { img.backgroundColor(img.rgb(15,15,15)); img.drawLetter(26,40,'arrow_down',img.rgb(255,255,255),'icon'); img.drawText(5,25,"PAGE DOWN",img.rgb(255,198,0),0); + + // we should probably invalidate the graphics or something here? or, maybe + // something underlying does that. håkon? + } graphics.prototype.getImagesForPage = function(page) { diff --git a/lib/userconfig.js b/lib/userconfig.js new file mode 100644 index 0000000000..1611987237 --- /dev/null +++ b/lib/userconfig.js @@ -0,0 +1,51 @@ +var debug = require('debug')('lib/userconfig'); +var system; + +function userconfig(system) { + var self = this; + + self.userconfig = {}; + + system.emit('db_get', 'userconfig', function(config) { + self.userconfig = config; + for (var key in config) { + system.emit('userconfig_update', key, config[key]); + } + }); + + system.on('get_userconfig', function(cb) { + cb(self.userconfig); + }); + + system.emit('io_get', function (io) { + + io.on('connect', function (socket) { + + debug('socket ' + socket.id + ' connected'); + + socket.on('set_userconfig_key', function(key,value) { + self.userconfig[key] = value; + debug('-------------- set_userconfig_key', key, value); + socket.broadcast.emit('set_userconfig_key', key, value); + system.emit('userconfig_update', key, value); + system.emit('db_save'); + }); + + socket.on('get_userconfig_all', function() { + socket.emit('get_userconfig_all', self.userconfig); + }); + + + + socket.on('disconnect', function () { + debug('socket ' + socket.id + ' disconnected'); + }); + + }); + }); + +} + +module.exports = function (system) { + return new userconfig(system); +}; diff --git a/public/index.html b/public/index.html index 5b7cf70041..01605edae3 100644 --- a/public/index.html +++ b/public/index.html @@ -223,6 +223,11 @@

Houston, we have a problem!

Devices + +
@@ -250,9 +255,8 @@

Houston, we have a problem!

-

Hint: Instances are the connections companion makes to other devices in order to - discover button actions.

- + Hint: Instances are the connections companion makes to other devices in order to + discover button actions. @@ -302,7 +306,7 @@

-

Hint: Click on the the button you like to design in the list above

+ Hint: Click on the the button you like to design in the list above @@ -334,6 +338,36 @@

+
+ + + + + + + + + + + + + + + + + + + +
User configurationValue
Flip counting direction on page up/down +
+ + +
+
+ + Settings applies instantaneously, don't worry about it! + +
@@ -389,6 +423,7 @@

+ diff --git a/public/userconfig.js b/public/userconfig.js new file mode 100644 index 0000000000..6760cd0930 --- /dev/null +++ b/public/userconfig.js @@ -0,0 +1,53 @@ +var userconfig = {}; + +$(function() { + + + var userConfigUpdate = function() { + + // set the page direction flipped option + var state = userconfig.page_direction_flipped; + var $cb = $('#userconfig_page_direction_flipped'); + + if (state === true) { + $cb.prop('checked', true); + } else { + $cb.prop('checked', false); + } + + }; + + // when userconfig is changed from the userconfig tab + $('#userconfig_page_direction_flipped').click(function() { + console.log('clicked', $(this).prop('checked') ); + if ($(this).prop('checked') == true) { + socket.emit('set_userconfig_key', 'page_direction_flipped', true); + } else { + socket.emit('set_userconfig_key', 'page_direction_flipped', false); + } + }); + + + + + + + + // when server updates the entire config array + socket.on('get_userconfig_all', function(config) { + console.log('updating entire userconfig:', config) + userconfig = config; + userConfigUpdate(); + }); + + // when other browsers update userconfig + socket.on('set_userconfig_key', function(key, value) { + userconfig[key]=value; + userConfigUpdate(); + }); + + // ask for the entire config + socket.emit('get_userconfig_all'); + + +});