Skip to content

Commit

Permalink
New format for legend
Browse files Browse the repository at this point in the history
  • Loading branch information
keesverruijt committed Aug 22, 2024
1 parent e121c46 commit 9bb6ccd
Showing 1 changed file with 93 additions and 59 deletions.
152 changes: 93 additions & 59 deletions src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ use axum::{
};
use log::info;
use miette::Result;
use serde::ser::Serializer;
use serde::Serialize;
use serde_with::skip_serializing_none;
use std::{
collections::HashMap,
fmt, io,
fmt,
hash::{Hash, Hasher},
io,
net::{IpAddr, Ipv4Addr, SocketAddr},
sync::{Arc, RwLock},
};
Expand Down Expand Up @@ -85,7 +87,7 @@ async fn root() -> String {
"Mayara v".to_string() + VERSION
}

#[derive(Serialize)]
#[derive(Serialize, PartialEq, Eq, Copy, Clone)]
enum PixelType {
Normal,
History,
Expand All @@ -94,6 +96,38 @@ enum PixelType {
DopplerReceding,
}

impl Hash for PixelType {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_usize(*self as usize);
}
}

#[derive(PartialEq, Eq, Copy, Clone)]
struct LookupValue {
v: u8,
}

impl Serialize for LookupValue {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.v.to_string())
}
}

impl Hash for LookupValue {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_u8(self.v);
}
}

impl From<u8> for LookupValue {
fn from(item: u8) -> Self {
LookupValue { v: item }
}
}

struct Colour {
r: u8,
g: u8,
Expand All @@ -111,8 +145,6 @@ impl fmt::Display for Colour {
}
}

use serde::ser::Serializer;

impl Serialize for Colour {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -122,12 +154,8 @@ impl Serialize for Colour {
}
}

#[skip_serializing_none]
#[derive(Serialize)]
struct Lookup {
key: u8,
value: Option<u8>,
r#type: PixelType,
colour: Colour,
}

Expand All @@ -138,7 +166,7 @@ struct RadarApi {
spokes: u16,
max_spoke_len: u16,
stream_url: String,
legend: Vec<Lookup>,
legend: HashMap<PixelType, HashMap<LookupValue, Lookup>>,
}

impl RadarApi {
Expand All @@ -149,11 +177,11 @@ impl RadarApi {
spokes,
max_spoke_len,
stream_url,
legend: Vec::new(),
legend: HashMap::new(),
}
}

fn set_legend(&mut self, legend: Vec<Lookup>) {
fn set_legend(&mut self, legend: HashMap<PixelType, HashMap<LookupValue, Lookup>>) {
self.legend = legend;
}
}
Expand All @@ -166,69 +194,75 @@ fn abs(n: f32) -> f32 {
}
}

fn fake_legend() -> Vec<Lookup> {
let mut legend = Vec::new();
fn fake_legend() -> HashMap<PixelType, HashMap<LookupValue, Lookup>> {
let mut legend: HashMap<PixelType, HashMap<LookupValue, Lookup>> = HashMap::new();

let mut history_map: HashMap<LookupValue, Lookup> = HashMap::new();
for history in 0..32 {
let colour = Colour {
r: 255,
g: 255,
b: 255,
a: history * 4,
};
legend.push(Lookup {
key: history,
value: Some(history),
r#type: PixelType::History,
colour,
});
history_map.insert(history.into(), Lookup { colour });
}
let mut normal_map: HashMap<LookupValue, Lookup> = HashMap::new();
for v in 0..13 {
legend.push(Lookup {
key: 32 + v,
value: Some(v),
r#type: PixelType::Normal,
normal_map.insert(
(32 + v).into(),
Lookup {
colour: Colour {
r: (v as f32 * (200. / 13.)) as u8,
g: (abs(7. - v as f32) * (200. / 13.)) as u8,
b: ((13 - v) as f32 * (200. / 13.)) as u8,
a: 255,
},
},
);
}
let mut border_map: HashMap<LookupValue, Lookup> = HashMap::new();
border_map.insert(
(32 + 13).into(),
Lookup {
colour: Colour {
r: (v as f32 * (200. / 13.)) as u8,
g: (abs(7. - v as f32) * (200. / 13.)) as u8,
b: ((13 - v) as f32 * (200. / 13.)) as u8,
r: 200,
g: 200,
b: 200,
a: 255,
},
});
}
legend.push(Lookup {
key: 32 + 13,
value: None,
r#type: PixelType::TargetBorder,
colour: Colour {
r: 200,
g: 200,
b: 200,
a: 255,
},
});
legend.push(Lookup {
key: 32 + 13 + 1,
value: None,
r#type: PixelType::DopplerApproaching,
colour: Colour {
r: 0,
g: 200,
b: 200,
a: 255,
);
let mut doppler_approaching: HashMap<LookupValue, Lookup> = HashMap::new();
doppler_approaching.insert(
(32 + 13 + 1).into(),
Lookup {
colour: Colour {
r: 0,
g: 200,
b: 200,
a: 255,
},
},
});
legend.push(Lookup {
key: 32 + 13 + 2,
value: None,
r#type: PixelType::DopplerReceding,
colour: Colour {
r: 0x90,
g: 0xd0,
b: 0xf0,
a: 255,
);
let mut doppler_receding: HashMap<LookupValue, Lookup> = HashMap::new();
doppler_receding.insert(
(32 + 13 + 2).into(),
Lookup {
colour: Colour {
r: 0x90,
g: 0xd0,
b: 0xf0,
a: 255,
},
},
});
);

legend.insert(PixelType::History, history_map);
legend.insert(PixelType::Normal, normal_map);
legend.insert(PixelType::TargetBorder, border_map);
legend.insert(PixelType::DopplerApproaching, doppler_approaching);
legend.insert(PixelType::DopplerReceding, doppler_receding);

legend
}
Expand Down

0 comments on commit 9bb6ccd

Please sign in to comment.