initial
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
2003
Cargo.lock
generated
Normal file
2003
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
13
Cargo.toml
Normal file
13
Cargo.toml
Normal file
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "Ambilight"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
okmain = "0.2.0"
|
||||
reqwest = "0.13.2"
|
||||
serde = { version = "1.0.228", default-features = false, features = ["derive"] }
|
||||
serde_json = { version = "1.0.149", default-features = false, features = ["std"] }
|
||||
tokio = { version = "1.50.0", default-features = false, features = ["macros", "rt-multi-thread"] }
|
||||
win-screenshot = "4.0.14"
|
||||
|
||||
2
README.md
Normal file
2
README.md
Normal file
@@ -0,0 +1,2 @@
|
||||
# Ambilight
|
||||
updates homasssistant based upon screenshots
|
||||
121
src/main.rs
Normal file
121
src/main.rs
Normal file
@@ -0,0 +1,121 @@
|
||||
use reqwest::Client;
|
||||
use serde::Serialize;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use okmain::rgb;
|
||||
use win_screenshot::prelude::*;
|
||||
|
||||
const URL: &str = "https://homeassistant.pvv.ntnu.no:8123/api/services/light/turn_on";
|
||||
const TOKEN: &str = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiI4YWVmZWI2ZjYwNDg0ZmJlODNkOWI2YTUyM2UwODk4NCIsImlhdCI6MTc3MzI0MzYxNywiZXhwIjoyMDg4NjAzNjE3fQ.xZ9pWCymgwfJfyQdgdHmiFMuDgmstfpx1vDohl34_rw";
|
||||
|
||||
#[derive(Serialize, Clone)]
|
||||
struct Payload {
|
||||
entity_id: String,
|
||||
brightness: u8,
|
||||
rgb_color: [u8; 3],
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let client = Client::new();
|
||||
let mut payload = Payload {
|
||||
entity_id: String::from("light.fargelys"),
|
||||
brightness: 200,
|
||||
rgb_color: [0, 0, 0],
|
||||
};
|
||||
let logging = true;
|
||||
let time_between_requests = Duration::from_millis(1000);
|
||||
loop {
|
||||
let frame_start = Instant::now();
|
||||
let v = loop {
|
||||
let buf = capture_display().unwrap();
|
||||
|
||||
let now = Instant::now();
|
||||
let v =
|
||||
get_next_rgb_value_for_asynchronously_setting_mood_lights_in_koserommet_at_pvv_by_using_a_mean(buf);
|
||||
let diff = color_difference_is_great_enough_for_making_a_request_to_change_light_colors_using_euclidian_distance(
|
||||
payload.rgb_color,
|
||||
v);
|
||||
let elapsed = now.elapsed();
|
||||
if diff > 0.1 {
|
||||
if logging {
|
||||
println!(
|
||||
"calculated average pixel value in: {} milliseconds",
|
||||
elapsed.as_millis()
|
||||
);
|
||||
println!("new_rgb_color: {:?}", v);
|
||||
println!("rgb_color: {:?}", payload.rgb_color);
|
||||
println!("Found difference of: {}", diff);
|
||||
}
|
||||
|
||||
payload.rgb_color = v;
|
||||
break v;
|
||||
}
|
||||
if frame_start.elapsed() > time_between_requests {
|
||||
break v;
|
||||
}
|
||||
};
|
||||
payload.rgb_color = v;
|
||||
let now = Instant::now();
|
||||
client
|
||||
.post(URL)
|
||||
.bearer_auth(TOKEN)
|
||||
.body(serde_json::to_string(&payload).unwrap())
|
||||
.send()
|
||||
.await
|
||||
.unwrap()
|
||||
.text()
|
||||
.await
|
||||
.unwrap();
|
||||
let elapsed = now.elapsed();
|
||||
if logging {
|
||||
println!("got response in: {} milliseconds", elapsed.as_millis());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_next_rgb_value_for_asynchronously_setting_mood_lights_in_koserommet_at_pvv_by_using_a_mean(
|
||||
frame: RgbBuf,
|
||||
) -> [u8; 3] {
|
||||
let pixel_count = frame.width as u64 * frame.height as u64;
|
||||
let v: [u8; 3] = frame
|
||||
.pixels
|
||||
.chunks_exact(4)
|
||||
.fold([0u64, 0u64, 0u64], |[acc_r, acc_g, acc_b], pixel| {
|
||||
[
|
||||
acc_r + pixel[0] as u64,
|
||||
acc_g + pixel[1] as u64,
|
||||
acc_b + pixel[2] as u64,
|
||||
]
|
||||
})
|
||||
.map(|sum| (sum / pixel_count) as u8);
|
||||
v
|
||||
}
|
||||
|
||||
fn get_next_rgb_value_for_asynchronously_setting_mood_lights_in_koserommet_at_pvv_by_finding_main_colors(
|
||||
frame: RgbBuf,
|
||||
) -> [u8; 3] {
|
||||
let v: Vec<u8> = frame
|
||||
.pixels
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(i, _)| i % 4 != 3)
|
||||
.map(|(_, n)| *n)
|
||||
.collect();
|
||||
let mut output = okmain::colors(
|
||||
okmain::InputImage::from_bytes(frame.width as u16, frame.height as u16, &v).unwrap(),
|
||||
);
|
||||
let a = output.pop().unwrap();
|
||||
[a.r, a.g, a.b]
|
||||
}
|
||||
|
||||
fn color_difference_is_great_enough_for_making_a_request_to_change_light_colors_using_euclidian_distance(
|
||||
a: [u8; 3],
|
||||
b: [u8; 3],
|
||||
) -> f64 {
|
||||
(((a[0].max(b[0]) - a[0].min(b[0])) as f64).powf(2.0)
|
||||
+ ((a[1].max(b[1]) - a[1].min(b[1])) as f64).powf(2.0)
|
||||
+ ((a[2].max(b[2]) - a[2].min(b[2])) as f64).powf(2.0))
|
||||
.sqrt()
|
||||
/ ((255.0f64.powf(2.0) * 3.0).sqrt())
|
||||
}
|
||||
Reference in New Issue
Block a user