fin
This commit is contained in:
72
src/curl.rs
72
src/curl.rs
@@ -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
|
||||
|
||||
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}");
|
||||
});
|
||||
|
||||
let mut request = curl::easy::Easy::new();
|
||||
|
||||
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}");
|
||||
});
|
||||
|
||||
transfer.perform().unwrap_or_else(|e| {
|
||||
panic!("[ERROR] Failed to perform the CURL request:\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;
|
||||
}
|
||||
|
||||
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
|
||||
},
|
||||
}
|
||||
}
|
16
src/main.rs
16
src/main.rs
@@ -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");
|
||||
fn main() {
|
||||
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);
|
||||
}
|
||||
|
||||
|
@@ -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(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user