Skip to content

Commit

Permalink
Merge pull request #176 from bradmiro/main
Browse files Browse the repository at this point in the history
feat: next24 updates
  • Loading branch information
bradmiro authored Apr 8, 2024
2 parents db1e86e + ebfd854 commit 92f301a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
10 changes: 5 additions & 5 deletions data-analytics/data-dash/pi/beam_break_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ def broken(self, cur_time):
self.is_broken = True
if cur_time - self.broken_time > 10:
self.broken_time = cur_time
self.upload(cur_time)
self.upload(cur_time, True)

def unbroken(self, cur_time):
self.unbroken_time = cur_time
self.upload(cur_time, False)
if self.is_broken:
print(f"SENSOR {self.id}: UNBROKEN")
self.is_broken = False

def upload(self, cur_time):
print((self.rowkey, self.id, self.broken_time))

def upload(self, cur_time, broken):
col = f"t{self.id}_s" if broken else f"t{self.id}_e"
column_family_id = "cf"

row = self.table.direct_row(self.rowkey)
row.set_cell(column_family_id, f"cp{self.id}", str(cur_time))
row.set_cell(column_family_id, col, str(cur_time))
row.commit()
5 changes: 3 additions & 2 deletions data-analytics/data-dash/pi/side_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ class SideController:
RIGHT_CONST = 1

LEFT_CARDS = {
173448457841
173448457841,
}
RIGHT_CARDS = {
769013855813
769013855813,
103979249382
}

def __init__(self):
Expand Down
38 changes: 18 additions & 20 deletions data-analytics/data-dash/webapp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from datetime import datetime
import os
from threading import Lock
import time
from datetime import datetime
from threading import Lock

import google.cloud.bigtable.row_filters as row_filters
from flask import Flask, render_template
from flask_socketio import SocketIO
from google.cloud import bigtable
import google.cloud.bigtable.row_filters as row_filters

async_mode = None

Expand All @@ -37,9 +37,9 @@
OK = 0
DONE = 1
WIN = 2
ERR = 3
ERR = 3

COLORS ={
COLORS = {
OK: 0,
DONE: 1,
WIN: 2,
Expand All @@ -53,15 +53,15 @@
client = bigtable.Client(project=project_id)
instance = client.instance(instance_id)
table = instance.table(table_id)
column_family_id = "cf"
column_family_id = "cf"


def get_data(track_id):
# get_track_data returns the data for the last race on a track
data = {}
_r = table.read_rows(
filter_=row_filters.RowKeyRegexFilter(
bytes(f"^track{track_id}#.*", "utf-8")),
limit=1
filter_=row_filters.RowKeyRegexFilter(bytes(f"^track{track_id}#.*", "utf-8")),
limit=1,
)

# Expand Bigtable read to a reusable object
Expand All @@ -72,14 +72,14 @@ def get_data(track_id):
data["car_id"] = row.cells["cf"][b"car_id"][0].value.decode("utf-8")
data["timestamp"] = datetime.timestamp(row.cells["cf"][b"car_id"][0].timestamp)

col_strf = "t{i}_s"
col_strf = "cp{i}"
checkpoints = {}
for i in range(1,9):
for i in range(1, 9):
col_name = bytes(col_strf.format(i=i), "utf-8")
col = row.cells["cf"].get(col_name)
checkpoints[i] = int(col[0].value.decode("utf-8")) if col else None
checkpoints[i] = float(col[0].value.decode("utf-8")) if col else None

for i in range(1,3):
for i in range(1, 3):
ts = checkpoints.get(i)
if ts:
if checkpoints.get(8):
Expand All @@ -95,12 +95,13 @@ def get_data(track_id):
data["status"] = status
return data


def background_thread():
while True:
socketio.sleep(1)
left_data = get_data(1)
right_data = get_data(2)

# Determine if there's a winner
if left_data["status"] == DONE and right_data["status"] == DONE:
if left_data["checkpoints"]["8"] < right_data["checkpoints"]["8"]:
Expand All @@ -111,25 +112,22 @@ def background_thread():
left_data["status"] = WIN
elif right_data["status"] == DONE:
right_data["status"] = WIN
socketio.emit(
"send_data",
{"left": left_data, "right": right_data}
)
socketio.emit("send_data", {"left": left_data, "right": right_data})


@app.route("/")
def index():
return render_template("index.html", async_mode=socketio.async_mode)


@socketio.event
def connect():
global thread
with thread_lock:
if thread is None:
thread = socketio.start_background_task(background_thread)
socketio.emit(
"set_pictures",
{"left_id": f"{DEFAULT_ID}", "right_id": f"{DEFAULT_ID}"}
"set_pictures", {"left_id": f"{DEFAULT_ID}", "right_id": f"{DEFAULT_ID}"}
)


Expand Down
8 changes: 5 additions & 3 deletions data-analytics/data-dash/webapp/static/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ $(document).ready(function () {


// console.log("setting checkpoints");
console.log(checkpointsMap)
for (let i = 0; i < 8; i++) {
let checkpoint = i + 1
let selector = div + " tr";
Expand Down Expand Up @@ -90,9 +91,10 @@ $(document).ready(function () {
socket.on("send_data", function (data) {
// console.log("send_data")

console.log(data);
// console.log(data.left);
// console.log(data.right);

console.log(data.left);
console.log(data.right);

leftData = data.left;
rightData = data.right;

Expand Down

0 comments on commit 92f301a

Please sign in to comment.