This commit is contained in:
Martin Vrhovšek 2025-01-16 22:10:18 +01:00
parent c42d550acf
commit 05b4f35d52
5 changed files with 274 additions and 50 deletions

196
Cargo.lock generated
View File

@ -8,8 +8,21 @@ version = "0.1.0"
dependencies = [
"curl",
"json",
"serialport",
]
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36"
[[package]]
name = "cc"
version = "1.2.9"
@ -19,6 +32,28 @@ dependencies = [
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "core-foundation"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b55271e5c8c478ad3f38ad24ef34923091e0548492a266d19b3c0b4d82574c63"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "curl"
version = "0.4.47"
@ -49,6 +84,16 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "io-kit-sys"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "617ee6cf8e3f66f3b4ea67a4058564628cde41901316e19f559e14c7c72c5e7b"
dependencies = [
"core-foundation-sys",
"mach2",
]
[[package]]
name = "json"
version = "0.12.4"
@ -61,6 +106,26 @@ version = "0.2.169"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
[[package]]
name = "libudev"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78b324152da65df7bb95acfcaab55e3097ceaab02fb19b228a9eb74d55f135e0"
dependencies = [
"libc",
"libudev-sys",
]
[[package]]
name = "libudev-sys"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c8469b4a23b962c1396b9b451dda50ef5b283e8dd309d69033475fa9b334324"
dependencies = [
"libc",
"pkg-config",
]
[[package]]
name = "libz-sys"
version = "1.1.21"
@ -73,6 +138,26 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "mach2"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19b955cdeb2a02b9117f121ce63aa52d08ade45de53e48fe6a38b39c10f6f709"
dependencies = [
"libc",
]
[[package]]
name = "nix"
version = "0.26.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "598beaf3cc6fdd9a5dfb1630c2800c7acd31df7aaf0f565796fba2b53ca1af1b"
dependencies = [
"bitflags 1.3.2",
"cfg-if",
"libc",
]
[[package]]
name = "openssl-probe"
version = "0.1.5"
@ -97,6 +182,24 @@ version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2"
[[package]]
name = "proc-macro2"
version = "1.0.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc"
dependencies = [
"proc-macro2",
]
[[package]]
name = "schannel"
version = "0.1.27"
@ -106,6 +209,31 @@ dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "serialport"
version = "4.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ecfc4858c2266c7695d8b8460bbd612fa81bd2e250f5f0dd16195e4b4f8b3d8"
dependencies = [
"bitflags 2.8.0",
"cfg-if",
"core-foundation",
"core-foundation-sys",
"io-kit-sys",
"libudev",
"mach2",
"nix",
"scopeguard",
"unescaper",
"winapi",
]
[[package]]
name = "shlex"
version = "1.3.0"
@ -122,12 +250,80 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "syn"
version = "2.0.96"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5d0adab1ae378d7f53bdebc67a39f1f151407ef230f0ce2883572f5d8985c80"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "thiserror"
version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unescaper"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c878a167baa8afd137494101a688ef8c67125089ff2249284bd2b5f9bfedb815"
dependencies = [
"thiserror",
]
[[package]]
name = "unicode-ident"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.52.0"

View File

@ -6,3 +6,4 @@ edition = "2021"
[dependencies]
json = "0.12.4"
curl = "0.4.47"
serialport = "4.7.0"

View File

@ -1,58 +1,52 @@
use std::collections;
use std::io::Read;
use std::string::String;
use std::collections::HashMap;
use curl::easy::{Easy, List};
pub fn request(auth: HashMap<String, String>) -> u32 {
pub fn startup(auth: &collections::HashMap<String, String>) -> (curl::easy::Easy, String) {
let url = auth.get("ha_url").expect("[ERROR] Failed to read the URL").as_str();
// escape character for { is {{ and for } is }}
let entity_id = auth.get("light_entity_id").expect("[ERROR] Failed to read the entity id").as_str();
let body = format!("{{\"entity_id\": {entity_id}}}");
let mut data = body.as_bytes();
let data = format!("{{\"entity_id\": {entity_id}}}");
let token = auth.get("access_token").expect("[ERROR] Failed to read the token").as_str();
let auth = format!("Authorization: Bearer {}", &token[1..token.len()-1]);
let mut list = List::new();
let mut list = curl::easy::List::new();
list.append(&auth).unwrap();
list.append("Content-Type: application/json").unwrap();
let mut request = Easy::new();
// proper error handling
let mut request = curl::easy::Easy::new();
request.url(&url[1..url.len()-1]).unwrap_or_else(|e| {
panic!("[ERROR] Failed to set the URL for the CURL request:\n{e}");
});
request.post(true).unwrap_or_else(|e| {
panic!("[ERROR] Failed to set the CURL request as POST:\n{e}");
});
request.http_headers(list).unwrap_or_else(|e| {
panic!("[ERROR] Failed to apply HTTP headers:\n{e}");
});
request.post_field_size(data.len() as u64).unwrap_or_else(|e| {
panic!("[ERROR] Failed to set the POST field size for CURL request:\n{e}");
});
request.url(&url[1..url.len()-1]).expect("[ERROR] Failed to set the URL for the CURL request");
request.post(true).expect("[ERROR] Failed to set the CURL request as POST");
request.http_headers(list).expect("ERROR] Failed to apply HTTP headers");
request.post_field_size(data.len() as u64).expect("[ERROR] Failed to set the POST field size for CURL request");
(request, data)
}
pub fn request(request: &mut curl::easy::Easy, data: &str) -> Option<u32> {
let mut transfer = request.transfer();
transfer.read_function(|buf| {
Ok(data.read(buf).unwrap_or(0))
}).unwrap_or_else(|e| {
panic!("[ERROR] Failed to read the data:\n{e}");
});
if let Err(e) = transfer.read_function(|buf| {
Ok(data.as_bytes().read(buf).unwrap_or(0))
}) {
eprintln!("[ERROR] Failed to read the data:\n{e}");
return None;
}
transfer.perform().unwrap_or_else(|e| {
panic!("[ERROR] Failed to perform the CURL request:\n{e}");
});
if let Err(e) = transfer.perform() {
eprintln!("[ERROR] Failed to perform the CURL request:\n{e}");
return None;
}
// drop transfer so we can access request.response_code below
drop(transfer);
match request.response_code() {
Ok(t) => t,
Err(e) => panic!("[ERROR] Failed to retrieve the response code:\n{e}"),
Ok(t) => Some(t),
Err(e) => {
eprintln!("[ERROR] Failed to retrieve the response code:\n{e}");
None
},
}
}

View File

@ -1,15 +1,15 @@
use std::path::Path;
use crate::curl::startup;
mod json;
mod curl;
mod serial;
fn main() {
let data_file = Path::new("data.json");
let data_file = std::path::Path::new("data.json");
let data = json::get_json(data_file);
// some loop for getting data from arduino
// send curl request
let res = curl::request(data);
println!("Response code: {res}\n");
let req;
let dt;
(req, dt) = startup(&data);
serial::read_messages(req, &dt);
}

View File

@ -1,3 +1,36 @@
fn get_port() {}
use std::time::Duration;
use serialport::SerialPortInfo;
fn read_messages() {}
fn get_port(ports: Vec<SerialPortInfo>) -> Option<SerialPortInfo> {
for p in ports {
if p.port_name.contains("ttyACM") {
return Some(p);
}
}
None
}
pub fn read_messages(mut request: curl::easy::Easy, data: &str) {
let ports = serialport::available_ports().expect("No ports found!");
let r = get_port(ports).unwrap().port_name;
let mut connection = serialport::new(r, 9600)
.timeout(Duration::from_millis(1000))
.open().expect("Failed to open the port");
let mut serial_buf: Vec<u8> = vec![0; 1024];
loop {
match connection.read(serial_buf.as_mut_slice()) {
Ok(t) => {
let output = String::from_utf8_lossy(&serial_buf[..t]);
if output.contains("1") {
// send curl request
let res = crate::curl::request(&mut request, &data);
println!("Response code: {}", res.unwrap());
}
},
Err(_) => {}
}
}
}