improve error tolerance and logs
This commit is contained in:
parent
5c2a75c240
commit
d4380260c1
@ -5,11 +5,16 @@ use elasticsearch::http::transport::SingleNodeConnectionPool;
|
|||||||
use elasticsearch::IndexParts;
|
use elasticsearch::IndexParts;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use elasticsearch::auth::Credentials;
|
use elasticsearch::auth::Credentials;
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
pub struct ElasticConection {
|
pub struct ElasticConection {
|
||||||
conection: Elasticsearch
|
conection: Elasticsearch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct ElasticConErr {
|
||||||
|
content: String,
|
||||||
|
}
|
||||||
|
|
||||||
impl ElasticConection {
|
impl ElasticConection {
|
||||||
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
|
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
let url = Url::parse("http://127.0.0.1:9200")?;
|
let url = Url::parse("http://127.0.0.1:9200")?;
|
||||||
@ -23,14 +28,40 @@ impl ElasticConection {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send(&self, data: Value) -> bool {
|
pub async fn send(&self, data: Value) -> Result<(), ElasticConErr> {
|
||||||
let response = self.conection
|
let raw_response = self.conection
|
||||||
.index(IndexParts::Index("nutbeat-0.1"))
|
.index(IndexParts::Index("nutbeat-0.1"))
|
||||||
.body(data)
|
.body(data)
|
||||||
.send()
|
.send()
|
||||||
.await.unwrap();
|
.await;
|
||||||
response.status_code().is_success()
|
let response = match raw_response {
|
||||||
|
Ok(r) => r,
|
||||||
|
Err(e) => return Err(ElasticConErr { content: e.to_string()}),
|
||||||
|
};
|
||||||
|
let status_code = response.status_code();
|
||||||
|
match status_code.clone().is_success() {
|
||||||
|
true => Ok(()),
|
||||||
|
false => {
|
||||||
|
let err_ret = match response.text().await {
|
||||||
|
Ok(ret) => ret,
|
||||||
|
Err(_e) => status_code.as_str().to_string(),
|
||||||
|
};
|
||||||
|
Err(ElasticConErr { content: err_ret })
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for ElasticConErr {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "{}", self.content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Debug for ElasticConErr {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(
|
||||||
|
f, "{}", self.content
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
24
src/main.rs
24
src/main.rs
@ -4,6 +4,7 @@ use nut::nut_client::NutClient;
|
|||||||
use elastic_conection::ElasticConection;
|
use elastic_conection::ElasticConection;
|
||||||
use chrono;
|
use chrono;
|
||||||
use tokio::time::{sleep, Duration};
|
use tokio::time::{sleep, Duration};
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
@ -17,8 +18,29 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn start_loop(e_con: ElasticConection, mut n_con: NutClient) {
|
async fn start_loop(e_con: ElasticConection, mut n_con: NutClient) {
|
||||||
|
let mut has_failed = false;
|
||||||
loop {
|
loop {
|
||||||
e_con.send(n_con.get_data(chrono::offset::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true)).unwrap()).await;
|
|
||||||
sleep(Duration::from_secs(10)).await;
|
sleep(Duration::from_secs(10)).await;
|
||||||
|
let payload = match n_con.get_data(chrono::offset::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true)) {
|
||||||
|
Ok(p) => p,
|
||||||
|
Err(e) => {
|
||||||
|
process_error(Box::new(e), "nut connection", &mut has_failed);
|
||||||
|
continue;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
match e_con.send(payload).await {
|
||||||
|
Ok(()) => has_failed = false,
|
||||||
|
Err(e) => {
|
||||||
|
process_error(Box::new(e), "elasticsearch", &mut has_failed);
|
||||||
|
continue;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn process_error(e: Box<dyn fmt::Display>, system: &str, has_failed: &mut bool) {
|
||||||
|
if !*has_failed {
|
||||||
|
*has_failed = true;
|
||||||
|
log::error!("error in {}: {}", system, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@ use nut_client::ConfigBuilder;
|
|||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use crate::nut::nut_data::Data;
|
|
||||||
|
|
||||||
pub struct NutClient {
|
pub struct NutClient {
|
||||||
conection: Connection,
|
conection: Connection,
|
||||||
@ -63,21 +62,3 @@ impl NutClient {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert(mut header: String, value: String, data: &mut Data) {
|
|
||||||
for key in header.split(".") {
|
|
||||||
|
|
||||||
}
|
|
||||||
process(header.split_off(header.find(".").unwrap()+1), value, None, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn process(mut header: String, value: String, pre: Option<String>, data: &mut Data) {
|
|
||||||
match header.find(".") {
|
|
||||||
Some(p) => {
|
|
||||||
process(header.split_off(p+1), value, Some(header), data);
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -18,24 +18,6 @@ pub struct Batery {
|
|||||||
voltage: Option<Voltage>,
|
voltage: Option<Voltage>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Batery {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
charge: None,
|
|
||||||
mrf_date: None,
|
|
||||||
runtime: None,
|
|
||||||
btype: None,
|
|
||||||
voltage: None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*pub fn add(&self, data: Vec<String>) {
|
|
||||||
match data.remove(0) {
|
|
||||||
"charge":
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Charge {
|
pub struct Charge {
|
||||||
value: Option<u8>,
|
value: Option<u8>,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user