Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: simulator should send action_status to bridge as ActionResponse #283

Merged
merged 1 commit into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions uplink/src/collector/simulator/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use std::time::Duration;
use crate::base::clock;
use crate::Payload;

use super::Event;

const RESET_LIMIT: u32 = 1500;

#[inline]
Expand Down Expand Up @@ -60,7 +62,7 @@ pub struct Gps {
}

impl Gps {
pub async fn simulate(tx: Sender<Payload>, device: DeviceData) {
pub async fn simulate(tx: Sender<Event>, device: DeviceData) {
let mut sequence = 0;
let mut interval = interval(DataType::Gps.duration());
let path_len = device.path.len() as u32;
Expand All @@ -74,12 +76,12 @@ impl Gps {
trace!("Data Event: {:?}", payload);

if let Err(e) = tx
.send_async(Payload {
.send_async(Event::Data(Payload {
timestamp: clock() as u64,
stream: "gps".to_string(),
sequence,
payload: json!(payload),
})
}))
.await
{
error!("{e}");
Expand Down Expand Up @@ -194,7 +196,7 @@ pub struct Bms {
}

impl Bms {
pub async fn simulate(tx: Sender<Payload>) {
pub async fn simulate(tx: Sender<Event>) {
let mut sequence = 0;
let mut interval = interval(DataType::Bms.duration());
loop {
Expand All @@ -205,12 +207,12 @@ impl Bms {
trace!("Data Event: {:?}", payload);

if let Err(e) = tx
.send_async(Payload {
.send_async(Event::Data(Payload {
timestamp: clock() as u64,
stream: "bms".to_string(),
sequence,
payload: json!(payload),
})
}))
.await
{
error!("{e}");
Expand Down Expand Up @@ -243,7 +245,7 @@ pub struct Imu {
}

impl Imu {
pub async fn simulate(tx: Sender<Payload>) {
pub async fn simulate(tx: Sender<Event>) {
let mut sequence = 0;
let mut interval = interval(DataType::Imu.duration());
loop {
Expand All @@ -254,12 +256,12 @@ impl Imu {
trace!("Data Event: {:?}", payload);

if let Err(e) = tx
.send_async(Payload {
.send_async(Event::Data(Payload {
timestamp: clock() as u64,
stream: "imu".to_string(),
sequence,
payload: json!(payload),
})
}))
.await
{
error!("{e}");
Expand All @@ -286,7 +288,7 @@ pub struct Motor {
}

impl Motor {
pub async fn simulate(tx: Sender<Payload>) {
pub async fn simulate(tx: Sender<Event>) {
let mut sequence = 0;
let mut interval = interval(DataType::Motor.duration());
loop {
Expand All @@ -297,12 +299,12 @@ impl Motor {
trace!("Data Event: {:?}", payload);

if let Err(e) = tx
.send_async(Payload {
.send_async(Event::Data(Payload {
timestamp: clock() as u64,
stream: "motor".to_string(),
sequence,
payload: json!(payload),
})
}))
.await
{
error!("{e}");
Expand Down Expand Up @@ -335,7 +337,7 @@ pub struct PeripheralState {
}

impl PeripheralState {
pub async fn simulate(tx: Sender<Payload>) {
pub async fn simulate(tx: Sender<Event>) {
let mut sequence = 0;
let mut interval = interval(DataType::PeripheralData.duration());
loop {
Expand All @@ -346,12 +348,12 @@ impl PeripheralState {
trace!("Data Event: {:?}", payload);

if let Err(e) = tx
.send_async(Payload {
.send_async(Event::Data(Payload {
timestamp: clock() as u64,
stream: "peripheral_state".to_string(),
sequence,
payload: json!(payload),
})
}))
.await
{
error!("{e}");
Expand Down Expand Up @@ -381,7 +383,7 @@ pub struct DeviceShadow {
}

impl DeviceShadow {
pub async fn simulate(tx: Sender<Payload>) {
pub async fn simulate(tx: Sender<Event>) {
let mut sequence = 0;
let mut interval = interval(DataType::DeviceShadow.duration());
loop {
Expand All @@ -392,12 +394,12 @@ impl DeviceShadow {
trace!("Data Event: {:?}", payload);

if let Err(e) = tx
.send_async(Payload {
.send_async(Event::Data(Payload {
timestamp: clock() as u64,
stream: "device_shadow".to_string(),
sequence,
payload: json!(payload),
})
}))
.await
{
error!("{e}");
Expand Down
69 changes: 20 additions & 49 deletions uplink/src/collector/simulator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use crate::base::bridge::{BridgeTx, Payload};
use crate::base::{clock, SimulatorConfig};
use crate::Action;
use crate::base::SimulatorConfig;
use crate::{Action, ActionResponse};
use data::{Bms, DeviceData, DeviceShadow, Gps, Imu, Motor, PeripheralState};
use flume::{bounded, Receiver, Sender};
use log::{error, info};
use rand::Rng;
use serde::Serialize;
use serde_json::json;
use thiserror::Error;
use tokio::time::interval;
use tokio::{select, spawn};
Expand All @@ -16,6 +14,11 @@ use std::{fs, io, sync::Arc};

mod data;

pub enum Event {
Data(Payload),
ActionResponse(ActionResponse),
}

#[derive(Error, Debug)]
pub enum Error {
#[error("Io error {0}")]
Expand All @@ -26,62 +29,31 @@ pub enum Error {
Json(#[from] serde_json::error::Error),
}

#[derive(Serialize)]
pub struct ActionResponse {
action_id: String,
state: String,
progress: u8,
errors: Vec<String>,
}

impl ActionResponse {
pub async fn simulate(action: Action, tx: Sender<Payload>) {
pub async fn simulate(action: Action, tx: Sender<Event>) {
let action_id = action.action_id;
info!("Generating action events for action: {action_id}");
let mut sequence = 0;
let mut interval = interval(Duration::from_secs(1));

// Action response, 10% completion per second
for i in 1..10 {
let response = ActionResponse {
action_id: action_id.clone(),
progress: i * 10 + rand::thread_rng().gen_range(0..10),
state: String::from("in_progress"),
errors: vec![],
};
let progress = i * 10 + rand::thread_rng().gen_range(0..10);
sequence += 1;
if let Err(e) = tx
.send_async(Payload {
stream: "action_status".to_string(),
sequence,
payload: json!(response),
timestamp: clock() as u64,
})
.await
{
let response = ActionResponse::progress(&action_id, "in_progress", progress)
.set_sequence(sequence);
if let Err(e) = tx.send_async(Event::ActionResponse(response)).await {
error!("{e}");
break;
}

interval.tick().await;
}

let response = ActionResponse {
action_id,
progress: 100,
state: String::from("Completed"),
errors: vec![],
};
sequence += 1;
if let Err(e) = tx
.send_async(Payload {
stream: "action_status".to_string(),
sequence,
payload: json!(response),
timestamp: clock() as u64,
})
.await
{
let response =
ActionResponse::progress(&action_id, "Completed", 100).set_sequence(sequence);
if let Err(e) = tx.send_async(Event::ActionResponse(response)).await {
error!("{e}");
}
info!("Successfully sent all action responses");
Expand All @@ -107,7 +79,7 @@ pub fn new_device_data(path: Arc<Vec<Gps>>) -> DeviceData {
DeviceData { path, path_offset: path_index }
}

pub fn spawn_data_simulators(device: DeviceData, tx: Sender<Payload>) {
pub fn spawn_data_simulators(device: DeviceData, tx: Sender<Event>) {
spawn(Gps::simulate(tx.clone(), device));
spawn(Bms::simulate(tx.clone()));
spawn(Imu::simulate(tx.clone()));
Expand All @@ -134,16 +106,15 @@ pub async fn start(
let action = action?;
spawn(ActionResponse::simulate(action, tx.clone()));
}
p = rx.recv_async() => {
let payload = match p {
Ok(p) => p,
event = rx.recv_async() => {
match event {
Ok(Event::ActionResponse(status)) => bridge_tx.send_action_response(status).await,
Ok(Event::Data(payload)) => bridge_tx.send_payload(payload).await,
Err(_) => {
error!("All generators have stopped!");
return Ok(())
}
};

bridge_tx.send_payload(payload).await;
}
}
}
Expand Down
Loading