Skip to content
Snippets Groups Projects
Commit 2cccd515 authored by Christian Elberfeld's avatar Christian Elberfeld
Browse files

status abfrage über mqtt

parent 0e4c80ea
No related branches found
No related tags found
No related merge requests found
......@@ -12,6 +12,7 @@ RUN pip3 install --upgrade pip
# pip Packages
RUN pip3 install \
flask==1.0.2 \
flask_mqtt \
pytz \
uwsgi \
--upgrade
......
docker run --rm -it ruimarinho/mosquitto mosquitto_pub -h 192.168.0.201 -t warpzone/door/status -m "CLOSED" --retain
docker run --rm -it ruimarinho/mosquitto mosquitto_pub -h 192.168.0.201 -t warpzone/door/status -m "OPEN" --retain
docker run --rm -it ruimarinho/mosquitto mosquitto_sub -h 192.168.0.201 -t warpzone/door/status
\ No newline at end of file
......@@ -6,16 +6,61 @@ from flask import redirect
from flask import render_template
from flask import send_from_directory
from flask import json
from flask_mqtt import Mqtt
from logging.config import dictConfig
dictConfig({
'version': 1,
'formatters': {'default': {
'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
}},
'handlers': {'wsgi': {
'class': 'logging.StreamHandler',
'stream': 'ext://flask.logging.wsgi_errors_stream',
'formatter': 'default'
}},
'root': {
'level': 'INFO',
'handlers': ['wsgi']
}
})
app = Flask(__name__, static_url_path='')
app.config['MQTT_BROKER_URL'] = '192.168.0.201'
app.config['MQTT_BROKER_PORT'] = 1883
app.config['MQTT_USERNAME'] = ''
app.config['MQTT_PASSWORD'] = ''
app.config['MQTT_KEEPALIVE'] = 5
app.config['MQTT_TLS_ENABLED'] = False
zone_door_status = "UNKNOWN"
mqtt = Mqtt(app)
if __name__ == "__main__":
app.run()
@mqtt.on_connect()
def handle_connect(client, userdata, flags, rc):
app.logger.info("mqtt connected")
mqtt.subscribe("warpzone/door/status")
app.logger.info("mqtt subscribed: warpzone/door/status")
@mqtt.on_message()
def handle_mqtt_message(client, userdata, message):
global zone_door_status
data = dict(
topic=message.topic,
payload=message.payload.decode()
)
app.logger.info("mqtt message")
app.logger.info(data)
zone_door_status = message.payload.decode()
app.logger.info("zone_door_status = " + zone_door_status)
# Startseite
@app.route('/')
def view_index():
return 'Warpzone API, see: https://gitlab.warpzone.ms/infrastruktur/warpapi/blob/master/README.md'
global zone_door_status
return 'Warpzone API, current status = ' + zone_door_status + ', see: https://gitlab.warpzone.ms/infrastruktur/warpapi/blob/master/README.md'
# Statische Dateien
@app.route('/files/<path:path>')
......@@ -25,41 +70,26 @@ def send_file(path):
# Statusanzeige auf der Webseite
@app.route('/static')
def view_static():
global zone_door_status
spaceopen = False
now = datetime.datetime.now(pytz.timezone('Europe/Berlin'))
dayofweek = now.strftime("%w")
# Hardcode Mittwoch Abend offen
if dayofweek == "3":
if now.hour >= 18:
spaceopen = True
# Hardcode Samstag Abend offen
if dayofweek == "6":
if now.hour >= 18:
spaceopen = True
if zone_door_status == "OPEN":
spaceopen = True
return render_template('static.html', open = spaceopen)
# Statusanzeige auf der Webseite
@app.route('/status')
def data_status():
global zone_door_status
spaceopen = 0
now = datetime.datetime.now(pytz.timezone('Europe/Berlin'))
dayofweek = now.strftime("%w")
# Hardcode Mittwoch Abend offen
if dayofweek == "3":
if now.hour >= 18:
spaceopen = 1
# Hardcode Samstag Abend offen
if dayofweek == "6":
if now.hour >= 18:
spaceopen = 1
if zone_door_status == "OPEN":
spaceopen = 1
data = {
"tuerOffen": spaceopen,
"tempLounge": 250,
"tempWerkstatt": 250,
"timestamp": 0,
"age": 500
}
}
response = app.response_class(
response=json.dumps(data),
status=200,
......@@ -76,17 +106,10 @@ def data_cccapi():
# Implementierung der SpaceAPI
@app.route('/spaceapi')
def data_spaceapi():
global zone_door_status
spaceopen = False
now = datetime.datetime.now(pytz.timezone('Europe/Berlin'))
dayofweek = now.strftime("%w")
# Hardcode Mittwoch Abend offen
if dayofweek == "3":
if now.hour >= 18:
spaceopen = True
# Hardcode Samstag Abend offen
if dayofweek == "6":
if now.hour >= 18:
spaceopen = True
if zone_door_status == "OPEN":
spaceopen = True
data = {
"api": "0.13",
"space": "warpzone ",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment