1use super::*;
2
3#[derive(Debug)]
4pub(crate) enum Request {
5 Custom(String, oneshot::Sender<Result<String>>),
6 Status(oneshot::Sender<Result<Status>>),
7 Config(oneshot::Sender<Result<Config>>),
8 Enable(oneshot::Sender<Result>),
9 Disable(oneshot::Sender<Result>),
10 SetValue(String, String, oneshot::Sender<Result>),
11 Shutdown,
12}
13
14impl ShutdownSignal for Request {
15 fn is_shutdown(&self) -> bool {
16 matches!(self, Request::Shutdown)
17 }
18 fn inform_of_shutdown(self) {
19 match self {
20 Request::Custom(_, response) => {
21 let _ = response.send(Err(error::Error::StartupAborted));
22 }
23 Request::Status(response) => {
24 let _ = response.send(Err(error::Error::StartupAborted));
25 }
26 Request::Config(response) => {
27 let _ = response.send(Err(error::Error::StartupAborted));
28 }
29 Request::Enable(response) | Request::Disable(response) => {
30 let _ = response.send(Err(error::Error::StartupAborted));
31 }
32 Request::SetValue(_, _, response) => {
33 let _ = response.send(Err(error::Error::StartupAborted));
34 }
35 Request::Shutdown => {}
36 }
37 }
38}
39
40#[derive(Clone)]
41pub struct RequestClient {
43 sender: mpsc::Sender<Request>,
44}
45
46impl RequestClient {
47 pub(crate) fn new(sender: mpsc::Sender<Request>) -> RequestClient {
48 RequestClient { sender }
49 }
50
51 async fn send_request(&self, request: Request) -> Result {
52 self.sender
53 .send(request)
54 .await
55 .map_err(|_| error::Error::WifiApRequestChannelClosed)?;
56 Ok(())
57 }
58
59 pub async fn send_custom(&self, custom: String) -> Result<String> {
60 let (response, request) = oneshot::channel();
61 self.sender
62 .send(Request::Custom(custom, response))
63 .await
64 .map_err(|_| error::Error::WifiApRequestChannelClosed)?;
65 request.await?
66 }
67
68 pub async fn get_status(&self) -> Result<Status> {
69 let (response, request) = oneshot::channel();
70 self.send_request(Request::Status(response)).await?;
71 request.await?
72 }
73
74 pub async fn get_config(&self) -> Result<Config> {
75 let (response, request) = oneshot::channel();
76 self.send_request(Request::Config(response)).await?;
77 request.await?
78 }
79
80 pub async fn enable(&self) -> Result {
81 let (response, request) = oneshot::channel();
82 self.send_request(Request::Enable(response)).await?;
83 request.await?
84 }
85
86 pub async fn disable(&self) -> Result {
87 let (response, request) = oneshot::channel();
88 self.send_request(Request::Disable(response)).await?;
89 request.await?
90 }
91
92 pub async fn set_value(&self, key: &str, value: &str) -> Result {
93 let (response, request) = oneshot::channel();
94 self.send_request(Request::SetValue(key.into(), value.into(), response))
95 .await?;
96 request.await?
97 }
98
99 pub async fn shutdown(&self) -> Result {
100 self.send_request(Request::Shutdown).await
101 }
102}
103
104#[derive(Debug, Clone)]
105pub enum Broadcast {
107 Ready,
108 Connected(String),
109 Disconnected(String),
110 UnknownEvent(String),
111}
112
113pub type BroadcastReceiver = broadcast::Receiver<Broadcast>;