aiot 0.10.0

Rust Link SDK (Aliyun IoT) based on Alink Protocol.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use crate::alink::{global_id_next, AlinkRequest, AlinkResponse, SysAck, ALINK_VERSION};
use crate::subdev::base::*;
use serde::{Deserialize, Serialize};

pub type SubDevLogin = DeviceInfo;

#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SubDevBatchLoginParams {
    pub device_list: Vec<DeviceInfo>,
}

#[derive(Clone)]
pub struct LoginParam {
    pub product_key: String,
    pub device_name: String,
    pub device_secret: String,
    pub clean_session: bool,
}

impl super::Module {
    /// 子设备上线
    ///
    /// # 参数
    ///
    /// * `login_param` - 子设备信息
    pub async fn login(&self, login_param: LoginParam) -> crate::Result<()> {
        let payload = SubDevLoginRequest {
            id: global_id_next().to_string(),
            params: DeviceInfo::new(
                login_param.product_key,
                login_param.device_name,
                Some(login_param.clean_session),
                login_param.device_secret,
                None,
            ),
            version: ALINK_VERSION.to_string(),
            sys: None,
            method: None,
        };
        self.publish(
            format!(
                "/ext/session/{}/{}/combine/login",
                self.three.product_key, self.three.device_name
            ),
            &payload,
        )
        .await
    }

    /// 子设备批量上线
    ///
    /// # 参数
    ///
    /// * `login_params` - 子设备信息数组
    pub async fn batch_login(&self, login_params: &[LoginParam]) -> crate::Result<()> {
        let payload = SubDevBatchLoginRequest {
            id: global_id_next().to_string(),
            params: SubDevBatchLoginParams {
                device_list: login_params
                    .iter()
                    .map(|n| {
                        DeviceInfo::new(
                            n.product_key.clone(),
                            n.device_name.clone(),
                            Some(n.clean_session),
                            n.device_secret.clone(),
                            None,
                        )
                    })
                    .collect(),
            },
            version: ALINK_VERSION.to_string(),
            sys: None,
            method: None,
        };
        self.publish(
            format!(
                "/ext/session/{}/{}/combine/batch_login",
                self.three.product_key, self.three.device_name
            ),
            &payload,
        )
        .await
    }

    /// 子设备下线
    ///
    /// * `device_info` - 子设备信息
    pub async fn logout(&self, device_info: DeviceInfoId) -> crate::Result<()> {
        let payload = SubDevLogoutRequest {
            id: global_id_next().to_string(),
            params: device_info,
            version: ALINK_VERSION.to_string(),
            sys: None,
            method: None,
        };
        self.publish(
            format!(
                "/ext/session/{}/{}/combine/logout",
                self.three.product_key, self.three.device_name
            ),
            &payload,
        )
        .await
    }

    /// 子设备批量下线
    ///
    /// # 参数
    ///
    /// * `device_infos` - 子设备信息数组
    pub async fn batch_logout(&self, device_infos: &[DeviceInfoId]) -> crate::Result<()> {
        let payload = SubDevBatchLogoutRequest {
            id: global_id_next().to_string(),
            params: device_infos.to_vec(),
            version: ALINK_VERSION.to_string(),
            sys: None,
            method: None,
        };
        self.publish(
            format!(
                "/ext/session/{}/{}/combine/batch_logout",
                self.three.product_key, self.three.device_name
            ),
            &payload,
        )
        .await
    }

