mabi-knx 1.5.0

Mabinogion - KNXnet/IP simulator
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
//! KNX Group Object management.
//!
//! This module provides the group object table for managing KNX group addresses
//! and their associated values.

use std::sync::Arc;

use dashmap::DashMap;
use parking_lot::RwLock;
use tokio::sync::broadcast;

use crate::address::GroupAddress;
use crate::dpt::{BoxedDptCodec, DptId, DptRegistry, DptValue};
use crate::error::{KnxError, KnxResult};

// ============================================================================
// Group Object Flags
// ============================================================================

/// Group object communication flags.
#[derive(Debug, Clone, Copy, Default)]
pub struct GroupObjectFlags {
    /// Communication enabled.
    pub communication: bool,
    /// Read enabled.
    pub read: bool,
    /// Write enabled.
    pub write: bool,
    /// Transmit on change.
    pub transmit: bool,
    /// Update on receive.
    pub update: bool,
}

impl GroupObjectFlags {
    /// Full read-write access.
    pub fn read_write() -> Self {
        Self {
            communication: true,
            read: true,
            write: true,
            transmit: true,
            update: true,
        }
    }

    /// Read-only access.
    pub fn read_only() -> Self {
        Self {
            communication: true,
            read: true,
            write: false,
            transmit: true,
            update: false,
        }
    }

    /// Write-only access.
    pub fn write_only() -> Self {
        Self {
            communication: true,
            read: false,
            write: true,
            transmit: false,
            update: true,
        }
    }

    /// Check if any communication is enabled.
    pub fn is_enabled(&self) -> bool {
        self.communication
    }
}

// ============================================================================
// Group Object
// ============================================================================

/// A KNX group object.
pub struct GroupObject {
    /// Group address.
    address: GroupAddress,
    /// Object name.
    name: String,
    /// Description.
    description: String,
    /// DPT codec.
    codec: Arc<BoxedDptCodec>,
    /// Communication flags.
    flags: GroupObjectFlags,
    /// Current raw value.
    value: RwLock<Vec<u8>>,
    /// Last update timestamp.
    last_update: RwLock<std::time::Instant>,
}

impl std::fmt::Debug for GroupObject {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("GroupObject")
            .field("address", &self.address)
            .field("name", &self.name)
            .field("description", &self.description)
            .field("dpt_id", &self.codec.id())
            .field("flags", &self.flags)
            .field("value", &self.value.read().as_slice())
            .finish()
    }
}

impl GroupObject {
    /// Create a new group object.
    pub fn new(address: GroupAddress, name: impl Into<String>, codec: Arc<BoxedDptCodec>) -> Self {
        let default_value = codec.default_value();
        let encoded = codec.encode(&default_value).unwrap_or_default();

        Self {
            address,
            name: name.into(),
            description: String::new(),
            codec,
            flags: GroupObjectFlags::read_write(),
            value: RwLock::new(encoded),
            last_update: RwLock::new(std::time::Instant::now()),
        }
    }

    /// Create with description.
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = description.into();
        self
    }

    /// Set flags.
    pub fn with_flags(mut self, flags: GroupObjectFlags) -> Self {
        self.flags = flags;
        self
    }

    /// Set initial value.
    pub fn with_value(self, value: &DptValue) -> KnxResult<Self> {
        self.write_value(value)?;
        Ok(self)
    }

    /// Get group address.
    pub fn address(&self) -> GroupAddress {
        self.address
    }

    /// Get name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Get description.
    pub fn description(&self) -> &str {
        &self.description
    }

    /// Get DPT ID.
    pub fn dpt_id(&self) -> DptId {
        self.codec.id()
    }

    /// Get flags.
    pub fn flags(&self) -> GroupObjectFlags {
        self.flags
    }

    /// Get raw value.
    pub fn read_raw(&self) -> Vec<u8> {
        self.value.read().clone()
    }

    /// Read decoded value.
    pub fn read_value(&self) -> KnxResult<DptValue> {
        let raw = self.value.read();
        self.codec.decode(&raw)
    }

    /// Write raw value.
    pub fn write_raw(&self, data: &[u8]) -> KnxResult<()> {
        if !self.flags.write {
            return Err(KnxError::GroupObjectWriteNotAllowed(self.name.clone()));
        }

        *self.value.write() = data.to_vec();
        *self.last_update.write() = std::time::Instant::now();
        Ok(())
    }

    /// Write decoded value.
    pub fn write_value(&self, value: &DptValue) -> KnxResult<()> {
        if !self.flags.write {
            return Err(KnxError::GroupObjectWriteNotAllowed(self.name.clone()));
        }

        let encoded = self.codec.encode(value)?;
        *self.value.write() = encoded;
        *self.last_update.write() = std::time::Instant::now();
        Ok(())
    }

    /// Check if read is allowed.
    pub fn can_read(&self) -> bool {
        self.flags.communication && self.flags.read
    }

    /// Check if write is allowed.
    pub fn can_write(&self) -> bool {
        self.flags.communication && self.flags.write
    }

    /// Get last update time.
    pub fn last_update(&self) -> std::time::Instant {
        *self.last_update.read()
    }

    /// Get age since last update.
    pub fn age(&self) -> std::time::Duration {
        self.last_update.read().elapsed()
    }
}

