This commit is contained in:
Kai Wansart 2018-02-02 17:23:44 +01:00
parent 801dd73469
commit 5b5e0b3c64
9 changed files with 395 additions and 84 deletions

View File

@ -6,6 +6,7 @@ class SSH(object):
##-----------------------------------------------------------------------------------## ##-----------------------------------------------------------------------------------##
def __init__(self, devices): def __init__(self, devices):
##-----------------------------------------------------------------------------------## ##-----------------------------------------------------------------------------------##
self.devices = {}
for device in devices: for device in devices:
self.devices[device] = {} self.devices[device] = {}
self.devices[device]['user'] = devices[device]['user'] self.devices[device]['user'] = devices[device]['user']
@ -25,6 +26,30 @@ class SSH(object):
if(command=="upgrade"): if(command=="upgrade"):
self.upgrade(device) self.upgrade(device)
##-----------------------------------------------------------------------------------##
def config(self):
##-----------------------------------------------------------------------------------##
config = {
"device" : {
"name" : "",
"ip" : "",
"user" : ""
},
"shutdown" : {
"device" : "name",
"state" : "button"
},
"reboot" : {
"device" : "name",
"state" : "button"
},
"upgrade" : {
"device" : "name",
"state" : "button"
}
}
return config
##-----------------------------------------------------------------------------------## ##-----------------------------------------------------------------------------------##
def shutdown(self, device): def shutdown(self, device):
##-----------------------------------------------------------------------------------## ##-----------------------------------------------------------------------------------##

View File

@ -7,9 +7,23 @@ class WOL(object):
def __init__(self, devices): def __init__(self, devices):
##-----------------------------------------------------------------------------------## ##-----------------------------------------------------------------------------------##
count = 0 count = 0
self.devices= {}
for device in devices: for device in devices:
self.devices[device] = devices[device]['mac'] self.devices[device] = devices[device]['mac']
##-----------------------------------------------------------------------------------##
def config(self):
##-----------------------------------------------------------------------------------##
config = {
"device" : {
"name" : "",
"mac" : ""
},
"wake" : {
"device" : "name",
"state" : "button"
}
}
return config
##-----------------------------------------------------------------------------------## ##-----------------------------------------------------------------------------------##
def wakeDevice(self, device): def wakeDevice(self, device):
##-----------------------------------------------------------------------------------## ##-----------------------------------------------------------------------------------##

View File

@ -26,6 +26,81 @@ class Yeelight(object):
except: except:
print(bulb + " ist in keiner Gruppe") print(bulb + " ist in keiner Gruppe")
##-----------------------------------------------------------------------------------##
def config(self):
##-----------------------------------------------------------------------------------##
config = {
"rgb" : {
"device" : {
"name" : "",
"ip" : "",
"type" : "rgb"
},
"power" : {
"device" : "name",
"state" : "switch"
},
"brightness" : {
"device" : "name",
"state" : "dimmer"
},
"temperature" : {
"device" : "name",
"state" : "dimmer"
},
"color" : {
"device" : "name",
"r" : "dimmer",
"g" : "dimmer",
"b" : "dimmer"
}
},
"white" : {
"device" : {
"name": "",
"ip" : "",
"type" : "white"
},
"power" : {
"device" : "name",
"state" : "switch"
},
"brightness" : {
"device" : "name",
"state" : "dimmer"
},
"temperature" : {
"device" : "name",
"state" : "dimmer"
}
}
}
return config
##-----------------------------------------------------------------------------------##
def state(self):
##-----------------------------------------------------------------------------------##
state = {}
for bulb in self.bulbs:
state[bulb] = {}
try:
properties = self.bulbs[bulb].get_properties()
except:
state[bulb]['offline'] = True
finally:
state[bulb]['offline'] = False
state[bulb]['power'] = properties['power']
state[bulb]['brightness'] = properties['bright']
state[bulb]['temperature'] = properties['ct']
state[bulb]['color'] = properties['rgb']
state[bulb]['groups'] = {}
for group in self.groups:
if(bulb in self.groups[group]):
state[bulb]['groups'][group] = ""
#r, g, b = tuple(int(properties['rgb'][i:i+2], 16) for i in (0, 2 ,4))
#print("r:" + str(r) + " g: " + str(g) + " b: " + str(b))
return state
##-----------------------------------------------------------------------------------## ##-----------------------------------------------------------------------------------##
def command(self, data): def command(self, data):
##-----------------------------------------------------------------------------------## ##-----------------------------------------------------------------------------------##

