use rs_macros::JsonSchemaOneOf;
use rship_sdk::{ActionArgs, EmitterArgs, InstanceArgs, SdkClient, TargetArgs};
use schemars::{schema_for, JsonSchema};
use serde::{Deserialize, Serialize};
use serde_json::json;
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct MyActionData {
pub data: String,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, JsonSchemaOneOf)]
#[serde(rename_all = "camelCase")]
pub enum ButtonState {
Off,
OnYellow,
OnRed,
OnGreen,
On,
Dimmed,
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchemaOneOf)]
#[serde(rename_all = "camelCase")]
pub enum ButtonColor {
Default,
Off,
White,
Warm,
Red,
Rose,
Pink,
Purple,
Amber,
Yellow,
DarkBlue,
Blue,
Ice,
Cyan,
Spring,
Green,
Mint,
LightGray,
DarkGray,
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct MyEmitterType {
pub data: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct SetButtonStateAction {
pub state: ButtonState,
pub color: ButtonColor,
}
pub async fn start_client() {
env_logger::init();
let address = std::env::var("RSHIP_ADDRESS").unwrap_or_else(|_| "localhost".to_string());
let port = std::env::var("RSHIP_PORT").unwrap_or_else(|_| "5155".to_string());
let url = format!("ws://{}:{}/myko", address, port);
log::info!("Connecting to: {}", url);
let sdk = SdkClient::init();
sdk.set_address(Some(url));
sdk.await_connection().await;
let instance = sdk
.add_instance(InstanceArgs {
name: "Rust SDK Example".into(),
short_id: "rust-sdk-example".into(),
code: "rust-sdk-example".into(),
service_id: "rust-sdk-service".into(),
cluster_id: None,
color: "#FF6B35".into(),
machine_id: "rust-sdk-machine".into(),
message: Some("Hello from Rust SDK!".into()),
status: rship_sdk::InstanceStatus::Available,
})
.await;
let mut target = instance
.add_target(TargetArgs {
name: "SDK Example Target".into(),
short_id: "sdk-example-target".into(),
category: "examples".into(),
parent_targets: None,
})
.await;
target
.add_action(
ActionArgs::<MyActionData>::new("Print Log".into(), "print-log".into()),
|action, data| {
println!("{}", data.data);
},
)
.await;
target
.add_action(
ActionArgs::<SetButtonStateAction>::new(
"Object With Enums".into(),
"object-with-enums".into(),
),
|action, data| {
println!("Object With Enums: {:?}", data);
},
)
.await;
println!("{}", json!(schema_for!(SetButtonStateAction)));
let emitter = target
.add_emitter(EmitterArgs::<MyEmitterType>::new(
"Number Go Up".into(),
"number-go-up".into(),
))
.await;
let mut counter = 0;
loop {
emitter
.pulse(MyEmitterType {
data: format!("{}", counter),
})
.await
.expect("Failed to pulse emitter");
counter += 1;
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
}
}
#[tokio::main]
async fn main() {
start_client().await;
}