Merge pull request #8 from ZeusWPI/better_threading_data
Better threading data
This commit is contained in:
commit
665ceaa1d9
3 changed files with 59 additions and 18 deletions
|
@ -4,9 +4,23 @@ from time import sleep
|
|||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
import serial
|
||||
import uuid
|
||||
from collections import deque
|
||||
|
||||
app = Flask(__name__)
|
||||
shared_message_log = []
|
||||
|
||||
server_id = uuid.uuid4()
|
||||
print("Server ID: ", server_id)
|
||||
|
||||
@dataclass
|
||||
class SharedData:
|
||||
messages: deque
|
||||
last_message_index: int
|
||||
|
||||
# Keep this the same as max_messages on the client!
|
||||
max_message_cache = 200
|
||||
shared_data = SharedData(deque(maxlen=max_message_cache), -1)
|
||||
|
||||
|
||||
|
||||
@dataclass
|
||||
|
@ -80,7 +94,7 @@ class Message:
|
|||
}
|
||||
|
||||
|
||||
def serial_reader(messagelog):
|
||||
def serial_reader(shared_data):
|
||||
with serial.Serial('/dev/ttyACM0', 115200, timeout=10) as ser:
|
||||
while True:
|
||||
line = ser.readline()
|
||||
|
@ -91,20 +105,24 @@ def serial_reader(messagelog):
|
|||
parts = line.split(' ')
|
||||
sender = int(parts[1])
|
||||
message = bytes(int(p) for p in parts[2:])
|
||||
received = Message(message, sender, datetime.now(), len(messagelog))
|
||||
messagelog.append(received.serialize())
|
||||
print(len(messagelog))
|
||||
received = Message(message, sender, datetime.now(), len(shared_data.messages))
|
||||
shared_data.messages.append(received.serialize())
|
||||
shared_data.last_message_index += 1
|
||||
print(shared_data.last_message_index)
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return send_file('static/index.html')
|
||||
|
||||
@app.route('/api.json')
|
||||
def api():
|
||||
return jsonify(shared_message_log)
|
||||
|
||||
@app.route('/<last_received>/api.json')
|
||||
def api(last_received):
|
||||
last_received = int(last_received)
|
||||
if last_received < shared_data.last_message_index - len(shared_data.messages):
|
||||
return jsonify({"server_id": server_id, "newest_msg": shared_data.last_message_index, "messages": list(shared_data.messages)})
|
||||
else:
|
||||
return jsonify({"server_id": server_id, "newest_msg": shared_data.last_message_index, "messages": list(shared_data.messages)[len(shared_data.messages) - (shared_data.last_message_index - last_received):]})
|
||||
|
||||
if __name__ == '__main__':
|
||||
thread = Thread(target=serial_reader, args=(shared_message_log, ))
|
||||
thread = Thread(target=serial_reader, args=(shared_data, ))
|
||||
thread.start()
|
||||
app.run(debug=False, host='0.0.0.0')
|
||||
|
|
|
@ -9,6 +9,9 @@
|
|||
font-family: sans-serif;
|
||||
}
|
||||
|
||||
button {
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
.parsed {
|
||||
background: lightgreen;
|
||||
}
|
||||
|
@ -93,7 +96,7 @@
|
|||
<label for="show_raw">Show raw address and payload</label>
|
||||
|
||||
<table id="message_table">
|
||||
<tr>
|
||||
<tr id="table_header">
|
||||
<th>Human-readable type</th>
|
||||
<th>Sender ID</th>
|
||||
<th>Parsed payload</th>
|
||||
|
|
|
@ -1,7 +1,14 @@
|
|||
let maxseen = 0;
|
||||
// Keep this the same as on the server!
|
||||
let max_messages = 200;
|
||||
|
||||
let paused = true;
|
||||
let updaterID = null;
|
||||
|
||||
let serverID = "";
|
||||
let newest_message_index = -1;
|
||||
let messageTable = document.getElementById('message_table');
|
||||
let header = document.getElementById("table_header").cloneNode(true);
|
||||
|
||||
let color_classes = {
|
||||
"RESERVED TYPE": "error",
|
||||
"controller": "controller",
|
||||
|
@ -14,7 +21,7 @@ function updateShow() {
|
|||
}
|
||||
|
||||
function updateMessages() {
|
||||
fetch('/api.json')
|
||||
fetch(`/${newest_message_index}/api.json`)
|
||||
.then(
|
||||
function(response) {
|
||||
if (response.status !== 200) {
|
||||
|
@ -22,13 +29,19 @@ function updateMessages() {
|
|||
return;
|
||||
}
|
||||
response.json().then(function(data) {
|
||||
if (data.length > maxseen) {
|
||||
let messageTable = document.getElementById('message_table');
|
||||
if (serverID !== data.server_id) {
|
||||
messageTable.textContent = '';
|
||||
messageTable.append(header);
|
||||
serverID = data.server_id;
|
||||
newest_message_index = -1;
|
||||
}
|
||||
|
||||
for (let i = maxseen; i < data.length; i++) {
|
||||
let new_messages = data.newest_msg - newest_message_index;
|
||||
if (new_messages > 0) {
|
||||
for (let message of data.messages) {
|
||||
let row = messageTable.insertRow(1);
|
||||
row.classList.add("fade");
|
||||
let current = data[i];
|
||||
let current = message;
|
||||
|
||||
let human_readable_type = row.insertCell(0)
|
||||
let colorblock = document.createElement("div");
|
||||
|
@ -64,8 +77,15 @@ function updateMessages() {
|
|||
raw_id.classList.add("raw");
|
||||
raw_id.classList.add("raw_id");
|
||||
}
|
||||
maxseen = data.length;
|
||||
}
|
||||
|
||||
// delete children if there are too many :O
|
||||
// -1 so we don't delete the header :)
|
||||
for (let i = 0; i < messageTable.children.length - max_messages - 1; i++) {
|
||||
messageTable.removeChild(messageTable.lastChild);
|
||||
}
|
||||
newest_message_index = data.newest_msg;
|
||||
|
||||
});
|
||||
}
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue