feat: 初始化项目结构并添加位置上报相关功能

This commit is contained in:
2025-07-28 23:29:21 +08:00
parent 86e4f1acdf
commit f87786fd3a
22 changed files with 128 additions and 46 deletions

7
.idea/dataSources.xml generated
View File

@@ -8,5 +8,12 @@
<jdbc-url>jdbc:postgresql://home.hzer.xyz:5432/postgres</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
<data-source source="LOCAL" name="database" uuid="f7f808d9-ca1a-4e93-912f-baf449326472">
<driver-ref>sqlite.xerial</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
<jdbc-url>jdbc:sqlite:D:\dev\code\mine\home-api\database.sqlite</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>

View File

@@ -1,14 +1,3 @@
[package]
name = "init"
version = "0.1.0"
edition = "2024"
[dependencies]
actix-web = "4.11.0"
actix-files = "0.6.6"
actix-cors = "0.7.0"
log = "0.4"
env_logger = "0.11.8"
r2d2 = "0.8.10"
r2d2_sqlite = "0.30.0"
rusqlite = { version = "0.36", features = ["bundled"] }
[workspace]
resolver = "2"
members = ["packages/gotify-ws", "packages/home-api"]

Binary file not shown.

View File

@@ -0,0 +1,11 @@
[package]
name = "gotify-ws"
version = "0.1.0"
edition = "2024"
[dependencies]
env_logger = "0.11.8"
futures = "0.3.31"
log = "0.4.27"
tokio = { version = "1.47.0", features = ["full"] }
tokio-tungstenite = { version = "0.27.0", features = ["native-tls"] }

View File

@@ -0,0 +1,26 @@
mod utils;
use futures::StreamExt;
use log::info;
use tokio_tungstenite::connect_async;
use utils::logger;
#[tokio::main]
async fn main() {
logger::init_logger();
const WS: &str = "wss://home.hzer.xyz/gotify/stream?token=CDIwYlYJuxWxVr5";
match connect_async(WS).await {
Ok((stream, _)) => {
info!("Connected to Gotify server {WS}");
let (_, mut read) = stream.split();
while let Some(msg) = read.next().await {
info!("Received message: {msg:?}");
}
}
Err(e) => {
info!("Failed to connect to Gotify server: {e}");
}
}
}

View File

@@ -0,0 +1,5 @@
use env_logger::{Builder, Env};
pub fn init_logger(){
Builder::from_env(Env::default().default_filter_or("info")).init();
}

View File

@@ -0,0 +1 @@
pub mod logger;

View File

@@ -0,0 +1,6 @@
[package]
name = "home-api"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@@ -0,0 +1,69 @@
mod sqlite {
use std::{
collections::HashMap,
fs::{File, read_to_string},
io::{BufRead, BufReader, Read},
};
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
#[test]
fn init() {
let manager = SqliteConnectionManager::file("database.sqlite");
let pool = r2d2::Pool::new(manager).unwrap();
let file = File::open("./resources/ID/id.txt").unwrap();
let reader = BufReader::new(file);
check_id_column_exist(&pool);
let mut id_map = HashMap::<String, String>::new();
for line in reader.lines() {
match line {
Ok(line) => {
handle_duplicate_id(&line, &mut id_map);
}
Err(e) => {
println!("{}", e);
}
}
}
// println!("id_map 中共有 {} 条记录", id_map.len());
}
fn check_id_column_exist(conn: &Pool<SqliteConnectionManager>) {
let conn: r2d2::PooledConnection<SqliteConnectionManager> = conn.get().unwrap();
conn.execute(
"CREATE TABLE IF NOT EXISTS id_card (
row_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
id TEXT NOT NULL,
create_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
update_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)",
[],
)
.unwrap();
}
/**
* 处理重复的id
*/
fn handle_duplicate_id(lineStr: &str, id_map: &mut HashMap<String, String>) {
let line = lineStr.split("----").collect::<Vec<&str>>();
if line.len() != 2 { println!("the line {} is invalid", line);
return;
}
let (name, id) = (line[0], line[1]);
if id_map.contains_key(id) {
println!("the id {} is duplicate, and the name is {}", id, name);
} else {
id_map.insert(id.to_string(), name.to_string());
}
}
}

View File

@@ -1,32 +0,0 @@
mod sqlite {
use std::{
fs::{File, read_to_string},
io::{BufRead, BufReader, Read},
};
use r2d2_sqlite::SqliteConnectionManager;
#[test]
fn init() {
let manager = SqliteConnectionManager::file("database.sqlite");
let pool = r2d2::Pool::new(manager).unwrap();
let file = File::open("./resources/ID/id.txt").unwrap();
let reader = BufReader::new(file);
for line in reader.lines() {
match line {
Ok(line) => {
let line = line.split("----").collect::<Vec<&str>>();
let (name, id) = (line[0], line[1]);
let conn = pool.get().unwrap();
let mut stmt = conn.prepare("INSERT INTO id (name, id) VALUES (?1, ?2)").unwrap();
stmt.execute((name, id)).unwrap();
}
Err(e) => {
println!("{}", e);
}
}
}
}
}