oxirs-stream 0.2.4

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
//! OPC UA Backend Types
//!
//! OPC UA Unified Architecture support for industrial automation

use serde::{Deserialize, Serialize};

/// OPC UA Client Configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpcUaConfig {
    /// OPC UA endpoint URL (opc.tcp://host:4840)
    pub endpoint_url: String,

    /// Application name
    pub application_name: String,

    /// Application URI
    pub application_uri: String,

    /// Security policy
    pub security_policy: SecurityPolicy,

    /// Security mode
    pub security_mode: MessageSecurityMode,

    /// User identity
    pub user_identity: UserIdentity,

    /// Session timeout (milliseconds)
    pub session_timeout_ms: u64,

    /// Publishing interval for subscriptions (milliseconds)
    pub publishing_interval_ms: u32,

    /// Sampling interval for monitored items (milliseconds)
    pub sampling_interval_ms: u32,

    /// Queue size for monitored items
    pub queue_size: u32,

    /// Client certificate configuration
    pub client_certificate: Option<CertificateConfig>,

    /// Server certificate path or PEM
    pub server_certificate: Option<String>,

    /// Auto-accept untrusted certificates (insecure, for testing)
    pub accept_untrusted_certs: bool,

    /// Session renewal settings
    pub session_renewal: SessionRenewalConfig,
}

impl Default for OpcUaConfig {
    fn default() -> Self {
        Self {
            endpoint_url: "opc.tcp://localhost:4840".to_string(),
            application_name: format!("OxiRS-OPC-UA-{}", uuid::Uuid::new_v4()),
            application_uri: "urn:OxiRS:OpcUaClient".to_string(),
            security_policy: SecurityPolicy::None,
            security_mode: MessageSecurityMode::None,
            user_identity: UserIdentity::Anonymous,
            session_timeout_ms: 60000,
            publishing_interval_ms: 1000,
            sampling_interval_ms: 100,
            queue_size: 10,
            client_certificate: None,
            server_certificate: None,
            accept_untrusted_certs: false,
            session_renewal: SessionRenewalConfig::default(),
        }
    }
}

/// Security Policies
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum SecurityPolicy {
    /// No security
    None,
    /// Basic128Rsa15 (deprecated, legacy only)
    Basic128Rsa15,
    /// Basic256 (deprecated, legacy only)
    Basic256,
    /// Basic256Sha256 (recommended minimum)
    Basic256Sha256,
    /// Aes128_Sha256_RsaOaep
    Aes128Sha256RsaOaep,
    /// Aes256_Sha256_RsaPss (highest security)
    Aes256Sha256RsaPss,
}

impl SecurityPolicy {
    /// Get the OPC UA security policy URI
    pub fn to_uri(&self) -> String {
        let base = "http://opcfoundation.org/UA/SecurityPolicy#";
        match self {
            SecurityPolicy::None => format!("{}None", base),
            SecurityPolicy::Basic128Rsa15 => format!("{}Basic128Rsa15", base),
            SecurityPolicy::Basic256 => format!("{}Basic256", base),
            SecurityPolicy::Basic256Sha256 => format!("{}Basic256Sha256", base),
            SecurityPolicy::Aes128Sha256RsaOaep => format!("{}Aes128_Sha256_RsaOaep", base),
            SecurityPolicy::Aes256Sha256RsaPss => format!("{}Aes256_Sha256_RsaPss", base),
        }
    }
}

/// Message Security Mode
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum MessageSecurityMode {
    /// No security
    None,
    /// Sign only
    Sign,
    /// Sign and encrypt
    SignAndEncrypt,
}

/// User Identity Options
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum UserIdentity {
    /// Anonymous (no authentication)
    Anonymous,

    /// Username and password
    UserPassword { username: String, password: String },

    /// X.509 certificate
    X509Certificate { cert_path: String, key_path: String },

    /// Issued token
    IssuedToken {
        token: Vec<u8>,
        encryption_algorithm: String,
    },
}

