sylvia_iot_data/models/
network_dldata.rs

1//! Traits and structs for network downlink data.
2
3use std::error::Error as StdError;
4
5use async_trait::async_trait;
6use chrono::{DateTime, Utc};
7use serde_json::{Map, Value};
8
9/// The item content.
10#[derive(Debug, PartialEq)]
11pub struct NetworkDlData {
12    pub data_id: String,
13    pub proc: DateTime<Utc>,
14    pub publish: DateTime<Utc>,
15    pub resp: Option<DateTime<Utc>>,
16    pub status: i32,
17    pub unit_id: String,
18    pub device_id: String,
19    pub network_code: String,
20    pub network_addr: String,
21    pub profile: String,
22    pub data: String,
23    pub extension: Option<Map<String, Value>>,
24}
25
26/// The sort keys for the list operation.
27pub enum SortKey {
28    Proc,
29    Pub,
30    Resp,
31    NetworkCode,
32    NetworkAddr,
33}
34
35/// The sort condition for the list operation.
36pub struct SortCond {
37    pub key: SortKey,
38    pub asc: bool,
39}
40
41/// The list operation options.
42pub struct ListOptions<'a> {
43    /// The query conditions.
44    pub cond: &'a ListQueryCond<'a>,
45    /// The data offset.
46    pub offset: Option<u64>,
47    /// The maximum number to query.
48    pub limit: Option<u64>,
49    /// The sort conditions.
50    pub sort: Option<&'a [SortCond]>,
51    /// The maximum number items one time the `list()` returns.
52    ///
53    /// Use cursors until reaching `limit` or all data.
54    pub cursor_max: Option<u64>,
55}
56
57/// The query condition to delete item(s).
58#[derive(Default)]
59pub struct QueryCond<'a> {
60    pub unit_id: Option<&'a str>,
61    pub device_id: Option<&'a str>,
62    pub proc_gte: Option<DateTime<Utc>>,
63    pub proc_lte: Option<DateTime<Utc>>,
64}
65
66/// The query condition for the list operation.
67#[derive(Default)]
68pub struct ListQueryCond<'a> {
69    /// To get the specified unit.
70    pub unit_id: Option<&'a str>,
71    /// To get the specified device.
72    pub device_id: Option<&'a str>,
73    /// To get the specified device's network code.
74    pub network_code: Option<&'a str>,
75    /// To get the specified device's network address.
76    pub network_addr: Option<&'a str>,
77    /// To get the specified device/data profile.
78    pub profile: Option<&'a str>,
79    /// To get data greater than and equal to the specified `proc` time.
80    pub proc_gte: Option<DateTime<Utc>>,
81    /// To get data less than and equal to the specified `proc` time.
82    pub proc_lte: Option<DateTime<Utc>>,
83    /// To get data greater than and equal to the specified `pub` time.
84    pub pub_gte: Option<DateTime<Utc>>,
85    /// To get data less than and equal to the specified `pub` time.
86    pub pub_lte: Option<DateTime<Utc>>,
87    /// To get data greater than and equal to the specified `resp` time.
88    pub resp_gte: Option<DateTime<Utc>>,
89    /// To get data less than and equal to the specified `resp` time.
90    pub resp_lte: Option<DateTime<Utc>>,
91}
92
93/// The query condition for the update operation.
94pub struct UpdateQueryCond<'a> {
95    /// The specified data.
96    pub data_id: &'a str,
97}
98
99/// The update fields.
100pub struct Updates {
101    pub resp: DateTime<Utc>,
102    /// The status will be changed for the following cases:
103    /// - Zero and positive value changes non-zero status data.
104    /// - Negative value changes smaller status value data.
105    pub status: i32,
106}
107
108/// Model operations.
109#[async_trait]
110pub trait NetworkDlDataModel: Sync {
111    /// To create and initialize the table/collection.
112    async fn init(&self) -> Result<(), Box<dyn StdError>>;
113
114    /// To get item count for the query condition.
115    ///
116    /// **Note**: this may take a long time.
117    async fn count(&self, cond: &ListQueryCond) -> Result<u64, Box<dyn StdError>>;
118
119    /// To get item list. The maximum number of returned items will be controlled by the
120    /// `cursor_max` of the list option.
121    ///
122    /// For the first time, `cursor` MUST use `None`. If one cursor is returned, it means that
123    /// there are more items to get. Use the returned cursor to get more data items.
124    ///
125    /// **Note**: using cursors is recommended to prevent exhausting memory.
126    async fn list(
127        &self,
128        opts: &ListOptions,
129        cursor: Option<Box<dyn Cursor>>,
130    ) -> Result<(Vec<NetworkDlData>, Option<Box<dyn Cursor>>), Box<dyn StdError>>;
131
132    /// To add an item.
133    async fn add(&self, data: &NetworkDlData) -> Result<(), Box<dyn StdError>>;
134
135    /// To delete one or more items.
136    async fn del(&self, cond: &QueryCond) -> Result<(), Box<dyn StdError>>;
137
138    /// To update one or more items.
139    async fn update(
140        &self,
141        cond: &UpdateQueryCond,
142        updates: &Updates,
143    ) -> Result<(), Box<dyn StdError>>;
144}
145
146/// The operations for cursors.
147///
148/// All functions are private to let programs to pass them as arguments directly without any
149/// operation.
150#[async_trait]
151pub trait Cursor: Send {
152    async fn try_next(&mut self) -> Result<Option<NetworkDlData>, Box<dyn StdError>>;
153
154    fn offset(&self) -> u64;
155}
156
157/// The expiration time of the data in seconds.
158pub const EXPIRES: i64 = 100 * 86400;