Skip to content

Commit

Permalink
Adding divoom gateway with swagger ui (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
r12f authored Aug 1, 2022
1 parent 69acfe3 commit c69538a
Show file tree
Hide file tree
Showing 19 changed files with 4,056 additions and 51 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[workspace]
members = [
"divoom",
"divoom_cli"
"divoom_cli",
"divoom_gateway",
]

[profile.dev]
Expand Down
30 changes: 15 additions & 15 deletions build/azure-pipeline/workflow-common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ stages:
#
# Pack
#
- script: just pack-prepare
- script: just pack-prepare-all
displayName: Prepare to pack packages
env:
BUILD_PROFILE: "release"
Expand All @@ -197,8 +197,8 @@ stages:
BUILD_PROFILE: "release"
BUILD_ARCH: $(target_arch)

- script: just pack-binary divoom_cli
displayName: Pack divoom-cli binaries
- script: just pack-binary-all
displayName: Pack divoom binaries
env:
BUILD_PROFILE: "release"
BUILD_ARCH: $(target_arch)
Expand All @@ -209,26 +209,26 @@ stages:
BUILD_SIGNING_CLIENT_SECRET: $(BuildSigningClientSecret)
BUILD_SIGNING_CERT_NAME: $(BuildSigningCertName)

- script: just pack-symbols divoom_cli
displayName: Pack divoom-cli symbols
- script: just pack-symbols-all
displayName: Pack divoom binary symbols
env:
BUILD_PROFILE: "release"
BUILD_ARCH: $(target_arch)

- script: just pack-binary-zip divoom_cli
displayName: Zip divoom-cli binaries
- script: just pack-binary-zip-all
displayName: Zip divoom binaries
env:
BUILD_PROFILE: "release"
BUILD_ARCH: $(target_arch)

- script: just pack-nuget divoom_cli
displayName: Pack divoom-cli nuget package
- script: just pack-nuget-all
displayName: Pack divoom nuget packages
env:
BUILD_PROFILE: "release"
BUILD_ARCH: $(target_arch)

- script: just pack-msix divoom_cli
displayName: Pack divoom-cli msix package
- script: just pack-msix-all
displayName: Pack divoom msix packages
condition: and(succeeded(), eq(variables['target_os'], 'windows'))
env:
BUILD_PROFILE: "release"
Expand All @@ -240,15 +240,15 @@ stages:
BUILD_SIGNING_CLIENT_SECRET: $(BuildSigningClientSecret)
BUILD_SIGNING_CERT_NAME: $(BuildSigningCertName)

- script: just pack-choco divoom_cli
displayName: Pack divoom-cli chocolatey package source
- script: just pack-choco-all
displayName: Pack divoom chocolatey package sources
condition: and(succeeded(), eq(variables['target_os'], 'windows'), eq(variables['target_arch'], 'x86_64'))
env:
BUILD_PROFILE: "release"
BUILD_ARCH: $(target_arch)

- script: just pack-scoop divoom_cli
displayName: Pack divoom-cli scoop package source
- script: just pack-scoop-all
displayName: Pack divoom scoop package sources
condition: and(succeeded(), eq(variables['target_os'], 'windows'), eq(variables['target_arch'], 'x86_64'))
env:
BUILD_PROFILE: "release"
Expand Down
3 changes: 2 additions & 1 deletion build/post-build/post-build.justfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ set dotenv-load
# Global variables
BUILD_VERSION := env_var_or_default("BUILD_VERSION", "")
BUILD_FOLDER_PREFIX := "./Build.Build."
BUILD_MODULES := "divoom_cli"

INTERMEDIATE_FOLDER := "./temp"
INTERMEDIATE_SYMBOL_PACKAGE_FOLDER := INTERMEDIATE_FOLDER + "/symbols"
Expand Down Expand Up @@ -72,6 +71,7 @@ pack-choco:
New-Item -ItemType Directory -Path "{{RELEASE_CHOCO_FOLDER}}" -Force | Out-Null

just _pack-choco-with-package divoom_cli
just _pack-choco-with-package divoom_gateway

_pack-choco-with-package PACKAGE="divoom_cli":
if (-not (Test-Path "{{INTERMEDIATE_CHOCO_PACKAGE_FOLDER}}/{{PACKAGE}}")) { \
Expand All @@ -93,6 +93,7 @@ pack-scoop:
New-Item -ItemType Directory -Path "{{RELEASE_SCOOP_FOLDER}}" -Force | Out-Null

just _pack-scoop-with-package divoom_cli
just _pack-scoop-with-package divoom_gateway

_pack-scoop-with-package PACKAGE="divoom_cli":
@Write-Host "Generating final scoop package for {{PACKAGE}} to {{RELEASE_SCOOP_FOLDER}} ..."
Expand Down
6 changes: 3 additions & 3 deletions divoom/src/clients/pixoo/pixoo_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::divoom_contracts::pixoo::common::*;
use crate::divoom_contracts::pixoo::system::*;
use crate::divoom_contracts::pixoo::tool::*;
use crate::dto::*;
use std::rc::Rc;
use std::sync::Arc;
use std::time::Duration;

#[cfg(feature = "animation-builder")]
Expand All @@ -27,7 +27,7 @@ use crate::animation::*;
/// // println!("{:?}", result);
/// ```
pub struct PixooClient {
client: Rc<DivoomRestAPIClient>,
client: Arc<DivoomRestAPIClient>,
}

macro_rules! impl_pixoo_client_api {
Expand Down Expand Up @@ -80,7 +80,7 @@ impl PixooClient {
/// Create new PixooClient with options
pub fn with_options(device_address: &str, timeout: Option<Duration>) -> PixooClient {
PixooClient {
client: Rc::new(DivoomRestAPIClient::new(
client: Arc::new(DivoomRestAPIClient::new(
format!("http://{}", device_address),
timeout,
)),
Expand Down
79 changes: 55 additions & 24 deletions divoom/src/clients/pixoo/pixoo_command_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,38 @@ use crate::divoom_contracts::pixoo::tool::*;
use crate::*;
use crate::{DivoomAPIError, DivoomAPIResult};
use serde::de::DeserializeOwned;
use std::rc::Rc;
use std::sync::{Arc, Mutex};

/// Pixoo command builder for creating the JSON payload of Pixoo commands.
pub struct PixooCommandBuilder {
command_store: Box<dyn PixooCommandStore>,
client: Rc<DivoomRestAPIClient>,
command_store: Arc<Mutex<Option<Box<dyn PixooCommandStore + Send>>>>,
client: Arc<DivoomRestAPIClient>,
}

/// Constructors, builder and executor
impl PixooCommandBuilder {
pub(crate) fn start(client: Rc<DivoomRestAPIClient>) -> PixooCommandBuilder {
pub(crate) fn start(client: Arc<DivoomRestAPIClient>) -> PixooCommandBuilder {
PixooCommandBuilder {
command_store: Box::new(PixooSingleCommandStore::new()),
command_store: Arc::new(Mutex::new(Some(Box::new(PixooSingleCommandStore::new())))),
client,
}
}

pub(crate) fn start_batch(client: Rc<DivoomRestAPIClient>) -> PixooCommandBuilder {
pub(crate) fn start_batch(client: Arc<DivoomRestAPIClient>) -> PixooCommandBuilder {
PixooCommandBuilder {
command_store: Box::new(PixooBatchedCommandStore::new()),
command_store: Arc::new(Mutex::new(Some(Box::new(PixooBatchedCommandStore::new())))),
client,
}
}

pub(crate) fn build(self) -> (Rc<DivoomRestAPIClient>, usize, String) {
let (command_count, request_body) = self.command_store.to_payload();
pub(crate) fn build(self) -> (Arc<DivoomRestAPIClient>, usize, String) {
let (command_count, request_body) = self
.command_store
.lock()
.unwrap()
.take()
.unwrap()
.to_payload();
(self.client, command_count, request_body)
}

Expand Down Expand Up @@ -67,7 +73,9 @@ impl PixooCommandBuilder {
}

pub async fn execute(self) -> DivoomAPIResult<()> {
if self.command_store.mode() == PixooCommandStoreMode::Single {
if self.command_store.lock().unwrap().as_ref().unwrap().mode()
== PixooCommandStoreMode::Single
{
return Err(DivoomAPIError::ParameterError(
"Command builder is not running in batch mode. Please use start_batch() instead."
.to_string(),
Expand All @@ -90,21 +98,21 @@ impl PixooCommandBuilder {
macro_rules! impl_command_builder {
($api_name:ident, $api_doc_path:literal, $req_type:ty) => (
#[doc = include_str!($api_doc_path)]
pub fn $api_name(mut self) -> PixooCommandBuilder {
pub fn $api_name(self) -> PixooCommandBuilder {
let request = <$req_type>::new();
let serialized_request = serde_json::to_string(&request).expect("Serializing pixoo command failed!");
self.command_store.append(serialized_request);
self.command_store.lock().unwrap().as_mut().unwrap().append(serialized_request);
self
}
);

($api_name:ident, $api_doc_path:literal, $req_type:ty, $req_payload_type:ty, $($api_arg:ident: $api_arg_type:ty),*) => (
#[doc = include_str!($api_doc_path)]
pub fn $api_name(mut self, $($api_arg: $api_arg_type),*) -> PixooCommandBuilder {
pub fn $api_name(self, $($api_arg: $api_arg_type),*) -> PixooCommandBuilder {
let payload = <$req_payload_type>::new($($api_arg),*);
let request = <$req_type>::new(payload);
let serialized_request = serde_json::to_string(&request).expect("Serializing pixoo command failed!");
self.command_store.append(serialized_request);
self.command_store.lock().unwrap().as_mut().unwrap().append(serialized_request);
self
}
)
Expand Down Expand Up @@ -334,7 +342,7 @@ impl PixooCommandBuilder {

#[doc = include_str!("../../divoom_contracts/pixoo/animation/api_send_image_animation_frame.md")]
pub fn send_image_animation(
mut self,
self,
id: i32,
animation: DivoomImageAnimation,
) -> PixooCommandBuilder {
Expand All @@ -346,7 +354,12 @@ impl PixooCommandBuilder {
let request = DivoomPixooCommandAnimationSendImageAnimationFrameRequest::new(payload);
let serialized_request =
serde_json::to_string(&request).expect("Serializing pixoo command failed!");
self.command_store.append(serialized_request);
self.command_store
.lock()
.unwrap()
.as_mut()
.unwrap()
.append(serialized_request);
});
self
}
Expand Down Expand Up @@ -389,8 +402,13 @@ impl PixooCommandBuilder {

/// Raw API implementations
impl PixooCommandBuilder {
pub fn send_raw_request(mut self, request: String) -> PixooCommandBuilder {
self.command_store.append(request);
pub fn send_raw_request(self, request: String) -> PixooCommandBuilder {
self.command_store
.lock()
.unwrap()
.as_mut()
.unwrap()
.append(request);
self
}
}
Expand All @@ -399,11 +417,24 @@ impl PixooCommandBuilder {
mod tests {
use super::*;
use std::collections::BTreeMap;
use std::sync::Arc;
use std::{env, fs};

fn is_send<T: Send>(_: T) {}

#[test]
fn pixoo_command_builder_should_be_send() {
let client = Arc::new(DivoomRestAPIClient::new(
"http://192.168.0.123".to_string(),
None,
));
let builder = PixooCommandBuilder::start_batch(client);
is_send(builder);
}

#[test]
fn pixoo_command_builder_should_work_with_channel_commands_in_batch_mode() {
let client = Rc::new(DivoomRestAPIClient::new(
let client = Arc::new(DivoomRestAPIClient::new(
"http://192.168.0.123".to_string(),
None,
));
Expand All @@ -425,7 +456,7 @@ mod tests {

#[test]
fn pixoo_command_builder_should_work_with_system_commands_in_batch_mode() {
let client = Rc::new(DivoomRestAPIClient::new(
let client = Arc::new(DivoomRestAPIClient::new(
"http://192.168.0.123".to_string(),
None,
));
Expand Down Expand Up @@ -453,7 +484,7 @@ mod tests {

#[test]
fn pixoo_command_builder_should_work_with_tool_commands_in_batch_mode() {
let client = Rc::new(DivoomRestAPIClient::new(
let client = Arc::new(DivoomRestAPIClient::new(
"http://192.168.0.123".to_string(),
None,
));
Expand Down Expand Up @@ -499,7 +530,7 @@ mod tests {
image_animation.frames.insert(0, vec![1, 2, 3]);
image_animation.frames.insert(3, vec![4, 5, 6]);

let client = Rc::new(DivoomRestAPIClient::new(
let client = Arc::new(DivoomRestAPIClient::new(
"http://192.168.0.123".to_string(),
None,
));
Expand All @@ -524,7 +555,7 @@ mod tests {

#[test]
fn pixoo_command_builder_should_work_with_batch_commands_in_batch_mode() {
let client = Rc::new(DivoomRestAPIClient::new(
let client = Arc::new(DivoomRestAPIClient::new(
"http://192.168.0.123".to_string(),
None,
));
Expand All @@ -540,7 +571,7 @@ mod tests {

#[test]
fn pixoo_command_builder_should_work_with_raw_commands_in_batch_mode() {
let client = Rc::new(DivoomRestAPIClient::new(
let client = Arc::new(DivoomRestAPIClient::new(
"http://192.168.0.123".to_string(),
None,
));
Expand Down
5 changes: 2 additions & 3 deletions divoom_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@ env_logger = "0.9.0"
tokio = { version = "1.13.0", features = ["rt-multi-thread", "time", "sync", "macros", "net", "io-util"] }
thiserror = "1.0.30"
serde = { version = "1.0.130", features = ["derive"] }
serde_yaml = "0.8.25"
serde_yaml = "0.9"
serde_json = "1.0.82"
rgb = "0.8.33"
structopt = "0.3.26"
divoom = { version = "0.0.1", path = "../divoom" }
tiny-skia = "0.7.0"
tiny-skia = { version = "0.7.0", features = ["std", "simd"]}

[build-dependencies]
winres = "0.1.12"

[dev-dependencies]
pretty_assertions = "1.0.0"
mockito = "0.31.0"

[package.metadata.winres]
LegalCopyright = "Copyright (c) 2022 r12f"
6 changes: 3 additions & 3 deletions divoom_cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ clock-id: 100
brightness: 100
```

## Installation
## How to install

### via Cargo
```bash
Expand Down Expand Up @@ -138,7 +138,7 @@ customPage

### More help

We can find more info in the commmand help like below.
We can find more info in the command help like below.

```bash
> divoom-cli
Expand Down Expand Up @@ -174,7 +174,7 @@ SUBCOMMANDS:

To debug and see the logs and the raw request that we send, we can use `RUST_LOG` environment variable to change the logging level to `debug` to enable the logs:

On windows with powershell:
On Windows with powershell:

```powershell
$env:RUST_LOG="debug"; divoom-cli 192.168.0.123 channel get
Expand Down
Loading

0 comments on commit c69538a

Please sign in to comment.