@@ -12,7 +12,7 @@ use tungstenite::Message;
1212use crate :: commands:: g_command;
1313use crate :: serialcom:: create_serialcom;
1414
15- use crate :: parser:: { m105, m114, m115, m119, m20, m33} ;
15+ use crate :: parser:: { m105, m114, m115, m119, m20, m27 , m31 , m33} ;
1616use crate :: structs:: MessageSender ;
1717use crate :: Config ;
1818use crate :: MessageType ;
@@ -73,7 +73,7 @@ async fn handle_connection(
7373 > ,
7474 ) -> Result < ( ) > {
7575 let json_str =
76- serde_json:: to_string ( & message) . expect ( "Failed to serialize messge into JSON" ) ;
76+ serde_json:: to_string ( & message) . expect ( "Failed to serialize message into JSON" ) ;
7777 let resp_message = Message :: Text ( json_str) ;
7878
7979 if let Err ( e) = ws_write. send ( resp_message) . await {
@@ -122,7 +122,7 @@ async fn handle_connection(
122122
123123 // Define response message
124124 let mut message_sender = MessageSender {
125- message_type : "MessageSender" . to_string ( ) ,
125+ message_type : cmd . to_string ( ) ,
126126 message : "" . to_string ( ) ,
127127 raw_message : response. clone ( ) ,
128128 timestamp,
@@ -133,38 +133,54 @@ async fn handle_connection(
133133 "M20" => {
134134 let response = m20 ( response) ;
135135 serde_json:: to_string ( & response) . expect (
136- "Failed to serialize messge into JSON" ,
136+ "Failed to serialize message into JSON" ,
137+ )
138+ }
139+ "M27" => {
140+ let response = m27 ( response) ;
141+ serde_json:: to_string ( & response) . expect (
142+ "Failed to serialize message into JSON" ,
143+ )
144+ }
145+ "M31" => {
146+ let response = m31 ( response) ;
147+ serde_json:: to_string ( & response) . expect (
148+ "Failed to serialize message into JSON" ,
149+ )
150+ }
151+ "M33" => {
152+ let response = m33 ( response) ;
153+ serde_json:: to_string ( & response) . expect (
154+ "Failed to serialize message into JSON" ,
137155 )
138156 }
139- "M33" => m33 ( response) ,
140157 "M105" => {
141158 let response = m105 ( response) ;
142159 serde_json:: to_string ( & response) . expect (
143- "Failed to serialize messge into JSON" ,
160+ "Failed to serialize message into JSON" ,
144161 )
145162 }
146163 "M114" => {
147164 let response = m114 ( response) ;
148165 serde_json:: to_string ( & response) . expect (
149- "Failed to serialize messge into JSON" ,
166+ "Failed to serialize message into JSON" ,
150167 )
151168 }
152169 "M115" => {
153170 let response = m115 ( response) ;
154171 serde_json:: to_string ( & response) . expect (
155- "Failed to serialize messge into JSON" ,
172+ "Failed to serialize message into JSON" ,
156173 )
157174 }
158175 "M119" => {
159176 let response = m119 ( response) ;
160177 serde_json:: to_string ( & response) . expect (
161- "Failed to serialize messge into JSON" ,
178+ "Failed to serialize message into JSON" ,
162179 )
163180 }
164181 _ => response. to_string ( ) ,
165182 } ;
166183 }
167- // Return response to WS clients
168184 send_message_back ( message_sender, & mut ws_write)
169185 . await ?;
170186 }
@@ -179,10 +195,53 @@ async fn handle_connection(
179195 }
180196 }
181197 MessageType :: SerialConfig => {
198+ // Not yet implemented, changes to the config loading is required
182199 debug ! ( "SerialConfig: {}" , message. message) ;
183- // Test GCode for printer info
184- // cmd = "M115";
185- //Expects message.message to be ex: /dev/USBtty01;119200
200+ }
201+ MessageType :: Terminal => {
202+ let cmd = message. message ;
203+ match create_serialcom (
204+ cmd,
205+ configuration. serial_port . to_string ( ) ,
206+ configuration. baud_rate ,
207+ ) {
208+ Ok ( response) => {
209+ debug ! ( "{:?}" , response) ;
210+
211+ // Get timestamp
212+ let since_epoch = now
213+ . duration_since ( UNIX_EPOCH )
214+ . expect ( "Time went backwards" ) ;
215+ let timestamp = since_epoch. as_secs ( ) ;
216+
217+ let message_sender = MessageSender {
218+ message_type : "terminal" . to_string ( ) ,
219+ message : response. to_string ( ) . clone ( ) ,
220+ raw_message : response,
221+ timestamp,
222+ } ;
223+
224+ send_message_back ( message_sender, & mut ws_write) . await ?;
225+ }
226+ Err ( e) => {
227+ error ! ( "{:?}" , e) ;
228+
229+ let since_epoch = now
230+ . duration_since ( UNIX_EPOCH )
231+ . expect ( "Time went backwards" ) ;
232+ let timestamp = since_epoch. as_secs ( ) ;
233+
234+ // Define response message
235+ let message_sender = MessageSender {
236+ message_type : "MessageSenderError" . to_string ( ) ,
237+ message : "Error executing command" . to_string ( ) ,
238+ raw_message : "Error executing command" . to_string ( ) ,
239+ timestamp,
240+ } ;
241+
242+ send_message_back ( message_sender, & mut ws_write) . await ?;
243+ }
244+ }
186245 }
187246 MessageType :: Unsafe => {
188247 let cmd = message. message ;
@@ -201,7 +260,7 @@ async fn handle_connection(
201260 let timestamp = since_epoch. as_secs ( ) ;
202261
203262 let message_sender = MessageSender {
204- message_type : "MessageSender " . to_string ( ) ,
263+ message_type : "Unsafe " . to_string ( ) ,
205264 message : response. to_string ( ) . clone ( ) ,
206265 raw_message : response,
207266 timestamp,
0 commit comments