NOSHP_Client/lib.rs
1//! # Nik's Open-source Smart Home Platform Client Library
2//!
3//! This crate is designed to serve as an easy to use API to communicate with the [NOSHP Server](https://github.com/niknik3610/IoT_Platform).
4//!
5//! # Example
6//! This is a simple example of how to use this library. It will the server about two buttons
7//! (defined in the config.toml file): "Turn On" and "Turn Off". In addition it also shows how a
8//! slider can be used.
9//!
10//! When either of these buttons are pressed, the callback assigned to them will be called, in this
11//! case printing their respective actions.
12//!
13//! The callback can have state assigned to it, and in the future will contain
14//! information about the user's actions. For example if they input a number into a field, this will be
15//! displayed in the request (not implemented yet).
16//!
17//! ```
18//! use std::error::Error;
19//! use NOSHP_Client::{
20//! client::{ClientState, NoshpClient, Request, UserDefinedState},
21//! client_config::{ClientConfig, ParsedConfig},
22//! };
23//!
24//! #[derive(Default)]
25//! struct ExampleState {
26//! pub text: String,
27//! pub current_brightness_lvl: u32
28//! }
29//! impl UserDefinedState for ExampleState {}
30//!
31//! const CONFIG_PATH: &str = "./example_config.toml";
32//! #[tokio::main]
33//! async fn main() -> Result<(), Box<dyn Error>> {
34//! let config = ClientConfig::load_config(CONFIG_PATH).unwrap();
35//!
36//! let client_handler = NoshpClient::new();
37//! client_handler
38//! .set_state(ExampleState {
39//! text: String::from("hello world"),
40//! current_brightness_lvl: 50,
41//! })
42//! .add_callback("Turn On", Box::new(turn_on_led))
43//! .add_callback("Turn Off", Box::new(turn_off_led))
44//! .add_callback("Brightness", Box::new(handle_brightness))
45//! .run(config)
46//! .await
47//! .unwrap();
48//!
49//! return Ok(());
50//! }
51//!
52//! fn turn_on_led(state: &mut ClientState<ExampleState>, _req: Request) {
53//! state.update_capability_availabillity("Turn On", false).unwrap();
54//! state.update_capability_availabillity("Turn Off", true).unwrap();
55//! state.user_state.text = String::from("Turned On");
56//!
57//! println!("State: {}", state.user_state.text);
58//! }
59//!
60//! fn turn_off_led(state: &mut ClientState<ExampleState>, _req: Request) {
61//! state.update_capability_availabillity("Turn On", true).unwrap();
62//! state.update_capability_availabillity("Turn Off", false).unwrap();
63//! state.user_state.text = String::from("Turned Off");
64//! println!("State: {}", state.user_state.text);
65//! }
66//!
67//! fn handle_brightness(state: &mut ClientState<ExampleState>, req: Request) {
68//! let new_brightness = req.value.unwrap();
69//! state.user_state.current_brightness_lvl = new_brightness as u32;
70//! println!("New brightness set: {}", state.user_state.current_brightness_lvl);
71//! }
72//!
73
74pub mod client;
75pub mod client_config;
76pub mod client_types;
77
78mod client_connection;
79mod client_polling;
80mod client_registration;