obniz-rust 0.1.0

A Rust client library for Obniz IoT platform
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
use serde::{Deserialize, Serialize};
use serde_json::json;
use tokio_tungstenite::tungstenite::protocol::Message;

use crate::error::{validate_pin, ObnizError, ObnizResult};
use crate::obniz::Obniz;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum OutputType {
    PushPull5v,
    PushPull3v,
    OpenDrain,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum PullType {
    PullUp5v,
    PullUp3v,
    PullDown,
    Float,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Direction {
    Input,
    Output,
}

#[derive(Debug, Clone)]
pub struct IoConfig {
    pub direction: Direction,
    pub value: Option<bool>,
    pub output_type: Option<OutputType>,
    pub pull_type: Option<PullType>,
    pub stream: Option<bool>,
}

#[derive(Debug)]
pub struct IoPin {
    pin: u8,
    obniz: Obniz,
}

impl IoPin {
    pub fn new(pin: u8, obniz: Obniz) -> Self {
        Self { pin, obniz }
    }

    pub fn pin_key(&self) -> String {
        format!("io{}", self.pin)
    }

    /// Get the current state of the pin
    pub async fn get(&self) -> ObnizResult<bool> {
        validate_pin(self.pin)?;

        let pin_key = self.pin_key();
        let request = json!([{&pin_key: "get"}]);
        let message = Message::from(request.to_string());

        let response = self
            .obniz
            .send_await_response(message, pin_key.clone())
            .await
            .map_err(|e| ObnizError::Connection(e.to_string()))?;

        // Parse the response to extract the boolean value
        // Response format is typically [{"io1": false}]
        if let Some(array) = response.as_array() {
            if let Some(first_item) = array.first() {
                if let Some(value) = first_item.get(&pin_key) {
                    return value.as_bool().ok_or_else(|| {
                        ObnizError::IoOperation("Invalid response format".to_string())
                    });
                }
            }
        }

        // Fallback: try direct object access
        if let Some(value) = response.get(&pin_key) {
            value
                .as_bool()
                .ok_or_else(|| ObnizError::IoOperation("Invalid response format".to_string()))
        } else {
            Err(ObnizError::IoOperation(format!(
                "No response for pin {}",
                self.pin
            )))
        }
    }

    /// Set the pin to a specific value
    pub async fn set(&self, value: bool) -> ObnizResult<()> {
        validate_pin(self.pin)?;

        let pin_key = self.pin_key();
        let request = json!([{&pin_key: value}]);
        let message = Message::from(request.to_string());

        self.obniz
            .send_message(message)
            .map_err(|e| ObnizError::Connection(e.to_string()))?;
        Ok(())
    }

    /// Configure the pin with detailed settings
    pub async fn configure(&self, config: IoConfig) -> ObnizResult<()> {
        validate_pin(self.pin)?;
        let pin_key = self.pin_key();
        let mut pin_config = json!({
            "direction": config.direction
        });

        if let Some(value) = config.value {
            pin_config["value"] = json!(value);
        }

        if let Some(output_type) = config.output_type {
            pin_config["output_type"] = json!(output_type);
        }

        if let Some(pull_type) = config.pull_type {
            pin_config["pull_type"] = json!(pull_type);
        }

        if let Some(stream) = config.stream {
            pin_config["stream"] = json!(stream);
        }

        let request = json!([{&pin_key: pin_config}]);
        let message = Message::from(request.to_string());

        self.obniz
            .send_message(message)
            .map_err(|e| ObnizError::Connection(e.to_string()))?;
        Ok(())
    }

    /// Set the pin as input with optional stream mode
    pub async fn set_as_input(&self, enable_stream: bool) -> ObnizResult<()> {
        validate_pin(self.pin)?;
        let config = IoConfig {
            direction: Direction::Input,
            value: None,
            output_type: None,
            pull_type: None,
            stream: Some(enable_stream),
        };
        self.configure(config).await
    }

    /// Set the pin as output with a specific value
    pub async fn set_as_output(&self, value: bool) -> ObnizResult<()> {
        validate_pin(self.pin)?;
        let config = IoConfig {
            direction: Direction::Output,
            value: Some(value),
            output_type: None,
            pull_type: None,
            stream: None,
        };
        self.configure(config).await
    }

    /// Set the output type of the pin
    pub async fn set_output_type(&self, output_type: OutputType) -> ObnizResult<()> {
        validate_pin(self.pin)?;
        let pin_key = self.pin_key();
        let request = json!([{&pin_key: {"output_type": output_type}}]);
        let message = Message::from(request.to_string());

        self.obniz
            .send_message(message)
            .map_err(|e| ObnizError::Connection(e.to_string()))?;
        Ok(())
    }

    /// Set the pull type of the pin
    pub async fn set_pull_type(&self, pull_type: PullType) -> ObnizResult<()> {
        validate_pin(self.pin)?;
        let pin_key = self.pin_key();
        let request = json!([{&pin_key: {"pull_type": pull_type}}]);
        let message = Message::from(request.to_string());

        self.obniz
            .send_message(message)
            .map_err(|e| ObnizError::Connection(e.to_string()))?;
        Ok(())
    }

    /// Register a callback for this pin's state changes (stream mode)
    /// This will automatically enable stream mode for the pin
    pub async fn on_change<F>(&self, callback: F) -> ObnizResult<()>
    where
        F: Fn(bool) + Send + Sync + 'static,
    {
        validate_pin(self.pin)?;
        // First, enable stream mode for this pin
        self.set_as_input(true).await?;

        let pin_key = self.pin_key();
        let pin_key_clone = pin_key.clone();

        self.obniz
            .register_callback(pin_key, move |response| {
                // Parse the response to extract the pin value
                if let Some(value) = response.get(&pin_key_clone) {
                    if let Some(bool_value) = value.as_bool() {
                        callback(bool_value);
                    }
                }
            })
            .map_err(|e| ObnizError::CallbackError(e.to_string()))?;

        Ok(())
    }

    /// Enable stream mode for this pin without setting up a callback
    pub async fn enable_stream(&self) -> ObnizResult<()> {
        self.set_as_input(true).await
    }

    /// Disable stream mode for this pin
    pub async fn disable_stream(&self) -> ObnizResult<()> {
        self.set_as_input(false).await
    }

    /// Remove the callback for this pin
    pub fn remove_callback(&self) -> ObnizResult<()> {
        validate_pin(self.pin)?;
        let pin_key = self.pin_key();
        self.obniz
            .unregister_callback(pin_key)
            .map_err(|e| ObnizError::CallbackError(e.to_string()))
    }

    /// Deinitialize the pin
    pub async fn deinit(&self) -> ObnizResult<()> {
        validate_pin(self.pin)?;
        let pin_key = self.pin_key();
        let request = json!([{&pin_key: null}]);
        let message = Message::from(request.to_string());

        self.obniz
            .send_message(message)
            .map_err(|e| ObnizError::Connection(e.to_string()))?;
        Ok(())
    }
}

/// IO Manager for handling multiple pins
#[derive(Debug)]
pub struct IoManager {
    obniz: Obniz,
}

impl IoManager {
    pub fn new(obniz: Obniz) -> Self {
        Self { obniz }
    }

    /// Get a specific pin (0-11)
    pub fn pin(&self, pin: u8) -> ObnizResult<IoPin> {
        validate_pin(pin)?;
        Ok(IoPin::new(pin, self.obniz.clone()))
    }

    /// Get the current state of a pin
    pub async fn get_pin(&self, pin: u8) -> ObnizResult<bool> {
        self.pin(pin)?.get().await
    }

    /// Set a pin to a specific value
    pub async fn set_pin(&self, pin: u8, value: bool) -> ObnizResult<()> {
        self.pin(pin)?.set(value).await
    }

    /// Configure a pin with detailed settings
    pub async fn configure_pin(&self, pin: u8, config: IoConfig) -> ObnizResult<()> {
        self.pin(pin)?.configure(config).await
    }

    /// Set a pin as input with optional stream mode
    pub async fn set_pin_as_input(&self, pin: u8, enable_stream: bool) -> ObnizResult<()> {
        self.pin(pin)?.set_as_input(enable_stream).await
    }

    /// Set a pin as output with a specific value
    pub async fn set_pin_as_output(&self, pin: u8, value: bool) -> ObnizResult<()> {
        self.pin(pin)?.set_as_output(value).await
    }

    /// Register a callback for a pin's state changes (stream mode)
    /// This will automatically enable stream mode for the pin
    pub async fn set_pin_callback<F>(&self, pin: u8, callback: F) -> ObnizResult<()>
    where
        F: Fn(bool) + Send + Sync + 'static,
    {
        self.pin(pin)?.on_change(callback).await
    }

    /// Remove the callback for a pin
    pub fn remove_pin_callback(&self, pin: u8) -> ObnizResult<()> {
        self.pin(pin)?.remove_callback()
    }

    /// Enable stream mode for a pin without setting up a callback
    pub async fn enable_pin_stream(&self, pin: u8) -> ObnizResult<()> {
        self.pin(pin)?.enable_stream().await
    }

    /// Disable stream mode for a pin
    pub async fn disable_pin_stream(&self, pin: u8) -> ObnizResult<()> {
        self.pin(pin)?.disable_stream().await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::error::validate_pin;

    #[test]
    fn test_pin_validation() {
        // Valid pins
        assert!(validate_pin(0).is_ok());
        assert!(validate_pin(5).is_ok());
        assert!(validate_pin(11).is_ok());

        // Invalid pins
        assert!(validate_pin(12).is_err());
        assert!(validate_pin(255).is_err());
    }

    // #[test]
    // fn test_pin_key_generation() {
    //     let obniz = create_mock_obniz();
    //     let pin = IoPin::new(5, obniz);
    //     assert_eq!(pin.pin_key(), "io5");
    //
    //     let pin = IoPin::new(0, obniz);
    //     assert_eq!(pin.pin_key(), "io0");
    // }

    #[test]
    fn test_io_config_serialization() {
        let config = IoConfig {
            direction: Direction::Output,
            value: Some(true),
            output_type: Some(OutputType::PushPull5v),
            pull_type: Some(PullType::PullUp5v),
            stream: Some(false),
        };

        // Test that the config can be created without errors
        assert_eq!(config.direction, Direction::Output);
        assert_eq!(config.value, Some(true));
    }

    #[test]
    fn test_output_type_serialization() {
        use serde_json;

        let output_type = OutputType::PushPull5v;
        let serialized = serde_json::to_string(&output_type).unwrap();
        assert_eq!(serialized, "\"push-pull5v\"");

        let output_type = OutputType::OpenDrain;
        let serialized = serde_json::to_string(&output_type).unwrap();
        assert_eq!(serialized, "\"open-drain\"");
    }

    #[test]
    fn test_pull_type_serialization() {
        use serde_json;

        let pull_type = PullType::PullUp5v;
        let serialized = serde_json::to_string(&pull_type).unwrap();
        assert_eq!(serialized, "\"pull-up5v\"");

        let pull_type = PullType::Float;
        let serialized = serde_json::to_string(&pull_type).unwrap();
        assert_eq!(serialized, "\"float\"");
    }

    #[test]
    fn test_direction_serialization() {
        use serde_json;

        let direction = Direction::Input;
        let serialized = serde_json::to_string(&direction).unwrap();
        assert_eq!(serialized, "\"input\"");

        let direction = Direction::Output;
        let serialized = serde_json::to_string(&direction).unwrap();
        assert_eq!(serialized, "\"output\"");
    }

    // Helper function to create a mock Obniz instance for testing
    // Note: This would need to be implemented with proper mocking
    // fn create_mock_obniz() -> Obniz {
    //     // This is a placeholder - in a real implementation, you'd use a mock
    //     // or test framework to create a mock Obniz instance
    //     unimplemented!("Mock Obniz creation for testing")
    // }
}