nle-cloud-sdk 0.1.1

Unofficial asynchronous Rust SDK for the NLECloud (新大陆物联网云平台) IoT platform
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
use crate::utils::flexible_datetime_opt;
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};

/// Telemetry data point (`SensorDataPointDTO`).
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct SensorDataPointDto {
    /// Sensor value (can be integer, float, string, boolean, or base64 binary).
    pub value: serde_json::Value,

    /// Time the value was recorded.
    #[serde(default, with = "flexible_datetime_opt")]
    pub record_time: Option<NaiveDateTime>,
}

impl SensorDataPointDto {
    /// Creates a data point with the specified value (record time defaults to server receipt time).
    pub fn new(value: impl Into<serde_json::Value>) -> Self {
        Self {
            value: value.into(),
            record_time: None,
        }
    }

    /// Creates a data point with an explicit recording timestamp.
    pub fn with_time(value: impl Into<serde_json::Value>, record_time: NaiveDateTime) -> Self {
        Self {
            value: value.into(),
            record_time: Some(record_time),
        }
    }
}

/// Base information for any sensor or actuator (`SensorBaseInfoDTO`).
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct SensorBaseInfoDto {
    /// Sensor tag (unique within device).
    pub api_tag: String,

    /// Group type (1: Sensor, 2: Actuator, 3: Camera, 4: LED).
    pub groups: u8,

    /// Communication protocol (0: Unknown, 1: Modbus, 2: Zigbee, 3: TCP, 4: UDP).
    pub protocol: u8,

    /// Sensor display name.
    pub name: Option<String>,

    /// Creation timestamp.
    #[serde(default, with = "flexible_datetime_opt")]
    pub create_date: Option<NaiveDateTime>,

    /// Transmission type (0: Report only, 1: Report and control, 2: Alarm, 3: Fault).
    pub trans_type: u8,

    /// Data type (0: Integer, 1: Float, 2: Boolean, 3: String, 4: Enum, 5: Binary).
    pub data_type: u8,

    /// Additional type attributes (e.g. enum options separated by commas).
    pub type_attrs: Option<serde_json::Value>,

    /// Associated device ID.
    #[serde(rename = "DeviceID", alias = "DeviceId")]
    pub device_id: i32,

    /// Sensor type category (e.g. "temperature", "humidity", "light").
    pub sensor_type: Option<String>,

    /// Latest reported value.
    #[serde(default)]
    pub value: serde_json::Value,

    /// Timestamp of the latest reported value.
    #[serde(default, with = "flexible_datetime_opt")]
    pub record_time: Option<NaiveDateTime>,
}

/// Extended sensor details (`SensorInfoDTO`).
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct SensorInfoDto {
    /// Base sensor fields.
    #[serde(flatten)]
    pub base: SensorBaseInfoDto,

    /// Engineering unit (e.g. "℃", "%").
    pub unit: Option<String>,
}

/// Extended actuator details (`ActuatorInfoDTO`).
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct ActuatorInfoDto {
    /// Base sensor fields.
    #[serde(flatten)]
    pub base: SensorBaseInfoDto,

    /// Operation type (1: Switch, 2: Switch/Stop, 3: Button, 4: Scale).
    pub oper_type: Option<u8>,

    /// Operation type attributes (e.g. JSON bounds for scale: `{"MaxRange": 180, ...}`).
    pub oper_type_attrs: Option<serde_json::Value>,
}

/// Extended camera details (`CameraInfoDTO`).
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct CameraInfoDto {
    /// Base sensor fields.
    #[serde(flatten)]
    pub base: SensorBaseInfoDto,

    /// Camera IP address.
    pub http_ip: Option<String>,

    /// Camera HTTP port.
    pub http_port: Option<i32>,

    /// Camera login username.
    pub user_name: Option<String>,

    /// Camera login password.
    pub password: Option<String>,
}

/// Payload for adding or updating a sensor (`SensorAddUpdate`).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct SensorAddUpdate {
    /// Sensor display name (2 to 10 characters).
    pub name: String,

    /// Unique sensor tag within device.
    pub api_tag: String,

    /// Transmission type (0: Report only, 1: Report & control, 2: Alarm, 3: Fault).
    #[serde(default)]
    pub trans_type: u8,

    /// Data type (0: Integer, 1: Float, 2: Boolean, 3: String, 4: Enum, 5: Binary).
    #[serde(default)]
    pub data_type: u8,

    /// Additional type attributes.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub type_attrs: Option<String>,

    /// Engineering unit (e.g. "C", "RH%").
    #[serde(skip_serializing_if = "Option::is_none")]
    pub unit: Option<String>,

    /// Decimal precision.
    #[serde(default)]
    pub precision: u8,
}

