-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added configs, updated mqtt singleton
- Loading branch information
Showing
20 changed files
with
340 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"python.pythonPath": "c:\\Users\\Me\\Dev\\Github\\BatteryTester\\code\\Python\\WebApp\\env\\Scripts\\python.exe" | ||
"python.languageServer": "Pylance" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
FROM tiangolo/uwsgi-nginx-flask:latest | ||
|
||
# Set the working directory to /app | ||
WORKDIR /app | ||
# | ||
# # Copy the current directory contents into the container at /app | ||
ADD requirements.txt /app | ||
COPY ./app /app | ||
WORKDIR /BatteryTester | ||
ENV STATIC_URL /static | ||
ENV STATIC_PATH /BatteryTester/app/static | ||
ADD requirements.txt /BatteryTester | ||
ADD main.py /BatteryTester | ||
ADD config.py /BatteryTester | ||
ADD uwsgi.ini /BatteryTester | ||
COPY ./app /BatteryTester/app | ||
# # Install the dependencies | ||
RUN pip install -r requirements.txt | ||
|
||
# run the command to start uWSGI | ||
# CMD ["uwsgi", "app.ini"] | ||
# CMD ["uwsgi", "uwsgi.ini"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
FROM crawforc3/raspberrypi-uwsgi-nginx-flask:latest | ||
WORKDIR /BatteryTester | ||
ENV STATIC_URL /static | ||
ENV STATIC_PATH /BatteryTester/app/static | ||
ADD requirements.txt /BatteryTester | ||
ADD main.py /BatteryTester | ||
ADD config.py /BatteryTester | ||
ADD uwsgi.ini /BatteryTester | ||
COPY ./app /BatteryTester/app | ||
# # Install the dependencies | ||
RUN pip install -r requirements.txt | ||
|
||
# run the command to start uWSGI | ||
# CMD ["uwsgi", "uwsgi.ini"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,127 @@ | ||
from flask import Flask, Response, request, url_for, render_template, jsonify, session, Config | ||
from flask import ( | ||
Flask, | ||
Response, | ||
request, | ||
url_for, | ||
redirect, | ||
render_template, | ||
jsonify, | ||
session, | ||
Config, | ||
g | ||
) | ||
from multiprocessing import Queue, Value | ||
import json | ||
from .settings import settings | ||
from .firmware import firmware | ||
from .mqtt import run, mqttPublish | ||
from .mqtt import mqtt | ||
from .logger import log | ||
from .message import Message | ||
|
||
# --------------------------------------------------------------------------- # | ||
# --------------------------------------------------------------------------- # | ||
# Sharing Data Between Processes Using multiprocessing Queue | ||
# --------------------------------------------------------------------------- # | ||
theQueue = Queue() | ||
cellCount = Value('i', 0) | ||
# --------------------------------------------------------------------------- # | ||
theQueue = Queue() | ||
testers = Value("i", 0) | ||
|
||
|
||
def create_app(config_class=Config): | ||
# create and configure the app | ||
app = Flask(__name__) | ||
app.config.from_object(config_class) | ||
app.register_blueprint(settings, url_prefix="") | ||
app.register_blueprint(firmware, url_prefix="") | ||
init_app() | ||
|
||
@app.route("/") | ||
def render_index(): | ||
return render_template("index.html", operation = 'Monitor', cellCount = cellCount.value) | ||
|
||
@app.route('/operation', methods=['POST']) | ||
def operation(): | ||
current_operation = request.form.get("operation") | ||
mqttPublish(current_operation, "operation") | ||
return jsonify(status="success") | ||
|
||
@app.route("/listen") | ||
def listen(): | ||
def respond_to_client(): | ||
global theQueue | ||
while not theQueue.empty(): | ||
theMessage = theQueue.get() | ||
yield f"data: {theMessage.data}\nevent: {theMessage.topic}\n\n" | ||
return Response(respond_to_client(), mimetype='text/event-stream') | ||
return app | ||
|
||
# --------------------------------------------------------------------------- # | ||
# create and configure the app | ||
app = Flask(__name__) | ||
if app.config["ENV"] == "production": | ||
app.config.from_object("config.ProductionConfig") | ||
else: | ||
app.config.from_object("config.DevelopmentConfig") | ||
app.register_blueprint(settings, url_prefix="") | ||
app.register_blueprint(firmware, url_prefix="") | ||
|
||
print(app.config) | ||
with app.app_context(): | ||
init_mqtt() | ||
# @app.teardown_appcontext | ||
# def teardown_mqtt(exception): | ||
# log.debug("************teardown_mqtt************") | ||
# mqtt = g.pop('mqtt', None) | ||
# if mqtt is not None: | ||
# mqtt.disconnect() | ||
@app.route("/") | ||
def render_index(): | ||
session["testers"] = testers.value | ||
return render_template( | ||
"index.html", operation="Monitor", cellCount=testers.value * 2 | ||
) | ||
|
||
@app.route("/refresh") | ||
def render_refresh(): | ||
testers.value = 0 | ||
con = mqtt.instance() | ||
con.mqttPublish("", "ping") | ||
return redirect(url_for("render_index")) | ||
|
||
@app.route("/operation", methods=["POST"]) | ||
def operation(): | ||
current_operation = request.form.get("operation") | ||
con = mqtt.instance() | ||
con.mqttPublish(current_operation, "operation") | ||
return jsonify(status="success") | ||
|
||
@app.route("/listen") | ||
def listen(): | ||
def respond_to_client(): | ||
global theQueue | ||
while not theQueue.empty(): | ||
theMessage = theQueue.get() | ||
yield f"data: {theMessage.data}\nevent: {theMessage.topic}\n\n" | ||
|
||
return Response(respond_to_client(), mimetype="text/event-stream") | ||
|
||
return app | ||
|
||
|
||
# --------------------------------------------------------------------------- # | ||
# MQTT On Message | ||
# --------------------------------------------------------------------------- # | ||
# --------------------------------------------------------------------------- # | ||
|
||
|
||
def on_stat(client, userdata, message): | ||
|
||
global theQueue | ||
msg = message.payload.decode(encoding='UTF-8') | ||
log.debug("Received STAT {} : {}".format(message.topic, msg)) | ||
if "monitor" in message.topic: | ||
message = json.loads(msg) | ||
theMessage = Message("monitor", json.dumps(message)) | ||
theQueue.put(theMessage) | ||
elif "mode" in message.topic: | ||
message = json.loads(msg) | ||
theMessage = Message("mode", json.dumps(message)) | ||
theQueue.put(theMessage) | ||
elif "result" in message.topic: | ||
message = json.loads(msg) | ||
theMessage = Message("result", json.dumps(message)) | ||
theQueue.put(theMessage) | ||
global theQueue | ||
msg = message.payload.decode(encoding="UTF-8") | ||
log.debug("Received STAT {} : {}".format(message.topic, msg)) | ||
if "monitor" in message.topic: | ||
theMessage = Message("monitor", json.dumps(json.loads(msg))) | ||
theQueue.put(theMessage) | ||
elif "mode" in message.topic: | ||
theMessage = Message("mode", json.dumps(json.loads(msg))) | ||
theQueue.put(theMessage) | ||
elif "result" in message.topic: | ||
theMessage = Message("result", json.dumps(json.loads(msg))) | ||
theQueue.put(theMessage) | ||
|
||
|
||
def on_tele(client, userdata, message): | ||
global testers | ||
msg = message.payload.decode(encoding="UTF-8").upper() | ||
log.debug("Received TELE {} : {}".format(message.topic, msg)) | ||
if "ping" in message.topic: | ||
pl = json.loads(msg) | ||
tester = int(pl["TESTERNUMBER"]) | ||
if tester > testers.value: | ||
testers.value = tester | ||
log.debug("cellCount at on_tele {} ".format(testers.value)) | ||
|
||
global cellCount | ||
msg = message.payload.decode(encoding='UTF-8').upper() | ||
log.debug("Received TELE {} : {}".format(message.topic, msg)) | ||
if "ping" in message.topic: | ||
cellCount.value += 2 | ||
log.debug("cellCount at on_tele {} ".format(cellCount.value)) | ||
|
||
def on_cmnd(client, userdata, message): | ||
|
||
global theQueue | ||
msg = message.payload.decode(encoding='UTF-8') | ||
log.debug("Received CMND {} : {}".format(message.topic, msg)) | ||
if "operation" in message.topic: | ||
theMessage = Message("operation", msg) | ||
theQueue.put(theMessage) | ||
|
||
def init_app(): | ||
log.info("********************************************BatteryTester init()") | ||
cellCount.value = 0 | ||
run(on_stat, on_tele, on_cmnd) | ||
|
||
# if __name__ == "__main__": | ||
# log.info("BatteryTester starting up in debug mode...") | ||
# init_app() | ||
# app.run(host='0.0.0.0', debug=True, port=80) | ||
global theQueue | ||
msg = message.payload.decode(encoding="UTF-8") | ||
log.debug("Received CMND {} : {}".format(message.topic, msg)) | ||
if "operation" in message.topic: | ||
theMessage = Message("operation", msg) | ||
theQueue.put(theMessage) | ||
|
||
|
||
def init_mqtt(): | ||
testers.value = 0 | ||
con = mqtt.instance(on_stat, on_tele, on_cmnd) | ||
log.info("************BatteryTester init_mqtt()*********") | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.