moonsock 0.1.2

Moonsock is a simple way to connect to a klipper/moonraker 3D printer websocket from anywhere where tokio can be ran and your printer is accessible from the internet.
Documentation
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use serde::{Serialize, Deserialize};

pub mod connection;
pub mod moon_method;
pub mod moon_param;

// Exports:
pub use connection::MoonConnection;
pub use moon_method::MoonMethod;
pub use moon_param::MoonParam;

/// ---------------------- Request Serializing ------------------------

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum JsonRpcVersion {
    #[serde(rename = "2.0")]
    V2_0
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum MoonMSG {
    MoonResult {
        jsonrpc: JsonRpcVersion,
        result: MoonResultData,
        id: u32,
    },
    MoonError {
        jsonrpc: JsonRpcVersion,
        error: MoonErrorContent,
        id: u32,
    },
    MethodParamID {
        jsonrpc: JsonRpcVersion,
        method: MoonMethod,
        params: MoonParam,
        id: u32,
    },
    MethodParamVecID {
        jsonrpc: JsonRpcVersion,
        method: MoonMethod,
        params: Vec<MoonParam>,
        id: u32,
    },
    MethodParamVec {
        jsonrpc: JsonRpcVersion,
        method: MoonMethod,
        params: Vec<MoonParam>,
    },
    MethodParam {
        jsonrpc: JsonRpcVersion,
        method: MoonMethod,
        params: MoonParam,
    },
    MethodID {
        jsonrpc: JsonRpcVersion,
        method: MoonMethod,
        id: u32,
    },
    Method {
        jsonrpc: JsonRpcVersion,
        method: MoonMethod,
    },
    CouldNotParseParamsID {
        jsonrpc: JsonRpcVersion,
        method: MoonMethod,
        params: serde_json::Value,
        id: u32,
    },
    CouldNotParseParams {
        jsonrpc: JsonRpcVersion,
        method: MoonMethod,
        params: serde_json::Value,
    },
    CouldNotParseMethodID {
        jsonrpc: JsonRpcVersion,
        method: String,
        params: MoonParam,
        id: u32,
    },
    CouldNotParseMethod {
        jsonrpc: JsonRpcVersion,
        method: String,
        params: MoonParam,
    },
    CouldNotParseMethodParamsID {
        jsonrpc: JsonRpcVersion,
        method: String,
        params: serde_json::Value,
        id: u32,
    },
    CouldNotParseMethodParams {
        jsonrpc: JsonRpcVersion,
        method: String,
        params: serde_json::Value,
    },
    Empty,
}
    // I think this is useless, but I don't want to scan through the moonraker API docs again.
    // NotRecognized { value: serde_json::Value },
    // ConnectionID { connection_id: u32 },
    // KlippyHostInfo {
    //     state: PrinterState,
    //     state_message: String,
    //     hostname: String,
    //     software_version: String,
    //     cpu_info: String,
    //     klipper_path: String,
    //     python_path: String,
    //     log_file: String,
    //     config_file: String,
    // },
    // #[serde(rename = "ok")]
    // Ok,
    // AvailablePrinterObjects { objects: Vec<String> },
    // PrinterObjectStatus { eventtime: f32, status: Vec<PrinterObject>},
    // EndstopStatuses { x: String, y: String, z: String },
    // ServerInfo {
    //     klippy_connected: bool,
    //     klippy_state: PrinterState,
    //     components: Vec<String>,
    //     failed_components: Vec<String>,
    //     registered_directories: Vec<String>,
    //     warnings: Vec<String>,
    //     websocket_count: u32,
    //     moonraker_version: String,
    //     api_version: [u32; 3],
    //     api_version_string: String,
    // },
    // ServerInfoWithPlugins {
    //     klippy_connected: bool,
    //     klippy_state: PrinterState,
    //     components: Vec<String>,
    //     failed_components: Vec<String>,
    //     plugins: Vec<String>,
    //     failed_plugins: Vec<String>,
    //     registered_directories: Vec<String>,
    //     warnings: Vec<String>,
    //     websocket_count: u32,
    //     moonraker_version: String,
    //     api_version: [u32; 3],
    //     api_version_string: String,
    // }


#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum MoonOk {
    #[serde(rename = "ok")]
    Ok,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum MoonResultData {
    #[serde(alias = "ok")]
    Ok(MoonOk),
    TemperatureStore(TemperatureStore),
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum TempStoreData {
    TempTgtsPowers {
        temperatures: Vec<f32>,
        targets: Vec<f32>,
        powers: Vec<f32>,
    },
    Temp {
        temperatures: Vec<f32>,
    },
}

/// The names of the items in the temperature store
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum HeaterNames {
    #[serde(rename = "heater_bed")]
    HeaterBed,
    #[serde(rename = "extruder")]
    Extruder,
    #[serde(rename = "extruder1")]
    Extruder1,
    #[serde(rename = "extruder2")]
    Extruder2,
    #[serde(rename = "extruder3")]
    Extruder3,
    #[serde(rename = "extruder4")]
    Extruder4,
    #[serde(rename = "extruder5")]
    Extruder5,
    #[serde(rename = "extruder6")]
    Extruder6,
    #[serde(rename = "extruder7")]
    Extruder7,
    #[serde(rename = "extruder8")]
    Extruder8,
    #[serde(rename = "extruder9")]
    Extruder9,
    #[serde(rename = "extruder10")]
    Extruder10,
    #[serde(rename = "temperature_fan")]
    TemperatureFan,
    #[serde(rename = "temperature_sensor")]
    TemperatureSensor,
    NameStr(String),
}
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TemperatureStore {
    #[serde(flatten)]
    pub items: HashMap<HeaterNames, TempStoreData>,
}

impl TemperatureStore {
    pub fn new() -> Self {
        Self {
            items: HashMap::new(),
        }
    }
    pub fn add_to_hashmap(&mut self, key: HeaterNames, value: TempStoreData)  {
        self.items.insert(key, value);
    }
}


#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct MoonErrorContent {
    pub code: u32,
    pub message: String,
}


#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct GcodeStore {
    message: String,
    time: f32,
    #[serde(rename = "type")]
    typee: GcodeType,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum GcodeType {
    Command,
    Response,
}

impl MoonMSG {
    /// Creates a new MoonMSG which can be sent to Moonraker via the websocket
    /// The method is the name of the method to be called and is required for all messages
    /// The params are the parameters to be passed to the method and are optional for some types of messages
    /// refer to the moonraker api docs for more information for now.
    /// The id is the id of the message and is optional for some types of messages. It allows you to match up responses to requests.
    /// Assuming you use unique ids for every message you send, a response with a match id is the response to the request with that id.
    pub fn new(method: MoonMethod, params: Option<MoonParam>, id: Option<u32>) -> MoonMSG {
        match (params, id) {
            (None, None) => MoonMSG::Method { jsonrpc: JsonRpcVersion::V2_0, method },
            (None, Some(id)) => MoonMSG::MethodID { jsonrpc: JsonRpcVersion::V2_0, method, id },
            (Some(params), None) => MoonMSG::MethodParam { jsonrpc: JsonRpcVersion::V2_0, method, params },
            (Some(params), Some(id)) => MoonMSG::MethodParamID { jsonrpc: JsonRpcVersion::V2_0, method, params, id },
        }
    }
    pub fn new_error(error: MoonErrorContent, id: u32) -> MoonMSG {
        MoonMSG::MoonError {
            jsonrpc: JsonRpcVersion::V2_0,
            error,
            id,
        }
    }
    pub fn new_result(result: MoonResultData, id: u32) -> MoonMSG {
        MoonMSG::MoonResult {
            jsonrpc: JsonRpcVersion::V2_0,
            result,
            id,
        }
    }
    pub fn gcode(gcode: String, id: u32) -> MoonMSG {
        MoonMSG::new(MoonMethod::GcodeScript, Some(MoonParam::GcodeScript { script: gcode.to_string() }), Some(id))
    }
    pub fn method(&self) ->  MoonMethod {
        match self {
            MoonMSG::MoonResult { result, .. } => {
                panic!("No method on type MoonMSG::MoonResult: {:?}", result);
            },
            MoonMSG::MoonError { error, .. } => {
                panic!("Error: {:?}", error);
            },
            MoonMSG::MethodParamID { method, .. } => method.clone(),
            MoonMSG::MethodParamVecID { method, .. } => method.clone(),
            MoonMSG::MethodParam { method, .. } => method.clone(),
            MoonMSG::MethodParamVec { method, .. } => method.clone(),
            MoonMSG::MethodID { method, .. } => method.clone(),
            MoonMSG::Method { method, .. } => method.clone(),
            MoonMSG::CouldNotParseParams { method, .. } => method.clone(),
            MoonMSG::CouldNotParseParamsID { method, .. } => method.clone(),
            MoonMSG::CouldNotParseMethod { method, .. } => {
                panic!("CouldNotParseMethod does not have a method of type MoonMethod, it is String: {:?}", method);
            },
            MoonMSG::CouldNotParseMethodID { method, .. } => {
                panic!("CouldNotParseMethodID does not have a method of type MoonMethod, it is String: {:?}", method);
            },
            MoonMSG::CouldNotParseMethodParams { method, .. } => {
                panic!("CouldNotParseMethodParams does not have a method of type MoonMethod, it is String: {:?}", method);
            },
            MoonMSG::CouldNotParseMethodParamsID { method, .. } => {
                panic!("CouldNotParseMethodParamsID does not have a method of type MoonMethod, it is String: {:?}", method);
            },
            MoonMSG::Empty => {
                MoonMethod::Empty
            },
        }
    }
    pub fn params(&self) -> Vec<MoonParam> {
        match self {
            MoonMSG::MoonResult { result, .. } => {
                panic!("No params on type MoonMSG::MoonResult \n{:?}", result);
            },
            MoonMSG::MoonError {..} => {
                panic!("MoonError has no params");
            },
            MoonMSG::MethodParamID { params, .. } => vec! {params.clone()},
            MoonMSG::MethodParamVecID { params, .. } => params.clone(),
            MoonMSG::MethodParam { params, .. } => vec! { params.clone() },
            MoonMSG::MethodParamVec { params, .. } => params.clone(),
            MoonMSG::MethodID {..} => {
                panic!("MethodID has no params");
            },
            MoonMSG::Method {..} => {
                panic!("Method has no params");
            },
            MoonMSG::CouldNotParseParams { params, .. } => {
                panic!("CouldNotParseParams has params of type serde_json::Value not MoonParam. This function cannot be called on MoonMSG::CouldNotParseParams \n{:?}", params);
            },
            MoonMSG::CouldNotParseParamsID { params, .. } => {
                panic!("CouldNotParseParamsID has params of type serde_json::Value not MoonParam. This function cannot be called on MoonMSG::CouldNotParseParamsID \n{:?}", params);
            },
            MoonMSG::CouldNotParseMethod { params, .. } => vec! {params.clone()},
            MoonMSG::CouldNotParseMethodID { params, .. } => vec! {params.clone()},
            MoonMSG::CouldNotParseMethodParams { params, .. } => {
                panic!("CouldNotParseMethodParams has params of type serde_json::Value not MoonParam. This method cannot be called on MoonMSG::CouldNotParseMethodParams \n{:?}", params);
            },
            MoonMSG::CouldNotParseMethodParamsID { params, .. } => {
                panic!("CouldNotParseMethodParamsID has params of type serde_json::Value not MoonParam. This function cannot be called on MoonMSG::CouldNotParseMethodParamsID \n{:?}", params);
            },
            MoonMSG::Empty => {
                panic!("MoonMSG::Empty has no method");
            },
        }
    }
    pub fn set_id(&self, id: u32) -> MoonMSG {
        match self {
            MoonMSG::MoonResult { result, .. } => {
                MoonMSG::new_result(result.clone(), id)
                // MoonMSG::MoonResult { jsonrpc: JsonRpcVersion::V2_0, result: result.clone(), id }
            },
            MoonMSG::MoonError { error, .. } => {
                MoonMSG::new_error(error.clone(), id)
                // MoonMSG::MoonError { jsonrpc: JsonRpcVersion::V2_0, error.clone(), id }
            },
            MoonMSG::MethodParamID { method, params, .. } => {
                MoonMSG::new(method.clone(), Some(params.clone()), Some(id))
                // MoonMSG::MethodParamID { jsonrpc: JsonRpcVersion::V2_0, method: method.clone(), params: params.clone(), id: id }
            },
            MoonMSG::MethodParamVecID { method, params, .. } => {
                // MoonMSG::new(method.clone(), Some(params.clone()), Some(id))
                MoonMSG::MethodParamVecID { jsonrpc: JsonRpcVersion::V2_0, method: method.clone(), params: params.clone(), id }
            },
            MoonMSG::MethodParam { method, params, .. } => MoonMSG::MethodParamID { jsonrpc: JsonRpcVersion::V2_0, method: method.clone(), params: params.clone(), id },
            MoonMSG::MethodParamVec { method, params, .. } => MoonMSG::MethodParamVecID { jsonrpc: JsonRpcVersion::V2_0, method: method.clone(), params: params.clone() , id },
            MoonMSG::MethodID { method, .. } => MoonMSG::MethodID { jsonrpc: JsonRpcVersion::V2_0, method: method.clone(), id },
            MoonMSG::Method { method, .. } => MoonMSG::MethodID { jsonrpc: JsonRpcVersion::V2_0, method: method.clone(), id},
            MoonMSG::CouldNotParseParams { method, params, .. } => {
                MoonMSG::CouldNotParseParamsID { jsonrpc: JsonRpcVersion::V2_0, method: method.clone(), params: params.clone(), id }
            },
            MoonMSG::CouldNotParseParamsID { method, params, .. } => {
                MoonMSG::CouldNotParseParamsID { jsonrpc: JsonRpcVersion::V2_0, method: method.clone(), params: params.clone(), id }
            },
            MoonMSG::CouldNotParseMethod { method, params, .. } => {
                MoonMSG::CouldNotParseMethodID { jsonrpc: JsonRpcVersion::V2_0, method: method.clone(), params: params.clone(), id }
            },
            MoonMSG::CouldNotParseMethodID { method, params, .. } => {
                MoonMSG::CouldNotParseMethodID { jsonrpc: JsonRpcVersion::V2_0, method: method.clone(), params: params.clone(), id }
            },
            MoonMSG::CouldNotParseMethodParams { method, params, .. } => {
                MoonMSG::CouldNotParseMethodParamsID { jsonrpc: JsonRpcVersion::V2_0, method: method.clone(), params: params.clone(), id }
            },
            MoonMSG::CouldNotParseMethodParamsID { method, params, .. } => {
                MoonMSG::CouldNotParseMethodParamsID { jsonrpc: JsonRpcVersion::V2_0, method: method.clone(), params: params.clone(), id }
            },
            MoonMSG::Empty => {
                MoonMSG::new(MoonMethod::Empty, None, Some(id))
            },
        }
    }
    pub fn id(&self) -> Option<u32> {
        match self {
            MoonMSG::MoonResult { id, .. } => Some(id.clone()),
            MoonMSG::MoonError { id, .. } => Some(id.clone()),
            MoonMSG::MethodParamID { id, .. } => Some(id.clone()),
            MoonMSG::MethodParamVecID { id, .. } => Some(id.clone()),
            MoonMSG::MethodID { id, .. } => Some(id.clone()),
            MoonMSG::MethodParam {..} => None,
            MoonMSG::MethodParamVec {..} => None,
            MoonMSG::Method {..} => None,
            MoonMSG::CouldNotParseParams {..} => None,
            MoonMSG::CouldNotParseParamsID {id, ..} => Some(id.clone()),
            MoonMSG::CouldNotParseMethod {..} => None,
            MoonMSG::CouldNotParseMethodID {id, ..} => Some(id.clone()),
            MoonMSG::CouldNotParseMethodParams {..} => None,
            MoonMSG::CouldNotParseMethodParamsID {id, ..} => Some(id.clone()),
            MoonMSG::Empty => None,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum PrinterState {
    Ready,
    Paused,
}