-
Notifications
You must be signed in to change notification settings - Fork 4
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
adds second kuksa client in zenoh-kuksa-provider #10
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,6 @@ use provider_config::ProviderConfig; | |
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
use std::{error::Error, fs}; | ||
use tokio::sync::Mutex; | ||
|
||
mod provider_config; | ||
mod utils; | ||
|
@@ -51,12 +50,10 @@ async fn handling_zenoh_subscribtion( | |
provider_config: Arc<ProviderConfig>, | ||
session: Arc<Session>, | ||
metadata_store: MetadataStore, | ||
kuksa_client: Arc<Mutex<kuksa::Client>>, | ||
mut kuksa_client: kuksa::Client, | ||
) { | ||
info!("Listening on selector: {:?}", provider_config.zenoh.key_exp); | ||
|
||
let mut sub_client = kuksa_client.lock().await; | ||
|
||
let provider_config_clone = Arc::clone(&provider_config); | ||
let subscriber = session | ||
.declare_subscriber(provider_config_clone.zenoh.key_exp.clone()) | ||
|
@@ -80,7 +77,7 @@ async fn handling_zenoh_subscribtion( | |
let datapoint_update = new_datapoint_for_update(&vss_path, &sample, &store); | ||
|
||
debug!("Forwarding: {:#?}", datapoint_update); | ||
sub_client | ||
kuksa_client | ||
.set_current_values(datapoint_update) | ||
.await | ||
.unwrap(); | ||
|
@@ -91,10 +88,8 @@ async fn handling_zenoh_subscribtion( | |
async fn publish_to_zenoh( | ||
provider_config: Arc<ProviderConfig>, | ||
session: Arc<Session>, | ||
kuksa_client: Arc<Mutex<kuksa::Client>>, | ||
mut kuksa_client: kuksa::Client, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use reference. mut not needed since you're not editing the client There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is necessary to have a mutable client because the implementation for the functions requires a mutable reference for self.
Correct me if I'm wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will check if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've checked it is necessary so all good here |
||
) { | ||
let mut actuation_client = kuksa_client.lock().await; | ||
|
||
let attachment = Some(String::from("type=targetValue")); | ||
|
||
let vss_paths = Vec::from_iter(provider_config.signals.iter().map(String::as_str)); | ||
|
@@ -114,7 +109,7 @@ async fn publish_to_zenoh( | |
vss_paths | ||
); | ||
|
||
match actuation_client.subscribe_target_values(vss_paths).await { | ||
match kuksa_client.subscribe_target_values(vss_paths).await { | ||
Ok(mut stream) => { | ||
while let Some(response) = stream.message().await.unwrap() { | ||
for update in &response.updates { | ||
|
@@ -123,7 +118,7 @@ async fn publish_to_zenoh( | |
let vss_path = &entry.path; | ||
|
||
if let Some(publisher) = publishers.get(vss_path.as_str()) { | ||
let buf = match datapoint_to_string(&datapoint) { | ||
let buf = match datapoint_to_string(datapoint) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please keep using the reference |
||
Some(v) => v, | ||
None => "null".to_string(), | ||
}; | ||
|
@@ -182,10 +177,11 @@ async fn main() { | |
|
||
let uri = kuksa::Uri::try_from(provider_config.kuksa.databroker_url.as_str()) | ||
.expect("Invalid URI for Kuksa Databroker connection."); | ||
let client = Arc::new(Mutex::new(kuksa::Client::new(uri))); | ||
let mut client = kuksa::Client::new(uri.clone()); | ||
let actuation_client = kuksa::Client::new(uri); | ||
|
||
fetch_metadata( | ||
client.clone(), | ||
client = fetch_metadata( | ||
client, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should work if you give a reference or clone the client |
||
provider_config.signals.iter().map(|s| s as &str).collect(), | ||
&metadata_store, | ||
) | ||
|
@@ -195,15 +191,13 @@ async fn main() { | |
let session = Arc::clone(&zenoh_session); | ||
let provider_config = Arc::clone(&provider_config); | ||
let metadata_store = Arc::clone(&metadata_store); | ||
let kuksa_client = Arc::clone(&client); | ||
handling_zenoh_subscribtion(provider_config, session, metadata_store, kuksa_client) | ||
handling_zenoh_subscribtion(provider_config, session, metadata_store, client) | ||
}); | ||
|
||
let publisher_handle = tokio::spawn({ | ||
let session = Arc::clone(&zenoh_session); | ||
let provider_config = Arc::clone(&provider_config); | ||
let kuksa_client = Arc::clone(&client); | ||
publish_to_zenoh(provider_config, session, kuksa_client) | ||
publish_to_zenoh(provider_config, session, actuation_client) | ||
}); | ||
|
||
let _ = subscriber_handle.await; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,24 +12,23 @@ | |
********************************************************************************/ | ||
|
||
use kuksa::proto::v1::{datapoint::Value, DataType, Datapoint}; | ||
use kuksa::Client; | ||
use log::warn; | ||
use prost_types::Timestamp; | ||
use std::time::{SystemTime, UNIX_EPOCH}; | ||
use std::{collections::HashMap, sync::Arc}; | ||
use tokio::sync::Mutex; | ||
use std::collections::HashMap; | ||
use zenoh::{buffers::ZBuf, sample::Sample}; | ||
|
||
use crate::utils::{metadata_store::MetadataInfo, zenoh_utils::zbuf_to_string}; | ||
|
||
pub async fn fetch_metadata( | ||
kuksa_client: Arc<Mutex<kuksa::Client>>, | ||
mut kuksa_client: Client, | ||
paths: Vec<&str>, | ||
metadata_store: &super::metadata_store::MetadataStore, | ||
) { | ||
let mut client = kuksa_client.lock().await; | ||
) -> Client { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should not return anything here right? |
||
let mut store = metadata_store.lock().await; | ||
|
||
let data_entries: Vec<kuksa::DataEntry> = client.get_metadata(paths).await.unwrap(); | ||
let data_entries: Vec<kuksa::DataEntry> = kuksa_client.get_metadata(paths).await.unwrap(); | ||
|
||
for entry in data_entries { | ||
store.insert( | ||
|
@@ -40,6 +39,8 @@ pub async fn fetch_metadata( | |
}, | ||
); | ||
} | ||
|
||
kuksa_client | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unnecessary to return this |
||
} | ||
|
||
pub fn new_datapoint(data_type: &DataType, payload: &ZBuf) -> Datapoint { | ||
|
@@ -68,10 +69,10 @@ pub fn new_datapoint(data_type: &DataType, payload: &ZBuf) -> Datapoint { | |
nanos: duration_since_epoch.subsec_nanos() as i32, | ||
}; | ||
|
||
return Datapoint { | ||
Datapoint { | ||
timestamp: Some(timestamp), // TODO: get timestamp right | ||
value: Some(value), | ||
}; | ||
} | ||
} | ||
|
||
pub fn new_datapoint_for_update( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use reference for kuksa_client