Skip to content

Commit

Permalink
♻️ refactor logger and bluetooth messages
Browse files Browse the repository at this point in the history
  • Loading branch information
chriamue committed Sep 29, 2023
1 parent 664e344 commit 08366f3
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 31 deletions.
15 changes: 1 addition & 14 deletions src/bluetooth/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,19 @@ use serde::{Deserialize, Serialize};
use crate::Sighting;

#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "op")]
#[serde(tag = "op", rename_all = "snake_case")]
pub enum Message {
#[serde(rename = "ping")]
Ping,
#[serde(rename = "pong")]
Pong,
#[serde(rename = "count_request")]
CountRequest,
#[serde(rename = "count_response")]
CountResponse { count: u64 },
#[serde(rename = "sighting_ids_request")]
SightingIdsRequest,
#[serde(rename = "sighting_ids_response")]
SightingIdsResponse { ids: Vec<String> },
#[serde(rename = "last_request")]
LastRequest,
#[serde(rename = "last_response")]
LastResponse { last: Sighting },
#[serde(rename = "sighting_request")]
SightingRequest { uuid: String },
#[serde(rename = "remove_sighting_request")]
RemoveSightingRequest { uuid: String },
#[serde(rename = "sighting_response")]
SightingResponse { sighting: Sighting },
#[serde(rename = "image_request")]
ImageRequest { uuid: String },
#[serde(rename = "image_response")]
ImageResponse { uuid: String, base64: String },
}
8 changes: 4 additions & 4 deletions src/bluetooth/rfcomm_srv.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::Message;
use crate::sighting::save_to_file;
use crate::Sighting;
use base64;
Expand All @@ -10,6 +11,7 @@ use bluer::{
use futures::StreamExt;
use image::{self, imageops::FilterType};
use std::error::Error;
use std::io::Cursor;
use std::{
sync::{Arc, Mutex},
time::Duration,
Expand All @@ -19,8 +21,6 @@ use tokio::{
time::sleep,
};

use super::Message;

use super::MANUFACTURER_ID;
pub const SERVICE_UUID: uuid::Uuid = uuid::Uuid::from_u128(0xF00DC0DE00001);
pub const CHARACTERISTIC_UUID: uuid::Uuid = uuid::Uuid::from_u128(0xF00DC0DE00002);
Expand Down Expand Up @@ -182,11 +182,11 @@ async fn handle_connection(
let buf = match image::open(format!("sightings/{}", filename)) {
Ok(base_img) => {
let base_img = base_img.resize(640, 480, FilterType::Gaussian);
let mut buf = vec![];
let mut buf = Cursor::new(Vec::new());
base_img
.write_to(&mut buf, image::ImageOutputFormat::Jpeg(60))
.unwrap();
buf
buf.into_inner()
}
Err(err) => {
println!("{:?}", err);
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub type Result<T> = std::result::Result<T, Error>;
pub mod capture;
pub mod cli;
pub mod config;
pub mod logger;
pub mod mjpeg;
pub mod sighting;

Expand Down
28 changes: 28 additions & 0 deletions src/logger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
pub fn init_logger(log_level: &Option<String>) {
let mut level_filter = match std::env::var("RUST_LOG") {
Ok(level) => match level.to_lowercase().as_str() {
"trace" => log::LevelFilter::Trace,
"debug" => log::LevelFilter::Debug,
"warn" => log::LevelFilter::Warn,
"error" => log::LevelFilter::Error,
_ => log::LevelFilter::Info,
},
Err(_) => log::LevelFilter::Info,
};

if let Some(level) = log_level {
level_filter = match level.to_lowercase().as_str() {
"trace" => log::LevelFilter::Trace,
"debug" => log::LevelFilter::Debug,
"warn" => log::LevelFilter::Warn,
"error" => log::LevelFilter::Error,
_ => log::LevelFilter::Info,
};
}

pretty_env_logger::formatted_timed_builder()
.filter_level(level_filter)
.filter_module("tract_core", log::LevelFilter::Warn)
.filter_module("tract_hir", log::LevelFilter::Warn)
.init();
}
15 changes: 2 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,16 @@ use ornithology_pi::cli::Cli;
use ornithology_pi::config;
#[cfg(feature = "hotspot")]
use ornithology_pi::hotspot::Hotspot;
use ornithology_pi::logger::init_logger;
#[cfg(feature = "server")]
use ornithology_pi::server::server;
use ornithology_pi::{Sighting, WebCam};
use std::sync::{Arc, Mutex};

fn init_logger(cli: &Cli) {
// default log level is info
if let Err(_) = std::env::var("RUST_LOG") {
std::env::set_var("RUST_LOG", "info");
}
// override log level if set in cli
if let Some(log_level) = cli.log_level.as_ref() {
std::env::set_var("RUST_LOG", log_level);
}
pretty_env_logger::init_timed();
}

#[tokio::main]
async fn main() {
let cli = Cli::new();
init_logger(&cli);
init_logger(&cli.log_level);

cli.evaluate();

Expand Down

0 comments on commit 08366f3

Please sign in to comment.