diff --git a/peach-web/src/main.rs b/peach-web/src/main.rs index f950665..9919b42 100644 --- a/peach-web/src/main.rs +++ b/peach-web/src/main.rs @@ -41,6 +41,7 @@ use crate::routes::catchers::*; use crate::routes::index::*; use crate::routes::scuttlebutt::*; use crate::routes::status::device::*; +use crate::routes::status::network::*; use crate::routes::status::ping::*; use crate::routes::settings::admin::*; @@ -58,7 +59,6 @@ fn init_rocket() -> Rocket { .mount( "/", routes![ - device_status, help, home, login, @@ -70,6 +70,8 @@ fn init_rocket() -> Rocket { settings_menu, ], ) + // STATUS HTML ROUTES + .mount("/status", routes![device_status, network_status]) // ADMIN SETTINGS HTML ROUTES .mount( "/settings/admin", diff --git a/peach-web/src/routes/settings/network.rs b/peach-web/src/routes/settings/network.rs index 61ae7be..76b8af9 100644 --- a/peach-web/src/routes/settings/network.rs +++ b/peach-web/src/routes/settings/network.rs @@ -102,7 +102,7 @@ pub fn wifi_password(ssid: &str, flash: Option, _auth: Authenticat context.flash_msg = Some(flash.message().to_string()); }; // template_dir is set in Rocket.toml - Template::render("settings/network/network_modify", &context) + Template::render("settings/network/modify_ap", &context) } #[post("/wifi/modify", data = "")] @@ -288,7 +288,7 @@ pub fn network_home(flash: Option, _auth: Authenticated) -> Templa context.flash_msg = Some(flash.message().to_string()); }; // template_dir is set in Rocket.toml - Template::render("settings/network/network_card", &context) + Template::render("settings/network/menu", &context) } // HELPERS AND ROUTES FOR /settings/network/ap/activate @@ -391,7 +391,7 @@ pub fn wifi_list(flash: Option, _auth: Authenticated) -> Template context.flash_msg = Some(flash.message().to_string()); }; // template_dir is set in Rocket.toml - Template::render("settings/network/network_list", &context) + Template::render("settings/network/list_aps", &context) } // HELPERS AND ROUTES FOR /settings/network/wifi @@ -557,7 +557,7 @@ pub fn network_detail(ssid: &str, flash: Option, _auth: Authentica context.flash_msg = Some(flash.message().to_string()); }; // template_dir is set in Rocket.toml - Template::render("settings/network/network_detail", &context) + Template::render("settings/network/ap_details", &context) } // HELPERS AND ROUTES FOR /settings/network/wifi/activate @@ -590,7 +590,7 @@ pub fn add_wifi(flash: Option, _auth: Authenticated) -> Template { context.flash_msg = Some(flash.message().to_string()); }; // template_dir is set in Rocket.toml - Template::render("settings/network/network_add", &context) + Template::render("settings/network/add_ap", &context) } // used in /settings/network/wifi/add? @@ -628,7 +628,7 @@ pub fn add_ssid(ssid: &str, flash: Option, _auth: Authenticated) - context.flash_msg = Some(flash.message().to_string()); }; // template_dir is set in Rocket.toml - Template::render("settings/network/network_add", &context) + Template::render("settings/network/add_ap", &context) } #[post("/wifi/add", data = "")] @@ -646,7 +646,7 @@ pub fn add_credentials(wifi: Form, _auth: Authenticated) -> Template { Some("Network credentials already exist for this access point".to_string()); context.title = Some("Add WiFi Network".to_string()); // return early from handler with "creds already exist" message - return Template::render("settings/network/network_add", &context); + return Template::render("settings/network/add_ap", &context); }; // if credentials not found, generate and write wifi config to wpa_supplicant @@ -663,7 +663,7 @@ pub fn add_credentials(wifi: Form, _auth: Authenticated) -> Template { context.flash_name = Some("success".to_string()); context.flash_msg = Some("Added WiFi credentials".to_string()); context.title = Some("Add WiFi Network".to_string()); - Template::render("settings/network/network_add", &context) + Template::render("settings/network/add_ap", &context) } Err(_) => { debug!("Failed to add WiFi credentials."); @@ -672,7 +672,7 @@ pub fn add_credentials(wifi: Form, _auth: Authenticated) -> Template { context.flash_name = Some("error".to_string()); context.flash_msg = Some("Failed to add WiFi credentials".to_string()); context.title = Some("Add WiFi Network".to_string()); - Template::render("settings/network/network_add", &context) + Template::render("settings/network/add_ap", &context) } } } @@ -738,7 +738,7 @@ pub fn wifi_usage(flash: Option, _auth: Authenticated) -> Template context.flash_msg = Some(flash.message().to_string()); }; // template_dir is set in Rocket.toml - Template::render("settings/network/network_usage", &context) + Template::render("settings/network/data_usage_limits", &context) } #[post("/wifi/usage", data = "")] diff --git a/peach-web/src/routes/status/device.rs b/peach-web/src/routes/status/device.rs index f67c9aa..49f71cb 100644 --- a/peach-web/src/routes/status/device.rs +++ b/peach-web/src/routes/status/device.rs @@ -150,7 +150,7 @@ impl StatusContext { } } -#[get("/status")] +#[get("/")] pub fn device_status(flash: Option, _auth: Authenticated) -> Template { // assign context through context_builder call let mut context = StatusContext::build(); @@ -163,7 +163,7 @@ pub fn device_status(flash: Option, _auth: Authenticated) -> Templ context.flash_msg = Some(flash.message().to_string()); }; // template_dir is set in Rocket.toml - Template::render("device", &context) + Template::render("status/device", &context) } // HELPERS AND ROUTES FOR /power/reboot diff --git a/peach-web/src/routes/status/mod.rs b/peach-web/src/routes/status/mod.rs index cd4d59c..8a35662 100644 --- a/peach-web/src/routes/status/mod.rs +++ b/peach-web/src/routes/status/mod.rs @@ -1,2 +1,3 @@ pub mod device; +pub mod network; pub mod ping; diff --git a/peach-web/src/routes/status/network.rs b/peach-web/src/routes/status/network.rs new file mode 100644 index 0000000..ff6c6af --- /dev/null +++ b/peach-web/src/routes/status/network.rs @@ -0,0 +1,164 @@ +use rocket::{get, request::FlashMessage}; +use rocket_dyn_templates::Template; +use serde::Serialize; + +use peach_lib::network_client; +use peach_lib::stats_client::Traffic; + +use crate::routes::authentication::Authenticated; + +// HELPERS AND ROUTES FOR /status/network + +#[derive(Debug, Serialize)] +pub struct NetworkContext { + pub ap_ip: String, + pub ap_ssid: String, + pub ap_state: String, + pub ap_traffic: Option, + pub wlan_ip: String, + pub wlan_rssi: Option, + pub wlan_ssid: String, + pub wlan_state: String, + pub wlan_status: String, + pub wlan_traffic: Option, + pub flash_name: Option, + pub flash_msg: Option, + // page title for header in navbar + pub title: Option, + // url for back-arrow link + pub back: Option, +} + +impl NetworkContext { + pub fn build() -> NetworkContext { + let ap_ip = match network_client::ip("ap0") { + Ok(ip) => ip, + Err(_) => "x.x.x.x".to_string(), + }; + let ap_ssid = match network_client::ssid("ap0") { + Ok(ssid) => ssid, + Err(_) => "Not currently activated".to_string(), + }; + let ap_state = match network_client::state("ap0") { + Ok(state) => state, + Err(_) => "Interface unavailable".to_string(), + }; + let ap_traffic = match network_client::traffic("ap0") { + Ok(traffic) => { + let mut t = traffic; + // modify traffic values & assign measurement unit + // based on received and transmitted values + // if received > 999 MB, convert it to GB + if t.received > 1_047_527_424 { + t.received /= 1_073_741_824; + t.rx_unit = Some("GB".to_string()); + } else if t.received > 0 { + // otherwise, convert it to MB + t.received = (t.received / 1024) / 1024; + t.rx_unit = Some("MB".to_string()); + } else { + t.received = 0; + t.rx_unit = Some("MB".to_string()); + } + + if t.transmitted > 1_047_527_424 { + t.transmitted /= 1_073_741_824; + t.tx_unit = Some("GB".to_string()); + } else if t.transmitted > 0 { + t.transmitted = (t.transmitted / 1024) / 1024; + t.tx_unit = Some("MB".to_string()); + } else { + t.transmitted = 0; + t.tx_unit = Some("MB".to_string()); + } + Some(t) + } + Err(_) => None, + }; + let wlan_ip = match network_client::ip("wlan0") { + Ok(ip) => ip, + Err(_) => "x.x.x.x".to_string(), + }; + let wlan_rssi = match network_client::rssi_percent("wlan0") { + Ok(rssi) => Some(rssi), + Err(_) => None, + }; + let wlan_ssid = match network_client::ssid("wlan0") { + Ok(ssid) => ssid, + Err(_) => "Not connected".to_string(), + }; + let wlan_state = match network_client::state("wlan0") { + Ok(state) => state, + Err(_) => "Interface unavailable".to_string(), + }; + let wlan_status = match network_client::status("wlan0") { + Ok(status) => status, + Err(_) => "Interface unavailable".to_string(), + }; + let wlan_traffic = match network_client::traffic("wlan0") { + Ok(traffic) => { + let mut t = traffic; + // modify traffic values & assign measurement unit + // based on received and transmitted values + // if received > 999 MB, convert it to GB + if t.received > 1_047_527_424 { + t.received /= 1_073_741_824; + t.rx_unit = Some("GB".to_string()); + } else if t.received > 0 { + // otherwise, convert it to MB + t.received = (t.received / 1024) / 1024; + t.rx_unit = Some("MB".to_string()); + } else { + t.received = 0; + t.rx_unit = Some("MB".to_string()); + } + + if t.transmitted > 1_047_527_424 { + t.transmitted /= 1_073_741_824; + t.tx_unit = Some("GB".to_string()); + } else if t.transmitted > 0 { + t.transmitted = (t.transmitted / 1024) / 1024; + t.tx_unit = Some("MB".to_string()); + } else { + t.transmitted = 0; + t.tx_unit = Some("MB".to_string()); + } + Some(t) + } + Err(_) => None, + }; + + NetworkContext { + ap_ip, + ap_ssid, + ap_state, + ap_traffic, + wlan_ip, + wlan_rssi, + wlan_ssid, + wlan_state, + wlan_status, + wlan_traffic, + flash_name: None, + flash_msg: None, + title: None, + back: None, + } + } +} + +#[get("/network")] +pub fn network_status(flash: Option, _auth: Authenticated) -> Template { + // assign context through context_builder call + let mut context = NetworkContext::build(); + context.back = Some("/status".to_string()); + context.title = Some("Network Status".to_string()); + // check to see if there is a flash message to display + if let Some(flash) = flash { + // add flash message contents to the context object + context.flash_name = Some(flash.kind().to_string()); + context.flash_msg = Some(flash.message().to_string()); + }; + // template_dir is set in Rocket.toml + Template::render("status/network", &context) +} diff --git a/peach-web/src/tests.rs b/peach-web/src/tests.rs index 251de09..18365cc 100644 --- a/peach-web/src/tests.rs +++ b/peach-web/src/tests.rs @@ -53,25 +53,17 @@ fn index_html() { } #[test] -fn network_card_html() { +fn network_settings_menu_html() { let client = Client::tracked(init_rocket()).expect("valid rocket instance"); let response = client.get("/settings/network").dispatch(); assert_eq!(response.status(), Status::Ok); assert_eq!(response.content_type(), Some(ContentType::HTML)); let body = response.into_string().unwrap(); - assert!(body.contains("MODE")); - assert!(body.contains("SSID")); - assert!(body.contains("IP")); - assert!(body.contains("Add WiFi Network")); - assert!(body.contains("Deploy Access Point")); - assert!(body.contains("List WiFi Networks")); - assert!(body.contains("SIGNAL")); - assert!(body.contains("DOWNLOAD")); - assert!(body.contains("UPLOAD")); + assert!(body.contains("Network Configuration")); } #[test] -fn network_list_html() { +fn list_aps_html() { let client = Client::tracked(init_rocket()).expect("valid rocket instance"); let response = client.get("/settings/network/wifi").dispatch(); assert_eq!(response.status(), Status::Ok); @@ -83,7 +75,7 @@ fn network_list_html() { // TODO: needs further testing once template has been refactored #[test] -fn network_detail_html() { +fn ap_details_html() { let client = Client::tracked(init_rocket()).expect("valid rocket instance"); let response = client.get("/settings/network/wifi?ssid=Home").dispatch(); assert_eq!(response.status(), Status::Ok); @@ -93,7 +85,7 @@ fn network_detail_html() { } #[test] -fn network_add_html() { +fn add_ap_html() { let client = Client::tracked(init_rocket()).expect("valid rocket instance"); let response = client.get("/settings/network/wifi/add").dispatch(); assert_eq!(response.status(), Status::Ok); @@ -107,7 +99,7 @@ fn network_add_html() { } #[test] -fn network_add_ssid_html() { +fn add_ap_ssid_html() { let client = Client::tracked(init_rocket()).expect("valid rocket instance"); let response = client .get("/settings/network/wifi/add?ssid=Home") @@ -281,7 +273,7 @@ fn power_html() { } #[test] -fn network_usage_html() { +fn data_usage_html() { let client = Client::tracked(init_rocket()).expect("valid rocket instance"); let response = client.get("/settings/network/wifi/usage").dispatch(); assert_eq!(response.status(), Status::Ok); diff --git a/peach-web/static/css/peachcloud.css b/peach-web/static/css/peachcloud.css index 456eb51..8e65b6f 100644 --- a/peach-web/static/css/peachcloud.css +++ b/peach-web/static/css/peachcloud.css @@ -215,14 +215,14 @@ body { border: var(--border-width-1) solid; border-radius: var(--border-radius-3); background-color: var(--light-gray); - margin-top: 1rem; - margin-bottom: 1rem; + /* margin-top: 1rem; */ + /* margin-bottom: 1rem; */ } -.capsule-profile { +.capsule-container { margin-left: 2rem; margin-right: 2rem; - padding-top: 1.5rem; + padding-top: 1rem; padding-bottom: 1rem; } @@ -247,8 +247,9 @@ body { .card-container { justify-content: center; - padding-top: 1rem; - padding-bottom: 1rem; + padding: 0.5rem; + /* padding-top: 1rem; */ + /* padding-bottom: 1rem; */ } .form-container { @@ -433,10 +434,16 @@ body { align-items: center; justify-content: center; justify-items: center; - margin-right: 2rem; - margin-left: 2rem; - padding-top: 1.5rem; padding-bottom: 1rem; + /* margin-right: 2rem; */ + /* margin-left: 2rem; */ + /* padding-top: 1.5rem; */ +} + +.two-grid-top-right { + grid-column: 2; + justify-self: right; + padding: 0; } .three-grid { @@ -822,7 +829,6 @@ meter::-moz-meter-bar { */ .nav-bar { - background-color: var(--moon-gray); display: flex; align-items: center; width: 100%; diff --git a/peach-web/templates/device.html.tera b/peach-web/templates/device.html.tera deleted file mode 100644 index 96d1cc7..0000000 --- a/peach-web/templates/device.html.tera +++ /dev/null @@ -1,132 +0,0 @@ -{%- extends "nav" -%} -{%- block card -%} - {# ASSIGN VARIABLES #} - {# ---------------- #} - {%- if mem_stats -%} - {% set mem_usage_percent = mem_stats.used / mem_stats.total * 100 | round -%} - {% set mem_used = mem_stats.used / 1024 | round -%} - {% set mem_free = mem_stats.free / 1024 | round -%} - {% set mem_total = mem_stats.total / 1024 | round -%} - {% endif -%} - {% if cpu_stat_percent -%} - {% set cpu_usage_percent = cpu_stat_percent.nice + cpu_stat_percent.system + cpu_stat_percent.user | round -%} - {%- endif -%} - {%- if disk_stats -%} - {%- for disk in disk_stats -%} - {%- set_global disk_usage_percent = disk.used_percentage -%} - {# Calculate free disk space in megabytes #} - {%- set_global disk_free = disk.one_k_blocks_free / 1024 | round -%} - {%- endfor -%} - {%- endif -%} - -
-
- {# Display microservice status for network, oled & stats #} -
- -
- Network -
- - -
-
- -
- Display -
- - -
-
- -
- Stats -
- - -
-
-
-
- -
- Dyndns -
- - -
-
- -
- Config -
- - -
-
- -
- Sbot -
- - -
-
-
- {# Display CPU usage meter #} - {%- if cpu_stat_percent -%} -
- CPU - {{ cpu_usage_percent }}% -
- -
- CPU Usage -
-
- {%- else -%} -

CPU usage data unavailable

- {% endif -%} - {# Display memory usage meter #} - {%- if mem_stats %} -
- Memory - {{ mem_usage_percent }}% ({{ mem_free }} MB free) -
- -
- Memory Usage -
-
- {%- else -%} -

Memory usage data unavailable

- {% endif -%} - {# Display disk usage meter #} - {%- if disk_stats %} -
- Disk - {{ disk_usage_percent }}% ({% if disk_free > 1024 %}{{ disk_free / 1024 | round }} GB{% else %}{{ disk_free }} MB{% endif %} free) -
- -
- Disk Usage -
-
- {%- else -%} -

Disk usage data unavailable

- {%- endif %} - {# Display system uptime in minutes #} - {%- if uptime and uptime < 60 %} -

Uptime: {{ uptime }} minutes

- {# Display system uptime in hours & minutes #} - {%- elif uptime and uptime > 60 -%} -

Uptime: {{ uptime / 60 | round(method="floor") }} hours, {{ uptime % 60 }} minutes

- {%- else -%} -

Uptime data unavailable

- {%- endif %} - - {% include "snippets/flash_message" %} -
-
-{%- endblock card %} diff --git a/peach-web/templates/power.html.tera b/peach-web/templates/power.html.tera index ffd0a1e..0aabbb0 100644 --- a/peach-web/templates/power.html.tera +++ b/peach-web/templates/power.html.tera @@ -7,7 +7,6 @@ {% include "snippets/flash_message" %} diff --git a/peach-web/templates/settings/network/network_add.html.tera b/peach-web/templates/settings/network/add_ap.html.tera similarity index 100% rename from peach-web/templates/settings/network/network_add.html.tera rename to peach-web/templates/settings/network/add_ap.html.tera diff --git a/peach-web/templates/settings/network/network_detail.html.tera b/peach-web/templates/settings/network/ap_details.html.tera similarity index 100% rename from peach-web/templates/settings/network/network_detail.html.tera rename to peach-web/templates/settings/network/ap_details.html.tera diff --git a/peach-web/templates/settings/network/network_usage.html.tera b/peach-web/templates/settings/network/data_usage_limits.html.tera similarity index 100% rename from peach-web/templates/settings/network/network_usage.html.tera rename to peach-web/templates/settings/network/data_usage_limits.html.tera diff --git a/peach-web/templates/settings/network/network_list.html.tera b/peach-web/templates/settings/network/list_aps.html.tera similarity index 100% rename from peach-web/templates/settings/network/network_list.html.tera rename to peach-web/templates/settings/network/list_aps.html.tera diff --git a/peach-web/templates/settings/network/menu.html.tera b/peach-web/templates/settings/network/menu.html.tera new file mode 100644 index 0000000..1d6eed9 --- /dev/null +++ b/peach-web/templates/settings/network/menu.html.tera @@ -0,0 +1,24 @@ +{%- extends "nav" -%} + +{%- block card %} + +
+ +
+ Add WiFi Network + Configure DNS + + {%- if ap_state == "up" %} + Enable WiFi + {%- else %} + Deploy Access Point + {%- endif -%} + List WiFi Networks + View Data Usage + View Network Status +
+ + {% include "snippets/flash_message" %} +
+ +{%- endblock card -%} diff --git a/peach-web/templates/settings/network/network_modify.html.tera b/peach-web/templates/settings/network/modify_ap.html.tera similarity index 100% rename from peach-web/templates/settings/network/network_modify.html.tera rename to peach-web/templates/settings/network/modify_ap.html.tera diff --git a/peach-web/templates/status/device.html.tera b/peach-web/templates/status/device.html.tera new file mode 100644 index 0000000..42b225c --- /dev/null +++ b/peach-web/templates/status/device.html.tera @@ -0,0 +1,135 @@ +{%- extends "nav" -%} +{%- block card -%} + {# ASSIGN VARIABLES #} + {# ---------------- #} + {%- if mem_stats -%} + {% set mem_usage_percent = mem_stats.used / mem_stats.total * 100 | round -%} + {% set mem_used = mem_stats.used / 1024 | round -%} + {% set mem_free = mem_stats.free / 1024 | round -%} + {% set mem_total = mem_stats.total / 1024 | round -%} + {% endif -%} + {% if cpu_stat_percent -%} + {% set cpu_usage_percent = cpu_stat_percent.nice + cpu_stat_percent.system + cpu_stat_percent.user | round -%} + {%- endif -%} + {%- if disk_stats -%} + {%- for disk in disk_stats -%} + {%- set_global disk_usage_percent = disk.used_percentage -%} + {# Calculate free disk space in megabytes #} + {%- set_global disk_free = disk.one_k_blocks_free / 1024 | round -%} + {%- endfor -%} + {%- endif -%} + +
+
+
+ {# Display microservice status for network, oled & stats #} + + +
+ Network +
+ + +
+
+
+ +
+ Display +
+ + +
+
+ +
+ Stats +
+ + +
+
+ {# Display status for dynsdns, config & sbot #} + +
+ Dyndns +
+ + +
+
+ +
+ Config +
+ + +
+
+ +
+ Sbot +
+ + +
+
+
+
+ {# Display CPU usage meter #} + {%- if cpu_stat_percent -%} +
+ CPU + {{ cpu_usage_percent }}% +
+ +
+ CPU Usage +
+
+ {%- else -%} +

CPU usage data unavailable

+ {% endif -%} + {# Display memory usage meter #} + {%- if mem_stats %} +
+ Memory + {{ mem_usage_percent }}% ({{ mem_free }} MB free) +
+ +
+ Memory Usage +
+
+ {%- else -%} +

Memory usage data unavailable

+ {% endif -%} + {# Display disk usage meter #} + {%- if disk_stats %} +
+ Disk + {{ disk_usage_percent }}% ({% if disk_free > 1024 %}{{ disk_free / 1024 | round }} GB{% else %}{{ disk_free }} MB{% endif %} free) +
+ +
+ Disk Usage +
+
+ {%- else -%} +

Disk usage data unavailable

+ {%- endif %} + {# Display system uptime in minutes #} + {%- if uptime and uptime < 60 %} +

Uptime: {{ uptime }} minutes

+ {# Display system uptime in hours & minutes #} + {%- elif uptime and uptime > 60 -%} +

Uptime: {{ uptime / 60 | round(method="floor") }} hours, {{ uptime % 60 }} minutes

+ {%- else -%} +

Uptime data unavailable

+ {%- endif %} +
+ + {% include "snippets/flash_message" %} +
+
+{%- endblock card %} diff --git a/peach-web/templates/settings/network/network_card.html.tera b/peach-web/templates/status/network.html.tera similarity index 52% rename from peach-web/templates/settings/network/network_card.html.tera rename to peach-web/templates/status/network.html.tera index e880a5e..eaf630a 100644 --- a/peach-web/templates/settings/network/network_card.html.tera +++ b/peach-web/templates/status/network.html.tera @@ -6,37 +6,34 @@
-
- - - -
- WiFi router - +
+ +
+ + + Configure + + + +
+ WiFi router + +
+ + +
+ +

Access Point

+ +

peach

+ +

{{ ap_ip }}

+
- - -
- -

Access Point

- -

peach

- -

{{ ap_ip }}

-
-
- - - - {% include "snippets/flash_message" %} - -
-
+ +
+ +
Digital devices
@@ -78,47 +75,45 @@
{%- if wlan_state == "up" %} -
- - - -
- WiFi online - +
+ +
+ + Configure + + + + +
+ WiFi online + {%- else %} -
-
- WiFi offline - +
+
+ + Configure + +
+ WiFi offline + {%- endif %} +
+
+ + + +

WiFi Client

+ +

{{ wlan_ssid }}

+ +

{{ wlan_ip }}

+
-
- - - -

WiFi Client

- -

{{ wlan_ssid }}

- -

{{ wlan_ip }}

-
-
- - - - {% include "snippets/flash_message" %} -
- + +
+ -
+
Signal
@@ -159,6 +154,5 @@
- {%- endif -%} {%- endblock card -%}