This commit is contained in:
Kai Wansart
2018-02-02 00:21:59 +01:00
commit 9c3e560900
36 changed files with 1114 additions and 0 deletions

BIN
app/.DS_Store vendored Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

80
app/config.py Normal file
View File

@ -0,0 +1,80 @@
import os.path
import json, codecs
##---------------------------------------------------------------------------------------##
class Config(object):
##---------------------------------------------------------------------------------------##
##-----------------------------------------------------------------------------------##
def __init__(self):
##-----------------------------------------------------------------------------------##
print("Config initialisiert")
##-----------------------------------------------------------------------------------##
def defaultSettings(self):
##-----------------------------------------------------------------------------------##
print("Returning Default-Settings")
data = {
"yeelight" : {
"dunkel" : "1",
"hell" : "100",
"warm" : "1700",
"normal" : "3000",
"kalt" : "6500"
}
}
return data
##-----------------------------------------------------------------------------------##
def defaultDevices(self):
##-----------------------------------------------------------------------------------##
print("Returning Default-Devices")
data = {}
return data
##-----------------------------------------------------------------------------------##
def read(self, type):
##-----------------------------------------------------------------------------------##
data = None
if(type == "settings"):
print("Reading 'settings.json'")
if(os.path.isfile("config/settings.json")):
with open('config/settings.json', encoding='utf-8') as data_file:
try:
data = json.loads(data_file.read())
except:
print("settings.json konnte nicht gelesen werden")
data = self.defaultSettings()
self.writeConfig("settings", data)
print("Settings loaded")
else:
data =self.defaultSettings()
self.writeConfig("settings", data)
else:
print("Reading 'devices.json'")
if(os.path.isfile("config/devices.json")):
with open('config/devices.json', encoding='utf-8') as data_file:
try:
data = json.loads(data_file.read())
except:
print("devices.json konnte nicht gelesen werden")
data = self.defaultDevices()
self.writeConfig("devices", data)
print("Devices loaded")
else:
data = self.defaultDevices()
self.writeConfig("devices", data)
return data
##-----------------------------------------------------------------------------------##
def writeConfig(self, type, data):
##-----------------------------------------------------------------------------------##
if(type == "settings"):
print("Writing 'settings.json'")
with open('config/settings.json', 'w') as outfile:
json.dump(data, outfile, indent=4)
print("Settings written")
else:
print("Writing 'devices.json'")
with open('config/devices.json', 'w') as outfile:
json.dump(data, outfile, indent=4)
print("Devices written")

BIN
app/devices/.DS_Store vendored Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

25
app/devices/denon.py Normal file
View File

@ -0,0 +1,25 @@
import denonavr
##---------------------------------------------------------------------------------------##
class Denon(object):
##---------------------------------------------------------------------------------------##
##-----------------------------------------------------------------------------------##
def __init__(self, device):
##-----------------------------------------------------------------------------------##
self.device = denonavr.DenonAVR(device['ip'])
##-----------------------------------------------------------------------------------##
def power(self, state=None):
##-----------------------------------------------------------------------------------##
if(state==None):
return -1
if(state=="on"):
self.device.power_on()
else:
self.device.power_off()
##-----------------------------------------------------------------------------------##
def setInput(self, input):
##-----------------------------------------------------------------------------------##
self.device.input_func = input

44
app/devices/ssh.py Normal file
View File

@ -0,0 +1,44 @@
from paramiko import SSHClient
##---------------------------------------------------------------------------------------##
class SSH(object):
##---------------------------------------------------------------------------------------##
##-----------------------------------------------------------------------------------##
def __init__(self, devices):
##-----------------------------------------------------------------------------------##
for device in devices:
self.devices[device] = {}
self.devices[device]['user'] = devices[device]['user']
self.devices[device]['ip'] = devices[device]['ip']
##-----------------------------------------------------------------------------------##
def command(self, device, command):
##-----------------------------------------------------------------------------------##
self.client = SSHClient()
self.client.load_system_host_keys()
self.client.connect(self.devices[device]['ip'], self.devices[device]['user'])
if(command=="shutdown"):
self.shutdown(device)
if(command=="reboot"):
self.reboot(device)
if(command=="upgrade"):
self.upgrade(device)
##-----------------------------------------------------------------------------------##
def shutdown(self, device):
##-----------------------------------------------------------------------------------##
self.connect()
self.client.exec_command('poweroff')
##-----------------------------------------------------------------------------------##
def reboot(self, device):
##-----------------------------------------------------------------------------------##
self.connect()
self.client.exec_command('reboot')
##-----------------------------------------------------------------------------------##
def upgrade(self, device):
##-----------------------------------------------------------------------------------##
self.connect()
self.client.exec_command('apt update && apt upgrade -y')

