commit 9c3e560900c516af55a1116b0bb2dfa3157292c8 Author: Kai Wansart Date: Fri Feb 2 00:21:59 2018 +0100 . diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..c285bcb Binary files /dev/null and b/.DS_Store differ diff --git a/app/.DS_Store b/app/.DS_Store new file mode 100644 index 0000000..f30855f Binary files /dev/null and b/app/.DS_Store differ diff --git a/app/__pycache__/config.cpython-36.pyc b/app/__pycache__/config.cpython-36.pyc new file mode 100644 index 0000000..ae72d76 Binary files /dev/null and b/app/__pycache__/config.cpython-36.pyc differ diff --git a/app/__pycache__/denon.cpython-36.pyc b/app/__pycache__/denon.cpython-36.pyc new file mode 100644 index 0000000..59fe42e Binary files /dev/null and b/app/__pycache__/denon.cpython-36.pyc differ diff --git a/app/__pycache__/linux.cpython-36.pyc b/app/__pycache__/linux.cpython-36.pyc new file mode 100644 index 0000000..550d7e2 Binary files /dev/null and b/app/__pycache__/linux.cpython-36.pyc differ diff --git a/app/__pycache__/webserver.cpython-36.pyc b/app/__pycache__/webserver.cpython-36.pyc new file mode 100644 index 0000000..5ba8b9c Binary files /dev/null and b/app/__pycache__/webserver.cpython-36.pyc differ diff --git a/app/__pycache__/yeelight.cpython-36.pyc b/app/__pycache__/yeelight.cpython-36.pyc new file mode 100644 index 0000000..6a2428d Binary files /dev/null and b/app/__pycache__/yeelight.cpython-36.pyc differ diff --git a/app/config.py b/app/config.py new file mode 100644 index 0000000..9e73812 --- /dev/null +++ b/app/config.py @@ -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") \ No newline at end of file diff --git a/app/devices/.DS_Store b/app/devices/.DS_Store new file mode 100644 index 0000000..5586656 Binary files /dev/null and b/app/devices/.DS_Store differ diff --git a/app/devices/__pycache__/ssh.cpython-36.pyc b/app/devices/__pycache__/ssh.cpython-36.pyc new file mode 100644 index 0000000..28778b6 Binary files /dev/null and b/app/devices/__pycache__/ssh.cpython-36.pyc differ diff --git a/app/devices/__pycache__/yeelight.cpython-36.pyc b/app/devices/__pycache__/yeelight.cpython-36.pyc new file mode 100644 index 0000000..922862f Binary files /dev/null and b/app/devices/__pycache__/yeelight.cpython-36.pyc differ diff --git a/app/devices/denon.py b/app/devices/denon.py new file mode 100644 index 0000000..1a99f29 --- /dev/null +++ b/app/devices/denon.py @@ -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 \ No newline at end of file diff --git a/app/devices/ssh.py b/app/devices/ssh.py new file mode 100644 index 0000000..4d6fdcf --- /dev/null +++ b/app/devices/ssh.py @@ -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') \ No newline at end of file diff --git a/app/devices/wol.py b/app/devices/wol.py new file mode 100644 index 0000000..c146c16 --- /dev/null +++ b/app/devices/wol.py @@ -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]) + + diff --git a/app/devices/yeelight.py b/app/devices/yeelight.py new file mode 100644 index 0000000..d814fdb --- /dev/null +++ b/app/devices/yeelight.py @@ -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 \ No newline at end of file diff --git a/app/webserver.py b/app/webserver.py new file mode 100644 index 0000000..6297782 --- /dev/null +++ b/app/webserver.py @@ -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') \ No newline at end of file diff --git a/config/.DS_Store b/config/.DS_Store new file mode 100644 index 0000000..0bcec6c Binary files /dev/null and b/config/.DS_Store differ diff --git a/config/devices.json b/config/devices.json new file mode 100644 index 0000000..0db1e5c --- /dev/null +++ b/config/devices.json @@ -0,0 +1,51 @@ +{ + "yeelight" : { + "fernseher" : { + "ip" : "192.168.178.127", + "type" : "rgb", + "groups" : { + "1" : "passiv", + "2" : "zimmer" + } + }, + "regal" : { + "ip" : "192.168.178.128", + "type" : "rgb", + "groups" : { + "1" : "passiv", + "2" : "zimmer" + } + }, + "monitor" : { + "ip" : "192.168.178.126", + "type" : "rgb", + "groups" : { + "1" : "passiv", + "2" : "zimmer" + } + }, + "bett" : { + "ip" : "192.168.178.122", + "type" : "rgb", + "groups" : { + "1" : "zimmer" + } + } + }, + "ssh" : { + "nas" : { + "ip" : "192.168.178.81", + "user" : "root" + } + }, + "wol" : { + "nas" : { "mac" : "38.EA.A7.A1.09.D7" } + }, + "denon" : { + "ip" : "192.168.178.69", + "input" : { + "TV" : "TV Audio", + "Internet Radio" : "Internet Radio" + } + } +} \ No newline at end of file diff --git a/config/devices.json.example b/config/devices.json.example new file mode 100644 index 0000000..5f77a7a --- /dev/null +++ b/config/devices.json.example @@ -0,0 +1,36 @@ +{ + "yeelight" : { + "fernseher" : { + "ip" : "192.168.178.127", + "type" : "rgb" + }, + "regal" : { + "ip" : "192.168.178.128", + "type" : "rgb" + }, + "monitor" : { + "ip" : "192.168.178.126", + "type" : "rgb" + }, + "bett" : { + "ip" : "192.168.178.122", + "type" : "rgb" + } + }, + "ssh" : { + "nas" : { + "ip" : "192.168.178.81", + "user" : "root" + } + }, + "wol" : { + "nas" : { "mac" : "38.EA.A7.A1.09.D7" } + }, + "denon" : { + "ip" : "192.168.178.69", + "input" : { + "TV" : "TV Audio", + "Internet Radio" : "Internet Radio" + } + } +} \ No newline at end of file diff --git a/config/settings.json b/config/settings.json new file mode 100644 index 0000000..6223ca7 --- /dev/null +++ b/config/settings.json @@ -0,0 +1,9 @@ +{ + "yeelight": { + "dunkel": "1", + "hell": "100", + "warm": "1700", + "normal": "3000", + "kalt": "6500" + } +} \ No newline at end of file diff --git a/config/settings.json.example b/config/settings.json.example new file mode 100644 index 0000000..a5d4c7a --- /dev/null +++ b/config/settings.json.example @@ -0,0 +1,9 @@ +{ + "yeelight" : { + "dunkel" : "1", + "hell" : "100", + "warm" : "1700", + "normal" : "3000", + "kalt" : "6500" + } +} \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..18a1f68 --- /dev/null +++ b/readme.md @@ -0,0 +1,8 @@ +# TODO + +1. Return available commands +2. Presets (Yeelight) +3. Website..... +4. Yamaha Receiver +5. Yeelight Flow +6. Wecker \ No newline at end of file diff --git a/remote.py b/remote.py new file mode 100644 index 0000000..7927e27 --- /dev/null +++ b/remote.py @@ -0,0 +1,54 @@ +import serial +import requests +import time +from app import yeelight + +bulbs = { + "fernseher" : { "ip" : "192.168.178.127" }, + "regal" : { "ip" : "192.168.178.128" }, + "monitor" : { "ip" : "192.168.178.126" }, + "bett" : { "ip" : "192.168.178.122" } + } +yeelight = yeelight.Yeelight(bulbs) +ser = serial.Serial('/dev/ttyUSB0', 9600) + +while 1 : + out = '' + while ser.inWaiting() > 0: + out = str(ser.readline()) + + if out != '': + l = list(out) + try: + del(l[0]) + del(l[0]) + del(l[2]) + del(l[2]) + del(l[2]) + del(l[2]) + del(l[2]) + out = "".join(l) + #print("try") + except: + print("Fehlerhafter Wert") + finally: + print(out) + # Taste 3 + if(out == "31"): # Abend + yeelight.color(0,0,255) + if(out == "30"): # Nacht + yeelight.temperature(1700) + yeelight.brightness(1) + + # Taste 4 + if(out == "41"): # Hell + yeelight.brightness(100) + if(out == "40"): # Dunkel + yeelight.brightness(1) + + # Taste 5 + if(out == "51"): # An + yeelight.power("on") + if(out == "50"): # Aus + yeelight.power("off") + time.sleep(.250) \ No newline at end of file diff --git a/server.py b/server.py new file mode 100644 index 0000000..f1d1a34 --- /dev/null +++ b/server.py @@ -0,0 +1,10 @@ +from app import config, webserver + +##---------------------------------------------------------------------------------------## +## Main Program +##---------------------------------------------------------------------------------------## +if __name__ == '__main__': + config = config.Config() + webserver = webserver.Webserver(config) + + webserver.run() \ No newline at end of file diff --git a/server_old.py b/server_old.py new file mode 100644 index 0000000..97b1836 --- /dev/null +++ b/server_old.py @@ -0,0 +1,180 @@ +#!flask/bin/python +from flask import Flask, request +from app import yeelight, linux, denon + +import pprint, json + +class LoggingMiddleware(object): + def __init__(self, app): + self._app = app + + def __call__(self, environ, resp): + errorlog = environ['wsgi.errors'] + pprint.pprint(('REQUEST', environ), stream=errorlog) + + def log_response(status, headers, *args): + pprint.pprint(('RESPONSE', status, headers), stream=errorlog) + return resp(status, headers, *args) + + return self._app(environ, log_response) + +global devices +devices = None + +def loadConfig(): + with open('devices.json', encoding='utf-8') as data_file: + devices = json.loads(data_file.read()) +##---------------------------------------------------------------------------------------## +## Config +##---------------------------------------------------------------------------------------## +#bulbs = { +# "fernseher" : { "ip" : "192.168.178.127" }, +# "regal" : { "ip" : "192.168.178.128" }, +# "monitor" : { "ip" : "192.168.178.126" }, +# "bett" : { "ip" : "192.168.178.122" } +# } +brightnessDark = 1 +brightnessBright = 100 +temperatureWarm = 1700 +temperatureNormal = 3000 +temperatureCold = 6500 + +#sshIp = "192.168.178.81" +#sshUser = "root" + +#wakeonlanMac = "38.EA.A7.A1.09.D7" +# +#denonIp = "192.168.178.69" + +##---------------------------------------------------------------------------------------## +## Init +##---------------------------------------------------------------------------------------## +def init(): + app = Flask(__name__) + yeelight = yeelight.Yeelight(devices['yeelight']['rgb']) + wakeonlan = linux.WakeOnLan() + sshcmd = linux.SSHCmd(devices['ssh']['nas']['ip'], devices['ssh']['nas']['user']) # fuer mehrere Verbindungen, mehrere Instanzen erzeugen! + denon = denon.Denon(devices['denon']['ip']) + +##---------------------------------------------------------------------------------------## +## HTTP REQUESTS +##---------------------------------------------------------------------------------------## + +##-----------------------------------------------------------------------------------## +## Yeelight +##-----------------------------------------------------------------------------------## +@app.route('/bulb/on', methods=['PUT', 'POST']) +def httpBulbOn(): + yeelight.power("on") + return 'OK' +@app.route('/bulb/off', methods=['PUT', 'POST']) +def httpBulbOff(): + yeelight.power("off") + return 'OK' +#@app.route('/bulb/brightness', methods=['PUT', 'POST']) +#def httpBrightness(): +# data = str(request.data) +# brightness = int(data["brightness"]) +# if brightness <= 100 and brightness >= 0: +# yeelight.brightness(brightness) +# return 'OK' +@app.route('/bulb/dark', methods=['PUT', 'POST']) +def httpBulbBrightnessDark(): + yeelight.brightness(brightnessDark) + return 'OK' +@app.route('/bulb/bright', methods=['PUT', 'POST']) +def httpBulbBrightnessBright(): + yeelight.brightness(brightnessBright) + return 'OK' +@app.route('/bulb/blue', methods=['PUT', 'POST']) +def httpBulbColorBlue(): + yeelight.color(0,0,255) + return 'OK' +@app.route('/bulb/red', methods=['PUT', 'POST']) +def httpBulbColorRed(): + yeelight.color(255,0,0) + return 'OK' +@app.route('/bulb/warm', methods=['PUT', 'POST']) +def httpBulbTempWarm(): + yeelight.temperature(temperatureWarm) + return 'OK' +@app.route('/bulb/cold', methods=['PUT', 'POST']) +def httpBulbTempCold(): + yeelight.temperature(temperatureCold) + return 'OK' +@app.route('/bulb/normal', methods=['PUT', 'POST']) +def httpBulbNormal(): + yeelight.temperature(temperatureNormal) + return 'OK' +@app.route('/bulb/abend', methods=['PUT', 'POST']) +def httpBulbEvening(): + yeelight.color(0,0,255) + return 'OK' +@app.route('/bulb/nacht', methods=['PUT', 'POST']) +def httpBulbNight(): + yeelight.temperature(temperatureWarm) + yeelight.brightness(brightnessDark) + return 'OK' +@app.route('/bulb/flowMorgen', methods=['PUT', 'POST']) +def httpBulbFlowMorning(): + yeelight.temperatureTransition(startTemp=1700, endTemp=6500, startBrightness=0, + endBrightness=100, seconds=60*15) + return 'OK' +@app.route('/bulb/flowNacht', methods=['PUT', 'POST']) +def httpBulbFlowNight(): + yeelight.temperatureTransition(startTemp=1700, endTemp=1700, startBrightness=100, + endBrightness=1, seconds=60*15) + return 'OK' +@app.route('/bulb/flowStopp', methods=['PUT', 'POST']) +def httpBulbFlowStop(): + yeelight.stopTransition() + return 'OK' + +##-----------------------------------------------------------------------------------## +## Denon +##-----------------------------------------------------------------------------------## +@app.route('/denon/on', methods=['PUT', 'POST']) +def httpReceiverOn(): + denon.power("on") + return 'OK' +@app.route('/denon/off', methods=['PUT', 'POST']) +def httpReceiverOff(): + denon.power("off") + return 'OK' +@app.route('/denon/inet', methods=['PUT', 'POST']) +def httpReceiverInet(): + denon.setInput("Internet Radio") + return 'OK' +@app.route('/denon/tv', methods=['PUT', 'POST']) +def httpReceiverTv(): + denon.setInput("TV Audio") + return 'OK' + +##-----------------------------------------------------------------------------------## +## SSH +##-----------------------------------------------------------------------------------## +@app.route('/wol/nas', methods=['PUT', 'POST']) +def httpNasOn(): + wakeonlan.wakeDevice(devices['wol']['nas']['mac']) + return 'OK' +@app.route('/nas/poweroff', methods=['PUT', 'POST']) +def httpNasOff(): + sshcmd.shutdown + return 'OK' +@app.route('/nas/reboot', methods=['PUT', 'POST']) +def httpNasReboot(): + sshcmd.reboot() + return 'OK' +@app.route('/nas/update', methods=['PUT', 'POST']) +def httpNasUpdate(): + sshcmd.upgrade() + return 'OK' + +##---------------------------------------------------------------------------------------## +## Main Program +##---------------------------------------------------------------------------------------## +if __name__ == '__main__': + #app.wsgi_app = LoggingMiddleware(app.wsgi_app) + loadConfig() + init() + app.run(debug=True, host='0.0.0.0') \ No newline at end of file diff --git a/test/yeelight.json b/test/yeelight.json new file mode 100644 index 0000000..fa20f34 --- /dev/null +++ b/test/yeelight.json @@ -0,0 +1,12 @@ +{ + "fernseher" : { + "power" : "on", + "brightness" : "50", + "r" : "100", + "g" : "0", + "b" : "100" + }, + "bett" : { + "power" : "off" + } +} \ No newline at end of file diff --git a/test/yeelightG.json b/test/yeelightG.json new file mode 100644 index 0000000..f698a77 --- /dev/null +++ b/test/yeelightG.json @@ -0,0 +1,9 @@ +{ + "zimmer" : { + "power" : "on", + "brightness" : "50", + "r" : "100", + "g" : "0", + "b" : "50" + } +} \ No newline at end of file diff --git a/www/.DS_Store b/www/.DS_Store new file mode 100644 index 0000000..a98d804 Binary files /dev/null and b/www/.DS_Store differ diff --git a/www/css/main.css b/www/css/main.css new file mode 100644 index 0000000..e69de29 diff --git a/www/html/.DS_Store b/www/html/.DS_Store new file mode 100644 index 0000000..f7608c6 Binary files /dev/null and b/www/html/.DS_Store differ diff --git a/www/html/devices.html b/www/html/devices.html new file mode 100644 index 0000000..dc722ac --- /dev/null +++ b/www/html/devices.html @@ -0,0 +1,33 @@ +
+

