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

132 lines
5.1 KiB
Python
Raw Normal View History

2018-02-02 17:23:44 +01:00
from flask import Flask, request, jsonify
2018-02-02 00:21:59 +01:00
##---------------------------------------------------------------------------------------##
class Webserver(object):
##---------------------------------------------------------------------------------------##
##-----------------------------------------------------------------------------------##
def __init__(self, config):
##-----------------------------------------------------------------------------------##
print("Webserver initialisiert")
self.settings = config.read("settings")
self.devices = config.read("devices")
2018-02-02 17:23:44 +01:00
self.config = {}
2018-02-02 18:06:07 +01:00
print(self.settings)
print(self.devices)
2018-02-02 17:23:44 +01:00
##-----------------------------------------------------------------------------------##
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
2018-02-02 00:21:59 +01:00
##-----------------------------------------------------------------------------------##
def run(self):
##-----------------------------------------------------------------------------------##
app = Flask(__name__)
2018-02-02 18:03:38 +01:00
if('yeelight' in self.devices):
2018-02-02 00:21:59 +01:00
print("Enabled Yeelight")
from app.devices import yeelight
self.yeelight = yeelight.Yeelight(self.devices['yeelight'])
2018-02-02 17:23:44 +01:00
devices = self.devices['yeelight']
config = self.yeelight.config()
self.createConfig("yeelight", devices, config)
2018-02-02 00:21:59 +01:00
2018-02-02 17:23:44 +01:00
@app.route('/rest/yeelight', methods=['POST'])
2018-02-02 00:21:59 +01:00
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")
2018-02-02 18:03:38 +01:00
if('ssh' in self.devices):
2018-02-02 00:21:59 +01:00
print("Enabled SSH")
from app.devices import ssh
self.ssh = ssh.SSH(self.devices['ssh'])
2018-02-02 17:23:44 +01:00
devices = self.devices['ssh']
config = self.ssh.config()
self.createConfig("ssh", devices, config)
2018-02-02 00:21:59 +01:00
2018-02-02 17:23:44 +01:00
@app.route('/rest/ssh', methods=['POST'])
2018-02-02 00:21:59 +01:00
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")
2018-02-02 18:03:38 +01:00
if('wol' in self.devices):
2018-02-02 00:21:59 +01:00
print("Enabled WOL")
from app.devices import wol
self.wol = wol.WOL(self.devices['wol'])
2018-02-02 17:23:44 +01:00
devices = self.devices['wol']
config = self.wol.config()
self.createConfig("wol", devices, config)
2018-02-02 00:21:59 +01:00
2018-02-02 17:23:44 +01:00
@app.route('/rest/wol', methods=['POST'])
2018-02-02 00:21:59 +01:00
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")
2018-02-02 17:23:44 +01:00
#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)
2018-02-02 00:21:59 +01:00
2018-02-02 17:23:44 +01:00
# @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)
2018-02-02 00:21:59 +01:00
2018-02-02 17:23:44 +01:00
app.run(debug=True, host='0.0.0.0')