oxirs-stream 0.2.2

Real-time streaming support with Kafka/NATS/MQTT/OPC-UA I/O, RDF Patch, and SPARQL Update delta
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
485
486
487
488
489
490
491
492
493
494
495
496
497
//! MQTT Backend Types
//!
//! MQTT 3.1.1 and 5.0 support for IoT and Industry 4.0 integration

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// MQTT Backend Configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MqttConfig {
    /// Broker URL (tcp://host:1883 or ssl://host:8883)
    pub broker_url: String,

    /// MQTT client ID
    pub client_id: String,

    /// Clean session flag (true = no persistent session)
    pub clean_session: bool,

    /// Keep alive interval in seconds
    pub keep_alive_secs: u16,

    /// Default QoS level
    pub default_qos: QoS,

    /// Username (optional)
    pub username: Option<String>,

    /// Password (optional)
    pub password: Option<String>,

    /// TLS configuration (for mqtts://)
    pub tls: Option<MqttTlsConfig>,

    /// Reconnect configuration
    pub reconnect: MqttReconnectConfig,

    /// Last will & testament (optional)
    pub last_will: Option<LastWillConfig>,

    /// MQTT protocol version
    pub protocol_version: MqttProtocolVersion,

    /// Session expiry interval (MQTT 5.0 only, seconds)
    pub session_expiry_interval: Option<u32>,

    /// Maximum packet size (MQTT 5.0 only, bytes)
    pub max_packet_size: Option<u32>,

    /// Request/response information (MQTT 5.0)
    pub request_response_info: bool,

    /// Request problem information (MQTT 5.0)
    pub request_problem_info: bool,
}

impl Default for MqttConfig {
    fn default() -> Self {
        Self {
            broker_url: "tcp://localhost:1883".to_string(),
            client_id: format!("oxirs-mqtt-{}", uuid::Uuid::new_v4()),
            clean_session: true,
            keep_alive_secs: 60,
            default_qos: QoS::AtLeastOnce,
            username: None,
            password: None,
            tls: None,
            reconnect: MqttReconnectConfig::default(),
            last_will: None,
            protocol_version: MqttProtocolVersion::V311,
            session_expiry_interval: None,
            max_packet_size: None,
            request_response_info: false,
            request_problem_info: true,
        }
    }
}

/// MQTT Quality of Service levels
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum QoS {
    /// At most once (fire and forget) - QoS 0
    AtMostOnce = 0,
    /// At least once (acknowledged delivery) - QoS 1
    AtLeastOnce = 1,
    /// Exactly once (assured delivery) - QoS 2
    ExactlyOnce = 2,
}

impl From<QoS> for u8 {
    fn from(qos: QoS) -> Self {
        qos as u8
    }
}

impl TryFrom<u8> for QoS {
    type Error = String;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(QoS::AtMostOnce),
            1 => Ok(QoS::AtLeastOnce),
            2 => Ok(QoS::ExactlyOnce),
            _ => Err(format!("Invalid QoS level: {}", value)),
        }
    }
}

/// MQTT Protocol Version
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum MqttProtocolVersion {
    /// MQTT 3.1.1
    V311,
    /// MQTT 5.0
    V5,
}

/// TLS Configuration for MQTT
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MqttTlsConfig {
    /// CA certificate path (PEM format)
    pub ca_cert_path: Option<String>,

    /// Client certificate path (PEM format)
    pub client_cert_path: Option<String>,

    /// Client private key path (PEM format)
    pub client_key_path: Option<String>,

    /// Skip certificate verification (insecure, for testing only)
    pub insecure_skip_verify: bool,

    /// ALPN protocols
    pub alpn_protocols: Vec<String>,
}

impl Default for MqttTlsConfig {
    fn default() -> Self {
        Self {
            ca_cert_path: None,
            client_cert_path: None,
            client_key_path: None,
            insecure_skip_verify: false,
            alpn_protocols: vec!["mqtt".to_string()],
        }
    }
}

