sylvia_iot_broker/models/
device_route.rs

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
//! Traits and structs for device routes.

use std::error::Error as StdError;

use async_trait::async_trait;
use chrono::{DateTime, Utc};

/// The item content.
#[derive(Debug, PartialEq)]
pub struct DeviceRoute {
    pub route_id: String,
    pub unit_id: String,
    pub unit_code: String, // Application's unit code.
    pub application_id: String,
    pub application_code: String,
    pub device_id: String,
    pub network_id: String,
    pub network_code: String,
    pub network_addr: String,
    pub profile: String,
    pub created_at: DateTime<Utc>,
    pub modified_at: DateTime<Utc>,
}

// The device route cache item for uplink data.
#[derive(Clone)]
pub struct DeviceRouteCacheUlData {
    pub app_mgr_keys: Vec<String>,
}

// The device route cache item for downlink data.
// All None or all Some.
#[derive(Clone)]
pub struct DeviceRouteCacheDlData {
    pub net_mgr_key: String,
    pub network_id: String,
    pub network_addr: String,
    pub device_id: String,
    pub profile: String,
}

/// The sort keys for the list operation.
pub enum SortKey {
    CreatedAt,
    ModifiedAt,
    ApplicationCode,
    NetworkCode,
    NetworkAddr,
}

/// The sort condition for the list operation.
pub struct SortCond {
    pub key: SortKey,
    pub asc: bool,
}

/// The list operation options.
pub struct ListOptions<'a> {
    /// The query conditions.
    pub cond: &'a ListQueryCond<'a>,
    /// The data offset.
    pub offset: Option<u64>,
    /// The maximum number to query.
    pub limit: Option<u64>,
    /// The sort conditions.
    pub sort: Option<&'a [SortCond]>,
    /// The maximum number items one time the `list()` returns.
    ///
    /// Use cursors until reaching `limit` or all data.
    pub cursor_max: Option<u64>,
}

/// The query condition to get item(s).
#[derive(Default)]
pub struct QueryCond<'a> {
    pub route_id: Option<&'a str>,
    pub unit_id: Option<&'a str>,
    pub application_id: Option<&'a str>,
    pub network_id: Option<&'a str>,
    pub device_id: Option<&'a str>,
    pub network_addrs: Option<&'a Vec<&'a str>>,
}

/// The query condition for the list operation.
#[derive(Default)]
pub struct ListQueryCond<'a> {
    /// To get the specified device route.
    pub route_id: Option<&'a str>,
    /// To get device routes of the specified unit.
    pub unit_id: Option<&'a str>,
    /// To get device routes of the specified unit code.
    pub unit_code: Option<&'a str>,
    /// To get device routes of the specified application.
    pub application_id: Option<&'a str>,
    /// To get device routes of the specified application code.
    pub application_code: Option<&'a str>,
    /// To get device routes of the specified network.
    pub network_id: Option<&'a str>,
    /// To get device routes of the specified network code.
    pub network_code: Option<&'a str>,
    /// To get device routes of the specified network address.
    pub network_addr: Option<&'a str>,
    /// To get devices of the specified network addresses.
    pub network_addrs: Option<&'a Vec<&'a str>>,
    /// To get device routes of the specified device.
    pub device_id: Option<&'a str>,
}

/// The query condition for the get cache operation.
pub struct GetCacheQueryCond<'a> {
    /// To get device routes of the specified network unit code.
    pub unit_code: &'a str,
    /// To get device routes of the specified network code.
    pub network_code: &'a str,
    /// To get device routes of the specified network address.
    pub network_addr: &'a str,
}

/// The query condition for the delete cache operation.
pub struct DelCacheQueryCond<'a> {
    /// To delete device routes of the specified network unit code. Empty for public network.
    pub unit_code: &'a str,
    /// To delete device routes of the specified network code.
    pub network_code: Option<&'a str>,
    /// To delete device routes of the specified network address.
    pub network_addr: Option<&'a str>,
}

/// The query condition for the get (public network) downlink data cache operation.
pub struct GetCachePubQueryCond<'a> {
    /// To get device routes of the specified device's unit ID.
    pub unit_id: &'a str,
    /// To get device routes of the specified device ID.
    pub device_id: &'a str,
}

/// The query condition for the delete (public network) downlink data cache operation.
pub struct DelCachePubQueryCond<'a> {
    /// To delete device routes of the specified device unit ID.
    pub unit_id: &'a str,
    /// To delete device routes of the specified device ID.
    pub device_id: Option<&'a str>,
}