impl SensorAddUpdate {
    /// Creates a sensor creation payload with default telemetry reporting settings.
    ///
    /// - `name`: Sensor display name (2 to 10 characters).
    /// - `api_tag`: Unique identifier tag within device.
    pub fn new(name: impl Into<String>, api_tag: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            api_tag: api_tag.into(),
            trans_type: 0,
            data_type: 0,
            type_attrs: None,
            unit: None,
            precision: 2,
        }
    }
}

/// Payload for adding or updating an actuator (`ActuatorAddUpdate`).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct ActuatorAddUpdate {
    /// Actuator display name.
    pub name: String,

    /// Unique actuator tag within device.
    pub api_tag: String,

    /// Transmission type.
    #[serde(default = "default_one")]
    pub trans_type: u8,

    /// Data type.
    #[serde(default)]
    pub data_type: u8,

    /// Additional type attributes.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub type_attrs: Option<String>,

    /// Operation type (1: Switch, 2: Switch/Stop, 3: Button, 4: Scale).
    pub oper_type: u8,

    /// Operation type attributes (e.g. JSON string).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub oper_type_attrs: Option<String>,

    /// Serial number / order index.
    #[serde(default)]
    pub serial_number: i32,
}

fn default_one() -> u8 {
    1
}

impl ActuatorAddUpdate {
    /// Creates an actuator creation payload.
    ///
    /// - `name`: Actuator display name.
    /// - `api_tag`: Unique actuator identifier tag within device.
    /// - `oper_type`: Operation type (1: Switch, 2: Switch/Stop, 3: Button, 4: Scale).
    pub fn new(name: impl Into<String>, api_tag: impl Into<String>, oper_type: u8) -> Self {
        Self {
            name: name.into(),
            api_tag: api_tag.into(),
            trans_type: 1,
            data_type: 0,
            type_attrs: None,
            oper_type,
            oper_type_attrs: None,
            serial_number: 0,
        }
    }
}

/// Payload for adding or updating a camera (`CameraAddUpdate`).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct CameraAddUpdate {
    /// Camera display name.
    pub name: String,

    /// Unique camera tag within device.
    pub api_tag: String,

    /// Transmission type.
    #[serde(default)]
    pub trans_type: u8,

    /// Data type.
    #[serde(default)]
    pub data_type: u8,

    /// Additional type attributes.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub type_attrs: Option<String>,

    /// Camera IP address.
    pub http_ip: String,

    /// Camera HTTP port.
    pub http_port: i32,

    /// Camera login username.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_name: Option<String>,

    /// Camera login password.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub password: Option<String>,
}

/// Telemetry item with tag and value (`SensorDataDTO`).
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct SensorDataDto {
    /// Sensor tag.
    pub api_tag: String,

    /// Sensor value.
    pub value: serde_json::Value,

    /// Recording timestamp.
    #[serde(default, with = "flexible_datetime_opt")]
    pub record_time: Option<NaiveDateTime>,
}

/// Ingest payload container for telemetry points (`SensorDataListAddDTO`).
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct SensorDataListAddDto {
    /// List of telemetry series per sensor tag.
    #[serde(rename = "DatasDTO", alias = "DatasDto")]
    pub datas_dto: Vec<SensorDataAddDto>,
}

impl SensorDataListAddDto {
    /// Wraps a list of sensor telemetry series into an upload container.
    pub fn new(datas: Vec<SensorDataAddDto>) -> Self {
        Self { datas_dto: datas }
    }
}

/// Telemetry series for a specific sensor (`SensorDataAddDTO`).
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct SensorDataAddDto {
    /// Target sensor tag.
    pub api_tag: String,

    /// List of data points.
    #[serde(rename = "PointDTO", alias = "PointDto")]
    pub point_dto: Vec<SensorDataPointDto>,
}

