use std::sync::Arc;
use cyfs_lib::SharedCyfsStack;
use cyfs_base::*;
use crate::{DSGJSON, JSONObject};
use crate::{CreatePullTask, CreatePushTask, DsgPullTaskState, DsgPushTaskState, GetTaskList, JsonProtocol, DsgRecoveryTaskState, ResultList, SetDMCAccount, StoreType};
use crate::{CyfsPath, SharedCyfsStackEx};
#[derive(Clone)]
pub struct DmcDsg {
stack: Arc<SharedCyfsStack>,
dec_id: ObjectId,
owner_id: ObjectId,
}
impl DmcDsg {
pub fn new(stack: Arc<SharedCyfsStack>, dec_id: ObjectId) -> Self {
let owner_id = stack.local_device().desc().owner().as_ref().unwrap().clone();
Self {
stack,
dec_id,
owner_id
}
}
pub async fn set_dmc_account(&self, dmc_account: &str, light_private_key: &str) -> BuckyResult<()> {
let req = JSONObject::new(
self.dec_id.clone(),
self.owner_id,
JsonProtocol::SetDMCAccount as u16,
&SetDMCAccount {
dmc_account: dmc_account.to_string(),
dmc_key: light_private_key.to_string()
})?;
let req_id = req.desc().calculate_id();
let req_path = CyfsPath::new(
self.stack.local_device_id().object_id().clone(),
self.dec_id.clone(),
"dmc_account_commands").to_path();
let _: JSONObject = self.stack.put_object_with_resp2(req_path.as_str(), req_id, req.to_vec()?).await?;
Ok(())
}
pub async fn get_dmc_account(&self) -> BuckyResult<String> {
let req = JSONObject::new(
self.dec_id.clone(),
self.owner_id,
JsonProtocol::GetDMCAccount as u16,
&"".to_string()
)?;
let req_id = req.desc().calculate_id();
let req_path = CyfsPath::new(
self.stack.local_device_id().object_id().clone(),
self.dec_id.clone(),
"commands").to_path();
let resp: JSONObject = self.stack.put_object_with_resp2(req_path.as_str(), req_id, req.to_vec()?).await?;
Ok(resp.get()?)
}
pub async fn create_push_task(
&self,
chunk_list: Vec<ChunkId>,
http_url: Option<String>,
store_type: StoreType) -> BuckyResult<String> {
let req = JSONObject::new(
self.dec_id.clone(),
self.owner_id,
JsonProtocol::CreatePullTask as u16,
&CreatePushTask {
chunk_list: chunk_list.iter().map(|id| id.to_string()).collect(),
http_url,
store_type,
dmc_account: None
})?;
let req_id = req.desc().calculate_id();
let req_path = CyfsPath::new(
self.stack.local_device_id().object_id().clone(),
self.dec_id.clone(),
"commands").to_path();
let resp: JSONObject = self.stack.put_object_with_resp2(req_path.as_str(), req_id, req.to_vec()?).await?;
Ok(resp.get()?)
}
pub async fn get_push_task_state(&self, task_id: &str) -> BuckyResult<DsgPushTaskState> {
let req = JSONObject::new(
self.dec_id.clone(),
self.owner_id,
JsonProtocol::GetPushTaskState as u16,
&task_id.to_string())?;
let req_id = req.desc().calculate_id();
let req_path = CyfsPath::new(
self.stack.local_device_id().object_id().clone(),
self.dec_id.clone(),
"commands").to_path();
let resp: JSONObject = self.stack.put_object_with_resp2(req_path.as_str(), req_id, req.to_vec()?).await?;
Ok(resp.get()?)
}
pub async fn create_pull_task(&self, task_id: &str) -> BuckyResult<String> {
let req = JSONObject::new(
self.dec_id.clone(),
self.owner_id,
JsonProtocol::CreatePullTask as u16,
&CreatePullTask {
task_id: task_id.to_string()
})?;
let req_id = req.desc().calculate_id();
let req_path = CyfsPath::new(
self.stack.local_device_id().object_id().clone(),
self.dec_id.clone(),
"commands").to_path();
let resp: JSONObject = self.stack.put_object_with_resp2(req_path.as_str(), req_id, req.to_vec()?).await?;
Ok(resp.get()?)
}
pub async fn get_pull_task_state(&self, task_id: &str) -> BuckyResult<DsgPullTaskState> {
let req = JSONObject::new(
self.dec_id.clone(),
self.owner_id,
JsonProtocol::GetPullTaskState as u16,
&task_id.to_string())?;
let req_id = req.desc().calculate_id();
let req_path = CyfsPath::new(
self.stack.local_device_id().object_id().clone(),
self.dec_id.clone(),
"commands").to_path();
let resp: JSONObject = self.stack.put_object_with_resp2(req_path.as_str(), req_id, req.to_vec()?).await?;
Ok(resp.get()?)
}
pub async fn create_recovery_task(&self) -> BuckyResult<String> {
let req = JSONObject::new(
self.dec_id.clone(),
self.owner_id,
JsonProtocol::Recovery as u16,
&"".to_string())?;
let req_id = req.desc().calculate_id();
let req_path = CyfsPath::new(
self.stack.local_device_id().object_id().clone(),
self.dec_id.clone(),
"dmc_account_commands").to_path();
let resp: JSONObject = self.stack.put_object_with_resp2(req_path.as_str(), req_id, req.to_vec()?).await?;
Ok(resp.get()?)
}
pub async fn get_recovery_task_state(&self, task_id: &str) -> BuckyResult<DsgRecoveryTaskState> {
let req = JSONObject::new(
self.dec_id.clone(),
self.owner_id,
JsonProtocol::GetRecoveryState as u16,
&task_id.to_string())?;
let req_id = req.desc().calculate_id();
let req_path = CyfsPath::new(
self.stack.local_device_id().object_id().clone(),
self.dec_id.clone(),
"dmc_account_commands").to_path();
let resp: JSONObject = self.stack.put_object_with_resp2(req_path.as_str(), req_id, req.to_vec()?).await?;
Ok(resp.get()?)
}
pub async fn get_task_list(&self, offset: u64, length: u64) -> BuckyResult<ResultList<Vec<String>>> {
let req = JSONObject::new(
self.dec_id.clone(),
self.owner_id,
JsonProtocol::GetTaskList as u16,
&GetTaskList {
offset,
length
})?;
let req_id = req.desc().calculate_id();
let req_path = CyfsPath::new(
self.stack.local_device_id().object_id().clone(),
self.dec_id.clone(),
"commands").to_path();
let resp: JSONObject = self.stack.put_object_with_resp2(req_path.as_str(), req_id, req.to_vec()?).await?;
Ok(resp.get()?)
}
}