|
|
|
@@ -1,3 +1,4 @@ |
|
|
|
use super::binding; |
|
|
|
use eyre::eyre; |
|
|
|
use eyre::WrapErr; |
|
|
|
use futures::future::join_all; |
|
|
|
@@ -9,32 +10,28 @@ use std::collections::{BTreeMap, HashMap}; |
|
|
|
use std::hash::Hash; |
|
|
|
use std::hash::Hasher; |
|
|
|
use std::time::{Duration, Instant}; |
|
|
|
use structopt::StructOpt; |
|
|
|
use tokio::time::timeout; |
|
|
|
use zenoh::config::Config; |
|
|
|
use zenoh::prelude::SplitBuffer; |
|
|
|
|
|
|
|
use crate::python_binding::{call, init}; |
|
|
|
|
|
|
|
static DURATION_MILLIS: u64 = 5; |
|
|
|
#[derive(Deserialize, Debug)] |
|
|
|
struct ConfigVariables { |
|
|
|
subscriptions: Vec<String>, |
|
|
|
|
|
|
|
#[derive(Deserialize, Debug, Clone, StructOpt)] |
|
|
|
pub struct PythonCommand { |
|
|
|
pub subscriptions: Vec<String>, |
|
|
|
pub app: String, |
|
|
|
pub function: String, |
|
|
|
} |
|
|
|
|
|
|
|
#[tokio::main] |
|
|
|
pub async fn main() -> PyResult<()> { |
|
|
|
pub async fn run(variables: PythonCommand) -> PyResult<()> { |
|
|
|
// Subscribe |
|
|
|
let variables = envy::from_env::<ConfigVariables>().unwrap(); |
|
|
|
|
|
|
|
env_logger::init(); |
|
|
|
let config = Config::default(); |
|
|
|
let session = zenoh::open(config).await.unwrap(); |
|
|
|
let session = zenoh::open(Config::default()).await.unwrap(); |
|
|
|
|
|
|
|
// Create a hashmap of all subscriptions. |
|
|
|
let mut subscribers = HashMap::new(); |
|
|
|
let subs = variables.subscriptions.clone(); |
|
|
|
|
|
|
|
for subscription in &subs { |
|
|
|
for subscription in &variables.subscriptions { |
|
|
|
subscribers.insert(subscription.clone(), session |
|
|
|
.subscribe(subscription) |
|
|
|
.await |
|
|
|
@@ -48,14 +45,13 @@ pub async fn main() -> PyResult<()> { |
|
|
|
let mut states = BTreeMap::new(); |
|
|
|
let mut states_hash = hash(&states); |
|
|
|
|
|
|
|
let py_function = init() |
|
|
|
let py_function = binding::init(&variables.app, &variables.function) |
|
|
|
.wrap_err("Failed to init the Python Function") |
|
|
|
.unwrap(); |
|
|
|
let duration = Duration::from_millis(DURATION_MILLIS); |
|
|
|
let mut futures_put = vec![]; |
|
|
|
|
|
|
|
loop { |
|
|
|
let now = Instant::now(); |
|
|
|
let mut futures = vec![]; |
|
|
|
for (_, v) in subscribers.iter_mut() { |
|
|
|
futures.push(timeout(duration, v.next())); |
|
|
|
@@ -63,7 +59,7 @@ pub async fn main() -> PyResult<()> { |
|
|
|
|
|
|
|
let results = join_all(futures).await; |
|
|
|
|
|
|
|
for (result, subscription) in results.into_iter().zip(&subs) { |
|
|
|
for (result, subscription) in results.into_iter().zip(&variables.subscriptions.clone()) { |
|
|
|
if let Ok(Some(data)) = result { |
|
|
|
let value = data.value.payload; |
|
|
|
let binary = value.contiguous(); |
|
|
|
@@ -82,7 +78,7 @@ pub async fn main() -> PyResult<()> { |
|
|
|
|
|
|
|
let now = Instant::now(); |
|
|
|
|
|
|
|
let outputs = call(&py_function, states.clone()).await.unwrap(); |
|
|
|
let outputs = binding::call(&py_function, states.clone()).await.unwrap(); |
|
|
|
println!("call python {:#?}", now.elapsed()); |
|
|
|
|
|
|
|
for (key, value) in outputs { |