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/app/webserver.py
2018-02-02 18:21:41 +01:00

132 lines
5.1 KiB
Python

from flask import Flask, request, jsonify
##---------------------------------------------------------------------------------------##
class Webserver(object):
##---------------------------------------------------------------------------------------##
##-----------------------------------------------------------------------------------##
def __init__(self, config):
##-----------------------------------------------------------------------------------##
print("Webserver initialisiert")
self.settings = config.read("settings")
self.devices = config.read("devices")
self.config = {}
print(self.settings)
print(self.devices)
##-----------------------------------------------------------------------------------##
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):
##-----------------------------------------------------------------------------------##
app = Flask(__name__)
if('yeelight' in self.devices):
print("Enabled Yeelight")
from app.devices import yeelight
self.yeelight = yeelight.Yeelight(self.devices['yeelight'])
devices = self.devices['yeelight']
config = self.yeelight.config()
self.createConfig("yeelight", devices, config)
@app.route('/rest/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('ssh' in self.devices):
print("Enabled SSH")
from app.devices import ssh
self.ssh = ssh.SSH(self.devices['ssh'])
devices = self.devices['ssh']
config = self.ssh.config()
self.createConfig("ssh", devices, config)
@app.route('/rest/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('wol' in self.devices):
print("Enabled WOL")
from app.devices import wol
self.wol = wol.WOL(self.devices['wol'])
devices = self.devices['wol']
config = self.wol.config()
self.createConfig("wol", devices, config)
@app.route('/rest/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 = denon.Denon(self.devices['denon'])
# devices = self.devices['denon']
# config = self.denon.config()
# self.createConfig("denon", devices, config)
# @app.route('/rest/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.route('/rest/config', methods=['GET'])
def config():
return jsonify(self.config)
app.run(debug=True, host='0.0.0.0', port=5003)