/// Certificate Configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CertificateConfig {
    /// Certificate path (PEM or DER)
    pub cert_path: String,

    /// Private key path (PEM or DER)
    pub key_path: String,

    /// Certificate format
    pub format: CertificateFormat,
}

/// Certificate Format
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum CertificateFormat {
    /// PEM format (base64 encoded)
    Pem,
    /// DER format (binary)
    Der,
}

/// Session Renewal Configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionRenewalConfig {
    /// Auto-renew session before expiry
    pub auto_renew: bool,

    /// Renew at percentage of timeout (0.0 - 1.0)
    pub renew_at_ratio: f64,

    /// Retry count for session renewal
    pub retry_count: u32,

    /// Retry delay (milliseconds)
    pub retry_delay_ms: u64,
}

impl Default for SessionRenewalConfig {
    fn default() -> Self {
        Self {
            auto_renew: true,
            renew_at_ratio: 0.75,
            retry_count: 3,
            retry_delay_ms: 5000,
        }
    }
}

/// Node Subscription Configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeSubscription {
    /// OPC UA Node ID (e.g., "ns=2;s=Temperature" or "ns=2;i=1001")
    pub node_id: String,

    /// Browse name (optional, for display)
    pub browse_name: Option<String>,

    /// Display name (optional, for display)
    pub display_name: Option<String>,

    /// RDF subject URI (entity this node belongs to)
    pub rdf_subject: String,

    /// RDF predicate URI (property this node represents)
    pub rdf_predicate: String,

    /// Named graph URI (optional)
    pub rdf_graph: Option<String>,

    /// Unit URI (QUDT, UCUM, or custom)
    pub unit_uri: Option<String>,

    /// SAMM property reference (optional)
    pub samm_property: Option<String>,

    /// Deadband configuration for value changes
    pub deadband: Option<Deadband>,

    /// Data change filter
    pub data_change_filter: Option<DataChangeFilter>,
}

/// Deadband Configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Deadband {
    /// Deadband type
    pub deadband_type: DeadbandType,

    /// Deadband value
    pub value: f64,
}

/// Deadband Type
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum DeadbandType {
    /// No deadband
    None,
    /// Absolute deadband
    Absolute,
    /// Percent deadband
    Percent,
}

/// Data Change Filter
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DataChangeFilter {
    /// Report on status change
    Status,
    /// Report on status or value change
    StatusValue,
    /// Report on status, value, or timestamp change
    StatusValueTimestamp,
}

/// OPC UA Data Change Event
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpcUaDataChange {
    /// Node ID
    pub node_id: String,

    /// Value
    pub value: OpcUaValue,

    /// Status code (good = 0x00000000)
    pub status_code: u32,

    /// Source timestamp (from device)
    pub source_timestamp: Option<chrono::DateTime<chrono::Utc>>,

    /// Server timestamp (from OPC UA server)
    pub server_timestamp: chrono::DateTime<chrono::Utc>,
}

/// OPC UA Value Types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum OpcUaValue {
    Boolean {
        value: bool,
    },
    SByte {
        value: i8,
    },
    Byte {
        value: u8,
    },
    Int16 {
        value: i16,
    },
    UInt16 {
        value: u16,
    },
    Int32 {
        value: i32,
    },
    UInt32 {
        value: u32,
    },
    Int64 {
        value: i64,
    },
    UInt64 {
        value: u64,
    },
    Float {
        value: f32,
    },
    Double {
        value: f64,
    },
    String {
        value: String,
    },
    DateTime {
        value: chrono::DateTime<chrono::Utc>,
    },
    Guid {
        value: String,
    },
    ByteString {
        value: Vec<u8>,
    },
    StatusCode {
        value: u32,
    },
    QualifiedName {
        namespace_index: u16,
        name: String,
    },
    LocalizedText {
        locale: Option<String>,
        text: String,
    },
}

