implemented actions get_zone_list and get_pipeline

This commit is contained in:
schrom01 2022-08-23 23:47:43 +02:00
parent 8c861b3cbd
commit 2747b79f37
5 changed files with 109 additions and 61 deletions

View File

@ -6,19 +6,12 @@
<link rel="stylesheet" href="{{ url_for('static', filename='Styles/switch.css') }}"> <link rel="stylesheet" href="{{ url_for('static', filename='Styles/switch.css') }}">
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta http-equiv="refresh" content="5" > <meta http-equiv="refresh" content="5" >
<script>
<script> function executeAction(command, index, value) {
function executeAction(command, index, value) { send_web_request('{{url_for("executeAction")}}', 'no', 'command=' + command + '&index=' + index + '&value=' + value);
send_web_request('{{url_for("executeAction")}}/' + command + '/' + index + '/' + value, 'no', 'variable'); }
} </script>
function executeActionByValueID(command, index, valueID, valueFactor){
var value = document.getElementById(valueID).value * valueFactor;
executeAction(command, index, value);
}
function switchZoneMode(autoMode, zone) {
executeAction('switch_zone_mode' ,zone, (autoMode ? 'automatic' : 'manual'))
}
</script>
</head> </head>
<body> <body>
<header> <header>
@ -43,6 +36,7 @@
<img src="{{ url_for('static', filename='img/header/blumenbeet.jpg') }}" alt="" class="header-img"> <img src="{{ url_for('static', filename='img/header/blumenbeet.jpg') }}" alt="" class="header-img">
</header> </header>
<script src="{{ url_for('static', filename='js/action.js') }}"></script>
<script src="{{ url_for('static', filename='js/webhook.js') }}"></script> <script src="{{ url_for('static', filename='js/webhook.js') }}"></script>
</body> </body>
</html> </html>

View File

@ -33,57 +33,67 @@ class Webserver:
return redirect(url_for('showDashboard')) return redirect(url_for('showDashboard'))
@app.route('/action', methods=['GET', 'POST']) @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: if(request.method == 'POST'):
index = int(index_str) try:
value = int(value_str) command = request.form['command']
except: except:
pass pass
match command: try:
case "switch_zone_on": index_str = request.form['index']
if (index and value): index = int(index_str)
self.zoneManager.switchZoneIndexState(zoneIndex=index, state=True, duration=value) except:
sucess = True pass
elif(index): try:
self.zoneManager.switchZoneIndexState(zoneIndex=index, state=True, duration=self.zoneManager.systemSettings.defaultManualIrrigationDuration) value_str = request.form['value']
sucess = True value = int(value_str)
case "switch_zone_off": except:
if (index and value): pass
self.zoneManager.switchZoneIndexState(zoneIndex=index, state=False, duration=value, instant=True) match command:
sucess = True case "switch_zone_on":
elif(index): if (index and value):
self.zoneManager.switchZoneIndexState(zoneIndex=index, state=False, duration=self.zoneManager.systemSettings.defaultManualOffDuration, instant=True) self.zoneManager.switchZoneIndexState(zoneIndex=index, state=True, duration=value)
sucess = True sucess = True
case "switch_zone_mode": elif(index):
if (index and value_str): self.zoneManager.switchZoneIndexState(zoneIndex=index, state=True, duration=self.zoneManager.systemSettings.defaultManualIrrigationDuration)
zone = self.zoneManager.getZone(index) sucess = True
match value_str: case "switch_zone_off":
case "automatic": if (index and value):
zone.switchMode(autoMode=True) self.zoneManager.switchZoneIndexState(zoneIndex=index, state=False, duration=value, instant=True)
sucess = True sucess = True
case "manual": elif(index):
zone.switchMode(autoMode=False) self.zoneManager.switchZoneIndexState(zoneIndex=index, state=False, duration=self.zoneManager.systemSettings.defaultManualOffDuration, instant=True)
sucess = True sucess = True
case "set_desired_humidity": case "switch_zone_mode":
if (index and value): if (index and value_str):
zone = self.zoneManager.getZone(index) zone = self.zoneManager.getZone(index)
zone.desiredHumidity = value match value_str:
sucess = True case "automatic":
case "delete_jobs_for_zone": zone.switchMode(autoMode=True)
if (index): sucess = True
zone = self.zoneManager.getZone(index) case "manual":
self.zoneManager.deleteIrrigationJobsForZone(zone) zone.switchMode(autoMode=False)
case "delete_job_by_id": sucess = True
if (index): case "set_desired_humidity":
self.zoneManager.deleteIrrigationJobByID(index) 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)
case "get_zone_list":
return self.zoneManager.zonesToJSON()
case "get_pipeline":
return self.zoneManager.pipelineToJSON()
return render_template('action.html', translater=self.translater, zones=self.zoneManager.zones, sucess=sucess) 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=self.translater, zoneManager=self.zoneManager) return render_template('dashboard.html', translater=self.translater, zoneManager=self.zoneManager)

View File

@ -0,0 +1,8 @@
function executeActionByValueID(command, index, valueID, valueFactor){
var value = document.getElementById(valueID).value * valueFactor;
executeAction(command, index, value);
}
function switchZoneMode(autoMode, zone) {
executeAction('switch_zone_mode' ,zone, (autoMode ? 'automatic' : 'manual'))
}

14
Zone.py
View File

@ -1,4 +1,5 @@
import time import time
import json
class Zone: class Zone:
@ -13,6 +14,19 @@ class Zone:
self.endTimeSetState = endTimeSetState self.endTimeSetState = endTimeSetState
self.planedDuration = planedDuration self.planedDuration = planedDuration
def toJSON(self):
return {
"number": self.number,
"name": self.name,
"actualHumidity": self.actualHumidity,
"desiredHumidity": self.desiredHumidity,
"autoMode": self.autoMode,
"state": self.state,
"setState": self.setState,
"endTimeSetState" : self.endTimeSetState,
"planedDuration": self.planedDuration,
}
def setZoneManager(self, zoneManager): def setZoneManager(self, zoneManager):
self.zoneManager = zoneManager self.zoneManager = zoneManager

View File

@ -1,6 +1,7 @@
import json
import threading import threading
from random import Random from random import Random
from time import time
from FileIO import FileIO from FileIO import FileIO
@ -15,6 +16,20 @@ class ZoneManager:
self.pipeLine = [] self.pipeLine = []
self.piplineMutexLock = threading.Lock() self.piplineMutexLock = threading.Lock()
def zonesToJSON(self):
zoneList = []
for zone in self.zones:
zoneList.append(zone.toJSON())
return zoneList
def pipelineToJSON(self):
jobList = []
with self.piplineMutexLock:
for irrigationJob in self.pipeLine:
jobList.append(irrigationJob.toJSON())
return jobList
def getZone(self, number): def getZone(self, number):
for zone in self.zones: for zone in self.zones:
if(zone.number == number): if(zone.number == number):
@ -100,5 +115,12 @@ class IrrigationJob:
self.duration = duration self.duration = duration
self.zone.switchState(state=True, duration=duration, instant=False) self.zone.switchState(state=True, duration=duration, instant=False)
def toJSON(self):
return {
"id": self.id,
"zone": self.zone.toJSON(),
"duration": self.duration,
}
def process(self): def process(self):
self.zone.switchState(state=True, duration=self.duration, instant=True) self.zone.switchState(state=True, duration=self.duration, instant=True)