/// The query condition for the update operation.
pub struct UpdateQueryCond<'a> {
    /// The specified device.
    pub device_id: &'a str,
}

/// The update fields by using [`Some`]s.
#[derive(Default)]
pub struct Updates<'a> {
    pub modified_at: Option<DateTime<Utc>>,
    pub profile: Option<&'a str>,
}

/// Model operations.
#[async_trait]
pub trait DeviceRouteModel: Sync {
    /// To create and initialize the table/collection.
    async fn init(&self) -> Result<(), Box<dyn StdError>>;

    /// To get item count for the query condition.
    ///
    /// **Note**: this may take a long time.
    async fn count(&self, cond: &ListQueryCond) -> Result<u64, Box<dyn StdError>>;

    /// To get item list. The maximum number of returned items will be controlled by the
    /// `cursor_max` of the list option.
    ///
    /// For the first time, `cursor` MUST use `None`. If one cursor is returned, it means that
    /// there are more items to get. Use the returned cursor to get more data items.
    ///
    /// **Note**: using cursors is recommended to prevent exhausting memory.
    async fn list(
        &self,
        opts: &ListOptions,
        cursor: Option<Box<dyn Cursor>>,
    ) -> Result<(Vec<DeviceRoute>, Option<Box<dyn Cursor>>), Box<dyn StdError>>;

    /// To get an item.
    ///
    /// **Note**: this is only used for function test.
    async fn get(&self, route_id: &str) -> Result<Option<DeviceRoute>, Box<dyn StdError>>;

    /// To add an item.
    async fn add(&self, route: &DeviceRoute) -> Result<(), Box<dyn StdError>>;

    /// To add items in bulk. Duplicate items will be skipped without errors.
    async fn add_bulk(&self, devices: &Vec<DeviceRoute>) -> Result<(), Box<dyn StdError>>;

    /// To delete one or more items.
    async fn del(&self, cond: &QueryCond) -> Result<(), Box<dyn StdError>>;

    /// To update one or more items.
    async fn update(
        &self,
        cond: &UpdateQueryCond,
        updates: &Updates,
    ) -> Result<(), Box<dyn StdError>>;
}

/// The operations for cursors.
///
/// All functions are private to let programs to pass them as arguments directly without any
/// operation.
#[async_trait]
pub trait Cursor: Send {
    async fn try_next(&mut self) -> Result<Option<DeviceRoute>, Box<dyn StdError>>;

    fn offset(&self) -> u64;
}

/// Cache operations.
#[async_trait]
pub trait DeviceRouteCache: Sync {
    /// To clear all device routes.
    async fn clear(&self) -> Result<(), Box<dyn StdError>>;

    /// To get device route for the uplink data.
    async fn get_uldata(
        &self,
        device_id: &str,
    ) -> Result<Option<DeviceRouteCacheUlData>, Box<dyn StdError>>;

    /// To set device route for the uplink data.
    async fn set_uldata(
        &self,
        device_id: &str,
        value: Option<&DeviceRouteCacheUlData>,
    ) -> Result<(), Box<dyn StdError>>;

    /// To delete device route for the uplink data.
    async fn del_uldata(&self, device_id: &str) -> Result<(), Box<dyn StdError>>;

    /// To get device route for the downlink data.
    async fn get_dldata(
        &self,
        cond: &GetCacheQueryCond,
    ) -> Result<Option<DeviceRouteCacheDlData>, Box<dyn StdError>>;

    /// To set device route for the downlink data.
    async fn set_dldata(
        &self,
        cond: &GetCacheQueryCond,
        value: Option<&DeviceRouteCacheDlData>,
    ) -> Result<(), Box<dyn StdError>>;

    /// To delete device route for the downlink data.
    async fn del_dldata(&self, cond: &DelCacheQueryCond) -> Result<(), Box<dyn StdError>>;

    /// To get device route for the (public network) downlink data.
    async fn get_dldata_pub(
        &self,
        cond: &GetCachePubQueryCond,
    ) -> Result<Option<DeviceRouteCacheDlData>, Box<dyn StdError>>;

    /// To set device route for the (public network) downlink data.
    async fn set_dldata_pub(
        &self,
        cond: &GetCachePubQueryCond,
        value: Option<&DeviceRouteCacheDlData>,
    ) -> Result<(), Box<dyn StdError>>;

    /// To delete device route for the (public network) downlink data.
    async fn del_dldata_pub(&self, cond: &DelCachePubQueryCond) -> Result<(), Box<dyn StdError>>;
}