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/server_old.py
Kai Wansart 9c3e560900 .
2018-02-02 00:21:59 +01:00

180 lines
6.1 KiB
Python

#!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')