Skip to content

Commit

Permalink
Update Controller::send_message() to try lock
Browse files Browse the repository at this point in the history
  • Loading branch information
cybergarage committed Jan 20, 2023
1 parent ecabca3 commit fa8fb7f
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,29 +113,32 @@ impl Controller {

/// Sends the specified message to the specified remote node. The function automatically updates the SEOJ (Source ECHONET-Lite object) and TID (Transaction ID) in the specified message, so you do not need to set the message fields.
pub fn send_message(&self, remote_node: &RemoteNode, msg: &mut Message) -> bool {
let ctrl = self.node.lock().unwrap();
ctrl.send_message(remote_node, msg)
if self.node.try_lock().is_err() {
return false;
}
let node = self.node.lock().unwrap();
node.send_message(remote_node, msg)
}

/// Posts the specified message to the remote node and waits for the response using the messaging channel. TThe function automatically updates the SEOJ (Source ECHONET-Lite object) and TID (Transaction ID) in the specified message, so you do not need to set the message fields.
pub fn post_message(&self, remote_node: &RemoteNode, msg: &mut Message) -> Receiver<Message> {
let ctrl = self.node.lock().unwrap();
ctrl.post_message(remote_node, msg)
let node = self.node.lock().unwrap();
node.post_message(remote_node, msg)
}

/// Starts the controller node to communicate with other ECHONET-Lite nodes on the local network.
pub fn start(&mut self) -> bool {
let mut ctrl = self.node.lock().unwrap();
if !ctrl.start() {
let mut node = self.node.lock().unwrap();
if !node.start() {
return false;
}
true
}

/// Stops the controller node, and clears all searched remote nodes.
pub fn stop(&mut self) -> bool {
let mut ctrl = self.node.lock().unwrap();
ctrl.stop()
let mut node = self.node.lock().unwrap();
node.stop()
}
}

Expand Down

0 comments on commit fa8fb7f

Please sign in to comment.