implemented Webhooks
This commit is contained in:
parent
f1d73676f5
commit
69d9638456
|
@ -8,5 +8,5 @@ class FileIO:
|
||||||
def loadZones(self):
|
def loadZones(self):
|
||||||
zones = []
|
zones = []
|
||||||
for i in range(12):
|
for i in range(12):
|
||||||
zones.append(Zone(number=i+1, name="Zone " + str(i+1), actualHumidity=100, desiredHumidity=0, autoMode=False, state=False, setState=0, endTimeSetState=0, planedDuration=0))
|
zones.append(Zone(number=i+1, name="Zone " + str(i+1), actualHumidity=50, desiredHumidity=30, autoMode=True, state=False, setState=0, endTimeSetState=0, planedDuration=0))
|
||||||
return zones
|
return zones
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
|
|
||||||
|
|
||||||
class SystemSettings:
|
class SystemSettings:
|
||||||
def __init__(self, dataDir="/Data", multiZoneIrrigation=False):
|
def __init__(self, dataDir="/Data", multiZoneIrrigation=False, defaultAutoIrrigationDuration=10, defaultManualIrrigationDuration=10, defaultManualOffDuration=10, webDurationOptions=[5, 10, 15, 20, 25, 30, 45, 60]):
|
||||||
self.dataDir = dataDir
|
self.dataDir = dataDir
|
||||||
self.multiZoneIrrigation = multiZoneIrrigation
|
self.multiZoneIrrigation = multiZoneIrrigation
|
||||||
|
self.defaultAutoIrrigationDuration = defaultAutoIrrigationDuration
|
||||||
|
self.defaultManualIrrigationDuration = defaultManualIrrigationDuration
|
||||||
|
self.defaultManualOffDuration = defaultManualOffDuration
|
||||||
|
self.webDurationOptions = webDurationOptions
|
|
@ -25,6 +25,7 @@ class Translater:
|
||||||
"automatic mode": "Automatikbetrieb",
|
"automatic mode": "Automatikbetrieb",
|
||||||
"switch to manual mode": "Auf Handbetrieb umstellen",
|
"switch to manual mode": "Auf Handbetrieb umstellen",
|
||||||
"switch to automatic mode": "Auf Automatikbetrieb umstellen",
|
"switch to automatic mode": "Auf Automatikbetrieb umstellen",
|
||||||
|
"minutes": "Minuten",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,22 +1,25 @@
|
||||||
from flask import Flask, render_template, request, redirect, url_for
|
from flask import Flask, render_template, request, redirect, url_for
|
||||||
from flask_navigation import Navigation
|
from flask_navigation import Navigation
|
||||||
from Webserver.Translater import Translater, Language
|
from Webserver.Translater import Translater, Language
|
||||||
from Zone import Zone
|
|
||||||
|
|
||||||
|
|
||||||
def startWebserver(port, zoneManager):
|
class Webserver:
|
||||||
translater_EN = Translater(Language.ENGLISH)
|
def __init__(self, zoneManager, port=80):
|
||||||
translater_DE = Translater(Language.GERMAN)
|
self.port = port
|
||||||
translater = translater_EN
|
self.zoneManager = zoneManager
|
||||||
|
self.translater_EN = Translater(Language.ENGLISH)
|
||||||
|
self.translater_DE = Translater(Language.GERMAN)
|
||||||
|
self.translater = self.translater_EN
|
||||||
|
|
||||||
|
def startWebserver(self):
|
||||||
app = Flask("Bewässerungssystem", template_folder="Webserver/templates", static_folder="Webserver/static")
|
app = Flask("Bewässerungssystem", template_folder="Webserver/templates", static_folder="Webserver/static")
|
||||||
nav = Navigation(app)
|
nav = Navigation(app)
|
||||||
|
|
||||||
nav.Bar('top', [
|
nav.Bar('top', [
|
||||||
nav.Item(translater.getTranslation('Dashboard'), 'showDashboard'),
|
nav.Item(self.translater.getTranslation('Dashboard'), 'showDashboard'),
|
||||||
nav.Item(translater.getTranslation('irrigation zones'), 'showZones'),
|
nav.Item(self.translater.getTranslation('irrigation zones'), 'showZones'),
|
||||||
nav.Item(translater.getTranslation('blocking times'), 'showTimes'),
|
nav.Item(self.translater.getTranslation('blocking times'), 'showTimes'),
|
||||||
nav.Item(translater.getTranslation('system settings'), 'showSystem')
|
nav.Item(self.translater.getTranslation('system settings'), 'showSystem')
|
||||||
# nav.Item('Gfg', 'gfg', {'page': 5}), #(example with pages)
|
# nav.Item('Gfg', 'gfg', {'page': 5}), #(example with pages)
|
||||||
])
|
])
|
||||||
|
|
||||||
|
@ -29,51 +32,68 @@ def startWebserver(port, zoneManager):
|
||||||
def startPage():
|
def startPage():
|
||||||
return redirect(url_for('showDashboard'))
|
return redirect(url_for('showDashboard'))
|
||||||
|
|
||||||
@app.route('/action/<command>/<index_str>/<value_str>')
|
@app.route('/action', methods=['GET', 'POST'])
|
||||||
|
@app.route('/action/<command>', methods=['GET', 'POST'])
|
||||||
|
@app.route('/action/<command>/<index_str>', methods=['GET', 'POST'])
|
||||||
|
@app.route('/action/<command>/<index_str>/<value_str>', methods=['GET', 'POST'])
|
||||||
def executeAction(command=False, index_str=False, value_str=False):
|
def executeAction(command=False, index_str=False, value_str=False):
|
||||||
sucess = False
|
sucess = False
|
||||||
|
try:
|
||||||
index = int(index_str)
|
index = int(index_str)
|
||||||
value = int(value_str)
|
value = int(value_str)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
match command:
|
match command:
|
||||||
case "switch_zone_on":
|
case "switch_zone_on":
|
||||||
if (index and value):
|
if (index and value):
|
||||||
zoneManager.switchZoneIndexState(zoneIndex=index, state=True, duration=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":
|
case "switch_zone_off":
|
||||||
if (index and value):
|
if (index and value):
|
||||||
zoneManager.switchZoneIndexState(zoneIndex=index, state=False, duration=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":
|
case "switch_zone_mode":
|
||||||
if (index and value):
|
if (index and value_str):
|
||||||
zone = zoneManager.getZone(index)
|
zone = self.zoneManager.getZone(index)
|
||||||
match value:
|
match value_str:
|
||||||
case "automatic":
|
case "automatic":
|
||||||
zone.switchMode(autoMode=True)
|
zone.switchMode(autoMode=True)
|
||||||
|
sucess = True
|
||||||
case "manual":
|
case "manual":
|
||||||
zone.switchMode(autoMode=False)
|
zone.switchMode(autoMode=False)
|
||||||
|
sucess = True
|
||||||
case "set_desired_humidity":
|
case "set_desired_humidity":
|
||||||
if (index and value):
|
if (index and value):
|
||||||
zone = zoneManager.getZone(index)
|
zone = self.zoneManager.getZone(index)
|
||||||
zone.desiredHumidity = value
|
zone.desiredHumidity = value
|
||||||
|
sucess = True
|
||||||
|
|
||||||
return render_template('action.html', translater=translater, zones=zoneManager.zones)
|
return render_template('action.html', translater=self.translater, zones=self.zoneManager.zones, sucess=sucess)
|
||||||
|
|
||||||
@app.route('/dashboard')
|
@app.route('/dashboard')
|
||||||
def showDashboard():
|
def showDashboard():
|
||||||
return render_template('dashboard.html', translater=translater, zones=zoneManager.zones)
|
return render_template('dashboard.html', translater=self.translater, zoneManager=self.zoneManager)
|
||||||
|
|
||||||
@app.route('/zones')
|
@app.route('/zones')
|
||||||
@app.route('/zones/<zoneNumber>')
|
@app.route('/zones/<zoneNumber>')
|
||||||
def showZones(zoneNumber=False):
|
def showZones(zoneNumber=False):
|
||||||
if (zoneNumber):
|
if (zoneNumber):
|
||||||
return render_template('zone.html', translater=translater, zone=zoneManager.getZone(zoneNumber))
|
return render_template('zone.html', translater=self.translater, zone=self.zoneManager.getZone(zoneNumber))
|
||||||
else:
|
else:
|
||||||
return render_template('zones.html', translater=translater, zones=zoneManager.zones)
|
return render_template('zones.html', translater=self.translater, zones=self.zoneManager.zones)
|
||||||
|
|
||||||
@app.route('/times')
|
@app.route('/times')
|
||||||
def showTimes():
|
def showTimes():
|
||||||
return render_template('times.html', translater=translater)
|
return render_template('times.html', translater=self.translater)
|
||||||
|
|
||||||
@app.route('/system')
|
@app.route('/system')
|
||||||
def showSystem():
|
def showSystem():
|
||||||
return render_template('system.html', translater=translater)
|
return render_template('system.html', translater=self.translater)
|
||||||
|
|
||||||
app.run(debug=True, port=port)
|
app.run(debug=True, port=self.port)
|
||||||
|
|
5
Zone.py
5
Zone.py
|
@ -13,6 +13,9 @@ class Zone:
|
||||||
self.endTimeSetState = endTimeSetState
|
self.endTimeSetState = endTimeSetState
|
||||||
self.planedDuration = planedDuration
|
self.planedDuration = planedDuration
|
||||||
|
|
||||||
|
def setZoneManager(self, zoneManager):
|
||||||
|
self.zoneManager = zoneManager
|
||||||
|
|
||||||
def timeOver(self):
|
def timeOver(self):
|
||||||
return time.time() > self.endTimeSetState
|
return time.time() > self.endTimeSetState
|
||||||
|
|
||||||
|
@ -20,7 +23,7 @@ class Zone:
|
||||||
match self.setState:
|
match self.setState:
|
||||||
case 0:
|
case 0:
|
||||||
if(self.desiredHumidity > self.actualHumidity):
|
if(self.desiredHumidity > self.actualHumidity):
|
||||||
self.state = True
|
self.zoneManager.switchZoneState(zone=self, state=True, duration=self.zoneManager.systemSettings.defaultAutoIrrigationDuration, instant=False)
|
||||||
else:
|
else:
|
||||||
self.state = False
|
self.state = False
|
||||||
case 1:
|
case 1:
|
||||||
|
|
|
@ -5,6 +5,8 @@ class ZoneManager:
|
||||||
self.systemSettings = systemSettings
|
self.systemSettings = systemSettings
|
||||||
self.fileIO = FileIO
|
self.fileIO = FileIO
|
||||||
self.zones = fileIO.loadZones()
|
self.zones = fileIO.loadZones()
|
||||||
|
for zone in self.zones:
|
||||||
|
zone.setZoneManager(self)
|
||||||
self.pipeLine = []
|
self.pipeLine = []
|
||||||
|
|
||||||
def getZone(self, number):
|
def getZone(self, number):
|
||||||
|
@ -44,6 +46,7 @@ class ZoneManager:
|
||||||
def cronJobs(self):
|
def cronJobs(self):
|
||||||
self.refreshStates()
|
self.refreshStates()
|
||||||
self.processPipeline()
|
self.processPipeline()
|
||||||
|
print("Executed Cron Jobs of ZoneManager")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
22
main.py
22
main.py
|
@ -1,12 +1,22 @@
|
||||||
from Webserver import startWebserver
|
import threading
|
||||||
|
import time
|
||||||
|
|
||||||
|
from Webserver import Webserver
|
||||||
from ZoneManager import ZoneManager
|
from ZoneManager import ZoneManager
|
||||||
from SystemSettings import SystemSettings
|
from SystemSettings import SystemSettings
|
||||||
from FileIO import FileIO
|
from FileIO import FileIO
|
||||||
|
|
||||||
|
systemSettings = SystemSettings(dataDir="/Data", multiZoneIrrigation=False, defaultAutoIrrigationDuration=10, defaultManualIrrigationDuration=10, defaultManualOffDuration=10, webDurationOptions=[5, 10, 15, 20, 25, 30, 45, 60])
|
||||||
|
fileIO = FileIO(systemSettings)
|
||||||
|
zoneManager = ZoneManager(systemSettings=systemSettings, fileIO=fileIO)
|
||||||
|
webserver = Webserver(zoneManager=zoneManager, port=80)
|
||||||
|
|
||||||
|
def cronJobs():
|
||||||
|
while True:
|
||||||
|
time.sleep(1)
|
||||||
|
zoneManager.cronJobs()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
systemSettings = SystemSettings(dataDir="/Data", multiZoneIrrigation=True)
|
cronjob_Thread = threading.Thread(target=cronJobs)
|
||||||
fileIO = FileIO(systemSettings)
|
cronjob_Thread.start()
|
||||||
zoneManager = ZoneManager(systemSettings=systemSettings, fileIO=fileIO)
|
webserver.startWebserver()
|
||||||
startWebserver(port=80, zoneManager=zoneManager)
|
|
||||||
print("webserver started")
|
|
||||||
|
|
Loading…
Reference in New Issue