Skip to content

Commit

Permalink
Modified web Ivy to print data
Browse files Browse the repository at this point in the history
  • Loading branch information
Nilanfor committed Nov 21, 2024
1 parent 1c12dab commit feeec46
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
6 changes: 4 additions & 2 deletions apps/speed_limit_assist/speed_limit_assist.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,17 @@ def traffic_sign_detection_callback(topic_name, msg, time):
vd_sub.set_callback(vehicle_dynamics_callback)


sla_pub = StringPublisher("sla_topic")
sla_pub = StringPublisher("sla")

if speedLimitAssist.limit > 0.0:
exceededSpeed = speedLimitAssist.check_speed_limit()

# Just don't exit
while ecal_core.ok():
if exceededSpeed > 0:
sla_pub("Warning: Youre to fast!")
sla_pub("Warning: Too fast!")
else:
sla_pub("")
time.sleep(0.5)

# finalize eCAL API
Expand Down
45 changes: 45 additions & 0 deletions apps/web_ivi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,51 @@ def callback_vehicle_dynamics(_topic_name, msg, _time):
vehicle_dynamics_generator(), media_type="text/event-stream"
)

@app.get("/sla")
async def sla():
"""
Asynchronous function to handle vehicle dynamics data streaming to the client browser.
This function initializes the eCAL API, subscribes to the "vehicle_dynamics" topic,
and sets a callback to handle incoming messages. It uses an asynchronous generator
to yield vehicle dynamics data as server-sent events (SSE).
Returns:
StreamingResponse: A streaming response with vehicle dynamics data in SSE format.
"""

async def sla_generator():
sla_queue = Queue() # Already synchronized queue

# Define an ecal callback triggered when receiving data through the ecal topic
def callback_sla(_topic_name, msg, _time):
sla_queue.put(msg)

# Initialize ecal
ecal_core.initialize(sys.argv, "WebIVI SLA")

# Subscribe to the vehicle_dynamics topic receiving json messages
sub = StringSubscriber("sla")

# Set the Callback
sub.set_callback(callback_sla)

while ecal_core.ok() and not stop_server_side_event.is_set():
if not sla_queue.empty():
sla_data = sla_queue.get()
logger.info(sla_data)
sla_queue.task_done()
sla_queue = Queue()
yield f"event: sla\ndata: {sla_data}\n\n"
await sleep(0.1)

# Finalize eCAL API
ecal_core.finalize()

return StreamingResponse(
sla_generator(), media_type="text/event-stream"
)


if __name__ == "__main__":
run(app, host="0.0.0.0", port=5500)
2 changes: 1 addition & 1 deletion apps/web_ivi/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<!-- three columns for speedometer, centered info-screen and torque meter -->
<div id="speedometer">
<img src="assets/speedometer.svg" width="500px">
<h2 id="speed-value">n.a.</h2>
<h2 id="speed-value">n.a.</h2><span id="sla-value"></span>
</div>
<div id="info-screen">
</div>
Expand Down
31 changes: 31 additions & 0 deletions apps/web_ivi/static/vehicle-dynamics.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ function updateSpeedAndTorque(speed_km_h) {
speedValue.textContent = speed_km_h;
}

/* update the sla */
function updateSla(sla_status) {
// update the sla value
let slaValue = document.getElementById("sla-value");

slaValue.textContent = sla_status;
}

// read from server-side event
const eventSource = new EventSource("/vehicle-dynamics");

Expand All @@ -66,4 +74,27 @@ eventSource.addEventListener("vehicle-dynamics", function (event) {
eventSource.onerror = function (event) {
console.log("Error: " + event);
eventSource.close();
}

// sla optional addon module
const eventSourceSla = new EventSource("/sla");

eventSourceSla.onopen = function (_event) {
console.log("Connection to /sla opened");
}

// add event listener for the event
eventSourceSla.addEventListener("sla", function (event) {
let raw_data = event.data;
let sla = JSON.parse(raw_data);
console.log(sla);
let sla_status = sla.status
console.log("Received sla: ", sla_status);
updateSla(sla_status);
});

// close the connection on error
eventSourceSla.onerror = function (event) {
console.log("Error: " + event);
eventSourceSla.close();
}

0 comments on commit feeec46

Please sign in to comment.