19
app/devices/wol.py Normal file
View File

@ -0,0 +1,19 @@
from wakeonlan import wol
##---------------------------------------------------------------------------------------##
class WOL(object):
##---------------------------------------------------------------------------------------##
##-----------------------------------------------------------------------------------##
def __init__(self, devices):
##-----------------------------------------------------------------------------------##
count = 0
for device in devices:
self.devices[device] = devices[device]['mac']
##-----------------------------------------------------------------------------------##
def wakeDevice(self, device):
##-----------------------------------------------------------------------------------##
if(device in self.devices):
wol.send_magic_packet(self.devices[device])

328
app/devices/yeelight.py Normal file
View File

@ -0,0 +1,328 @@
from yeelight import *
import time
##---------------------------------------------------------------------------------------##
class Yeelight(object):
##---------------------------------------------------------------------------------------##
##-----------------------------------------------------------------------------------##
def __init__(self, bulbs):
##-----------------------------------------------------------------------------------##
self.bulbs = {}
self.groups = {}
for bulb in bulbs:
try:
self.bulbs.update({bulb : Bulb(bulbs[bulb]['ip'],auto_on=True)})
except:
print("ERROR: Config not saved for " + bulb)
try:
for group in bulbs[bulb]['groups']:
groupName = bulbs[bulb]['groups'][group]
if(groupName not in self.groups):
print("Create new Group " + groupName)
self.groups[groupName] = {}
self.groups[groupName][bulb] = bulb
except:
print(bulb + " ist in keiner Gruppe")
##-----------------------------------------------------------------------------------##
def command(self, data):
##-----------------------------------------------------------------------------------##
for device in data:
group = 0
if device in self.groups:
group = True
if(device in self.bulbs or device in self.groups):
if 'power' in data[device]:
self.power(device, group, data[device]['power'])
if(data[device]['power'] == "off"):
return 0
if 'brightness' in data[device]:
self.brightness(device, group, data[device]['brightness'])
if 'r' in data[device] or 'g' in data[device] or 'b' in data[device]:
if 'r' in data[device]:
r = data[device]['r']
else:
r = "0"
if 'g' in data[device]:
g = data[device]['g']
else:
g = "0"
if 'b' in data[device]:
b = data[device]['b']
else:
b = "0"
self.color(device, group, r, g, b)
if 'temperature' in data[device]:
self.temperature(device, group, data[device]['temperature'])
##-----------------------------------------------------------------------------------##
def power(self, device, group=None, state=None):
##-----------------------------------------------------------------------------------##
if(state==None):
return -1
if(device==None):
for bulb in self.bulbs:
self.powerBulb(device, state)
if(group == True):
for bulb in self.groups[device]:
self.powerBulb(bulb, state)
else:
if(device in self.bulbs):
self.powerBulb(device, state)
##-----------------------------------------------------------------------------------##
def powerBulb(self, device, state):
##-----------------------------------------------------------------------------------##
if(device in self.bulbs):
error = False
try:
oldState = self.bulbs[device].get_properties()
except:
print(device + ": Konnte nicht angesteuert werden")
error = True
finally:
if(str(oldState['power']) != str(state)):
if(state=="on"):
self.bulbs[device].turn_on()
print(device + " turned on")
else:
self.bulbs[device].turn_off()
print(device + " turned off")
if(error == True):
for count in range(0,10):
try:
oldState = self.bulbs[device].get_properties()
except:
print(device + ": Konnte nicht angesteuert werden")
finally:
if(str(oldState['power']) != str(state)):
if(state=="on"):
self.bulbs[device].turn_on()
print(device + " turned on")
else:
self.bulbs[device].turn_off()
print(device + " turned off")
try:
oldState = self.bulbs[device].get_properties()
if(oldState['power'] == str(state)):
break
except:
print(device + " keine Veraenderung")
##-----------------------------------------------------------------------------------##
def brightness(self, device, group, state=None):
##-----------------------------------------------------------------------------------##
if(state==None):
return -1
if(device==None):
for bulb in self.bulbs:
self.brightnessBulb(device, state)
if(group == True):
for bulb in self.groups[device]:
self.brightnessBulb(bulb, state)
else:
if(device in self.bulbs):
self.brightnessBulb(device, state)
##-----------------------------------------------------------------------------------##
def brightnessBulb(self, device, state):
##-----------------------------------------------------------------------------------##
if(device in self.bulbs):
error = False
try:
oldState = self.bulbs[device].get_properties()
print(oldState)
except:
print(device + ": Konnte nicht angesteuert werden")
error = True
finally:
if(str(oldState['bright']) != str(state)):
self.bulbs[device].set_brightness(int(state))
print(device + " turned brightness to " + str(state))
if(error == True):
for count in range(0,10):
try:
oldState = self.bulbs[device].get_properties()
except:
print(device + ": Konnte nicht angesteuert werden")
finally:
if(str(oldState['brigh']) != str(state)):
self.bulbs[device].set_brightness(int(state))
print(device + " turned brightness to " + str(state))
try:
oldState = self.bulbs[device].get_properties()
if(oldState['bright'] == str(state)):
break
except:
print(device + " keine Veraenderung")
##-----------------------------------------------------------------------------------##
def temperature(self, device, group, state=None):
##-----------------------------------------------------------------------------------##
if(state==None):
return -1
if(device==None):
for bulb in self.bulbs:
self.temperatureBulb(device, state)
if(group == True):
for bulb in self.groups[device]:
self.temperatureBulb(bulb, state)
else:
if(device in self.bulbs):
self.temperatureBulb(device, state)
##-----------------------------------------------------------------------------------##
def temperatureBulb(self, device, state):
##-----------------------------------------------------------------------------------##
if(device in self.bulbs):
error = False
try:
oldState = self.bulbs[device].get_properties()
except:
print(device + ": Konnte nicht angesteuert werden")
error = True
finally:
if(str(oldState['ct']) != str(state)):
self.bulbs[device].set_color_temp(int(state))
print(device + " turned temperature to " + str(state))
if(error == True):
for count in range(0,10):
try:
oldState = self.bulbs[device].get_properties()
except:
print(device + ": Konnte nicht angesteuert werden")
finally:
if(str(oldState['ct']) != str(state)):
self.bulbs[device].set_color_temp(int(state))
print(device + " turned temperature to " + str(state))
try:
oldState = self.bulbs[device].get_properties()
if(oldState['ct'] == str(state)):
break
except:
print(device + " keine Veraenderung")
##-----------------------------------------------------------------------------------##
def color(self, device, group, r, g, b):
##-----------------------------------------------------------------------------------##
if(device==None):
for bulb in self.bulbs:
self.colorBulb(device, r, g, b)
if(group == True):
for bulb in self.groups[device]:
self.colorBulb(bulb, r, g, b)
else:
if(device in self.bulbs):
self.colorBulb(device, r, g, b)
##-----------------------------------------------------------------------------------##
def colorBulb(self, device, r, g, b):
##-----------------------------------------------------------------------------------##
bulbCount = 0
bulbCheck = 0
if(device in self.bulbs):
error = False
try:
bulbCount = bulbCount + 1
oldState = self.bulbs[device].get_properties()
except:
print(device + ": Konnte nicht angesteuert werden")
error = True
finally:
self.bulbs[device].set_rgb(int(r), int(g), int(b))
print(device + " color r:" + str(r) + " g:" + str(g) + " b:" + str(b))
bulbCheck = bulbCheck + 1
if(bulbCount != bulbCheck):
for count in range(0,10):
bulbCount = 0
bulbCheck = 0
try:
bulbCount = bulbCount + 1
oldState = self.bulbs[device].get_properties()
except:
print(device + ": Konnte nicht angesteuert werden")
error = True
finally:
self.bulbs[device].set_rgb(int(r), int(g), int(b))
print(device + " color r:" + str(r) + " g:" + str(g) + " b:" + str(b))
bulbCheck = bulbCheck + 1
if(bulbCheck==bulbCount):
return 0
##-----------------------------------------------------------------------------------##
def temperatureTransition(self, startTemp=1700, endTemp=6500, startBrightness=100,
endBrightness=100, seconds=0, count=None):
##-----------------------------------------------------------------------------------##
if(count==None):
count=10
duration = seconds*1000
transitions = [
TemperatureTransition(startTemp, duration, startBrightness),
SleepTransition(duration),
TemperatureTransition(endTemp, duration, endBrightness)
]
flow = Flow(
count=1,
action=Flow.actions.recover,
transitions=transitions
)
self.temperature(startBrightness)
self.brightness(startBrightness)
bulbCount = 0
bulbCheck = 0
for bulb in self.bulbs:
bulbCount = bulbCount + 1
try:
oldState = self.bulbs[bulb].get_properties()
except:
print(bulb + ": Konnte nicht angesteuert werden")
finally:
self.bulbs[bulb].start_flow(flow)
print(bulb + " Flow")
bulbCheck = bulbCheck + 1
if(bulbCount == bulbCheck):
return 0
else:
if(count!=0):
time.sleep(.250)
temperatureTransition(startTemp, endTemp, startBrightness, endBrightness,seconds,count-1)
else:
print("Zu viele Versuche. Fehlgeschlagen!")
return -1
return 0
##-----------------------------------------------------------------------------------##
def stopTransition(self, count=None):
##-----------------------------------------------------------------------------------##
if(count==None):
count=10
bulbCount = 0
bulbCheck = 0
for bulb in self.bulbs:
bulbCount = bulbCount + 1
try:
oldState = self.bulbs[bulb].get_properties()
except:
print(bulb + ": Konnte nicht angesteuert werden")
finally:
self.bulbs[bulb].stop_flow()
bulbCheck = bulbCheck + 1
if(bulbCount == bulbCheck):
return 0
else:
if(count!=0):
time.sleep(.250)
stopTransition(count-1)
else:
print("Zu viele Versuche. Fehlgeschlagen!")
return -1
return 0

