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
use std::io::{Read, Write};
use std::net::TcpStream;

/// The `OpenShowVar` structure is used to connect to a robot control system and read/write variable values over a TCP connection.
pub struct OpenShowVar {
    /// IP address for the TCP connection.
    tcp_ip: String,
    /// Port number for the TCP connection.
    tcp_port: u16,
    /// TCP connection.
    pub conn: Option<TcpStream>,
}

impl OpenShowVar {
    /// Creates a new instance of `OpenShowVar`.
    ///
    /// # Arguments
    ///
    /// * `tcp_ip` - IP address of the TCP server to connect to.
    /// * `tcp_port` - Port number of the TCP server to connect to.
    ///
    /// # Returns
    ///
    /// Returns a new instance of OpenShowVar.
    ///
    /// # Example
    ///
    /// ```
    /// use rs_openshowvar::OpenShowVar;
    /// let mut osv = OpenShowVar::new("127.0.0.1".to_string(), 7000);
    /// ```
    pub fn new(tcp_ip: String, tcp_port: u16) -> OpenShowVar {
        OpenShowVar {
            tcp_ip,
            tcp_port,
            conn: None,
        }
    }

    /// Connects to the TCP server.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` if the TCP connection is successful.
    /// Returns `std::io::Error` if the TCP connection fails.
    ///
    /// # Example
    ///
    /// ```
    /// use rs_openshowvar::OpenShowVar;
    /// let mut osv = OpenShowVar::new("127.0.0.1".to_string(), 7000);
    /// match osv.connect() {
    ///     Ok(_) => println!("Connection successful"),
    ///     Err(e) => println!("Connection error: {}", e),
    /// }
    /// ```
    pub fn connect(&mut self) -> std::io::Result<()> {
        // Create address by combining IP address and port number
        let addr = format!("{}:{}", self.tcp_ip, self.tcp_port);
        // Establish TCP connection
        self.conn = Some(TcpStream::connect(addr)?);
        Ok(())
    }

