//------------------------------------------------------------------------------ // Engine - Hauptfunktion //------------------------------------------------------------------------------ var config = {} //-----------------------------------------------------------------------------------// function bodyDataAction () //-----------------------------------------------------------------------------------// { var menuButtons = document.getElementById("menuBtn"). getElementsByTagName("A"); for (var i = 0; i < menuButtons.length; i++) { menuButtons[i].onclick = function(evt) { console.log("Menu"); var res = evt.target.id; menu(res); }; } var contentButtons = document.getElementsByTagName("button"); for (var i = 0; i < contentButtons.length; i++) { contentButtons[i].onclick = function(evt) { console.log("Button"); var res = evt.target.id; menu('switchRequest', res); }; } } //-----------------------------------------------------------------------------------// function menu (message, request=null) //-----------------------------------------------------------------------------------// { var selectedId = $(".selected").attr('id'); switch (message) { case 'pageDevices': console.log( "pageDevices") $( ".rendered" ).empty(); generateDevices(); document.getElementById('pageDevices').className = "nav-link active"; document.getElementById('pageSettings').className = "nav-link"; break; case 'pageSettings': console.log( "pageSettings") $( ".rendered" ).empty(); document.getElementById('pageDevices').className = "nav-link"; document.getElementById('pageSettings').className = "nav-link active"; break; case 'switchRequest': console.log( request ) switchRequest(request); break; default: alert ('[Engine] Unbekannte Anfrage: ' + message); } } //-----------------------------------------------------------------------------------// function switchRequest (request) //-----------------------------------------------------------------------------------// { var req = request.split("."); switch (req[0]) { case 'yeelight': var bulb = req[1] var type = req[2] var value = req[3] var cmd = {}; cmd[bulb] = {}; cmd[bulb][type] = value; postYeelight(cmd); break; default: alert ('[POST] Unbekannte Anfrage: ' + req[0]); } } //-----------------------------------------------------------------------------------// function generateYeelightBulbs ( data , type) //-----------------------------------------------------------------------------------// { $( ".rendered" ).append($('
')); $( ".yeelight"+type ).append($("

").text("Yeelight " + type)); $( ".yeelight"+type ).append($('
')); for (bulb in data) { $( ".yeelight"+type+"row" ).append($('
')); $( "." + bulb + "col" ).append($("

").text(bulb)); $( "." + bulb + "col" ).append($('
')); var configBulb = data[bulb]['config'] for (setting in configBulb) { if (configBulb[setting]['state'] == "switch") { var command = "on"; $( "." + bulb + "btn" ).append($('')); var buttonOn = document.getElementById('yeelight.'+bulb+'.'+setting+'.on'); buttonOn.onclick = function(evt) { var res = evt.target.id; menu('switchRequest', res);} var command = "off"; $( "." + bulb + "btn" ).append($('')); var buttonOff = document.getElementById('yeelight.'+bulb+'.'+setting+'.off'); buttonOff.onclick = function(evt) { var res = evt.target.id; menu('switchRequest', res);} } } } } //-----------------------------------------------------------------------------------// function renderYeelightGroups ( data ) //-----------------------------------------------------------------------------------// { $( ".rendered" ).append('

YeelightGruppen

'); $( ".rendered" ).append('
'); for(group in data) { var devicesString = "" for (device in data[group]['devices']) { if(devicesString == "") { devicesString = device; } else { devicesString = devicesString + ', ' + device; } } $( ".rowGrp" ).append('
'+ '
'+ '
'+ '

'+ ''+ group + ''+ '

'+ '
'+ '
'+ ''+ ''+ '
'+ '
'+ '

'+ '

'+ '
'+ 'Geräte'+ '
'+ '

'+devicesString+'

'+ '
'+ '
'); } } //-----------------------------------------------------------------------------------// function loadYeelightGroups ( data ) //-----------------------------------------------------------------------------------// { var groups = {} for (bulb in data) { for (group in data[bulb]['groups']) { if (group in groups) { groups[group]['devices'][bulb] = ""; } else { groups[group] = {}; groups[group] = data[bulb]; groups[group]['devices'] = {}; groups[group]['devices'][bulb] = ""; } } } return groups; } //-----------------------------------------------------------------------------------// function generateDevices ( ) //-----------------------------------------------------------------------------------// { for (module in config) { if(module.localeCompare("yeelight") == 0) { yeelightGroups = loadYeelightGroups(config[module]); renderYeelightGroups(yeelightGroups); } } bodyDataAction(); return 0; } //-----------------------------------------------------------------------------------// function getConfig ( ) //-----------------------------------------------------------------------------------// { $.ajax('/rest/config', { type : 'GET', dataType: "json", success : function(data) { config = data; bodyDataAction(); menu("pageDevices"); } }); } //-----------------------------------------------------------------------------------// function postYeelight ( cmd ) //-----------------------------------------------------------------------------------// { $.ajax('/rest/yeelight', { type : 'POST', dataType: "json", contentType: 'application/json', data : JSON.stringify(cmd), success : function(data) { console.log(data); } }); } //-----------------------------------------------------------------------------------// $(function() { console.log("ONLOAD"); getConfig(); console.log("Engine LOADED"); }); //-----------------------------------------------------------------------------------//