    /// 添加拓扑关系
    ///
    /// # 参数
    ///
    /// * `device_infos` - 子设备信息数组
    /// * `ack` - 是否需要响应
    pub async fn add_topological_relation(
        &self,
        device_infos: &[DeviceInfoWithSecret],
        ack: bool,
    ) -> crate::Result<()> {
        let payload = SubDevAddTopologicalRelationRequest {
            id: global_id_next().to_string(),
            version: String::from(ALINK_VERSION),
            params: device_infos
                .iter()
                .map(|n| {
                    DeviceInfoNoCS::new(
                        n.product_key.clone(),
                        n.device_name.clone(),
                        n.device_secret.clone(),
                        None,
                    )
                })
                .collect::<Vec<DeviceInfoNoCS>>(),
            sys: Some(SysAck { ack: ack.into() }),
            method: Some(String::from("thing.topo.add")),
        };
        self.publish(
            format!(
                "/sys/{}/{}/thing/topo/add",
                self.three.product_key, self.three.device_name
            ),
            &payload,
        )
        .await
    }

    /// 删除拓扑关系
    ///
    /// # 参数
    ///
    /// * `device_infos` - 子设备信息数组
    /// * `ack` - 是否需要响应
    pub async fn delete_topological_relation(
        &self,
        device_infos: &[DeviceInfoId],
        ack: bool,
    ) -> crate::Result<()> {
        let payload = SubDevDeleteTopologicalRelationRequest {
            id: global_id_next().to_string(),
            version: String::from(ALINK_VERSION),
            params: device_infos.to_vec(),
            sys: Some(SysAck { ack: ack.into() }),
            method: Some(String::from("thing.topo.delete")),
        };
        self.publish(
            format!(
                "/sys/{}/{}/thing/topo/delete",
                self.three.product_key, self.three.device_name
            ),
            &payload,
        )
        .await
    }

    /// 获取拓扑关系
    ///
    /// # 参数
    ///
    /// * `ack` - 是否需要响应
    pub async fn get_topological_relation(&self, ack: bool) -> crate::Result<()> {
        let payload = SubDevGetTopologicalRelationRequest {
            id: global_id_next().to_string(),
            version: String::from(ALINK_VERSION),
            sys: Some(SysAck { ack: ack.into() }),
            method: Some(String::from("thing.topo.get")),
            params: None,
        };
        self.publish(
            format!(
                "/sys/{}/{}/thing/topo/get",
                self.three.product_key, self.three.device_name
            ),
            &payload,
        )
        .await
    }

    /// 发现设备信息上报
    ///
    /// # 参数
    ///
    /// * `device_infos` - 子设备信息数组
    /// * `ack` - 是否需要响应
    pub async fn found_report(
        &self,
        device_infos: &[DeviceInfoId],
        ack: bool,
    ) -> crate::Result<()> {
        let payload = SubDevFoundReportRequest {
            id: global_id_next().to_string(),
            version: String::from(ALINK_VERSION),
            params: device_infos.to_vec(),
            sys: Some(SysAck { ack: ack.into() }),
            method: Some(String::from("thing.topo.get")),
        };
        self.publish(
            format!(
                "/sys/{}/{}/thing/list/found",
                self.three.product_key, self.three.device_name
            ),
            &payload,
        )
        .await
    }

    /// 动态注册
    ///
    /// # 参数
    ///
    /// * `device_infos` - 子设备信息数组
    /// * `ack` - 是否需要响应
    pub async fn register(&self, device_infos: &[DeviceInfoId], ack: bool) -> crate::Result<()> {
        let payload = SubDevRegisterRequest {
            id: global_id_next().to_string(),
            version: String::from(ALINK_VERSION),
            params: device_infos.to_vec(),
            sys: Some(SysAck { ack: ack.into() }),
            method: Some(String::from("thing.sub.register")),
        };
        self.publish(
            format!(
                "/sys/{}/{}/thing/sub/register",
                self.three.product_key, self.three.device_name
            ),
            &payload,
        )
        .await
    }

    /// 通知网关添加设备拓扑关系
    ///
    /// # 参数
    ///
    /// * `id` - Id
    /// * `code` - code
    pub async fn notify_add_topological_relation(
        &self,
        id: String,
        code: u64,
    ) -> crate::Result<()> {
        let payload = AlinkResponse {
            id,
            code,
            method: None,
            version: None,
            data: (),
            message: None,
        };
        self.publish(
            format!(
                "/sys/{}/{}/thing/topo/add/notify_reply",
                self.three.product_key, self.three.device_name
            ),
            &payload,
        )
        .await
    }

