Skip to content

Commit

Permalink
jsonl (#10)
Browse files Browse the repository at this point in the history
* jsonl

* format
  • Loading branch information
cgorski authored Apr 21, 2024
1 parent ef13eaa commit 5cf2ca0
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 31 deletions.
31 changes: 27 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,7 @@ Rust ecosystem.

## Library Usage

To include this library in your Rust project, add the following to your `Cargo.toml`:

```toml
[dependencies]
glow-control-lib = { version = "0.3.5", path = "../glow-control-lib" }
```

Here's a simple example of how to use the library to set Twinkly devices to a specific mode:
A simple example of how to use the library to set Twinkly devices to a specific mode:

```rust
use std::collections::HashSet;
Expand Down
12 changes: 6 additions & 6 deletions glow-control-lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "glow-control-lib"
version = "0.3.5"
version = "0.4.0"
edition = "2021"
description = "A library for controlling programmable LED lights"
license = "MIT OR Apache-2.0"
Expand All @@ -19,9 +19,9 @@ reqwest = { version = "0.12", features = ["json"] }
tokio = { version = "1.0", features = ["full"] }
clap = { version = "4.4", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1"
log = "0"
env_logger = "0.10"
serde_json = "1.0"
log = "0.4"
env_logger = "0.11"
base64 = "0.22"
rand = "0.8"
sha1 = "0.10"
Expand All @@ -31,5 +31,5 @@ rand_distr = "0.4"
chrono = "0.4"
bytes = "1.1"
palette = "0.7"
derivative = "2.2.0"
uuid = { version = "1.7.0", features = ["v4"] }
derivative = "2.2"
uuid = { version = "1.8", features = ["v4"] }
53 changes: 48 additions & 5 deletions glow-control-lib/src/control_interface/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::cmp::Ordering;
use std::collections::HashSet;
use std::fmt;
use std::io::{BufReader, Read};
use std::io::{BufRead, BufReader, Read};
use std::path::Path;
use std::str::FromStr;
use std::time::Duration;
Expand Down Expand Up @@ -359,7 +359,9 @@ impl ControlInterface {
RtStdinFormat::Binary => {
self.show_real_time_stdin_stream_binary(&mut reader).await?
} // RtStdinFormat::Ascii => process_ascii_stream(reader)?,
// RtStdinFormat::JsonLines => process_json_lines_stream(reader)?,
RtStdinFormat::JsonLines => {
self.show_real_time_stdin_stream_jsonl(&mut reader).await?
}
};
match error_mode {
RtStdinErrorMode::IgnoreInvalidAddress => {}
Expand Down Expand Up @@ -417,6 +419,21 @@ impl ControlInterface {
Ok(led)
}

async fn show_real_time_stdin_stream_jsonl(
&self,
reader: &mut BufReader<impl Read>,
) -> anyhow::Result<AddressableLed> {
// Read a line from the input stream
let mut line = String::new();
reader.read_line(&mut line)?;
// Deserialize the JSON line
let led: AddressableLedJsonLFormat = serde_json::from_str(&line)?;
// Convert the JSON format into the standard format
let led: AddressableLed = led.into();

Ok(led)
}

pub async fn show_real_time_test_color_wheel(
&self,
step: f64,
Expand Down Expand Up @@ -1042,13 +1059,20 @@ pub struct Challenge {
challenge: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct RGB {
pub red: u8,
pub green: u8,
pub blue: u8,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct RgbJsonLFormat {
pub red: u8,
pub green: u8,
pub blue: u8,
}

// implement From for (u8,u8,u8) to RGB and vice verse
impl From<(u8, u8, u8)> for RGB {
fn from(tuple: (u8, u8, u8)) -> Self {
Expand Down Expand Up @@ -1394,7 +1418,7 @@ async fn send_challenge(
pub enum RtStdinFormat {
Binary,
// Ascii,
// JsonLines,
JsonLines,
}

#[derive(Clone, Copy, PartialEq, Eq, ValueEnum)]
Expand All @@ -1412,12 +1436,31 @@ pub struct BinaryStreamFormat {
pub blue: u8,
}

#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct AddressableLed {
pub address: u16,
pub color: RGB,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct AddressableLedJsonLFormat {
pub address: u16,
pub color: RgbJsonLFormat,
}

impl From<AddressableLedJsonLFormat> for AddressableLed {
fn from(data: AddressableLedJsonLFormat) -> Self {
AddressableLed {
address: data.address,
color: RGB {
red: data.color.red,
green: data.color.green,
blue: data.color.blue,
},
}
}
}

impl AddressableLed {
pub fn merge_frame_array(new_values: &Vec<AddressableLed>, old_frame: &mut [(u8, u8, u8)]) {
for led in new_values {
Expand Down
12 changes: 6 additions & 6 deletions glow-control-website/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tokio = { version = "1", features = ["full"] }
mime_guess = "2"
aws-sdk-s3 = "1"
aws-config = "1"
anyhow = "1"
futures = "0.3.30"
tokio = { version = "1.37", features = ["full"] }
mime_guess = "2.0"
aws-sdk-s3 = "1.23"
aws-config = "1.2"
anyhow = "1.0"
futures = "0.3"
4 changes: 2 additions & 2 deletions glow-control/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "glow-control"
version = "0.3.5"
version = "0.4.0"
edition = "2021"
description = "A CLI for controlling programmable LED lights"
license = "MIT OR Apache-2.0"
Expand All @@ -18,7 +18,7 @@ anyhow = "1.0"
tokio = { version = "1.0", features = ["full"] }
clap = { version = "4.4", features = ["derive"] }
env_logger = "0.10"
glow-control-lib = { version = "0.3.5", path = "../glow-control-lib" }
glow-control-lib = { version = "0.4.0", path = "../glow-control-lib" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_yaml = "0.9"

0 comments on commit 5cf2ca0

Please sign in to comment.