improvements to netcode and new functionality
This commit is contained in:
parent
765fca635a
commit
24f8a35162
38
src/curl.rs
38
src/curl.rs
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
|
||||||
pub fn startup(json: &crate::json::JsonStruct) -> (curl::easy::Easy, String) {
|
pub fn startup(json: &crate::json::JsonStruct) -> (curl::easy::Easy, String) {
|
||||||
@ -19,29 +18,26 @@ pub fn startup(json: &crate::json::JsonStruct) -> (curl::easy::Easy, String) {
|
|||||||
|
|
||||||
(request, data)
|
(request, data)
|
||||||
}
|
}
|
||||||
pub fn request(request: &mut curl::easy::Easy, data: &str) -> Result<u32, curl::Error> {
|
pub fn request(request: &mut curl::easy::Easy, data: &str) -> Result<(u32, String), curl::Error> {
|
||||||
|
|
||||||
|
let mut buf = String::new();
|
||||||
|
|
||||||
let mut transfer = request.transfer();
|
let mut transfer = request.transfer();
|
||||||
|
transfer.read_function(|buf| {
|
||||||
if let Err(e) = transfer.read_function(|buf| {
|
|
||||||
Ok(data.as_bytes().read(buf).unwrap_or(0))
|
Ok(data.as_bytes().read(buf).unwrap_or(0))
|
||||||
}) {
|
})?;
|
||||||
eprintln!("[ERROR] Failed to read the data:\n{e}");
|
|
||||||
return Err(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Err(e) = transfer.perform() {
|
transfer.write_function(|rdata| {
|
||||||
eprintln!("[ERROR] Failed to perform the CURL request:\n{e}");
|
buf.push_str(&String::from_utf8_lossy(rdata));
|
||||||
return Err(e);
|
Ok(rdata.len())
|
||||||
}
|
})?;
|
||||||
|
|
||||||
|
// returns errors if it fails, contuinues normally if not
|
||||||
|
transfer.perform()?;
|
||||||
|
|
||||||
// drop transfer so we can access request.response_code below
|
|
||||||
drop(transfer);
|
drop(transfer);
|
||||||
|
// return error if this fails
|
||||||
match request.response_code() {
|
let result = request.response_code()?;
|
||||||
Ok(t) => Ok(t),
|
// return result
|
||||||
Err(e) => {
|
Ok((result, buf))
|
||||||
eprintln!("[ERROR] Failed to retrieve the response code:\n{e}");
|
|
||||||
Err(e)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
12
src/json.rs
12
src/json.rs
@ -1,6 +1,7 @@
|
|||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{ErrorKind, Read};
|
use std::io::{ErrorKind, Read};
|
||||||
|
use json::Error as JsonError;
|
||||||
|
|
||||||
pub struct JsonStruct {
|
pub struct JsonStruct {
|
||||||
pub ha_url: String,
|
pub ha_url: String,
|
||||||
@ -51,3 +52,14 @@ fn read_file(path: &Path) -> String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_state(response: String) -> Result<Option<bool>, JsonError> {
|
||||||
|
let json = json::parse(response.as_str())?;
|
||||||
|
let state = json::stringify(json[0]["state"].as_str());
|
||||||
|
|
||||||
|
match &state[1..&state.len()-1] {
|
||||||
|
"on" => Ok(Some(true)),
|
||||||
|
"off" => Ok(Some(false)),
|
||||||
|
_ => Ok(None),
|
||||||
|
}
|
||||||
|
}
|
@ -6,9 +6,7 @@ mod serial;
|
|||||||
fn main() {
|
fn main() {
|
||||||
let data_file = std::path::Path::new("data.json");
|
let data_file = std::path::Path::new("data.json");
|
||||||
let data = json::get_json(data_file);
|
let data = json::get_json(data_file);
|
||||||
let req;
|
let (req, dt) = curl::startup(&data);
|
||||||
let dt;
|
serial::read_messages(req, dt);
|
||||||
(req, dt) = curl::startup(&data);
|
|
||||||
serial::read_messages(req, &dt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
use std::time::Duration;
|
|
||||||
use serialport::SerialPortInfo;
|
use serialport::SerialPortInfo;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
const BAUD: u32 = 9600;
|
||||||
|
const TIMEOUT: u64 = 1000;
|
||||||
|
|
||||||
fn get_port(ports: Vec<SerialPortInfo>) -> Option<SerialPortInfo> {
|
fn get_port(ports: Vec<SerialPortInfo>) -> Option<SerialPortInfo> {
|
||||||
for p in ports {
|
for p in ports {
|
||||||
@ -11,30 +14,40 @@ fn get_port(ports: Vec<SerialPortInfo>) -> Option<SerialPortInfo> {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_messages(mut request: curl::easy::Easy, data: &str) {
|
pub fn read_messages(mut request: curl::easy::Easy, data: String) {
|
||||||
let ports = serialport::available_ports().expect("No ports found!");
|
let ports = serialport::available_ports().expect("No ports found!");
|
||||||
let r = get_port(ports).unwrap().port_name;
|
let r = get_port(ports)
|
||||||
let mut connection = serialport::new(r, 9600)
|
.expect("[ERROR] Failed to get the port")
|
||||||
.timeout(Duration::from_millis(1000))
|
.port_name;
|
||||||
.open().expect("Failed to open the port");
|
let mut connection = serialport::new(r, BAUD)
|
||||||
|
.timeout(Duration::from_millis(TIMEOUT))
|
||||||
|
.open()
|
||||||
|
.expect("Failed to open the port");
|
||||||
|
|
||||||
let mut serial_buf: Vec<u8> = vec![0; 1024];
|
let mut serial_buf: Vec<u8> = vec![0; 1024];
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match connection.read(serial_buf.as_mut_slice()) {
|
if let Ok(bytes) = connection.read(serial_buf.as_mut_slice()) {
|
||||||
Ok(t) => {
|
if bytes < 1 { continue; }
|
||||||
if t < 1 { continue; }
|
let output = String::from_utf8_lossy(&serial_buf[..bytes]);
|
||||||
let output = String::from_utf8_lossy(&serial_buf[..t]);
|
|
||||||
if output.contains("1") {
|
if output.contains("1") {
|
||||||
// send curl request
|
// send curl request
|
||||||
let res = crate::curl::request(&mut request, &data);
|
let res = crate::curl::request(&mut request, &data);
|
||||||
match res {
|
match res {
|
||||||
Ok(t) => println!("Response code: {t}"),
|
Ok((code, response)) => {
|
||||||
|
println!("Response code: {code}");
|
||||||
|
match crate::json::get_state(response) {
|
||||||
|
Ok(state) => {
|
||||||
|
if let Some(valid_state) = state {
|
||||||
|
println!("State: {}", valid_state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => eprintln!("[ERROR] Failed to acquire entity state:\n{e}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
Err(e) => eprintln!("[ERROR] Something went wrong with the CURL request:\n{e}"),
|
Err(e) => eprintln!("[ERROR] Something went wrong with the CURL request:\n{e}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
Err(_) => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user