1use std::sync::Arc;
2use cyfs_lib::SharedCyfsStack;
3use cyfs_base::*;
4use crate::{DSGJSON, JSONObject};
5use crate::{CreatePullTask, CreatePushTask, DsgPullTaskState, DsgPushTaskState, GetTaskList, JsonProtocol, DsgRecoveryTaskState, ResultList, SetDMCAccount, StoreType};
6use crate::{CyfsPath, SharedCyfsStackEx};
7
8#[derive(Clone)]
9pub struct DmcDsg {
10 stack: Arc<SharedCyfsStack>,
11 dec_id: ObjectId,
12 owner_id: ObjectId,
13}
14
15impl DmcDsg {
16 pub fn new(stack: Arc<SharedCyfsStack>, dec_id: ObjectId) -> Self {
17 let owner_id = stack.local_device().desc().owner().as_ref().unwrap().clone();
18 Self {
19 stack,
20 dec_id,
21 owner_id
22 }
23 }
24
25 pub async fn set_dmc_account(&self, dmc_account: &str, light_private_key: &str) -> BuckyResult<()> {
26 let req = JSONObject::new(
27 self.dec_id.clone(),
28 self.owner_id,
29 JsonProtocol::SetDMCAccount as u16,
30 &SetDMCAccount {
31 dmc_account: dmc_account.to_string(),
32 dmc_key: light_private_key.to_string()
33 })?;
34 let req_id = req.desc().calculate_id();
35 let req_path = CyfsPath::new(
36 self.stack.local_device_id().object_id().clone(),
37 self.dec_id.clone(),
38 "dmc_account_commands").to_path();
39 let _: JSONObject = self.stack.put_object_with_resp2(req_path.as_str(), req_id, req.to_vec()?).await?;
40 Ok(())
41 }
42
43 pub async fn get_dmc_account(&self) -> BuckyResult<String> {
44 let req = JSONObject::new(
45 self.dec_id.clone(),
46 self.owner_id,
47 JsonProtocol::GetDMCAccount as u16,
48 &"".to_string()
49 )?;
50 let req_id = req.desc().calculate_id();
51 let req_path = CyfsPath::new(
52 self.stack.local_device_id().object_id().clone(),
53 self.dec_id.clone(),
54 "commands").to_path();
55 let resp: JSONObject = self.stack.put_object_with_resp2(req_path.as_str(), req_id, req.to_vec()?).await?;
56 Ok(resp.get()?)
57 }
58
59 pub async fn create_push_task(
60 &self,
61 chunk_list: Vec<ChunkId>,
62 http_url: Option<String>,
63 store_type: StoreType) -> BuckyResult<String> {
64 let req = JSONObject::new(
65 self.dec_id.clone(),
66 self.owner_id,
67 JsonProtocol::CreatePullTask as u16,
68 &CreatePushTask {
69 chunk_list: chunk_list.iter().map(|id| id.to_string()).collect(),
70 http_url,
71 store_type,
72 dmc_account: None
73 })?;
74 let req_id = req.desc().calculate_id();
75 let req_path = CyfsPath::new(
76 self.stack.local_device_id().object_id().clone(),
77 self.dec_id.clone(),
78 "commands").to_path();
79 let resp: JSONObject = self.stack.put_object_with_resp2(req_path.as_str(), req_id, req.to_vec()?).await?;
80 Ok(resp.get()?)
81 }
82
83 pub async fn get_push_task_state(&self, task_id: &str) -> BuckyResult<DsgPushTaskState> {
84 let req = JSONObject::new(
85 self.dec_id.clone(),
86 self.owner_id,
87 JsonProtocol::GetPushTaskState as u16,
88 &task_id.to_string())?;
89 let req_id = req.desc().calculate_id();
90 let req_path = CyfsPath::new(
91 self.stack.local_device_id().object_id().clone(),
92 self.dec_id.clone(),
93 "commands").to_path();
94 let resp: JSONObject = self.stack.put_object_with_resp2(req_path.as_str(), req_id, req.to_vec()?).await?;
95 Ok(resp.get()?)
96 }
97
98 pub async fn create_pull_task(&self, task_id: &str) -> BuckyResult<String> {
99 let req = JSONObject::new(
100 self.dec_id.clone(),
101 self.owner_id,
102 JsonProtocol::CreatePullTask as u16,
103 &CreatePullTask {
104 task_id: task_id.to_string()
105 })?;
106 let req_id = req.desc().calculate_id();
107 let req_path = CyfsPath::new(
108 self.stack.local_device_id().object_id().clone(),
109 self.dec_id.clone(),
110 "commands").to_path();
111 let resp: JSONObject = self.stack.put_object_with_resp2(req_path.as_str(), req_id, req.to_vec()?).await?;
112 Ok(resp.get()?)
113 }
114
115 pub async fn get_pull_task_state(&self, task_id: &str) -> BuckyResult<DsgPullTaskState> {
116 let req = JSONObject::new(
117 self.dec_id.clone(),
118 self.owner_id,
119 JsonProtocol::GetPullTaskState as u16,
120 &task_id.to_string())?;
121 let req_id = req.desc().calculate_id();
122 let req_path = CyfsPath::new(
123 self.stack.local_device_id().object_id().clone(),
124 self.dec_id.clone(),
125 "commands").to_path();
126 let resp: JSONObject = self.stack.put_object_with_resp2(req_path.as_str(), req_id, req.to_vec()?).await?;
127 Ok(resp.get()?)
128 }
129
130 pub async fn create_recovery_task(&self) -> BuckyResult<String> {
131 let req = JSONObject::new(
132 self.dec_id.clone(),
133 self.owner_id,
134 JsonProtocol::Recovery as u16,
135 &"".to_string())?;
136 let req_id = req.desc().calculate_id();
137 let req_path = CyfsPath::new(
138 self.stack.local_device_id().object_id().clone(),
139 self.dec_id.clone(),
140 "dmc_account_commands").to_path();
141 let resp: JSONObject = self.stack.put_object_with_resp2(req_path.as_str(), req_id, req.to_vec()?).await?;
142 Ok(resp.get()?)
143 }
144
145 pub async fn get_recovery_task_state(&self, task_id: &str) -> BuckyResult<DsgRecoveryTaskState> {
146 let req = JSONObject::new(
147 self.dec_id.clone(),
148 self.owner_id,
149 JsonProtocol::GetRecoveryState as u16,
150 &task_id.to_string())?;
151 let req_id = req.desc().calculate_id();
152 let req_path = CyfsPath::new(
153 self.stack.local_device_id().object_id().clone(),
154 self.dec_id.clone(),
155 "dmc_account_commands").to_path();
156 let resp: JSONObject = self.stack.put_object_with_resp2(req_path.as_str(), req_id, req.to_vec()?).await?;
157 Ok(resp.get()?)
158 }
159
160 pub async fn get_task_list(&self, offset: u64, length: u64) -> BuckyResult<ResultList<Vec<String>>> {
161 let req = JSONObject::new(
162 self.dec_id.clone(),
163 self.owner_id,
164 JsonProtocol::GetTaskList as u16,
165 &GetTaskList {
166 offset,
167 length
168 })?;
169 let req_id = req.desc().calculate_id();
170 let req_path = CyfsPath::new(
171 self.stack.local_device_id().object_id().clone(),
172 self.dec_id.clone(),
173 "commands").to_path();
174 let resp: JSONObject = self.stack.put_object_with_resp2(req_path.as_str(), req_id, req.to_vec()?).await?;
175 Ok(resp.get()?)
176 }
177}