2026-05-05 17:03:33 +07:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
2026-04-28 16:43:22 +07:00
|
|
|
use super::model::*;
|
|
|
|
|
use axum::extract::ws::{CloseFrame, Message, WebSocket};
|
|
|
|
|
use redis::{TypedCommands, cmd};
|
|
|
|
|
|
|
|
|
|
#[deprecated]
|
|
|
|
|
pub async fn send_close_message(mut socket: WebSocket, code: u16, reason: &str) {
|
|
|
|
|
_ = socket
|
|
|
|
|
.send(Message::Close(Some(CloseFrame {
|
|
|
|
|
code,
|
|
|
|
|
reason: reason.into(),
|
|
|
|
|
})))
|
|
|
|
|
.await;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[deprecated]
|
|
|
|
|
pub async fn fetch_content_from_redis(redis: redis::Client, key: &str) -> Result<String, String> {
|
|
|
|
|
let mut rcli = redis.clone();
|
|
|
|
|
match rcli.get(key) {
|
|
|
|
|
Ok(s) => {
|
|
|
|
|
if let Some(res) = s {
|
|
|
|
|
Ok(res)
|
|
|
|
|
} else {
|
|
|
|
|
Err(format!("result error from key: {key}"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(e) => Err(format!("redis get failed: {e}")),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn fetch_content_from_redis_byte(
|
|
|
|
|
redis: redis::Client,
|
|
|
|
|
key: &str,
|
|
|
|
|
) -> Result<Vec<u8>, String> {
|
|
|
|
|
let mut conn = match redis.get_connection() {
|
|
|
|
|
Ok(cnn) => cnn,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
println!("get connection fail, {e}");
|
|
|
|
|
return Ok(vec![]);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let res = cmd("GET").arg(key).query::<Vec<u8>>(&mut conn);
|
|
|
|
|
|
|
|
|
|
match res {
|
|
|
|
|
Ok(res) => Ok(res),
|
|
|
|
|
Err(e) => {
|
|
|
|
|
println!("get fail, {e}");
|
|
|
|
|
return Ok(vec![]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[deprecated]
|
|
|
|
|
pub fn convert_ack_command(cmd_req: &serde_json::Value) -> Option<CommandRequestPayload> {
|
|
|
|
|
match serde_json::from_value(cmd_req.clone()) {
|
|
|
|
|
Ok(req) => Some(req),
|
|
|
|
|
Err(_) => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn convert_sys_msg_command(msg: &serde_json::Value) -> Option<SysMessage> {
|
|
|
|
|
match serde_json::from_value(msg.clone()) {
|
|
|
|
|
Ok(req) => Some(req),
|
|
|
|
|
Err(_) => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-05 17:03:33 +07:00
|
|
|
|
|
|
|
|
pub fn get_extra_parameters(s: String) -> HashMap<String, String> {
|
|
|
|
|
let mut result = HashMap::new();
|
|
|
|
|
|
|
|
|
|
let plist: Vec<String> = s.split(",").map(|x| x.to_string()).collect();
|
|
|
|
|
|
|
|
|
|
for pl in plist {
|
|
|
|
|
let sm: Vec<String> = pl.split("=").map(|x| x.to_string()).collect();
|
|
|
|
|
|
|
|
|
|
if sm.len() != 2 {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.insert(sm[0].to_string(), sm[1].to_string());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result
|
|
|
|
|
}
|