// ============================================================================
// Group Event
// ============================================================================

/// Event emitted when group object state changes.
#[derive(Debug, Clone)]
pub enum GroupEvent {
    /// Value was written.
    ValueWrite {
        address: GroupAddress,
        value: Vec<u8>,
        source: Option<String>,
    },
    /// Read request received.
    ReadRequest {
        address: GroupAddress,
        source: Option<String>,
    },
    /// Read response sent.
    ReadResponse {
        address: GroupAddress,
        value: Vec<u8>,
    },
    /// Object created.
    ObjectCreated { address: GroupAddress },
    /// Object removed.
    ObjectRemoved { address: GroupAddress },
}

impl GroupEvent {
    /// Get the address associated with this event.
    pub fn address(&self) -> GroupAddress {
        match self {
            Self::ValueWrite { address, .. } => *address,
            Self::ReadRequest { address, .. } => *address,
            Self::ReadResponse { address, .. } => *address,
            Self::ObjectCreated { address } => *address,
            Self::ObjectRemoved { address } => *address,
        }
    }
}

// ============================================================================
// Group Object Table
// ============================================================================

/// Group object table for managing multiple group objects.
pub struct GroupObjectTable {
    /// Objects indexed by group address.
    objects: DashMap<GroupAddress, Arc<GroupObject>>,
    /// Event broadcaster.
    event_tx: broadcast::Sender<GroupEvent>,
    /// DPT registry for creating objects.
    dpt_registry: Arc<DptRegistry>,
}

impl GroupObjectTable {
    /// Create a new group object table.
    pub fn new() -> Self {
        Self::with_registry(Arc::new(DptRegistry::new()))
    }

    /// Create with custom DPT registry.
    pub fn with_registry(registry: Arc<DptRegistry>) -> Self {
        let (tx, _) = broadcast::channel(1000);
        Self {
            objects: DashMap::new(),
            event_tx: tx,
            dpt_registry: registry,
        }
    }

    /// Subscribe to group events.
    pub fn subscribe(&self) -> broadcast::Receiver<GroupEvent> {
        self.event_tx.subscribe()
    }

    /// Add a group object.
    pub fn add(&self, object: GroupObject) {
        let address = object.address;
        self.objects.insert(address, Arc::new(object));
        let _ = self.event_tx.send(GroupEvent::ObjectCreated { address });
    }

    /// Create and add a group object.
    pub fn create(
        &self,
        address: GroupAddress,
        name: impl Into<String>,
        dpt_id: &DptId,
    ) -> KnxResult<Arc<GroupObject>> {
        let codec = self
            .dpt_registry
            .get(dpt_id)
            .ok_or_else(|| KnxError::InvalidDpt(format!("Unknown DPT: {}", dpt_id)))?;

        let object = Arc::new(GroupObject::new(address, name, codec));
        self.objects.insert(address, object.clone());
        let _ = self.event_tx.send(GroupEvent::ObjectCreated { address });

        Ok(object)
    }

    /// Remove a group object.
    pub fn remove(&self, address: &GroupAddress) -> Option<Arc<GroupObject>> {
        let removed = self.objects.remove(address).map(|(_, v)| v);
        if removed.is_some() {
            let _ = self
                .event_tx
                .send(GroupEvent::ObjectRemoved { address: *address });
        }
        removed
    }

    /// Get a group object.
    pub fn get(&self, address: &GroupAddress) -> Option<Arc<GroupObject>> {
        self.objects.get(address).map(|v| v.clone())
    }

