Json write (#313)

* always output latest version of config file to webui

* fix permissions after default config export

* tune code

* set permissions for exported effects

* use qt setperm instead of chmod
update effects code style a bit

* add fallback when config is not readable

* ui: when sending config, convert to utf8 to save size and avoid jumbo frames (todo: minify it)
jsonclient: add some constants for websocket frames (taken from https://github.com/zaphoyd/websocketpp/blob/master/websocketpp/frame.hpp)

* webui:
refactory of websocket connector
sended json data is always convert to utf8
This commit is contained in:
redPanther
2016-12-05 11:10:44 +01:00
committed by GitHub
parent 8d55154164
commit 9428586195
4 changed files with 149 additions and 48 deletions

View File

@@ -116,7 +116,20 @@ function initWebSocket()
}
}
function sendToHyperion(command, subcommand, msg)
{
if (typeof subcommand != 'undefined' && subcommand.length > 0)
subcommand = ',"subcommand":"'+subcommand+'"';
else
subcommand = "";
if (typeof msg != 'undefined' && msg.length > 0)
msg = ","+msg;
else
msg = "";
websocket.send(encode_utf8('{"command":"'+command+'", "tan":'+wsTan+subcommand+msg+'}'));
}
// -----------------------------------------------------------
// wrapped server commands
@@ -124,55 +137,55 @@ function initWebSocket()
// also used for watchdog
function requestServerInfo() {
watchdog++;
websocket.send('{"command":"serverinfo", "tan":'+wsTan+'}');
sendToHyperion("serverinfo");
}
function requestServerConfigSchema() {
websocket.send('{"command":"config", "tan":'+wsTan+',"subcommand":"getschema"}');
sendToHyperion("config","getschema");
}
function requestServerConfig() {
websocket.send('{"command":"config", "tan":'+wsTan+',"subcommand":"getconfig"}');
sendToHyperion("config", "getconfig");
}
function requestServerConfigReload() {
websocket.send('{"command":"config", "tan":'+wsTan+',"subcommand":"reload"}');
sendToHyperion("config", "reload");
}
function requestLedColorsStart() {
ledStreamActive=true;
websocket.send('{"command":"ledcolors", "tan":'+wsTan+',"subcommand":"ledstream-start"}');
sendToHyperion("ledcolors", "ledstream-start");
}
function requestLedColorsStop() {
ledStreamActive=false;
websocket.send('{"command":"ledcolors", "tan":'+wsTan+',"subcommand":"ledstream-stop"}');
sendToHyperion("ledcolors", "ledstream-stop");
}
function requestPriorityClear() {
websocket.send('{"command":"clear", "tan":'+wsTan+', "priority":1}');
sendToHyperion("clear", "", '"priority":1');
}
function requestPlayEffect(effectName) {
websocket.send('{"command":"effect", "tan":'+wsTan+',"effect":{"name":"'+effectName+'"},"priority":1}');
sendToHyperion("effect", "", '"effect":{"name":"'+effectName+'"},"priority":1');
}
function requestSetColor(r,g,b) {
websocket.send('{"command":"color", "tan":'+wsTan+', "color":['+r+','+g+','+b+'], "priority":1}');
sendToHyperion("color", "", '"color":['+r+','+g+','+b+'], "priority":1');
}
function requestSetComponentState(comp, state){
state_str = state?"true":"false";
websocket.send('{"command":"componentstate", "tan":'+wsTan+',"componentstate":{"component":"'+comp+'","state":'+state_str+'}}');
state_str = state ? "true" : "false";
sendToHyperion("componentstate", "", '"componentstate":{"component":"'+comp+'","state":'+state_str+'}');
console.log(comp+' state: '+state_str);
}
function requestSetSource(prio)
{
if ( prio == "auto" )
websocket.send('{"command":"sourceselect", "tan":'+wsTan+', "auto" : true}');
sendToHyperion("sourceselect", "", '"auto":true');
else
websocket.send('{"command":"sourceselect", "tan":'+wsTan+', "priority" : '+prio+'}');
sendToHyperion("sourceselect", "", '"priority":'+prio);
}
function requestWriteConfig(config)
@@ -181,30 +194,36 @@ function requestWriteConfig(config)
jQuery.each(config, function(i, val) {
complete_config[i] = val;
});
websocket.send('{"command":"config","subcommand":"setconfig", "tan":'+wsTan+', "config":'+JSON.stringify(complete_config)+'}');
var config_str = JSON.stringify(complete_config);
console.log(config_str.length);
sendToHyperion("config","setconfig", '"config":'+config_str);
}
function requestWriteEffect(effectName,effectPy,effectArgs)
{
var cutArgs = effectArgs.slice(1, -1);
websocket.send('{"command":"create-effect","name":"'+effectName+'", "script":"'+effectPy+'", '+cutArgs+'}');
sendToHyperion("create-effect", "", '"name":"'+effectName+'", "script":"'+effectPy+'", '+cutArgs);
}
function requestTestEffect(effectName,effectPy,effectArgs) {
websocket.send('{"command":"effect", "tan":'+wsTan+',"effect":{"name":"'+effectName+'", "args":'+effectArgs+'},"priority":1, "pythonScript":"'+effectPy+'"}');
sendToHyperion("effect", "", '"effect":{"name":"'+effectName+'", "args":'+effectArgs+'},"priority":1, "pythonScript":"'+effectPy+'"}');
}
function requestDeleteEffect(effectName) {
websocket.send('{"command":"delete-effect", "tan":'+wsTan+',"name":"'+effectName+'"}');
function requestDeleteEffect(effectName)
{
sendToHyperion("delete-effect", "", '"name":"'+effectName+'"');
}
function requestLoggingStart() {
function requestLoggingStart()
{
loggingStreamActive=true;
websocket.send('{"command":"logging", "tan":'+wsTan+',"subcommand":"start"}');
sendToHyperion("logging", "start");
}
function requestLoggingStop() {
function requestLoggingStop()
{
loggingStreamActive=false;
websocket.send('{"command":"logging", "tan":'+wsTan+',"subcommand":"stop"}');
sendToHyperion("logging", "stop");
}

View File

@@ -120,30 +120,41 @@ function createJsonEditor(container,schema,setconfig)
return editor;
}
function createSelGroup(group){
function createSelGroup(group)
{
var el = document.createElement('optgroup');
el.setAttribute('label', group);
return el
return el;
}
function createSelOpt(opt){
function createSelOpt(opt)
{
var el = document.createElement('option');
el.setAttribute('value', opt);
el.innerHTML = opt;
return el
return el;
}
function createSel(array, group){
if (array.length != "0"){
var el = createSelGroup(group);
for(var i=0; i<array.length; i++){
function createSel(array, group)
{
if (array.length != "0")
{
var el = createSelGroup(group);
for(var i=0; i<array.length; i++)
{
var opt = createSelOpt(array[i])
el.appendChild(opt);
}
return el;
return el;
}
}
function performTranslation(){
function performTranslation()
{
$('#wrapper').i18n();
}
function encode_utf8(s)
{
return unescape(encodeURIComponent(s));
}