Autorefresh of Dashboard #3

Merged
Roman_Schenk merged 6 commits from Webserver_Layout into master 2022-08-27 00:31:54 +02:00
5 changed files with 109 additions and 61 deletions
Showing only changes of commit 2747b79f37 - Show all commits

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")}}/' + command + '/' + index + '/' + value, 'no', 'variable'); send_web_request('{{url_for("executeAction")}}', 'no', 'command=' + command + '&index=' + index + '&value=' + value);
}
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> </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,13 +33,20 @@ 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
if(request.method == 'POST'):
try: try:
command = request.form['command']
except:
pass
try:
index_str = request.form['index']
index = int(index_str) index = int(index_str)
except:
pass
try:
value_str = request.form['value']
value = int(value_str) value = int(value_str)
except: except:
pass pass
@ -80,10 +87,13 @@ class Webserver:
case "delete_job_by_id": case "delete_job_by_id":
if (index): if (index):
self.zoneManager.deleteIrrigationJobByID(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)