    /// Check if address exists.
    pub fn contains(&self, address: &GroupAddress) -> bool {
        self.objects.contains_key(address)
    }

    /// Get all addresses.
    pub fn addresses(&self) -> Vec<GroupAddress> {
        self.objects.iter().map(|r| *r.key()).collect()
    }

    /// Get count of objects.
    pub fn len(&self) -> usize {
        self.objects.len()
    }

    /// Check if empty.
    pub fn is_empty(&self) -> bool {
        self.objects.is_empty()
    }

    /// Read value from group object.
    pub fn read(&self, address: &GroupAddress) -> KnxResult<Vec<u8>> {
        let obj = self
            .get(address)
            .ok_or_else(|| KnxError::GroupObjectNotFound(address.to_string()))?;

        if !obj.can_read() {
            return Err(KnxError::GroupObjectReadNotAllowed(obj.name().to_string()));
        }

        Ok(obj.read_raw())
    }

    /// Read decoded value.
    pub fn read_value(&self, address: &GroupAddress) -> KnxResult<DptValue> {
        let obj = self
            .get(address)
            .ok_or_else(|| KnxError::GroupObjectNotFound(address.to_string()))?;

        if !obj.can_read() {
            return Err(KnxError::GroupObjectReadNotAllowed(obj.name().to_string()));
        }

        obj.read_value()
    }

    /// Write value to group object.
    pub fn write(
        &self,
        address: &GroupAddress,
        data: &[u8],
        source: Option<String>,
    ) -> KnxResult<()> {
        let obj = self
            .get(address)
            .ok_or_else(|| KnxError::GroupObjectNotFound(address.to_string()))?;

        obj.write_raw(data)?;

        let _ = self.event_tx.send(GroupEvent::ValueWrite {
            address: *address,
            value: data.to_vec(),
            source,
        });

        Ok(())
    }

    /// Write decoded value.
    pub fn write_value(
        &self,
        address: &GroupAddress,
        value: &DptValue,
        source: Option<String>,
    ) -> KnxResult<()> {
        let obj = self
            .get(address)
            .ok_or_else(|| KnxError::GroupObjectNotFound(address.to_string()))?;

        obj.write_value(value)?;

        let _ = self.event_tx.send(GroupEvent::ValueWrite {
            address: *address,
            value: obj.read_raw(),
            source,
        });

        Ok(())
    }

    /// Handle read request (record event, return value).
    pub fn handle_read_request(
        &self,
        address: &GroupAddress,
        source: Option<String>,
    ) -> KnxResult<Vec<u8>> {
        let _ = self.event_tx.send(GroupEvent::ReadRequest {
            address: *address,
            source,
        });

        let value = self.read(address)?;

        let _ = self.event_tx.send(GroupEvent::ReadResponse {
            address: *address,
            value: value.clone(),
        });

        Ok(value)
    }

    /// Iterate over all objects.
    pub fn iter(&self) -> impl Iterator<Item = Arc<GroupObject>> + '_ {
        self.objects.iter().map(|r| r.value().clone())
    }

    /// Get objects matching a predicate.
    pub fn filter<F>(&self, predicate: F) -> Vec<Arc<GroupObject>>
    where
        F: Fn(&GroupObject) -> bool,
    {
        self.objects
            .iter()
            .filter(|r| predicate(r.value()))
            .map(|r| r.value().clone())
            .collect()
    }

    /// Get objects in address range.
    pub fn range(&self, start: GroupAddress, end: GroupAddress) -> Vec<Arc<GroupObject>> {
        self.objects
            .iter()
            .filter(|r| {
                let addr = r.key().raw();
                addr >= start.raw() && addr <= end.raw()
            })
            .map(|r| r.value().clone())
            .collect()
    }
}

impl Default for GroupObjectTable {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Builder
// ============================================================================

/// Builder for creating group objects.
pub struct GroupObjectBuilder {
    address: GroupAddress,
    name: String,
    description: String,
    dpt_id: DptId,
    flags: GroupObjectFlags,
    initial_value: Option<DptValue>,
}

impl GroupObjectBuilder {
    /// Create a new builder.
    pub fn new(address: GroupAddress, dpt_id: DptId) -> Self {
        Self {
            address,
            name: format!("Group {}", address),
            description: String::new(),
            dpt_id,
            flags: GroupObjectFlags::read_write(),
            initial_value: None,
        }
    }