impl OpcUaValue {
    /// Convert to RDF literal with datatype
    pub fn to_rdf_literal(&self) -> String {
        match self {
            OpcUaValue::Boolean { value } => {
                format!("\"{}\"^^<http://www.w3.org/2001/XMLSchema#boolean>", value)
            }
            OpcUaValue::Int32 { value: _ } | OpcUaValue::Int16 { value: _ } => {
                format!(
                    "\"{}\"^^<http://www.w3.org/2001/XMLSchema#integer>",
                    match self {
                        OpcUaValue::Int32 { value } => *value as i64,
                        OpcUaValue::Int16 { value } => *value as i64,
                        _ => 0,
                    }
                )
            }
            OpcUaValue::Float { value } => {
                format!("\"{}\"^^<http://www.w3.org/2001/XMLSchema#float>", value)
            }
            OpcUaValue::Double { value } => {
                format!("\"{}\"^^<http://www.w3.org/2001/XMLSchema#double>", value)
            }
            OpcUaValue::String { value } => format!("\"{}\"", value),
            OpcUaValue::DateTime { value } => {
                format!(
                    "\"{}\"^^<http://www.w3.org/2001/XMLSchema#dateTime>",
                    value.to_rfc3339()
                )
            }
            _ => format!("\"{}\"", serde_json::to_string(self).unwrap_or_default()),
        }
    }

    /// Get the XSD datatype URI
    pub fn xsd_datatype(&self) -> &'static str {
        match self {
            OpcUaValue::Boolean { .. } => "http://www.w3.org/2001/XMLSchema#boolean",
            OpcUaValue::Int32 { .. } | OpcUaValue::Int16 { .. } | OpcUaValue::Int64 { .. } => {
                "http://www.w3.org/2001/XMLSchema#integer"
            }
            OpcUaValue::UInt32 { .. } | OpcUaValue::UInt16 { .. } | OpcUaValue::UInt64 { .. } => {
                "http://www.w3.org/2001/XMLSchema#nonNegativeInteger"
            }
            OpcUaValue::Float { .. } => "http://www.w3.org/2001/XMLSchema#float",
            OpcUaValue::Double { .. } => "http://www.w3.org/2001/XMLSchema#double",
            OpcUaValue::String { .. } => "http://www.w3.org/2001/XMLSchema#string",
            OpcUaValue::DateTime { .. } => "http://www.w3.org/2001/XMLSchema#dateTime",
            _ => "http://www.w3.org/2001/XMLSchema#string",
        }
    }
}

/// OPC UA Statistics
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct OpcUaStats {
    /// Data changes received
    pub data_changes_received: u64,

    /// Events received
    pub events_received: u64,

    /// Session count
    pub session_count: u64,

    /// Session renewal count
    pub session_renewals: u64,

    /// Subscription count
    pub subscription_count: u64,

    /// Monitored items count
    pub monitored_items_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>>,

    /// Publish requests sent
    pub publish_requests: u64,

    /// Read operations
    pub read_operations: u64,

    /// Write operations
    pub write_operations: u64,

    /// Browse operations
    pub browse_operations: u64,

    /// Error count
    pub error_count: u64,
}

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

    #[test]
    fn test_security_policy_uri() {
        assert_eq!(
            SecurityPolicy::None.to_uri(),
            "http://opcfoundation.org/UA/SecurityPolicy#None"
        );
        assert_eq!(
            SecurityPolicy::Basic256Sha256.to_uri(),
            "http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256"
        );
    }

    #[test]
    fn test_opcua_value_to_rdf() {
        let value = OpcUaValue::Double { value: 25.5 };
        let rdf = value.to_rdf_literal();
        assert!(rdf.contains("25.5"));
        assert!(rdf.contains("double"));

        let value = OpcUaValue::Boolean { value: true };
        let rdf = value.to_rdf_literal();
        assert!(rdf.contains("true"));
        assert!(rdf.contains("boolean"));
    }

    #[test]
    fn test_default_config() {
        let config = OpcUaConfig::default();
        assert!(config.endpoint_url.starts_with("opc.tcp://"));
        assert_eq!(config.security_policy, SecurityPolicy::None);
        assert!(matches!(config.user_identity, UserIdentity::Anonymous));
    }
}