91
app/webserver.py Normal file
View File

@ -0,0 +1,91 @@
from flask import Flask, request
##---------------------------------------------------------------------------------------##
class Webserver(object):
##---------------------------------------------------------------------------------------##
##-----------------------------------------------------------------------------------##
def __init__(self, config):
##-----------------------------------------------------------------------------------##
print("Webserver initialisiert")
self.settings = config.read("settings")
self.devices = config.read("devices")
##-----------------------------------------------------------------------------------##
def run(self):
##-----------------------------------------------------------------------------------##
app = Flask(__name__)
if(self.devices['yeelight'] != None):
print("Enabled Yeelight")
from app.devices import yeelight
self.yeelight = yeelight.Yeelight(self.devices['yeelight'])
@app.route('/yeelight', methods=['POST'])
def yeelight():
app.logger.debug("JSON received...")
app.logger.debug(request.json)
if request.json:
data = request.get_json(force=True)
self.yeelight.command(data)
return '200'
else:
return '500'
else:
print("Disabled Yeelight")
if(self.devices['ssh'] != None):
print("Enabled SSH")
from app.devices import ssh
self.ssh = ssh.SSH(self.devices['ssh'])
@app.route('/ssh', methods=['POST'])
def ssh():
app.logger.debug("JSON received...")
app.logger.debug(request.json)
if request.json:
data = request.get_json(force=True)
self.ssh.command(data)
return '200'
else:
return '500'
else:
print("Disabled SSH")
if(self.devices['wol'] != None):
print("Enabled WOL")
from app.devices import wol
self.wol = wol.WOL(self.devices['wol'])
@app.route('/wol', methods=['POST'])
def wol():
app.logger.debug("JSON received...")
app.logger.debug(request.json)
if request.json:
data = request.get_json(force=True)
self.wol.command(data)
return '200'
else:
return '500'
else:
print("Disabled SSH")
if(self.devices['denon'] != None):
print("Enabled Denon")
from app.devices import denon
self.denon = wol.WOL(self.devices['denon'])
@app.route('/denon', methods=['POST'])
def denon():
app.logger.debug("JSON received...")
app.logger.debug(request.json)
if request.json:
data = request.get_json(force=True)
self.denon.command(data)
return '200'
else:
return '500'
else:
print("Disabled Denon")
app.run(debug=False, host='0.0.0.0')