diff --git a/peach-web/src/routes/settings/network.rs b/peach-web/src/routes/settings/network.rs index 61ae7be2..76b8af90 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 f67c9aa8..49f71cb7 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 cd4d59c7..8a356623 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 00000000..ff6c6af7 --- /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/templates/device.html.tera b/peach-web/templates/device.html.tera deleted file mode 100644 index 96d1cc79..00000000 --- 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/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 00000000..1d6eed9d --- /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 00000000..42b225c2 --- /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 54% rename from peach-web/templates/settings/network/network_card.html.tera rename to peach-web/templates/status/network.html.tera index 30e56aaf..eaf630aa 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,45 +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
@@ -157,6 +154,5 @@
- {%- endif -%} {%- endblock card -%}