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
|
2022-07-30 00:19:30 +02:00
|
|
|
|
|
|
|
|
2022-08-16 03:13:10 +02:00
|
|
|
class Webserver:
|
|
|
|
def __init__(self, zoneManager, port=80):
|
|
|
|
self.port = port
|
|
|
|
self.zoneManager = zoneManager
|
|
|
|
self.translater_EN = Translater(Language.ENGLISH)
|
|
|
|
self.translater_DE = Translater(Language.GERMAN)
|
2022-08-17 02:14:53 +02:00
|
|
|
self.translater = self.translater_DE
|
2022-07-30 00:19:30 +02:00
|
|
|
|
2022-08-16 03:13:10 +02:00
|
|
|
def startWebserver(self):
|
|
|
|
app = Flask("Bewässerungssystem", template_folder="Webserver/templates", static_folder="Webserver/static")
|
|
|
|
nav = Navigation(app)
|
2022-07-30 00:19:30 +02:00
|
|
|
|
2022-08-16 03:13:10 +02:00
|
|
|
nav.Bar('top', [
|
|
|
|
nav.Item(self.translater.getTranslation('Dashboard'), 'showDashboard'),
|
|
|
|
nav.Item(self.translater.getTranslation('irrigation zones'), 'showZones'),
|
|
|
|
nav.Item(self.translater.getTranslation('blocking times'), 'showTimes'),
|
|
|
|
nav.Item(self.translater.getTranslation('system settings'), 'showSystem')
|
|
|
|
# nav.Item('Gfg', 'gfg', {'page': 5}), #(example with pages)
|
|
|
|
])
|
2022-07-30 00:19:30 +02:00
|
|
|
|
2022-08-16 03:13:10 +02:00
|
|
|
# Example Route with pages:
|
|
|
|
# @app.route('/gfg/<int:page>')
|
|
|
|
# def gfg(page):
|
|
|
|
# return render_template('gfg.html', page=page)
|
2022-07-30 00:19:30 +02:00
|
|
|
|
2022-08-16 03:13:10 +02:00
|
|
|
@app.route('/')
|
|
|
|
def startPage():
|
|
|
|
return redirect(url_for('showDashboard'))
|
2022-08-15 01:10:34 +02:00
|
|
|
|
2022-08-16 03:13:10 +02:00
|
|
|
@app.route('/action', methods=['GET', 'POST'])
|
|
|
|
def executeAction(command=False, index_str=False, value_str=False):
|
|
|
|
sucess = False
|
2022-08-23 23:47:43 +02:00
|
|
|
if(request.method == 'POST'):
|
|
|
|
try:
|
|
|
|
command = request.form['command']
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
index_str = request.form['index']
|
|
|
|
index = int(index_str)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
value_str = request.form['value']
|
|
|
|
value = int(value_str)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
match command:
|
|
|
|
case "switch_zone_on":
|
|
|
|
if (index and value):
|
|
|
|
self.zoneManager.switchZoneIndexState(zoneIndex=index, state=True, duration=value)
|
|
|
|
sucess = True
|
|
|
|
elif(index):
|
|
|
|
self.zoneManager.switchZoneIndexState(zoneIndex=index, state=True, duration=self.zoneManager.systemSettings.defaultManualIrrigationDuration)
|
|
|
|
sucess = True
|
|
|
|
case "switch_zone_off":
|
|
|
|
if (index and value):
|
|
|
|
self.zoneManager.switchZoneIndexState(zoneIndex=index, state=False, duration=value, instant=True)
|
|
|
|
sucess = True
|
|
|
|
elif(index):
|
|
|
|
self.zoneManager.switchZoneIndexState(zoneIndex=index, state=False, duration=self.zoneManager.systemSettings.defaultManualOffDuration, instant=True)
|
|
|
|
sucess = True
|
|
|
|
case "switch_zone_mode":
|
|
|
|
if (index and value_str):
|
|
|
|
zone = self.zoneManager.getZone(index)
|
|
|
|
match value_str:
|
|
|
|
case "automatic":
|
|
|
|
zone.switchMode(autoMode=True)
|
|
|
|
sucess = True
|
|
|
|
case "manual":
|
|
|
|
zone.switchMode(autoMode=False)
|
|
|
|
sucess = True
|
|
|
|
case "set_desired_humidity":
|
|
|
|
if (index and value):
|
|
|
|
zone = self.zoneManager.getZone(index)
|
|
|
|
zone.desiredHumidity = value
|
|
|
|
sucess = True
|
|
|
|
case "delete_jobs_for_zone":
|
|
|
|
if (index):
|
|
|
|
zone = self.zoneManager.getZone(index)
|
|
|
|
self.zoneManager.deleteIrrigationJobsForZone(zone)
|
|
|
|
case "delete_job_by_id":
|
|
|
|
if (index):
|
|
|
|
self.zoneManager.deleteIrrigationJobByID(index)
|
2022-08-25 13:24:11 +02:00
|
|
|
case "get_dashboard_zone_html":
|
|
|
|
if (index):
|
|
|
|
zone = self.zoneManager.getZone(index)
|
|
|
|
return render_template('dashboard/zone.html', translater=self.translater, zoneManager=self.zoneManager, zone=zone)
|
2022-08-25 15:37:54 +02:00
|
|
|
case "get_dashboard_pipeline_html":
|
|
|
|
return render_template("dashboard/pipeline.html", translater=self.translater, zoneManager=self.zoneManager)
|
2022-08-23 23:47:43 +02:00
|
|
|
case "get_zone_list":
|
|
|
|
return self.zoneManager.zonesToJSON()
|
2022-08-24 21:20:54 +02:00
|
|
|
case "get_zone_info":
|
|
|
|
if (index):
|
|
|
|
zone = self.zoneManager.getZone(index)
|
|
|
|
return zone.toJSON()
|
2022-08-23 23:47:43 +02:00
|
|
|
case "get_pipeline":
|
|
|
|
return self.zoneManager.pipelineToJSON()
|
2022-08-25 13:24:11 +02:00
|
|
|
return self.zoneManager.zonesToJSON()
|
|
|
|
#return render_template('action.html', translater=self.translater, zones=self.zoneManager.zones, sucess=sucess)
|
2022-08-15 23:58:07 +02:00
|
|
|
|
2022-08-23 23:47:43 +02:00
|
|
|
|
2022-08-16 03:13:10 +02:00
|
|
|
@app.route('/dashboard')
|
|
|
|
def showDashboard():
|
2022-08-25 13:24:11 +02:00
|
|
|
return render_template('dashboard/dashboard.html', translater=self.translater, zoneManager=self.zoneManager)
|
2022-08-15 01:10:34 +02:00
|
|
|
|
2022-08-16 03:13:10 +02:00
|
|
|
@app.route('/zones')
|
|
|
|
@app.route('/zones/<zoneNumber>')
|
|
|
|
def showZones(zoneNumber=False):
|
|
|
|
if (zoneNumber):
|
2022-08-25 15:37:54 +02:00
|
|
|
return render_template('zones/zone.html', translater=self.translater, zone=self.zoneManager.getZone(zoneNumber))
|
2022-08-16 03:13:10 +02:00
|
|
|
else:
|
2022-08-25 15:37:54 +02:00
|
|
|
return render_template('zones/zones.html', translater=self.translater, zones=self.zoneManager.zones)
|
2022-08-15 01:10:34 +02:00
|
|
|
|
2022-08-16 03:13:10 +02:00
|
|
|
@app.route('/times')
|
|
|
|
def showTimes():
|
|
|
|
return render_template('times.html', translater=self.translater)
|
2022-08-15 01:10:34 +02:00
|
|
|
|
2022-08-16 03:13:10 +02:00
|
|
|
@app.route('/system')
|
|
|
|
def showSystem():
|
|
|
|
return render_template('system.html', translater=self.translater)
|
2022-08-15 01:10:34 +02:00
|
|
|
|
2022-08-16 03:13:10 +02:00
|
|
|
app.run(debug=True, port=self.port)
|