    /// Sends a variable value.
    ///
    /// # Arguments
    ///
    /// * `var_name` - Name of the variable to send.
    /// * `val` - Value of the variable to send.
    ///
    /// # Returns
    ///
    /// Returns a `std::io::Result` depending on the sent data.
    ///
    /// # Example
    ///
    /// ```
    /// use rs_openshowvar::OpenShowVar;
    /// let mut osv = OpenShowVar::new("127.0.0.1".to_string(), 7000);
    /// match osv.send("variable_name", "value") {
    ///     Ok(data) => println!("Data sent: {:?}", data),
    ///     Err(e) => println!("Sending error: {}", e),
    /// }
    /// ```
    pub fn send(&mut self, var_name: &str, val: &str) -> std::io::Result<Vec<u8>> {
        // Calculate lengths of variable name and value
        let mut msg = Vec::new();
        let mut temp = Vec::new();

        // If value exists, add its length and value
        if !val.is_empty() {
            let val_len = val.len();
            msg.push(((val_len & 0xff00) >> 8) as u8);
            msg.push((val_len & 0x00ff) as u8);
            msg.extend_from_slice(val.as_bytes());
        }

        // If variable name exists, add its length and name
        if !var_name.is_empty() {
            let var_name_len = var_name.len();
            temp.push(((var_name_len & 0xff00) >> 8) as u8);
            temp.push((var_name_len & 0x00ff) as u8);
            temp.extend_from_slice(var_name.as_bytes());
        }

        // If value exists, indicate its presence by adding a byte. Indicates whether it's Read or Write
        if !val.is_empty() {
            temp.insert(0, 1);
        } else {
            temp.insert(0, 0);
        }

        msg = [&temp[..], &msg[..]].concat();

        // Calculate message length and create header
        let msg_len = msg.len() as u16;
        let mut header = vec![0u8; 4];
        header[0] = ((0 & 0xff00) >> 8) as u8;
        header[1] = (0 & 0x00ff) as u8;
        header[2] = ((msg_len & 0xff00) >> 8) as u8;
        header[3] = (msg_len & 0x00ff) as u8;

        // Create request by combining header and message
        let request = [&header[..], &msg[..]].concat();

        // If connection exists, send the request
        if let Some(ref mut conn) = self.conn {
            conn.write_all(&request)?;
            let mut response = vec![0u8; 1024];
            let n = conn.read(&mut response)?;
            response.truncate(n);

            // Filter visible characters and process the response
            let visible_chars: Vec<u8> = response
                .iter()
                .cloned()
                .filter(|&byte| byte >= 32 && byte <= 126)
                .collect();

            let response_str = String::from_utf8_lossy(&visible_chars).to_string();
            // Check for error conditions
            if response_str.trim().is_empty() || response[response.len() - 1] == 0 {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::NotFound,
                    "Variable not found",
                ));
            }
            Ok(response)
        } else {
            Err(std::io::Error::new(
                std::io::ErrorKind::NotConnected,
                "Not connected",
            ))
        }
    }

    /// Reads the specified variable.
    ///
    /// # Arguments
    ///
    /// * `var_name` - Name of the variable to read.
    ///
    /// # Returns
    ///
    /// Returns the read variable value inside `std::io::Result<String>`.
    ///
    /// # Example
    ///
    /// ```
    /// use rs_openshowvar::OpenShowVar;
    /// let mut osv = OpenShowVar::new("127.0.0.1".to_string(), 7000);
    /// match osv.read("variable_name") {
    ///     Ok(val) => println!("Read value: {}", val),
    ///     Err(e) => println!("Reading error: {}", e),
    /// }
    /// ```
    pub fn read(&mut self, var_name: &str) -> std::io::Result<String> {
        // Return error if variable name to read is empty
        if var_name.is_empty() {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                "Empty variable name",
            ));
        }

        // Read variable value
        let response = self.send(var_name, "")?;

        // Check if the response is valid
        if response.len() < 7 {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Invalid response length",
            ));
        }

        // Get value length
        let val_len = ((response[5] as u16) << 8) | (response[6] as u16);

        // Return error if response length doesn't match value length
        if response.len() < (7 + val_len as usize) {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Response length does not match value length",
            ));
        }

        // Return the variable value as a string
        let var_value = String::from_utf8_lossy(&response[7..(7 + val_len as usize)]).to_string();
        Ok(var_value)
    }

    /// Writes a value to the specified variable.
    ///
    /// # Arguments
    ///
    /// * `var_name` - Name of the variable to write.
    /// * `val` - Value to write to the variable.
    ///
    /// # Returns
    ///
    /// Returns the written variable value inside `std::io::Result<String>`.
    ///
    /// # Example
    ///
    /// ```
    /// use rs_openshowvar::OpenShowVar;
    /// let mut osv = OpenShowVar::new("127.0.0.1".to_string(), 7000);
    /// match osv.write("variable_name", "value") {
    ///     Ok(val) => println!("Written value: {}", val),
    ///     Err(e) => println!("Writing error: {}", e),
    /// }
    /// ```
    pub fn write(&mut self, var_name: &str, val: &str) -> std::io::Result<String> {
        // Return error if variable name to write is empty
        if var_name.is_empty() {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                "Empty variable name",
            ));
        }

        // Return error if value to write is empty
        if val.is_empty() {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                "Empty value",
            ));
        }

        // Write the variable value
        let response = self.send(var_name, val)?;

        // Check if the response is valid
        if response.len() < 7 {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Invalid response length",
            ));
        }

        // Get value length
        let val_len = ((response[5] as u16) << 8) | (response[6] as u16);

        // Return error if response length doesn't match value length
        if response.len() < (7 + val_len as usize) {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Response length does not match value length",
            ));
        }

        // Return the variable value as a string
        let var_value = String::from_utf8_lossy(&response[7..(7 + val_len as usize)]).to_string();
        Ok(var_value)
    }

    /// Terminates the TCP connection.
    ///
    /// # Example
    ///
    /// ```
    /// use rs_openshowvar::OpenShowVar;
    /// let mut osv = OpenShowVar::new("127.0.0.1".to_string(), 7000);
    /// osv.disconnect();
    /// ```
    pub fn disconnect(&mut self) {
        // Close the connection if it exists
        if let Some(ref mut conn) = self.conn {
            let _ = conn.shutdown(std::net::Shutdown::Both);
            self.conn = None;
        }
    }
}