This repository has been archived on 2023-06-10. You can view files and clone it, but cannot push or open issues or pull requests.
2018-02-03 23:02:02 +01:00

205 lines
7.1 KiB
JavaScript

//------------------------------------------------------------------------------
// 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($('<div class="yeelight'+type+' container-fluid text-center bg-grey">'));
$( ".yeelight"+type ).append($("<h3></h3>").text("Yeelight " + type));
$( ".yeelight"+type ).append($('<div class="yeelight'+type+'row row text-center">'));
for (bulb in data)
{
$( ".yeelight"+type+"row" ).append($('<div class="'+ bulb + 'col col-sm-3 bg-grey">'));
$( "." + bulb + "col" ).append($("<p></p>").text(bulb));
$( "." + bulb + "col" ).append($('<div class="' + bulb + 'btn btn-group">'));
var configBulb = data[bulb]['config']
for (setting in configBulb)
{
if (configBulb[setting]['state'] == "switch")
{
var command = "on";
$( "." + bulb + "btn" ).append($('<button '+
'id="yeelight.'+bulb+'.'+setting+'.on" ' +
'type="button" class="' + bulb + 'btnAn btn btn-primary">An</button>'));
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($('<button '+
'id="yeelight.'+bulb+'.'+setting+'.off" ' +
'type="button" class="' + bulb + 'btnAn btn btn-primary">Aus</button>'));
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");
});
//-----------------------------------------------------------------------------------//