Skip to content

Commit

Permalink
rants - nats for rust - incorporated into tauri
Browse files Browse the repository at this point in the history
  • Loading branch information
K7theCompSciMan committed Nov 4, 2024
1 parent c25eda9 commit 5198423
Show file tree
Hide file tree
Showing 17 changed files with 553 additions and 10 deletions.
85 changes: 78 additions & 7 deletions client/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mini-redis = "0.4"
futures = "0.3.30"
tauri-runtime-wry = "0.14.9"
fs = "0.0.5"
rants = "0.6.0"
# simple_transcribe_rs = "1.0.3"
# pv_recorder = "*"
[features]
Expand Down
34 changes: 32 additions & 2 deletions client/src-tauri/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tauri_plugin_store::Store;
use crate::{app::run_command, xata_structs::{self, Device}};
use crate::{store};
use tauri::api::process::Command;

use rants::Client;
fn default_device() -> Device {
Device {
name: "default".to_string(),
Expand Down Expand Up @@ -58,16 +58,46 @@ pub fn handle_server_device_updates() {
});
}

async fn run_nats_backend(subject: str) {
let address = "0.0.0.0:4222".parse().unwrap();
let client = Client::new(vec![address]);

client.connect_mut().await.echo(true);

client.connect().await;

let subject = subject.parse().unwrap();

let (_, mut subscription) = client.subscribe(&subject, 1024).await.unwrap();

client
.publish(&subject, "Test")
.await
.unwrap();

let message = subscription.next().await.unwrap();
let message = String::from_utf8(message.into_payload()).unwrap();
println!("Recieved message: {}", message);

client.disconnect().await;
}
pub fn handle_device_updates_api_call() -> Result<(), Error> {
let path = "stores/store.json".to_string();
println!("Getting Updated Device");
let mut device: Device = serde_json::from_value::<Device>(store::get(path.clone(), "device".to_string())).unwrap_or(default_device());
// println!("Device from store: {:?}", device);
let mut device_type = serde_json::from_value::<String>(store::get(path.clone(), "deviceType".to_string())).unwrap_or("default".to_string());
handle_server_device_updates();
let mut backend_nats = serde_json::from_value::<bool>(store::get(path.clone(), "backendNATS".to_string())).unwrap_or(false);
if backend_nats {
tauri::async_runtime::spawn(async move {
run_nats_backend(device.id.clone().to_str()).await;
})
}
return Ok(());
loop {
let mut running_backend = serde_json::from_value::<bool>(store::get(path.clone(), "runningClientBackend".to_string())).unwrap_or(false);
if device.id!="default".to_string() && device_type!="none".to_string() && running_backend {
if device.id!="default".to_string() && device_type!="none".to_string() && running_backend {
let past_messages = device.messages.clone();
let request_url = format!("https://spark-api.fly.dev/device/{device_type}/{device_id}/", device_type = device_type, device_id = device.id);
let updated_device: Device = reqwest::blocking::get(&request_url)?.json()?;
Expand Down
2 changes: 1 addition & 1 deletion client/src/routes/(dashboard)/servers/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
invoke('run_command', { command: command.command });
}
</script>

<h1>Erm what the flip</h1>
<div
class="bg-dark-background-600 w-[80vw] ml-[5%] rounded-[2rem] shadow-2xl h-screen overflow-auto relative"
>
Expand Down
31 changes: 31 additions & 0 deletions webapp/src/routes/(setup)/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { LayoutServerLoad } from './$types';

// get `locals.user` and pass it to the `page` store
export const load: LayoutServerLoad = async ({ locals }) => {
if(locals.user) {
if(locals.accessToken) {
if(locals.device) {
return {
user: locals.user,
accessToken: locals.accessToken,
device: locals.device
}
}
return {
user: locals.user,
accessToken: locals.accessToken,
device: null
}
}
return {
user: locals.user,
accessToken: '',
device: null
};
}
return {
user: null,
accessToken: '',
device: null
};
};
2 changes: 2 additions & 0 deletions webapp/src/routes/(setup)/+layout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const prerender = true
export const ssr = false
21 changes: 21 additions & 0 deletions webapp/src/routes/(setup)/device-info/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script lang="ts">
import { goto } from "$app/navigation";
</script>
<div class="text-center h-1/2 top-1/4 relative w-1/2 left-1/4 flex flex-col justify-items-center text-amber-600">
<h1 class="text-6xl mt-[4%]">
Name Your Device, and Choose its type
</h1>
<p class="text-2xl mt-[4%] w-[75%] left-[12.5%] relative">
Devices can be clients, which are controlled by your assistant, or servers, which control your assistant, and communicate with the clients.
</p>
<button on:click={() => goto("/groups")}>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-8 absolute bottom-1/4 left-[23%]">
<path stroke-linecap="round" stroke-linejoin="round" d="M15.75 19.5 8.25 12l7.5-7.5" />
</svg>
</button>
<button on:click={() => goto("/device-setup")}>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-8 absolute bottom-1/4 left-[67%]">
<path stroke-linecap="round" stroke-linejoin="round" d="m8.25 4.5 7.5 7.5-7.5 7.5" />
</svg>
</button>
</div>
Loading

0 comments on commit 5198423

Please sign in to comment.