1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use tokio::sync::mpsc::{
error::SendError,
Permit,
};
use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};
use core::pin::Pin;
use futures_util::{sink::*, StreamExt};
use url::Url;
// use crate::*;
use crate::{MoonMSG, MoonResultData};
/// A WebSocket connection to a Moonraker server.
pub struct MoonConnection {
write_stream: tokio::sync::mpsc::Sender<MoonMSG>,
read_stream: tokio::sync::mpsc::Receiver<MoonMSG>,
}
impl MoonConnection {
/// Creates a new `MoonConnection` instance and establishes a WebSocket connection to the specified `url`.
///
/// # Arguments
///
/// * `url` - A `String` containing the URL of the Moonraker instance to connect to.
/// * `writer_buffer_size` - The size of the buffer used to store outgoing messages.
/// * `reader_buffer_size` - The size of the buffer used to store incoming messages.
///
/// # Returns
///
/// A new `MoonConnection` instance.
pub async fn new(url: String, writer_buffer_size: usize, reader_buffer_size: usize) -> MoonConnection {
let (ws_writer_sender, mut ws_writer_receiver) = tokio::sync::mpsc::channel(writer_buffer_size);
let connect_addr = Url::parse(&url).unwrap();
let (ws_stream, _) = match connect_async(&connect_addr).await {
Ok(stuff) => stuff,
Err(_) => panic!("Error connecting to websocket"),
};
// println!("WebSocket handshake has been successfully completed");
let (mut moon_socket_sink, mut moon_socket_stream) = ws_stream.split();
// Spawns Moonraker Websocket writer thread
tokio::spawn(async move {
while let Some(msg) = ws_writer_receiver.recv().await {
let mut vec = serde_json::to_vec(&msg).expect("Could not Serialize Request");
vec.truncate(vec.len());
let result = Pin::new(&mut moon_socket_sink)
.send(Message::binary(vec))
.await;
match result {
Ok(_) => continue,
Err(_) => println!("Unable to send to moon_socket_sink"),
}
}
});
// println!("Split websocket Stream");
let (ws_reader_sender, ws_reader_receiver) = tokio::sync::mpsc::channel(reader_buffer_size);
// println!("Created Reader Runtime");
// Spawns Moonraker Websocket reader thread
tokio::spawn(async move {
while let Some(message) = moon_socket_stream.next().await {
match message {
Ok(msg) => {
if msg.len() == 0 {
continue;
}
let message = msg.into_text().unwrap();
let parsed = serde_json::from_str(&message);
match parsed {
Ok(message) => match ws_reader_sender.send(message).await {
Ok(()) => continue,
Err(_) => println!("Unable to send to ws_reader_sender"),
},
Err(_) => {
println!("----------------------------MESSAGE NOT PARSED----------------------------");
println!("Message Length: {}", message.len());
println!("{}", message);
println!("--------------------------------------------------------------------------");
},
}
}
Err(_) => println!("Hi, I'm a error"),
}
}
});
MoonConnection {
write_stream: ws_writer_sender,
read_stream: ws_reader_receiver,
}
}
/// Creates a new `MoonConnection` instance and establishes a WebSocket connection to the specified `url`.
/// The only difference between this and `new` is that this function println!s out all received messages.
///
/// # Arguments
///
/// * `url` - A `String` containing the URL of the Moonraker instance to connect to.
/// * `writer_buffer_size` - The size of the buffer used to store outgoing messages.
/// * `reader_buffer_size` - The size of the buffer used to store incoming messages.
///
/// # Returns
///
/// A new `MoonConnection` instance.
pub async fn new_debug(url: String, writer_buffer_size: usize, reader_buffer_size: usize) -> MoonConnection {
let (ws_writer_sender, mut ws_writer_receiver) = tokio::sync::mpsc::channel(writer_buffer_size);
let connect_addr = Url::parse(&url).unwrap();
let (ws_stream, _) = match connect_async(&connect_addr).await {
Ok(stuff) => stuff,
Err(_) => panic!("Error connecting to websocket"),
};
println!("WebSocket handshake has been successfully completed");
let (mut moon_socket_sink, mut moon_socket_stream) = ws_stream.split();
// Spawns Moonraker Websocket writer thread
tokio::spawn(async move {
while let Some(msg) = ws_writer_receiver.recv().await {
let mut vec = serde_json::to_vec(&msg).expect("Could not Serialize Request");
vec.truncate(vec.len());
let result = Pin::new(&mut moon_socket_sink)
.send(Message::binary(vec))
.await;
match result {
Ok(_) => continue,
Err(_) => println!("Unable to send to moon_socket_sink"),
}
}
});
println!("Split websocket Stream");
let (ws_reader_sender, ws_reader_receiver) = tokio::sync::mpsc::channel(reader_buffer_size);
println!("Created Reader Runtime");
// Spawns Moonraker Websocket reader thread
tokio::spawn(async move {
while let Some(message) = moon_socket_stream.next().await {
match message {
Ok(msg) => {
if msg.len() == 0 {
continue;
}
let message = msg.into_text().unwrap();
println!("Received: {}", message);
let parsed = serde_json::from_str(&message);
match parsed {
Ok(message) => match ws_reader_sender.send(message).await {
Ok(()) => continue,
Err(_) => println!("Unable to send to ws_reader_sender"),
},
Err(_) => {
println!("----------------------------MESSAGE NOT PARSED----------------------------");
println!("Message Length: {}", message.len());
println!("{}", message);
println!("--------------------------------------------------------------------------");
},
}
}
Err(_) => println!("Hi, I'm a error"),
}
}
});
MoonConnection {
write_stream: ws_writer_sender,
read_stream: ws_reader_receiver,
}
}
/// The most basic of the sender methods. Adds a message to the WebSocket writer queue to be sent to the Moonraker instance.
/// This function is non-blocking and returns immediately.
/// When the websocket writer queue is full, this function will not block, but will yield back to the parent function until a spot opens up in the queue which
/// can be probelmatic if the order of messages you're sending to the printer is important. In that case, consider using the `send_reserved` function instead.
///
/// # Arguments
///
/// * `message` - The `MoonMSG` message to send.
///
/// # Returns
///
/// Returns `Ok(())` if the message was successfully added to the queue, or a `SendError<MoonMSG>` if the queue is full.
pub async fn send(&mut self, message: MoonMSG) -> Result<(), SendError<MoonMSG>> {
self.write_stream.send(message).await?;
Ok(())
}
/// Sends a message over the WebSocket connection, using a reserved spot in the writer queue.
///
/// This function is similar to `send`, but it uses a reserved spot in the writer queue, so it
/// doesn't block the thread if the queue is full, but also ensures that the order of messages is exactly as your program describes.
/// Use this function if you are sending a lot of messages in a short amount of time and the order of those messages matters.
///
/// Essentially, you are putting a dynmic buffer on top of the fixed-sized primary message buffer to ensure that messages are sent in the order you want.
///
/// For example, say you wanted to ensure the printer recieved a `G28` (Home Printer) command before a `G1 Z10` (move printer bed up) command.
/// Although, in this case, you'll want to sleep for a bit after the `G28` command to ensure the printer has time to home before moving the bed up.
///
/// # Arguments
///
/// * `message` - The message to send over the WebSocket connection.
///
/// # Returns
///
/// A `Result` indicating whether the message was successfully sent or not. An error here indicates that the websocket channel is probably closed.
pub async fn send_reserved(&mut self, message: MoonMSG) -> Result<(), SendError<()>> {
let permit = self.reserve().await?;
permit.send(message);
Ok(())
}
/// Reserves a permit from the WebSocket writer queue for sending a message.
///
/// # Returns
///
/// Returns a `Permit<MoonMSG>` if a permit was successfully reserved, or a `SendError<()>` if the connection has closed.
pub async fn reserve(&self) -> Result<Permit<MoonMSG>, SendError<()>> {
self.write_stream.reserve().await
}
/// Waits for a message to be received from the Moonraker instance.
///
/// # Returns
///
/// Returns an `Option<MoonMSG>` containing the received message, or `None` if the receiver channel has been closed.
pub async fn recv(&mut self) -> Option<MoonMSG> {
self.read_stream.recv().await
}
pub async fn send_checked(&mut self, message: MoonMSG) -> Result<(), SendError<MoonMSG>> {
let this_id = 3243;
let msg = message.set_id(this_id);
self.send(msg).await?;
// let mut ok_received = false;
loop {
match self.recv().await {
Some(msg) => {
match msg {
MoonMSG::MoonResult { id, result, .. } => {
if id == this_id {
match result {
MoonResultData::Ok(..) => {
return Ok(());
}
_ => continue,
}
}
},
_ => continue,
}
},
None => continue,
}
}
// Ok(())
}
pub async fn send_listen(&mut self, message: MoonMSG) -> Result<MoonMSG, SendError<MoonMSG>> {
let this_id = message.id().expect("Message must have an ID");
self.send(message).await?;
loop {
match self.recv().await {
Some(msg) => {
match msg.id() {
Some(id) => {
if id == this_id {
return Ok(msg)
}
},
None => continue,
}
},
None => continue,
}
}
// Ok(())
}
}