impl SensorDataAddDto {
    /// Creates a telemetry series for a specific sensor tag.
    pub fn new(api_tag: impl Into<String>, points: Vec<SensorDataPointDto>) -> Self {
        Self {
            api_tag: api_tag.into(),
            point_dto: points,
        }
    }
}

/// Parameters for querying telemetry data points (`SensorDataFuzzyQryPagingParas`).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct SensorDataFuzzyQryPagingParas {
    /// Device identifier.
    #[serde(rename = "DeviceID", alias = "DeviceId")]
    pub device_id: i32,

    /// Comma-separated sensor tags (max 50).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub api_tags: Option<String>,

    /// Query method (1: min, 2: hour, 3: day, 4: week, 5: month, 6: explicit date range). Defaults to 6.
    #[serde(default = "default_method")]
    pub method: i32,

    /// Time ago value paired with method.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub time_ago: Option<f64>,

    /// Start date (YYYY-MM-DD HH:mm:ss).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_date: Option<String>,

    /// End date (YYYY-MM-DD HH:mm:ss).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub end_date: Option<String>,

    /// Sort direction ("DESC" or "ASC"). Defaults to "DESC".
    #[serde(default = "default_sort_desc")]
    pub sort: String,

    /// Items per page (default 1000, max 3000).
    #[serde(default = "default_page_size_1000")]
    pub page_size: i32,

    /// 1-based page index.
    #[serde(default = "default_one_i32")]
    pub page_index: i32,
}

fn default_method() -> i32 {
    6
}
fn default_sort_desc() -> String {
    "DESC".to_string()
}
fn default_page_size_1000() -> i32 {
    1000
}
fn default_one_i32() -> i32 {
    1
}

impl SensorDataFuzzyQryPagingParas {
    /// Creates query parameters with default descending sort and max page size for the device.
    pub fn new(device_id: i32) -> Self {
        Self {
            device_id,
            api_tags: None,
            method: 6,
            time_ago: None,
            start_date: None,
            end_date: None,
            sort: "DESC".to_string(),
            page_size: 1000,
            page_index: 1,
        }
    }
}

/// Telemetry dataset container (`SensorDataInfoDTO`).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct SensorDataInfoDto {
    /// Number of points returned.
    pub count: i32,

    /// Associated device ID.
    #[serde(rename = "DeviceId", alias = "DeviceID")]
    pub device_id: i32,

    /// Telemetry series points.
    #[serde(default)]
    pub data_points: Option<Vec<SensorDataAddDto>>,
}

/// Paginated telemetry dataset (`SensorDataPageDTO`).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct SensorDataPageDto {
    /// Number of points in this page.
    pub count: i32,

    /// Associated device ID.
    #[serde(rename = "DeviceId", alias = "DeviceID")]
    pub device_id: i32,

    /// Telemetry series points.
    #[serde(default)]
    pub data_points: Option<Vec<SensorDataAddDto>>,

    /// Total number of pages.
    pub page_count: i32,

    /// Current page index.
    pub page_index: i32,

    /// Total number of records.
    pub record_count: i32,
}

/// Parameters for aggregated telemetry queries (`SensorDataJuHeQryPagingParas`).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct SensorDataJuHeQryPagingParas {
    /// Device identifier.
    #[serde(rename = "DeviceID", alias = "DeviceId")]
    pub device_id: i32,

    /// Comma-separated sensor tags (max 50).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub api_tags: Option<String>,

    /// Grouping level (1: minute, 2: hour, 3: day, 4: month). Defaults to 2.
    #[serde(default = "default_group_by")]
    pub group_by: i32,

    /// Aggregation function ("MAX", "MIN", "COUNT"). Defaults to "MAX".
    #[serde(default = "default_func_max")]
    pub func: String,

    /// Start date (YYYY-MM-DD HH:mm:ss).
    pub start_date: String,

    /// End date (YYYY-MM-DD HH:mm:ss).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub end_date: Option<String>,
}

fn default_group_by() -> i32 {
    2
}
fn default_func_max() -> String {
    "MAX".to_string()
}

impl SensorDataJuHeQryPagingParas {
    /// Creates aggregation query parameters for a device starting from `start_date`.
    pub fn new(device_id: i32, start_date: impl Into<String>) -> Self {
        Self {
            device_id,
            api_tags: None,
            group_by: 2,
            func: "MAX".to_string(),
            start_date: start_date.into(),
            end_date: None,
        }
    }
}