View File

@ -1,4 +1,4 @@
from flask import Flask, request from flask import Flask, request, jsonify
##---------------------------------------------------------------------------------------## ##---------------------------------------------------------------------------------------##
@ -10,6 +10,29 @@ class Webserver(object):
print("Webserver initialisiert") print("Webserver initialisiert")
self.settings = config.read("settings") self.settings = config.read("settings")
self.devices = config.read("devices") self.devices = config.read("devices")
self.config = {}
##-----------------------------------------------------------------------------------##
def createConfig(self, module, devices, config):
##-----------------------------------------------------------------------------------##
#print("Module: " + str(module))
#print("Devices: " + str(devices))
#print("Config: " + str(config))
self.config[module] = {}
if(module == "yeelight"):
for device in devices:
self.config[module][device] = {}
self.config[module][device] = self.devices[module][device]
if(devices[device]['type'] == "rgb"):
self.config[module][device]['config'] = config['rgb']
if(devices[device]['type'] == "white"):
self.config[module][device]['config'] = config['white']
return
for device in devices:
self.config[module][device] = {}
self.config[module][device] = self.devices[module][device]
self.config[module][device]['config'] = config
##-----------------------------------------------------------------------------------## ##-----------------------------------------------------------------------------------##
def run(self): def run(self):
@ -20,8 +43,11 @@ class Webserver(object):
print("Enabled Yeelight") print("Enabled Yeelight")
from app.devices import yeelight from app.devices import yeelight
self.yeelight = yeelight.Yeelight(self.devices['yeelight']) self.yeelight = yeelight.Yeelight(self.devices['yeelight'])
devices = self.devices['yeelight']
config = self.yeelight.config()
self.createConfig("yeelight", devices, config)
@app.route('/yeelight', methods=['POST']) @app.route('/rest/yeelight', methods=['POST'])
def yeelight(): def yeelight():
app.logger.debug("JSON received...") app.logger.debug("JSON received...")
app.logger.debug(request.json) app.logger.debug(request.json)
@ -38,8 +64,11 @@ class Webserver(object):
print("Enabled SSH") print("Enabled SSH")
from app.devices import ssh from app.devices import ssh
self.ssh = ssh.SSH(self.devices['ssh']) self.ssh = ssh.SSH(self.devices['ssh'])
devices = self.devices['ssh']
config = self.ssh.config()
self.createConfig("ssh", devices, config)
@app.route('/ssh', methods=['POST']) @app.route('/rest/ssh', methods=['POST'])
def ssh(): def ssh():
app.logger.debug("JSON received...") app.logger.debug("JSON received...")
app.logger.debug(request.json) app.logger.debug(request.json)
@ -56,8 +85,11 @@ class Webserver(object):
print("Enabled WOL") print("Enabled WOL")
from app.devices import wol from app.devices import wol
self.wol = wol.WOL(self.devices['wol']) self.wol = wol.WOL(self.devices['wol'])
devices = self.devices['wol']
config = self.wol.config()
self.createConfig("wol", devices, config)
@app.route('/wol', methods=['POST']) @app.route('/rest/wol', methods=['POST'])
def wol(): def wol():
app.logger.debug("JSON received...") app.logger.debug("JSON received...")
app.logger.debug(request.json) app.logger.debug(request.json)
@ -70,22 +102,29 @@ class Webserver(object):
else: else:
print("Disabled SSH") print("Disabled SSH")
if(self.devices['denon'] != None): #if(self.devices['denon'] != None):
print("Enabled Denon") # print("Enabled Denon")
from app.devices import denon # from app.devices import denon
self.denon = wol.WOL(self.devices['denon']) # self.denon = denon.Denon(self.devices['denon'])
# devices = self.devices['denon']
# config = self.denon.config()
# self.createConfig("denon", devices, config)
@app.route('/denon', methods=['POST']) # @app.route('/rest/denon', methods=['POST'])
def denon(): # def denon():
app.logger.debug("JSON received...") # app.logger.debug("JSON received...")
app.logger.debug(request.json) # app.logger.debug(request.json)
if request.json: # if request.json:
data = request.get_json(force=True) # data = request.get_json(force=True)
self.denon.command(data) # self.denon.command(data)
return '200' # return '200'
else: # else:
return '500' # return '500'
else: #else:
print("Disabled Denon") # print("Disabled Denon")
app.run(debug=False, host='0.0.0.0') @app.route('/rest/config', methods=['GET'])
def config():
return jsonify(self.config)
app.run(debug=True, host='0.0.0.0')

