Skip to content

Commit

Permalink
Add endpoint for getting colors
Browse files Browse the repository at this point in the history
  • Loading branch information
Maccraft123 committed Apr 3, 2023
1 parent 21f0d6f commit b7c3e95
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

## API:
Everything is controlled via HTTP requests.
Currently there is just one "endpoint" that sets RGB colors in a certain mode:
`GET 127.0.0.1:21371/set/{mode}/{r}/{g}/{b}`
Endpoints:

`GET 127.0.0.1:21371/set/{mode}/{r}/{g}/{b}` - Sets color for `mode`
`GET 127.0.0.1:21371/get/{mode}` - Gets color for `mode` in `{r}:{g}:{b}\n` format

With `mode` being:
- `charging`, meaning device charging and below 90% battery capacity
- `low_bat`, meaning device having battery capacity between 0 and 20%
Expand All @@ -19,6 +22,7 @@ scaled accordingly.
### Example API calls
- `curl 127.0.0.1:21371/set/normal/200/0/200` sets "normal" color to purple
- `curl 127.0.0.1:21371/set/charging/255/0/0` sets "charging" color to red
- `curl 127.0.0.1:21371/get/charging` gets "charging" color

### Errors:
- `400 Bad Request` - Invalid mode was chosen
Expand Down
13 changes: 13 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,19 @@ fn http_thread(theme: Arc<Mutex<Theme>>) {
drop(t);
Response::empty_204()
},
(GET) (/get/{mode: String}) => {
let t = theme.lock().unwrap();
let data = match mode.as_str() {
"charging" => &t.charging,
"low_bat" => &t.low_bat,
"full" => &t.full,
"normal" => &t.normal,
_ => return Response::empty_400(),
};
let response = Response::text(format!("{}:{}:{}\n", data.0, data.1, data.2));
drop(t);
response
},
_ => Response::empty_404()
)
});
Expand Down

0 comments on commit b7c3e95

Please sign in to comment.