Bewaesserungsanlage/Webserver/__init__.py

80 lines
3.1 KiB
Python
Raw Normal View History

2022-07-30 00:19:30 +02:00
from flask import Flask, render_template, request, redirect, url_for
from flask_navigation import Navigation
2022-08-15 23:58:07 +02:00
from Webserver.Translater import Translater, Language
from Zone import Zone
2022-07-30 00:19:30 +02:00
2022-08-15 23:58:07 +02:00
def startWebserver(port, zoneManager):
translater_EN = Translater(Language.ENGLISH)
translater_DE = Translater(Language.GERMAN)
translater = translater_EN
2022-07-30 00:19:30 +02:00
2022-08-15 23:58:07 +02:00
app = Flask("Bewässerungssystem", template_folder="Webserver/templates", static_folder="Webserver/static")
2022-08-15 01:10:34 +02:00
nav = Navigation(app)
2022-07-30 00:19:30 +02:00
2022-08-15 01:10:34 +02:00
nav.Bar('top', [
nav.Item(translater.getTranslation('Dashboard'), 'showDashboard'),
nav.Item(translater.getTranslation('irrigation zones'), 'showZones'),
nav.Item(translater.getTranslation('blocking times'), 'showTimes'),
nav.Item(translater.getTranslation('system settings'), 'showSystem')
2022-08-15 23:58:07 +02:00
# nav.Item('Gfg', 'gfg', {'page': 5}), #(example with pages)
2022-08-15 01:10:34 +02:00
])
2022-07-30 00:19:30 +02:00
2022-08-15 23:58:07 +02:00
# Example Route with pages:
# @app.route('/gfg/<int:page>')
# def gfg(page):
2022-08-15 01:10:34 +02:00
# return render_template('gfg.html', page=page)
2022-07-30 00:19:30 +02:00
2022-08-15 01:10:34 +02:00
@app.route('/')
def startPage():
return redirect(url_for('showDashboard'))
2022-08-15 23:58:07 +02:00
@app.route('/action/<command>/<index_str>/<value_str>')
def executeAction(command=False, index_str=False, value_str=False):
sucess = False
index = int(index_str)
value = int(value_str)
match command:
case "switch_zone_on":
if (index and value):
zoneManager.switchZoneIndexState(zoneIndex=index, state=True, duration=value)
case "switch_zone_off":
if (index and value):
zoneManager.switchZoneIndexState(zoneIndex=index, state=False, duration=value)
case "switch_zone_mode":
if (index and value):
zone = zoneManager.getZone(index)
match value:
case "automatic":
zone.switchMode(autoMode=True)
case "manual":
zone.switchMode(autoMode=False)
case "set_desired_humidity":
if (index and value):
zone = zoneManager.getZone(index)
zone.desiredHumidity = value
return render_template('action.html', translater=translater, zones=zoneManager.zones)
2022-08-15 01:10:34 +02:00
@app.route('/dashboard')
def showDashboard():
2022-08-15 23:58:07 +02:00
return render_template('dashboard.html', translater=translater, zones=zoneManager.zones)
2022-08-15 01:10:34 +02:00
@app.route('/zones')
@app.route('/zones/<zoneNumber>')
def showZones(zoneNumber=False):
2022-08-15 23:58:07 +02:00
if (zoneNumber):
return render_template('zone.html', translater=translater, zone=zoneManager.getZone(zoneNumber))
2022-08-15 01:10:34 +02:00
else:
2022-08-15 23:58:07 +02:00
return render_template('zones.html', translater=translater, zones=zoneManager.zones)
2022-08-15 01:10:34 +02:00
@app.route('/times')
def showTimes():
return render_template('times.html', translater=translater)
@app.route('/system')
def showSystem():
return render_template('system.html', translater=translater)
app.run(debug=True, port=port)