[WIP] Working on QR Code generation, already got the local address to be displayed

This commit is contained in:
Valmo Trindade
2024-11-29 04:35:40 -03:00
parent d183c0b437
commit 522424ba7b
4 changed files with 85 additions and 11 deletions

33
Cargo.lock generated
View File

@@ -93,6 +93,7 @@ dependencies = [
"log",
"log4rs",
"once_cell",
"qrcode",
"regex",
"reqwest",
"serde",
@@ -190,12 +191,24 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7"
[[package]]
name = "bytemuck"
version = "1.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b37c88a63ffd85d15b406896cc343916d7cf57838a847b3a6f2ca5d39a5695a"
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "byteorder-lite"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495"
[[package]]
name = "bytes"
version = "0.4.12"
@@ -751,6 +764,17 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "image"
version = "0.25.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd6f44aed642f18953a158afeb30206f4d50da59fbc66ecb53c66488de73563b"
dependencies = [
"bytemuck",
"byteorder-lite",
"num-traits",
]
[[package]]
name = "indexmap"
version = "2.6.0"
@@ -1176,6 +1200,15 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "qrcode"
version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d68782463e408eb1e668cf6152704bd856c78c5b6417adaee3203d8f4c1fc9ec"
dependencies = [
"image",
]
[[package]]
name = "quote"
version = "1.0.37"

View File

@@ -12,6 +12,7 @@ lazy_static = "1.5.0"
log = "0.4.22"
log4rs = "1.3.0"
once_cell = "1.19.0"
qrcode = "0.14.1"
regex = "1.10.6"
reqwest = { version = "0.12.7", features = ["blocking"] }
serde = { version = "1.0.210", features = ["derive"] }

View File

@@ -1,9 +1,11 @@
use std::sync::{Arc, Mutex};
use std::sync::mpsc::{self, Sender, Receiver};
use std::thread;
use log::info;
use ws::{listen, Message, Result as WsResult};
use lazy_static::lazy_static;
use log::info;
use std::net::{IpAddr, UdpSocket};
use std::sync::mpsc::{self, Receiver, Sender};
use std::sync::{Arc, Mutex};
use std::thread;
use ws::{listen, Message, Result as WsResult};
use qrcode::{render::unicode::{self}, QrCode};
use crate::structs::{IntoMessage, LocationPayload};
@@ -25,7 +27,7 @@ impl WsServer {
let mut running = true;
let ws_thread = thread::spawn(move || {
listen("127.0.0.1:3012", |out| {
listen("0.0.0.0:4152", |out| {
let clients_inner = Arc::clone(&clients_clone);
{
let mut clients_guard = clients_inner.lock().unwrap();
@@ -36,7 +38,8 @@ impl WsServer {
info!("Received: {}", msg);
Ok(())
}
}).unwrap();
})
.unwrap();
});
while running {
@@ -51,8 +54,8 @@ impl WsServer {
running = false;
info!("Stopping WebSocket server.");
}
Err(_) => {
info!("Error receiving command.");
Err(error) => {
info!("Error receiving command: {}", error.to_string());
}
}
}
@@ -118,3 +121,39 @@ pub fn stop() -> &'static str {
"Stopping WebSocket server"
}
pub fn local_qrcode() -> Vec<String> {
let mut result = Vec::<String>::new();
fn get_local_ip() -> Result<IpAddr, String> {
let socket = UdpSocket::bind("0.0.0.0:0").map_err(|e| e.to_string())?;
socket
.connect("8.8.8.8:80")
.map_err(|e| e.to_string())?;
socket
.local_addr()
.map(|addr| addr.ip())
.map_err(|e| e.to_string())
}
fn draw_qrcode(data: String) -> String {
let code = QrCode::new(data).expect("Failed to generate QR Code");
let ascii_qr = code.render::<unicode::Dense1x2>().quiet_zone(false).build();
return ascii_qr.replace("\n", ",")
}
let parsed_data = get_local_ip();
match parsed_data {
Ok(ip) => {
result.push(draw_qrcode(ip.to_string()));
result.push(ip.to_string())
},
Err(_) => {
result.push("not provided".to_string());
result.push("not provided".to_string());
},
}
return result;
}

View File

@@ -29,6 +29,7 @@ pub fn init() -> Extension {
Extension::build()
.command("start", commands::start)
.command("stop", commands::stop)
.command("local_ip", commands::local_qrcode)
.command("message", commands::message)
.command("location", commands::location)
.finish()