View File

@ -2,20 +2,35 @@
"yeelight" : { "yeelight" : {
"fernseher" : { "fernseher" : {
"ip" : "192.168.178.127", "ip" : "192.168.178.127",
"type" : "rgb" "type" : "rgb",
}, "groups" : {
"zimmer" : "zimmer",
"passiv" : "passiv"
}
},
"regal" : { "regal" : {
"ip" : "192.168.178.128", "ip" : "192.168.178.128",
"type" : "rgb" "type" : "rgb",
}, "groups" : {
"zimmer" : "zimmer",
"passiv" : "passiv"
}
},
"monitor" : { "monitor" : {
"ip" : "192.168.178.126", "ip" : "192.168.178.126",
"type" : "rgb" "type" : "rgb",
}, "groups" : {
"zimmer" : "zimmer",
"passiv" : "passiv"
}
},
"bett" : { "bett" : {
"ip" : "192.168.178.122", "ip" : "192.168.178.122",
"type" : "rgb" "type" : "rgb",
"groups" : {
"zimmer" : "zimmer"
} }
}
}, },
"ssh" : { "ssh" : {
"nas" : { "nas" : {

View File

@ -1,8 +1,8 @@
# TODO # TODO
1. Return available commands 1. ~~Return available commands~~
2. Presets (Yeelight) 2. Presets (Yeelight) .... Progress
3. Website..... 3. Website..... Progress
4. Yamaha Receiver 4. Yamaha Receiver
5. Yeelight Flow 5. Yeelight Flow
6. Wecker 6. Wecker

View File

@ -1,4 +1,4 @@
<div class="container-fluid text-center bg-grey"> <div class="yeelight container-fluid text-center bg-grey">
<h2>Lampen</h2> <h2>Lampen</h2>
<div class="row text-center"> <div class="row text-center">
<div class="col-sm-4 bg-grey"> <div class="col-sm-4 bg-grey">

0
www/html/yeelight.html Normal file
View File

View File

@ -2,61 +2,204 @@
// CM - Hauptfunktion // CM - Hauptfunktion
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
class CM var config = {}
//-----------------------------------------------------------------------------------//
function bodyDataAction ()
//-----------------------------------------------------------------------------------//
{ {
constructor () var menuButtons = document.getElementById("navbarBtn").
{ getElementsByTagName("button");
this.BodyDataAction(); for (var i = 0; i < menuButtons.length; i++) {
menuButtons[i].onclick = function(evt) {
console.log("CM LOADED"); var res = evt.target.id;
} menu(res);
};
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;
CM_o.Menu(res);
};
}
}
Menu (message)
{
var selectedId = $(".selected").attr('id');
switch (message)
{
case 'pageDevices':
console.log( "pageDevices")
this.LoadContent('html/devices.html');
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")
this.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;
default:
alert ('[CM] Unbekannte Anfrage: ' + message);
}
}
LoadContent ( path )
{
$.get( path, function( data ) {
$( ".content" ).html( data );
console.log( "Load was performed." );
});
} }
} }
//-----------------------------------------------------------------------------------//
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() { $(function() {
console.log("ONLOAD"); console.log("ONLOAD");
CM_o = new CM();
CM_o.Menu("pageDevices"); getConfig();
console.log("CM LOADED");
}); });
//-----------------------------------------------------------------------------------//