Skip to content

Commit

Permalink
feat(frontend): show updates to DietPi
Browse files Browse the repository at this point in the history
  • Loading branch information
ravenclaw900 committed Oct 3, 2021
1 parent db128e6 commit 1ba4b97
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/backend/src/sockets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,28 @@ pub async fn socket_handler(socket: warp::ws::WebSocket) {
if data.is_close() {
break;
}
dbg!(&data);
req = serde_json::from_str(data.to_str().unwrap()).unwrap();
data_send.send(req.clone()).unwrap();
println!("sent data {:?}", req);
if req.cmd.is_empty() {
if first_message {
first_message = false;
} else {
quit.swap(true, Relaxed);
println!("sent quit");
}
}
}
});
// Send global message (shown on all pages)
let _send = socket_send
.send(Message::text(
serde_json::to_string(&systemdata::global()).unwrap(),
))
.await;
while let Ok(message) = data_recv.recv().await {
println!("got msg, {}", message.page);
match message.page.as_str() {
"/" => main_handler(&mut socket_send, &quit_clone).await,
"/process" => {
Expand Down
7 changes: 7 additions & 0 deletions src/backend/src/systemdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,10 @@ pub fn services() -> Vec<types::ServiceData> {
}
services_list
}

pub fn global() -> types::GlobalData {
let update =
fs::read_to_string("/run/dietpi/.update_available").unwrap_or_else(|_| String::new());
dbg!(&update);
types::GlobalData { update }
}
7 changes: 6 additions & 1 deletion src/backend/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub struct HostData {
pub upgrades: u32,
}

#[derive(serde::Serialize, Debug)]
#[derive(serde::Serialize)]
pub struct ServiceData {
pub name: String,
pub log: String,
Expand All @@ -83,3 +83,8 @@ pub struct ServiceData {
pub struct ServiceList {
pub services: Vec<ServiceData>,
}

#[derive(serde::Serialize)]
pub struct GlobalData {
pub update: String,
}
14 changes: 13 additions & 1 deletion src/frontend/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
software?: software[];
response?: string;
processes?: processes[];
services: services[];
services?: services[];
update?: string;
}
interface software {
Expand Down Expand Up @@ -58,8 +59,13 @@
let socketData: socketData = {};
let shown = false;
let menu = window.innerWidth > 768;
let update = "";
const socketMessageListener = (e) => {
socketData = JSON.parse(e.data);
if (socketData.update != undefined) {
update = socketData.update;
}
};
const socketOpenListener = () => {
console.log("Connected");
Expand All @@ -82,6 +88,7 @@
function pollServer() {
socket.send(JSON.stringify({ page: window.location.pathname }));
//update = "";
}
onMount(() => {
Expand Down Expand Up @@ -145,6 +152,11 @@
class="h-10"
/></a
>
{#if update != undefined}
<span class="text-red-500 justify-self-center"
>DietPi update avalible: {update}</span
>
{/if}
</header>
<div class="dark:bg-gray-800 bg-gray-100 flex-grow p-6">
{#if shown}
Expand Down

0 comments on commit 1ba4b97

Please sign in to comment.