Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Harshil Jani <harshiljani2002@gmail.com>
  • Loading branch information
Harshil-Jani committed Aug 27, 2023
1 parent ffd7ccd commit a8451cd
Show file tree
Hide file tree
Showing 14 changed files with 16 additions and 1,793 deletions.
64 changes: 0 additions & 64 deletions rust/clients/bridge/src/ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//! Control functions for the Bluetooth Low Energy Module.
use prost::Message;
use super::rpc::Rpc;

/// include generated protobuf RPC rust definition file
mod proto { include!("../../../libqaul/src/rpc/protobuf_generated/rust/qaul.rpc.ble.rs"); }
Expand All @@ -16,69 +15,6 @@ mod proto_sys { include!("../../../libqaul/src/rpc/protobuf_generated/rust/qaul.
pub struct Ble {}

impl Ble {
/// CLI command interpretation
///
/// The CLI commands of BLE module are processed here
pub fn cli(command: &str) {
match command {
// request BLE device info
cmd if cmd.starts_with("info") => {
// create rpc message
let proto_message = proto::Ble {
message: Some(proto::ble::Message::InfoRequest(
proto::InfoRequest{}
)),
};
// send the message
Self::rpc_send(proto_message);
},
// send start request for BLE module
cmd if cmd.starts_with("start") => {
// create rpc message
let proto_message = proto::Ble {
message: Some(proto::ble::Message::StartRequest(
proto::StartRequest{}
)),
};
// send the message
Self::rpc_send(proto_message);
},
// send stop request for BLE module
cmd if cmd.starts_with("stop") => {
// create rpc message
let proto_message = proto::Ble {
message: Some(proto::ble::Message::StopRequest(
proto::StopRequest{}
)),
};
// send the message
Self::rpc_send(proto_message);
},
// request discovered devices
cmd if cmd.starts_with("discovered") => {
// create rpc message
let proto_message = proto::Ble {
message: Some(proto::ble::Message::DiscoveredRequest(
proto::DiscoveredRequest{}
)),
};
// send the message
Self::rpc_send(proto_message);
},
// unknown command
_ => log::error!("unknown BLE command"),
}
}

/// Send rpc message to libqaul
fn rpc_send(proto_message: proto::Ble) {
// encode message
let mut buf = Vec::with_capacity(proto_message.encoded_len());
proto_message.encode(&mut buf).expect("Vec<u8> provides capacity as needed");

// send message
Rpc::send_message(buf, super::rpc::proto::Modules::Ble.into(), "".to_string());
}

/// Print BLE module information
fn print_info(info: proto::InfoResponse) {
Expand Down
163 changes: 1 addition & 162 deletions rust/clients/bridge/src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use matrix_sdk::{
RoomId,
},
};
use mime::{self, Mime, STAR, STAR_STAR};
use mime::{self, Mime, STAR_STAR};
use prost::Message;
use std::{
collections::HashMap,
Expand All @@ -45,103 +45,8 @@ mod proto_file {

/// chat module function handling
pub struct Chat {}
pub struct QaulMenu {}

impl Chat {
/// CLI command interpretation
///
/// The CLI commands of feed module are processed here
pub fn cli(command: &str) {
match command {
// send chat message
cmd if cmd.starts_with("send ") => {
// get group id
let command_string = cmd.strip_prefix("send ").unwrap().to_string();
let mut iter = command_string.split_whitespace();

if let Some(group_id_str) = iter.next() {
// convert group id from string to binary version
let mut group_id = vec![];
match Self::id_string_to_bin(group_id_str.to_string()) {
Ok(v) => {
group_id = v.clone();
}
_ => match Self::uuid_string_to_bin(group_id_str.to_string()) {
Ok(v) => {
group_id = v.clone();
}
_ => {
log::error!("invalid group id format");
}
},
}
// get message string
if let Some(message) = command_string.strip_prefix(group_id_str) {
// send message
Self::send_chat_message(group_id, message.to_string().trim().to_string());
println!("chat message sent [{}] {}", group_id_str, message);
return;
} else {
log::error!("prefix '{}' not found", group_id_str);
return;
}
} else {
log::error!("chat send command incorrectly formatted");
}
}
// request chat conversation
cmd if cmd.starts_with("conversation") => {
match cmd.strip_prefix("conversation ") {
Some(command_str) => {
let command_string = command_str.to_string();
let mut iter = command_string.split_whitespace();
let mut group_id = Vec::new();
let mut last_index = 0;

// convert group id from string to binary version
if let Some(group_id_str) = iter.next() {
match Self::id_string_to_bin(group_id_str.to_string()) {
Ok(id) => {
group_id = id;
}
Err(_e) => {
match Self::uuid_string_to_bin(group_id_str.to_string()) {
Ok(id) => {
group_id = id;
}
_ => {
log::error!("invalid converstion id");
return;
}
}
}
}
}

// convert last_received index string to number
if let Some(index_str) = iter.next() {
// option: get last_received
if let Ok(index) = index_str.parse::<u64>() {
last_index = index;
} else {
log::error!("chat conversation index is not a valid number");
return;
}
}

// request chat conversation
Self::request_chat_conversation(group_id, last_index);
}
None => {
// request all messages
log::error!("chat conversation command not correctly formatted");
}
}
}
// unknown command
_ => log::error!("unknown chat command"),
}
}

/// Convert Group ID from String to Binary
pub fn id_string_to_bin(id: String) -> Result<Vec<u8>, String> {
Expand All @@ -168,26 +73,6 @@ impl Chat {
}
}

/// Create and send feed message via rpc
fn send_chat_message(group_id: Vec<u8>, message_text: String) {
// create feed send message
let proto_message = proto::Chat {
message: Some(proto::chat::Message::Send(proto::ChatMessageSend {
group_id,
content: message_text,
})),
};

// encode message
let mut buf = Vec::with_capacity(proto_message.encoded_len());
proto_message
.encode(&mut buf)
.expect("Vec<u8> provides capacity as needed");

// send message
Rpc::send_message(buf, super::rpc::proto::Modules::Chat.into(), "".to_string());
}

/// Request chat conversation via rpc
///
/// This provides all chat messages of a specific conversation.
Expand Down Expand Up @@ -298,48 +183,10 @@ impl Chat {
match chat.message {
Some(proto::chat::Message::ConversationList(proto_conversation)) => {
// Conversation table
let group_id_byte = proto_conversation.clone().group_id;
let group_id =
uuid::Uuid::from_bytes(proto_conversation.group_id.try_into().unwrap());

// println!("Conversation [ {} ]", group_id.to_string());
// println!("");
// println!("No. | Status | Sent At | Sender ID");
// println!(" [Message ID] Received At");
// println!(" Message Content");
// println!("");
let mut config = MATRIX_CONFIG.get().write().unwrap();
if !config.room_map.contains_key(&group_id) {
// print all messages in the group list
// for message in proto_conversation.message_list {
// if let Ok(ss) = Self::analyze_content(&message.content) {
// print! {"{} | ", message.index};
// match proto::MessageStatus::from_i32(message.status).unwrap() {
// proto::MessageStatus::Sending => print!(".. | "),
// proto::MessageStatus::Sent => print!("✓. | "),
// proto::MessageStatus::Confirmed => print!("✓✓ | "),
// proto::MessageStatus::ConfirmedByAll => print!("✓✓✓| "),
// proto::MessageStatus::Receiving => print!("🚚 | "),
// proto::MessageStatus::Received => print!("📨 | "),
// }

// print!("{} | ", message.sent_at);
// println!("{}", bs58::encode(message.sender_id).into_string());
// println!(
// " [{}] {}",
// bs58::encode(message.message_id).into_string(),
// message.received_at
// );

// for s in ss {
// // This part does not have any mapping with matrix room.
// // Show and navigate with help command.
// QaulMenu::help(group_id_byte.clone());
// println!("\t{}", s);
// }
// println!("");
// }
// }
println!("No Mapping found");
} else {
let matrix_room = config.room_map.get_mut(&group_id).unwrap();
Expand Down Expand Up @@ -431,14 +278,6 @@ impl Chat {
}
}

impl QaulMenu {
pub fn help(group_id: Vec<u8>) {
let msg = format!("/users : To know all the existing users in matrix room.\n/invite : To invite users into the existing room.");
Chat::send_chat_message(group_id, msg);
//
}
}

fn send_file_to_matrix(file_path: String, room_id: &RoomId, extension: String, file_name: String) {
let path = std::env::current_dir().unwrap();
let mut storage_path = path.as_path().to_str().unwrap().to_string();
Expand Down
Loading

0 comments on commit a8451cd

Please sign in to comment.