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.
HomeControl/www/js/engine.js

241 lines
8.5 KiB
JavaScript
Raw Permalink Normal View History

2018-02-02 00:21:59 +01:00
//------------------------------------------------------------------------------
2018-02-03 23:02:02 +01:00
// Engine - Hauptfunktion
2018-02-02 00:21:59 +01:00
//------------------------------------------------------------------------------
2018-02-02 17:23:44 +01:00
var config = {}
//-----------------------------------------------------------------------------------//
function bodyDataAction ()
//-----------------------------------------------------------------------------------//
{
2018-02-03 23:02:02 +01:00
var menuButtons = document.getElementById("menuBtn").
getElementsByTagName("A");
2018-02-02 17:23:44 +01:00
for (var i = 0; i < menuButtons.length; i++) {
menuButtons[i].onclick = function(evt) {
2018-02-03 23:02:02 +01:00
console.log("Menu");
2018-02-02 17:23:44 +01:00
var res = evt.target.id;
menu(res);
};
}
2018-02-03 23:02:02 +01:00
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);
};
}
2018-02-02 17:23:44 +01:00
}
//-----------------------------------------------------------------------------------//
function menu (message, request=null)
//-----------------------------------------------------------------------------------//
2018-02-02 00:21:59 +01:00
{
2018-02-02 17:23:44 +01:00
var selectedId = $(".selected").attr('id');
switch (message)
2018-02-02 00:21:59 +01:00
{
2018-02-02 17:23:44 +01:00
case 'pageDevices':
console.log( "pageDevices")
2018-02-03 23:02:02 +01:00
$( ".rendered" ).empty();
2018-02-02 17:23:44 +01:00
generateDevices();
2018-02-03 23:02:02 +01:00
document.getElementById('pageDevices').className = "nav-link active";
document.getElementById('pageSettings').className = "nav-link";
2018-02-02 17:23:44 +01:00
break;
case 'pageSettings':
2018-02-03 23:02:02 +01:00
console.log( "pageSettings")
$( ".rendered" ).empty();
document.getElementById('pageDevices').className = "nav-link";
document.getElementById('pageSettings').className = "nav-link active";
2018-02-02 17:23:44 +01:00
break;
case 'switchRequest':
console.log( request )
switchRequest(request);
break;
default:
2018-02-03 23:02:02 +01:00
alert ('[Engine] Unbekannte Anfrage: ' + message);
2018-02-02 17:23:44 +01:00
}
}
2018-02-02 00:21:59 +01:00
2018-02-02 17:23:44 +01:00
//-----------------------------------------------------------------------------------//
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]);
2018-02-02 00:21:59 +01:00
}
2018-02-02 17:23:44 +01:00
}
2018-02-02 00:21:59 +01:00
2018-02-02 17:23:44 +01:00
//-----------------------------------------------------------------------------------//
function generateYeelightBulbs ( data , type)
//-----------------------------------------------------------------------------------//
{
2018-02-03 23:02:02 +01:00
$( ".rendered" ).append($('<div class="yeelight'+type+' container-fluid text-center bg-grey">'));
2018-02-02 17:23:44 +01:00
$( ".yeelight"+type ).append($("<h3></h3>").text("Yeelight " + type));
$( ".yeelight"+type ).append($('<div class="yeelight'+type+'row row text-center">'));
for (bulb in data)
2018-02-02 00:21:59 +01:00
{
2018-02-02 17:23:44 +01:00
$( ".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) {
2018-02-02 00:21:59 +01:00
var res = evt.target.id;
2018-02-02 17:23:44 +01:00
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);}
}
2018-02-02 00:21:59 +01:00
}
}
2018-02-02 17:23:44 +01:00
}
2018-02-02 00:21:59 +01:00
2018-02-02 17:23:44 +01:00
//-----------------------------------------------------------------------------------//
2018-02-03 23:02:02 +01:00
function renderYeelightGroups ( data )
//-----------------------------------------------------------------------------------//
{
$( ".rendered" ).append('<h1 class="grp my-4">Yeelight<small>Gruppen</small></h1>');
$( ".rendered" ).append('<div class="rowGrp row">');
for(group in data)
{
var devicesString = ""
for (device in data[group]['devices'])
{
if(devicesString == "")
{
devicesString = device;
}
else
{
devicesString = devicesString + ', ' + device;
}
}
$( ".rowGrp" ).append(' <div class="col-lg-3 col-md-4 col-sm-6 portfolio-item">'+
'<div class="card h-100">'+
'<div class="card-body">'+
'<h4 class="card-title">'+
'<a href="#">'+ group + '</a>'+
'</h4>'+
'<div class="card-text">'+
'<div class="btn-group">'+
'<button id="yeelight.'+group+'.power.on" type="button" class="btn btn-primary">An</button>'+
'<button id="yeelight.'+group+'.power.off" type="button" class="btn btn-primary">Aus</button>'+
'</div>'+
'</div>'+
'<p class="card-text"></p>'+
'<p class="card-text"></p>'+
'<h5 class="card-title">'+
'<a href="#">Geräte</a>'+
'</h5>'+
'<p class="card-text">'+devicesString+'</p>'+
'</div>'+
'</div>');
}
}
//-----------------------------------------------------------------------------------//
function loadYeelightGroups ( data )
2018-02-02 17:23:44 +01:00
//-----------------------------------------------------------------------------------//
{
var groups = {}
for (bulb in data)
2018-02-02 00:21:59 +01:00
{
2018-02-02 17:23:44 +01:00
for (group in data[bulb]['groups'])
2018-02-02 00:21:59 +01:00
{
2018-02-02 17:23:44 +01:00
if (group in groups)
{
2018-02-03 23:02:02 +01:00
groups[group]['devices'][bulb] = "";
2018-02-02 17:23:44 +01:00
}
else
{
groups[group] = {};
groups[group] = data[bulb];
2018-02-03 23:02:02 +01:00
groups[group]['devices'] = {};
groups[group]['devices'][bulb] = "";
2018-02-02 17:23:44 +01:00
}
2018-02-02 00:21:59 +01:00
}
2018-02-03 23:02:02 +01:00
}
return groups;
2018-02-02 17:23:44 +01:00
}
2018-02-02 00:21:59 +01:00
2018-02-02 17:23:44 +01:00
//-----------------------------------------------------------------------------------//
function generateDevices ( )
//-----------------------------------------------------------------------------------//
{
for (module in config)
2018-02-02 00:21:59 +01:00
{
2018-02-03 23:02:02 +01:00
if(module.localeCompare("yeelight") == 0)
2018-02-02 17:23:44 +01:00
{
2018-02-03 23:02:02 +01:00
yeelightGroups = loadYeelightGroups(config[module]);
renderYeelightGroups(yeelightGroups);
2018-02-02 17:23:44 +01:00
}
2018-02-02 00:21:59 +01:00
}
2018-02-03 23:02:02 +01:00
bodyDataAction();
2018-02-02 17:23:44 +01:00
return 0;
}
//-----------------------------------------------------------------------------------//
function getConfig ( )
//-----------------------------------------------------------------------------------//
{
$.ajax('/rest/config', {
type : 'GET',
dataType: "json",
success : function(data) {
config = data;
bodyDataAction();
menu("pageDevices");
}
});
2018-02-02 00:21:59 +01:00
}
2018-02-02 17:23:44 +01:00
//-----------------------------------------------------------------------------------//
function postYeelight ( cmd )
//-----------------------------------------------------------------------------------//
{
$.ajax('/rest/yeelight', {
type : 'POST',
dataType: "json",
contentType: 'application/json',
data : JSON.stringify(cmd),
success : function(data) {
console.log(data);
}
});
}
2018-02-02 00:21:59 +01:00
2018-02-02 17:23:44 +01:00
//-----------------------------------------------------------------------------------//
2018-02-02 00:21:59 +01:00
$(function() {
console.log("ONLOAD");
2018-02-02 17:23:44 +01:00
getConfig();
2018-02-03 23:02:02 +01:00
console.log("Engine LOADED");
2018-02-02 17:23:44 +01:00
});
//-----------------------------------------------------------------------------------//