parent
3af7b892b2
commit
f10c7df262
10 changed files with 276 additions and 326 deletions
|
@ -42,15 +42,33 @@ pub async fn handle_mqtt(topic: String, data: Vec<u8>) {
|
|||
}
|
||||
"respond" => {
|
||||
let resp: ServerResponse = serde_json::from_slice(&dec.payload).unwrap();
|
||||
|
||||
log::info!("Got response {:?}", resp);
|
||||
|
||||
let entry = crate::DISPATCH.get().unwrap().get(&resp.id).unwrap();
|
||||
entry.send(resp);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TaskWaiter {
|
||||
pub id: ulid::Ulid,
|
||||
pub recv: crossbeam::channel::Receiver<ServerResponse>,
|
||||
}
|
||||
|
||||
impl TaskWaiter {
|
||||
pub async fn wait_for(&self, timeout: std::time::Duration) -> Option<ServerResponse> {
|
||||
// TODO tokio spawn blocking?
|
||||
self.recv.recv_timeout(timeout).ok()
|
||||
}
|
||||
}
|
||||
|
||||
/// Send a message to a registered `machine`
|
||||
pub async fn send_msg<T: Serialize>(client: &AsyncClient, machine: &Model<Machine>, request: T) {
|
||||
pub async fn send_msg(
|
||||
client: &AsyncClient,
|
||||
machine: &Model<Machine>,
|
||||
request: ClientAction,
|
||||
) -> TaskWaiter {
|
||||
let data = serde_json::to_string(&request).unwrap();
|
||||
let pk = &machine.read().identity;
|
||||
let rec = pk.enc_key().unwrap();
|
||||
|
@ -66,6 +84,14 @@ pub async fn send_msg<T: Serialize>(client: &AsyncClient, machine: &Model<Machin
|
|||
.publish(topic, rumqttc::QoS::AtMostOnce, true, payload)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let (sender, recv) = tokio::sync::mpsc::channel(100);
|
||||
crate::DISPATCH.get().unwrap().insert(request.id, sender);
|
||||
|
||||
TaskWaiter {
|
||||
id: request.id,
|
||||
recv,
|
||||
}
|
||||
}
|
||||
|
||||
/// Subscribe to all `device->server` topics
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue