//------------------------------------------------------------------------------ // CM - Hauptfunktion //------------------------------------------------------------------------------ var config = {} //-----------------------------------------------------------------------------------// function bodyDataAction () //-----------------------------------------------------------------------------------// { var menuButtons = document.getElementById("navbarBtn"). getElementsByTagName("button"); for (var i = 0; i < menuButtons.length; i++) { menuButtons[i].onclick = function(evt) { var res = evt.target.id; menu(res); }; } } //-----------------------------------------------------------------------------------// function menu (message, request=null) //-----------------------------------------------------------------------------------// { var selectedId = $(".selected").attr('id'); switch (message) { case 'pageDevices': console.log( "pageDevices") //loadContent('html/devices.html'); generateDevices(); document.getElementById('pageDevices').className = "btn btn-outline-light text-dark active"; document.getElementById('pageSettings').className = "btn btn-outline-light text-dark"; break; case 'pageSettings': console.log( "pageDevices") //loadContent('html/settings.html'); document.getElementById('pageDevices').className = "btn btn-outline-light text-dark"; document.getElementById('pageSettings').className = "btn btn-outline-light text-dark active"; break; case 'switchRequest': console.log( request ) switchRequest(request); break; default: alert ('[CM] 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) //-----------------------------------------------------------------------------------// { $( ".content" ).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 generateYeelightGroups ( data ) //-----------------------------------------------------------------------------------// { console.log(data); var groups = {} for (bulb in data) { for (group in data[bulb]['groups']) { if (group in groups) { //groups[group] = data[bulb]; } else { groups[group] = {}; groups[group] = data[bulb]; } } }; console.log(groups); generateYeelightBulbs(groups,"Gruppen"); } //-----------------------------------------------------------------------------------// function generateYeelight ( data ) //-----------------------------------------------------------------------------------// { generateYeelightBulbs(data,"Bulbs"); generateYeelightGroups(data); } //-----------------------------------------------------------------------------------// function generateDevices ( ) //-----------------------------------------------------------------------------------// { for (module in config) { if(module == "yeelight") { generateYeelight(config[module]); } } 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 loadContent ( content ) //-----------------------------------------------------------------------------------// { $.get( content, function( data ) { $( ".content" ).html( data ); console.log( "Load was performed." ); }); } //-----------------------------------------------------------------------------------// $(function() { console.log("ONLOAD"); getConfig(); console.log("CM LOADED"); }); //-----------------------------------------------------------------------------------//