Switch from socket.io to REST for cadence streaming in attempt to fix timeout issue
This commit is contained in:
2
Procfile
2
Procfile
@@ -1 +1 @@
|
|||||||
web: gunicorn --worker-class eventlet -w 1 --timeout 600 app:app
|
web: gunicorn app:app --workers=4
|
||||||
28
app.py
28
app.py
@@ -3,7 +3,7 @@ import json
|
|||||||
from urllib import response
|
from urllib import response
|
||||||
from dateutil.relativedelta import relativedelta
|
from dateutil.relativedelta import relativedelta
|
||||||
import os
|
import os
|
||||||
from flask import Flask, render_template
|
from flask import Flask, render_template, request
|
||||||
import jinja_partials
|
import jinja_partials
|
||||||
from flask_htmx import HTMX
|
from flask_htmx import HTMX
|
||||||
import minify_html
|
import minify_html
|
||||||
@@ -18,7 +18,7 @@ app = Flask(__name__)
|
|||||||
app.config['SECRET_KEY'] = 'secret!'
|
app.config['SECRET_KEY'] = 'secret!'
|
||||||
jinja_partials.register_extensions(app)
|
jinja_partials.register_extensions(app)
|
||||||
htmx = HTMX(app)
|
htmx = HTMX(app)
|
||||||
socketio = SocketIO(app, cors_allowed_origins='*')
|
#socketio = SocketIO(app, cors_allowed_origins='*')
|
||||||
db = DataBase(app)
|
db = DataBase(app)
|
||||||
|
|
||||||
|
|
||||||
@@ -37,12 +37,12 @@ def response_minify(response):
|
|||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
@socketio.on('message')
|
# @socketio.on('message')
|
||||||
def handle_message(data):
|
# def handle_message(data):
|
||||||
current_time = datetime.now().replace(
|
# current_time = datetime.now().replace(
|
||||||
microsecond=0).isoformat()
|
# microsecond=0).isoformat()
|
||||||
print('' + current_time + ' ' + json.dumps(data))
|
# print('' + current_time + ' ' + json.dumps(data))
|
||||||
db.insert_cadence(data['rpm'], data['id'])
|
# db.insert_cadence(data['rpm'], data['id'])
|
||||||
|
|
||||||
|
|
||||||
@ app.route("/")
|
@ app.route("/")
|
||||||
@@ -73,7 +73,17 @@ def overview(device_id):
|
|||||||
return render_template('overview.html', last_cadence=last_cadence, cadences=cadences[-15:], graph_data=graph_data)
|
return render_template('overview.html', last_cadence=last_cadence, cadences=cadences[-15:], graph_data=graph_data)
|
||||||
|
|
||||||
|
|
||||||
|
@ app.route("/cadence", methods=['POST'])
|
||||||
|
def cadence():
|
||||||
|
data = request.get_json()
|
||||||
|
print('' + datetime.now().replace(microsecond=0).isoformat() +
|
||||||
|
' ' + json.dumps(data))
|
||||||
|
db.insert_cadence(data['rpm'], data['id'])
|
||||||
|
return 'ok'
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# Bind to PORT if defined, otherwise default to 5000.
|
# Bind to PORT if defined, otherwise default to 5000.
|
||||||
port = int(os.environ.get('PORT', 5000))
|
port = int(os.environ.get('PORT', 5000))
|
||||||
socketio.run(app, host='127.0.0.1', port=port)
|
#socketio.run(app, host='127.0.0.1', port=port)
|
||||||
|
app.run(host='127.0.0.1', port=port)
|
||||||
|
|||||||
4
db.py
4
db.py
@@ -59,3 +59,7 @@ class DataBase():
|
|||||||
|
|
||||||
def get_device(self, device_id):
|
def get_device(self, device_id):
|
||||||
return self.execute('SELECT * FROM device WHERE id = %s', [device_id], one=True)
|
return self.execute('SELECT * FROM device WHERE id = %s', [device_id], one=True)
|
||||||
|
|
||||||
|
def insert_cadence(self, rpm, device_id):
|
||||||
|
self.execute('INSERT INTO cadence (rpm, device_id) VALUES (%s, %s)',
|
||||||
|
[rpm, device_id], commit=True)
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ async function connect(props) {
|
|||||||
console.log("> Notifications started");
|
console.log("> Notifications started");
|
||||||
characteristic.addEventListener("characteristicvaluechanged", props.onChange);
|
characteristic.addEventListener("characteristicvaluechanged", props.onChange);
|
||||||
console.log("> Characteristic value changed event listener added");
|
console.log("> Characteristic value changed event listener added");
|
||||||
|
/*
|
||||||
socket = io({
|
socket = io({
|
||||||
reconnection: true,
|
reconnection: true,
|
||||||
reconnectionDelay: 1000,
|
reconnectionDelay: 1000,
|
||||||
@@ -43,6 +44,7 @@ async function connect(props) {
|
|||||||
}
|
}
|
||||||
// else the socket will automatically try to reconnect
|
// else the socket will automatically try to reconnect
|
||||||
});
|
});
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
async function disconnect() {
|
async function disconnect() {
|
||||||
@@ -88,7 +90,15 @@ function parseCSC(e) {
|
|||||||
if (prevRes) {
|
if (prevRes) {
|
||||||
let rpm = revsToRPM(prevRes, res);
|
let rpm = revsToRPM(prevRes, res);
|
||||||
if (rpm > 0)
|
if (rpm > 0)
|
||||||
socket.emit("message", { rpm: parseFloat(rpm.toFixed(2)), id: 1 });
|
//socket.emit("message", { rpm: parseFloat(rpm.toFixed(2)), id: 1 });
|
||||||
|
fetch("/cadence", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
Accept: "application/json, text/plain, */*",
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ rpm: parseFloat(rpm.toFixed(2)), id: 1 }),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
prevRes = res;
|
prevRes = res;
|
||||||
return res;
|
return res;
|
||||||
|
|||||||
Reference in New Issue
Block a user