    /// Set name.
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = name.into();
        self
    }

    /// Set description.
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.description = description.into();
        self
    }

    /// Set flags.
    pub fn flags(mut self, flags: GroupObjectFlags) -> Self {
        self.flags = flags;
        self
    }

    /// Set read-only.
    pub fn read_only(mut self) -> Self {
        self.flags = GroupObjectFlags::read_only();
        self
    }

    /// Set write-only.
    pub fn write_only(mut self) -> Self {
        self.flags = GroupObjectFlags::write_only();
        self
    }

    /// Set initial value.
    pub fn initial_value(mut self, value: DptValue) -> Self {
        self.initial_value = Some(value);
        self
    }

    /// Build the group object.
    pub fn build(self, registry: &DptRegistry) -> KnxResult<GroupObject> {
        let codec = registry
            .get(&self.dpt_id)
            .ok_or_else(|| KnxError::InvalidDpt(format!("Unknown DPT: {}", self.dpt_id)))?;

        let mut obj = GroupObject::new(self.address, self.name, codec)
            .with_description(self.description)
            .with_flags(self.flags);

        if let Some(value) = self.initial_value {
            obj = obj.with_value(&value)?;
        }

        Ok(obj)
    }
}

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

    #[test]
    fn test_group_object_read_write() {
        let registry = DptRegistry::new();
        let codec = registry.get(&DptId::new(9, 1)).unwrap();

        let obj = GroupObject::new(GroupAddress::three_level(1, 2, 3), "Temperature", codec);

        obj.write_value(&DptValue::F16(25.5)).unwrap();

        let value = obj.read_value().unwrap();
        if let DptValue::F16(v) = value {
            assert!((v - 25.5).abs() < 0.1);
        } else {
            panic!("Expected F16");
        }
    }

    #[test]
    fn test_group_object_flags() {
        let registry = DptRegistry::new();
        let codec = registry.get(&DptId::new(1, 1)).unwrap();

        let obj = GroupObject::new(GroupAddress::three_level(1, 0, 1), "Switch", codec)
            .with_flags(GroupObjectFlags::read_only());

        assert!(obj.can_read());
        assert!(!obj.can_write());

        let result = obj.write_value(&DptValue::Bool(true));
        assert!(result.is_err());
    }

    #[test]
    fn test_group_object_table() {
        let table = GroupObjectTable::new();

        let addr1 = GroupAddress::three_level(1, 0, 1);
        let addr2 = GroupAddress::three_level(1, 0, 2);

        table.create(addr1, "Light 1", &DptId::new(1, 1)).unwrap();
        table.create(addr2, "Light 2", &DptId::new(1, 1)).unwrap();

        assert_eq!(table.len(), 2);
        assert!(table.contains(&addr1));

        table.write(&addr1, &[1], None).unwrap();
        let value = table.read(&addr1).unwrap();
        assert_eq!(value, vec![1]);
    }

    #[test]
    fn test_group_object_builder() {
        let registry = DptRegistry::new();

        // Test read-only object (no initial value since it's read-only from outside)
        let obj = GroupObjectBuilder::new(GroupAddress::three_level(2, 0, 1), DptId::new(9, 1))
            .name("Room Temperature")
            .description("Living room temperature sensor")
            .read_only()
            .build(&registry)
            .unwrap();

        assert_eq!(obj.name(), "Room Temperature");
        assert!(obj.can_read());
        assert!(!obj.can_write());

        // Test read-write object with initial value
        let obj2 = GroupObjectBuilder::new(GroupAddress::three_level(2, 0, 2), DptId::new(9, 1))
            .name("Setpoint")
            .initial_value(DptValue::F16(22.0))
            .build(&registry)
            .unwrap();

        assert_eq!(obj2.name(), "Setpoint");
        assert!(obj2.can_read());
        assert!(obj2.can_write());
    }

    #[test]
    fn test_group_event_subscription() {
        let table = GroupObjectTable::new();
        let mut rx = table.subscribe();

        let addr = GroupAddress::three_level(1, 0, 1);
        table.create(addr, "Test", &DptId::new(1, 1)).unwrap();

        // Should receive ObjectCreated event
        let event = rx.try_recv().unwrap();
        assert!(matches!(event, GroupEvent::ObjectCreated { .. }));
    }
}