Skip to content

Commit

Permalink
Merge pull request #181 from nodecross/get-device-info
Browse files Browse the repository at this point in the history
Add send_device_info
  • Loading branch information
Dongri Jin authored Sep 7, 2023
2 parents 5036766 + c0b716c commit 3709a9a
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 2 deletions.
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ rumqttc = { version = "0.20.0" }
cuid = { version = "1.3.1" }
shadow-rs = "0.21.0"
dotenv = "0.15.0"
mac_address = "1.1.5"

[dev-dependencies]
rstest = { version = "0.17.0" }
Expand Down
24 changes: 24 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use crate::config::ServerConfig;
use crate::network::Network;
use dotenv::dotenv;
use handlers::Command;
use mac_address::get_mac_address;
use std::env;

mod config;
mod controllers;
Expand Down Expand Up @@ -180,6 +182,7 @@ async fn main() -> std::io::Result<()> {

// NOTE: hub initilize
hub_initilize(did.did_document.id.clone()).await;
send_device_info(did.did_document.id.clone()).await;

let sock_path = runtime_dir.clone().join("nodex.sock");

Expand Down Expand Up @@ -323,6 +326,27 @@ async fn hub_initilize(did: String) {
};
}

async fn send_device_info(did: String) {
const VERSION: &str = env!("CARGO_PKG_VERSION");
const OS: &str = env::consts::OS;
let mac_address: String = match get_mac_address() {
Ok(Some(ma)) => ma.to_string(),
_ => String::from("No MAC address found."),
};

let hub = Hub::new();
match hub
.send_device_info(did, mac_address, VERSION.to_string(), OS.to_string())
.await
{
Ok(()) => (),
Err(e) => {
log::error!("{:?}", e);
panic!()
}
};
}

use env_logger::fmt::Color;
use log::Level;

Expand Down
50 changes: 48 additions & 2 deletions src/nodex/utils/hub_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use reqwest::{
};
use sha2::Sha256;

use crate::services::internal::didcomm_encrypted::DIDCommEncryptedService;
use serde_json::json;

type HmacSha256 = Hmac<Sha256>;

pub struct HubClientConfig {
Expand Down Expand Up @@ -82,8 +85,8 @@ impl HubClient {
}
}

pub async fn post(&self, _path: &str, body: &str) -> Result<reqwest::Response, NodeXError> {
let url = self.base_url.join(_path);
pub async fn post(&self, path: &str, body: &str) -> Result<reqwest::Response, NodeXError> {
let url = self.base_url.join(path);
let headers = self.auth_headers(body.to_string());
if let Err(e) = headers {
log::error!("{:?}", e);
Expand All @@ -105,6 +108,49 @@ impl HubClient {
}
}

pub async fn send_device_info(
&self,
path: &str,
to_did: &str,
mac_address: &str,
version: &str,
os: &str,
) -> Result<reqwest::Response, NodeXError> {
let message = json!({
"mac_address": mac_address,
"version": version,
"os": os,
});
let payload = match DIDCommEncryptedService::generate(to_did, &json!(message), None).await {
Ok(v) => v,
Err(e) => {
log::error!("{:?}", e);
return Err(NodeXError {});
}
};
let payload = match serde_json::to_string(&payload) {
Ok(v) => v,
Err(e) => {
log::error!("{:?}", e);
return Err(NodeXError {});
}
};
let url = self.base_url.join(path);
match self
.instance
.post(&url.unwrap().to_string())
.body(payload.to_string())
.send()
.await
{
Ok(v) => Ok(v),
Err(e) => {
log::error!("{:?}", e);
Err(NodeXError {})
}
}
}

#[allow(dead_code)]
pub async fn put(&self, _path: &str) -> Result<reqwest::Response, NodeXError> {
let url = self.base_url.join(_path);
Expand Down
35 changes: 35 additions & 0 deletions src/services/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ struct RegisterDeviceRequest {
project_id: String,
}

#[derive(Debug, Serialize, Deserialize)]
struct SendDeviceInfoRequest {
device_did: String,
mac_address: String,
version: String,
os: String,
}

impl Hub {
pub fn new() -> Self {
let server_config = server_config();
Expand Down Expand Up @@ -59,4 +67,31 @@ impl Hub {
}
}
}

pub async fn send_device_info(
&self,
to_did: String,
mac_address: String,
version: String,
os: String,
) -> Result<(), NodeXError> {
let res = match self
.http_client
.send_device_info("/v1/device_info", &to_did, &mac_address, &version, &os)
.await
{
Ok(v) => v,
Err(e) => {
log::error!("{:?}", e);
return Err(NodeXError {});
}
};
match res.json::<EmptyResponse>().await {
Ok(_) => Ok(()),
Err(e) => {
log::error!("{:?}", e);
Err(NodeXError {})
}
}
}
}

0 comments on commit 3709a9a

Please sign in to comment.