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 { 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, 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::>(&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 { 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 { match serde_json::from_value(msg.clone()) { Ok(req) => Some(req), Err(_) => None, } } pub fn get_extra_parameters(s: String) -> HashMap { let mut result = HashMap::new(); let plist: Vec = s.split(",").map(|x| x.to_string()).collect(); for pl in plist { let sm: Vec = 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, Box> { 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::>(); } Ok(res) }