    /// 禁用设备回应
    ///
    /// # 参数
    ///
    /// * `id` - id
    /// * `code` - code
    pub async fn disable_reply(&self, id: String, code: u64) -> crate::Result<()> {
        let payload = AlinkResponse {
            id,
            code,
            data: (),
            message: None,
            method: None,
            version: None,
        };
        self.publish(
            format!(
                "/sys/{}/{}/thing/disable_reply",
                self.three.product_key, self.three.device_name
            ),
            &payload,
        )
        .await
    }

    /// 启用设备回应
    ///
    /// # 参数
    ///
    /// * `id` - id
    /// * `code` - code
    pub async fn enable_reply(&self, id: String, code: u64) -> crate::Result<()> {
        let payload = AlinkResponse {
            id,
            code,
            data: (),
            message: None,
            method: None,
            version: None,
        };
        self.publish(
            format!(
                "/sys/{}/{}/thing/enable_reply",
                self.three.product_key, self.three.device_name
            ),
            &payload,
        )
        .await
    }

    /// 删除设备回应
    ///
    /// # 参数
    ///
    /// * `id` - id
    /// * `code` - code
    pub async fn delete_reply(&self, id: String, code: u64) -> crate::Result<()> {
        let payload = AlinkResponse {
            id,
            code,
            data: (),
            message: None,
            method: None,
            version: None,
        };
        self.publish(
            format!(
                "/sys/{}/{}/thing/delete_reply",
                self.three.product_key, self.three.device_name
            ),
            &payload,
        )
        .await
    }
}

// 子设备上线
// /ext/session/${productKey}/${deviceName}/combine/login
pub type SubDevLoginRequest = AlinkRequest<SubDevLogin>;

// 子设备批量上线
// /ext/session/${productKey}/${deviceName}/combine/batch_login
pub type SubDevBatchLoginRequest = AlinkRequest<SubDevBatchLoginParams>;

// 子设备下线
// /ext/session/{productKey}/{deviceName}/combine/logout
pub type SubDevLogoutRequest = AlinkRequest<DeviceInfoId>;

// 子设备批量下线
// /ext/session/{productKey}/{deviceName}/combine/batch_logout
pub type SubDevBatchLogoutRequest = AlinkRequest<Vec<DeviceInfoId>>;

// 添加拓扑关系
// /sys/{productKey}/{deviceName}/thing/topo/add
pub type SubDevAddTopologicalRelationRequest = AlinkRequest<Vec<DeviceInfoNoCS>>;

// 删除拓扑关系
// /sys/{productKey}/{deviceName}/thing/topo/delete
pub type SubDevDeleteTopologicalRelationRequest = AlinkRequest<Vec<DeviceInfoId>>;

// 获取拓扑关系
// /sys/{productKey}/{deviceName}/thing/topo/get
pub type SubDevGetTopologicalRelationRequest = AlinkRequest;

// 发现设备信息上报
// /sys/{productKey}/{deviceName}/thing/list/found
pub type SubDevFoundReportRequest = AlinkRequest<Vec<DeviceInfoId>>;

// 通知网关添加设备拓扑关系响应
pub type SubDevAddTopologicalRelationNotifyResponse = AlinkRequest;

// 通知网关拓扑关系变化响应
pub type SubDevChangeTopologicalRelationNotifyResponse = AlinkRequest;

// 子设备禁用
pub type SubDevDisableResponse = AlinkRequest;
// 子设备启用
pub type SubDevEnableResponse = AlinkRequest;
// 子设备删除
pub type SubDevDeleteResponse = AlinkRequest;

// 子设备动态注册
pub type SubDevRegisterRequest = AlinkRequest<Vec<DeviceInfoId>>;