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') }}">
<meta charset="UTF-8">
<meta http-equiv="refresh" content="5" >
<script>
function executeAction(command, index, value) {
send_web_request('{{url_for("executeAction")}}', 'no', 'command=' + command + '&index=' + index + '&value=' + value);
}
</script>
<script>
function executeAction(command, index, value) {
send_web_request('{{url_for("executeAction")}}/' + command + '/' + index + '/' + value, 'no', 'variable');
}
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>
<body>
<header>
@ -43,6 +36,7 @@
<img src="{{ url_for('static', filename='img/header/blumenbeet.jpg') }}" alt="" class="header-img">
</header>
<script src="{{ url_for('static', filename='js/action.js') }}"></script>
<script src="{{ url_for('static', filename='js/webhook.js') }}"></script>
</body>
</html>

View File

@ -33,57 +33,67 @@ class Webserver:
return redirect(url_for('showDashboard'))
@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):
sucess = False
try:
index = int(index_str)
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)
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)
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)
@app.route('/dashboard')
def showDashboard():
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 json
class Zone:
@ -13,6 +14,19 @@ class Zone:
self.endTimeSetState = endTimeSetState
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):
self.zoneManager = zoneManager

View File

@ -1,6 +1,7 @@
import json
import threading
from random import Random
from time import time
from FileIO import FileIO
@ -15,6 +16,20 @@ class ZoneManager:
self.pipeLine = []
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):
for zone in self.zones:
if(zone.number == number):
@ -100,5 +115,12 @@ class IrrigationJob:
self.duration = duration
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):
self.zone.switchState(state=True, duration=self.duration, instant=True)