/// Reconnect Configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MqttReconnectConfig {
    /// Initial delay before first reconnect attempt (milliseconds)
    pub initial_delay_ms: u64,

    /// Maximum delay between reconnect attempts (milliseconds)
    pub max_delay_ms: u64,

    /// Exponential backoff factor
    pub backoff_factor: f64,

    /// Maximum number of reconnect attempts (0 = infinite)
    pub max_attempts: u32,

    /// Jitter for reconnect delays (0.0 - 1.0)
    pub jitter: f64,
}

impl Default for MqttReconnectConfig {
    fn default() -> Self {
        Self {
            initial_delay_ms: 1000,
            max_delay_ms: 60000,
            backoff_factor: 2.0,
            max_attempts: 0,
            jitter: 0.1,
        }
    }
}

/// Last Will and Testament Configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LastWillConfig {
    /// LWT topic
    pub topic: String,

    /// LWT message payload
    pub payload: Vec<u8>,

    /// LWT QoS
    pub qos: QoS,

    /// LWT retain flag
    pub retain: bool,

    /// Will delay interval (MQTT 5.0 only, seconds)
    pub will_delay_interval: Option<u32>,

    /// Message expiry interval (MQTT 5.0 only, seconds)
    pub message_expiry_interval: Option<u32>,

    /// Content type (MQTT 5.0 only)
    pub content_type: Option<String>,

    /// Response topic (MQTT 5.0 only)
    pub response_topic: Option<String>,

    /// User properties (MQTT 5.0 only)
    pub user_properties: HashMap<String, String>,
}

/// Topic Subscription with RDF Mapping
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TopicSubscription {
    /// MQTT topic pattern (supports wildcards: +, #)
    /// Examples: "factory/+/sensor/#", "plant/building1/temperature"
    pub topic_pattern: String,

    /// QoS for this subscription
    pub qos: QoS,

    /// Payload format for parsing
    pub payload_format: PayloadFormat,

    /// RDF mapping configuration
    pub rdf_mapping: TopicRdfMapping,

    /// Subscription options (MQTT 5.0)
    pub options: Option<SubscriptionOptions>,
}

/// Payload Format Types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum PayloadFormat {
    /// JSON payload with optional schema
    Json {
        schema: Option<String>,
        root_path: Option<String>,
    },

    /// Eclipse Sparkplug B (Industry 4.0 standard)
    SparkplugB {
        /// Sparkplug namespace (spBv1.0)
        namespace: String,
    },

    /// Protocol Buffers
    Protobuf {
        /// Protobuf schema definition
        schema: String,
        /// Message type name
        message_type: String,
    },

    /// Apache Avro
    Avro {
        /// Avro schema (JSON)
        schema: String,
    },

    /// CSV format
    Csv {
        /// CSV delimiter
        delimiter: char,
        /// Column headers
        headers: Vec<String>,
        /// Skip header row
        skip_header: bool,
    },

    /// Plain text (single value)
    PlainText {
        /// Value datatype (xsd:string, xsd:double, etc.)
        datatype: String,
    },

    /// Raw bytes (base64 encoded)
    Raw,
}

/// Topic to RDF Mapping Rules
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TopicRdfMapping {
    /// Subject URI pattern
    ///
    /// Supports placeholders:
    ///   - `{topic.0}`, `{topic.1}`, ... for topic segments
    ///   - `{payload.field}` for payload fields
    ///
    /// Example: `"urn:factory:{topic.0}:sensor:{topic.2}"`
    pub subject_pattern: String,

    /// Predicate mappings (payload field -> RDF predicate URI)
    pub predicate_map: HashMap<String, String>,

    /// Named graph pattern (optional)
    pub graph_pattern: Option<String>,

    /// rdf:type for the entity
    pub type_uri: Option<String>,

    /// Timestamp field in payload (if any)
    pub timestamp_field: Option<String>,

    /// Timestamp predicate URI (default: sosa:resultTime)
    pub timestamp_predicate: Option<String>,

    /// Value transformation rules
    pub transformations: Vec<ValueTransformation>,
}

/// Value Transformation Rule
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValueTransformation {
    /// Field to transform
    pub field: String,

    /// Transformation type
    pub operation: TransformOperation,
}

