Skip to content

Commit b2c8ba1

Browse files
committed
feat(api): JSON API
1 parent 4ccb57c commit b2c8ba1

15 files changed

+623
-191
lines changed

Cargo.lock

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ sqlx = { version = "0.5", features = ["sqlite", "runtime-tokio-rustls"] }
4242
structopt = { version = "0.3", features = ["paw"] }
4343
strum = "0.20"
4444
strum_macros = "0.20"
45+
sysinfo = { version = "0.18", default-features = false }
4546
thiserror = "1.0"
4647
tokio = { version = "1", features = ["macros", "net", "rt-multi-thread", "signal", "sync", "time"] }
4748
tokio-util = { version = "0.6", features = ["codec"] }

src/api/flat.rs

+25-15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::sync::Arc;
55
use thiserror::Error;
66

77
use crate::{
8+
component::ComponentName,
89
global::{Global, InputMessage, InputMessageData, InputSourceHandle, InputSourceName},
910
image::{RawImage, RawImageError},
1011
models::Color,
@@ -43,11 +44,14 @@ pub async fn handle_request(
4344
if let Some(clear) = request.command_as_clear() {
4445
// Update state
4546
if clear.priority() < 0 {
46-
source.send(InputMessageData::ClearAll)?;
47+
source.send(ComponentName::FlatbufServer, InputMessageData::ClearAll)?;
4748
} else {
48-
source.send(InputMessageData::Clear {
49-
priority: clear.priority(),
50-
})?;
49+
source.send(
50+
ComponentName::FlatbufServer,
51+
InputMessageData::Clear {
52+
priority: clear.priority(),
53+
},
54+
)?;
5155
}
5256
} else if let Some(color) = request.command_as_color() {
5357
let rgb = color.data();
@@ -58,12 +62,15 @@ pub async fn handle_request(
5862
);
5963

6064
// Update state
61-
source.send(InputMessageData::SolidColor {
62-
// TODO
63-
priority: 0,
64-
duration: i32_to_duration(Some(color.duration())),
65-
color: Color::from_components(rgb),
66-
})?;
65+
source.send(
66+
ComponentName::FlatbufServer,
67+
InputMessageData::SolidColor {
68+
// TODO
69+
priority: 0,
70+
duration: i32_to_duration(Some(color.duration())),
71+
color: Color::from_components(rgb),
72+
},
73+
)?;
6774
} else if let Some(image) = request.command_as_image() {
6875
// Get raw image
6976
let data = image
@@ -82,11 +89,14 @@ pub async fn handle_request(
8289
let raw_image = RawImage::try_from((data.to_vec(), width, height))?;
8390

8491
// Update state
85-
source.send(InputMessageData::Image {
86-
priority,
87-
duration: i32_to_duration(Some(duration)),
88-
image: Arc::new(raw_image),
89-
})?;
92+
source.send(
93+
ComponentName::FlatbufServer,
94+
InputMessageData::Image {
95+
priority,
96+
duration: i32_to_duration(Some(duration)),
97+
image: Arc::new(raw_image),
98+
},
99+
)?;
90100
} else if let Some(_) = request.command_as_register() {
91101
return Err(FlatApiError::AlreadyRegistered);
92102
} else {

src/api/json.rs

+44-27
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use thiserror::Error;
55
use validator::Validate;
66

77
use crate::{
8+
component::ComponentName,
89
global::{Global, InputMessage, InputMessageData, InputSourceHandle},
910
image::{RawImage, RawImageError},
1011
};
@@ -47,12 +48,14 @@ impl ClientConnection {
4748
match request.command {
4849
HyperionCommand::ClearAll => {
4950
// Update state
50-
self.source.send(InputMessageData::ClearAll)?;
51+
self.source
52+
.send(ComponentName::All, InputMessageData::ClearAll)?;
5153
}
5254

5355
HyperionCommand::Clear(message::Clear { priority }) => {
5456
// Update state
55-
self.source.send(InputMessageData::Clear { priority })?;
57+
self.source
58+
.send(ComponentName::All, InputMessageData::Clear { priority })?;
5659
}
5760

5861
HyperionCommand::Color(message::Color {
@@ -64,11 +67,14 @@ impl ClientConnection {
6467
// TODO: Handle origin field
6568

6669
// Update state
67-
self.source.send(InputMessageData::SolidColor {
68-
priority,
69-
duration: duration.map(|ms| chrono::Duration::milliseconds(ms as _)),
70-
color,
71-
})?;
70+
self.source.send(
71+
ComponentName::Color,
72+
InputMessageData::SolidColor {
73+
priority,
74+
duration: duration.map(|ms| chrono::Duration::milliseconds(ms as _)),
75+
color,
76+
},
77+
)?;
7278
}
7379

7480
HyperionCommand::Image(message::Image {
@@ -86,44 +92,55 @@ impl ClientConnection {
8692

8793
let raw_image = RawImage::try_from((imagedata, imagewidth, imageheight))?;
8894

89-
self.source.send(InputMessageData::Image {
90-
priority,
91-
duration: duration.map(|ms| chrono::Duration::milliseconds(ms as _)),
92-
image: Arc::new(raw_image),
93-
})?;
95+
self.source.send(
96+
ComponentName::Image,
97+
InputMessageData::Image {
98+
priority,
99+
duration: duration.map(|ms| chrono::Duration::milliseconds(ms as _)),
100+
image: Arc::new(raw_image),
101+
},
102+
)?;
94103
}
95104

96105
HyperionCommand::ServerInfo(message::ServerInfoRequest { subscribe: _ }) => {
97106
// TODO: Handle subscribe field
98107

99108
// Request priority information
100109
let (sender, receiver) = tokio::sync::oneshot::channel();
101-
self.source.send(InputMessageData::PrioritiesRequest {
102-
response: Arc::new(std::sync::Mutex::new(Some(sender))),
103-
})?;
110+
self.source.send(
111+
ComponentName::All,
112+
InputMessageData::PrioritiesRequest {
113+
response: Arc::new(std::sync::Mutex::new(Some(sender))),
114+
},
115+
)?;
104116

105117
// Receive priority information
106-
let priorities = receiver
107-
.await?
108-
.into_iter()
109-
.map(message::PriorityInfo::from)
110-
.collect();
118+
let priorities = receiver.await?.into_iter().collect();
111119

112120
// Just answer the serverinfo request, no need to update state
113-
return Ok(Some(HyperionResponse::server_info(
114-
request.tan,
115-
vec![],
116-
priorities,
121+
122+
return Ok(Some(
117123
global
118124
.read_config(|config| {
119-
config
125+
let instances = config
120126
.instances
121127
.iter()
122128
.map(|instance_config| (&instance_config.1.instance).into())
123-
.collect()
129+
.collect();
130+
131+
HyperionResponse::server_info(
132+
request.tan,
133+
// TODO: Priorities only for current instance
134+
priorities,
135+
// TODO: Fill adjustments
136+
vec![],
137+
// TODO: Fill effects
138+
vec![],
139+
instances,
140+
)
124141
})
125142
.await,
126-
)));
143+
));
127144
}
128145

129146
HyperionCommand::Authorize(message::Authorize { subcommand, .. }) => match subcommand {

0 commit comments

Comments
 (0)