106 lines
2.7 KiB
Rust
106 lines
2.7 KiB
Rust
use std::{collections::HashMap, fs::File, io::BufReader};
|
|
|
|
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,
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
pub fn read_sheet_config() -> Result<Vec<String>, Box<dyn std::error::Error>> {
|
|
let mut res = Vec::new();
|
|
|
|
let config_file = File::open("./sheet-api.json")?;
|
|
let mut buf = BufReader::new(config_file);
|
|
|
|
let val: serde_json::Value = serde_json::from_reader(&mut buf)?;
|
|
|
|
if let Some(eobj) = val.get("endpoints")
|
|
&& let Some(endpoint_array) = eobj.as_array().clone()
|
|
{
|
|
res = endpoint_array
|
|
.iter()
|
|
.map(|x| x.as_str().unwrap_or_default().to_string())
|
|
.collect::<Vec<String>>();
|
|
}
|
|
|
|
Ok(res)
|
|
}
|