Lampen

+
+
+

Regal

+
+ + +
+
+
+

Monitor

+
+ + +
+
+
+

Fernseher

+
+ + +
+
+
+

Bett

+
+ + +
+
+
+
\ No newline at end of file diff --git a/www/html/settings.html b/www/html/settings.html new file mode 100644 index 0000000..cc33490 --- /dev/null +++ b/www/html/settings.html @@ -0,0 +1,18 @@ +
+
+
+
+ +
+
+ +
+
+ +
+
+
+
+ +
+
\ No newline at end of file diff --git a/www/img/.DS_Store b/www/img/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/www/img/.DS_Store differ diff --git a/www/index.html b/www/index.html new file mode 100644 index 0000000..7664cec --- /dev/null +++ b/www/index.html @@ -0,0 +1,36 @@ + + + + + HomeControl + + + + + + + + + + + + + + +
+
+ + + + + + + diff --git a/www/js/.DS_Store b/www/js/.DS_Store new file mode 100644 index 0000000..c83695f Binary files /dev/null and b/www/js/.DS_Store differ diff --git a/www/js/cm.js b/www/js/cm.js new file mode 100644 index 0000000..651edf0 --- /dev/null +++ b/www/js/cm.js @@ -0,0 +1,62 @@ +//------------------------------------------------------------------------------ +// CM - Hauptfunktion +//------------------------------------------------------------------------------ + +class CM +{ + constructor () + { + this.BodyDataAction(); + + console.log("CM LOADED"); + } + + 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() { + console.log("ONLOAD"); + CM_o = new CM(); + CM_o.Menu("pageDevices"); +}); \ No newline at end of file