/// Transformation Operations
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum TransformOperation {
    /// Scale by factor
    Scale { factor: f64 },

    /// Add offset
    Offset { value: f64 },

    /// Unit conversion
    UnitConversion { from: String, to: String },

    /// Apply formula
    Formula { expression: String },

    /// Lookup table
    LookupTable { table: HashMap<String, String> },

    /// Regex replace
    RegexReplace {
        pattern: String,
        replacement: String,
    },
}

/// Subscription Options (MQTT 5.0)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubscriptionOptions {
    /// No local flag
    pub no_local: bool,

    /// Retain as published flag
    pub retain_as_published: bool,

    /// Retain handling
    pub retain_handling: RetainHandling,
}

/// Retain Handling (MQTT 5.0)
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum RetainHandling {
    /// Send retained messages at subscribe
    SendAtSubscribe = 0,
    /// Send retained messages at subscribe if new subscription
    SendAtSubscribeNew = 1,
    /// Don't send retained messages
    DontSend = 2,
}

/// MQTT Statistics
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct MqttStats {
    /// Messages published
    pub messages_published: u64,

    /// Messages received
    pub messages_received: u64,

    /// Bytes sent
    pub bytes_sent: u64,

    /// Bytes received
    pub bytes_received: u64,

    /// Connection count
    pub connection_count: u64,

    /// Reconnection count
    pub reconnection_count: u64,

    /// Last connection time
    pub last_connected_at: Option<chrono::DateTime<chrono::Utc>>,

    /// Last disconnection time
    pub last_disconnected_at: Option<chrono::DateTime<chrono::Utc>>,

    /// QoS 0 messages
    pub qos0_count: u64,

    /// QoS 1 messages
    pub qos1_count: u64,

    /// QoS 2 messages
    pub qos2_count: u64,

    /// Publish failures
    pub publish_failures: u64,

    /// Subscribe failures
    pub subscribe_failures: u64,
}

/// MQTT Message with metadata
#[derive(Debug, Clone)]
pub struct MqttMessage {
    /// Topic name
    pub topic: String,

    /// QoS level
    pub qos: QoS,

    /// Retain flag
    pub retain: bool,

    /// Payload
    pub payload: Vec<u8>,

    /// Message properties (MQTT 5.0)
    pub properties: Option<MqttMessageProperties>,

    /// Reception timestamp
    pub received_at: chrono::DateTime<chrono::Utc>,
}

/// MQTT 5.0 Message Properties
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MqttMessageProperties {
    /// Payload format indicator (0 = bytes, 1 = UTF-8)
    pub payload_format_indicator: Option<u8>,

    /// Message expiry interval (seconds)
    pub message_expiry_interval: Option<u32>,

    /// Topic alias
    pub topic_alias: Option<u16>,

    /// Response topic
    pub response_topic: Option<String>,

    /// Correlation data
    pub correlation_data: Option<Vec<u8>>,

    /// User properties
    pub user_properties: HashMap<String, String>,

    /// Subscription identifier
    pub subscription_identifier: Option<u32>,

    /// Content type
    pub content_type: Option<String>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_qos_conversion() {
        assert_eq!(u8::from(QoS::AtMostOnce), 0);
        assert_eq!(u8::from(QoS::AtLeastOnce), 1);
        assert_eq!(u8::from(QoS::ExactlyOnce), 2);

        assert_eq!(QoS::try_from(0).unwrap(), QoS::AtMostOnce);
        assert_eq!(QoS::try_from(1).unwrap(), QoS::AtLeastOnce);
        assert_eq!(QoS::try_from(2).unwrap(), QoS::ExactlyOnce);
        assert!(QoS::try_from(3).is_err());
    }

    #[test]
    fn test_default_config() {
        let config = MqttConfig::default();
        assert!(config.broker_url.starts_with("tcp://"));
        assert!(!config.client_id.is_empty());
        assert_eq!(config.default_qos, QoS::AtLeastOnce);
        assert_eq!(config.keep_alive_secs, 60);
    }

    #[test]
    fn test_default_stats() {
        let stats = MqttStats::default();
        assert_eq!(stats.messages_published, 0);
        assert_eq!(stats.messages_received, 0);
        assert_eq!(stats.connection_count, 0);
    }
}