rustifi 1.0.0

Open source Rust library to interface with Ubiquiti's UniFi Controller API.
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
use crate::models::common::{IpAddress, MacAddress};
use serde::Deserialize;

/// Port connector type.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
pub enum PortConnector {
    #[serde(rename = "RJ45")]
    Rj45,
    #[serde(rename = "SFP")]
    Sfp,
    #[serde(rename = "SFPPLUS")]
    SfpPlus,
    #[serde(rename = "SFP28")]
    Sfp28,
    #[serde(rename = "QSFP28")]
    Qsfp28,
    #[serde(other)]
    Unknown,
}

/// Port interface state.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum InterfaceState {
    Up,
    Down,
    #[serde(other)]
    Unknown,
}

/// Power over Ethernet configuration and state.
#[derive(Clone, Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PoE {
    /// PoE standard (e.g., "802.3bt")
    #[serde(default)]
    pub standard: Option<String>,

    /// PoE type identifier
    #[serde(default, rename = "type")]
    pub r#type: Option<i32>,

    /// Whether PoE is enabled
    #[serde(default)]
    pub enabled: Option<bool>,

    /// Current PoE state
    #[serde(default)]
    pub state: Option<String>,
}

impl PoE {
    /// Check if PoE is enabled on this port.
    pub fn is_enabled(&self) -> bool {
        self.enabled.unwrap_or(false)
    }

    /// Check if PoE is actively delivering power (enabled and state is UP).
    pub fn is_active(&self) -> bool {
        self.is_enabled() && self.state.as_deref() == Some("UP")
    }
}

/// Port information for physical interfaces.
#[derive(Clone, Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Port {
    /// Port index identifier (1-based)
    pub idx: i32,

    /// Current port state
    pub state: InterfaceState,

    /// Port connector type
    pub connector: PortConnector,

    /// Maximum speed in Mbps
    pub max_speed_mbps: i32,

    /// Current speed in Mbps
    #[serde(default)]
    pub speed_mbps: Option<i32>,

    /// Power over Ethernet configuration
    #[serde(default)]
    pub poe: Option<PoE>,
}

impl Port {
    /// Check if this port has PoE capability.
    pub fn has_poe(&self) -> bool {
        self.poe.is_some()
    }

    /// Check if this port is actively delivering PoE power.
    pub fn is_poe_active(&self) -> bool {
        self.poe.as_ref().map(|p| p.is_active()).unwrap_or(false)
    }

    /// Check if PoE is enabled on this port.
    pub fn is_poe_enabled(&self) -> bool {
        self.poe.as_ref().map(|p| p.is_enabled()).unwrap_or(false)
    }

    /// Check if the port link is up.
    pub fn is_up(&self) -> bool {
        self.state == InterfaceState::Up
    }

    /// Check if the port link is down.
    pub fn is_down(&self) -> bool {
        self.state == InterfaceState::Down
    }

    /// Get the current speed in Gbps, if available.
    pub fn speed_gbps(&self) -> Option<f64> {
        self.speed_mbps.map(|s| s as f64 / 1000.0)
    }
}

/// Wireless radio standard.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
pub enum WirelessStandard {
    #[serde(rename = "802.11a")]
    Standard802_11a,
    #[serde(rename = "802.11b")]
    Standard802_11b,
    #[serde(rename = "802.11g")]
    Standard802_11g,
    #[serde(rename = "802.11n")]
    Standard802_11n,
    #[serde(rename = "802.11ac")]
    Standard802_11ac,
    #[serde(rename = "802.11ax")]
    Standard802_11ax,
    #[serde(rename = "802.11be")]
    Standard802_11be,
    #[serde(other)]
    Unknown,
}

/// Wireless radio information.
#[derive(Clone, Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Radio {
    /// Wireless standard (802.11a, 802.11ac, etc.)
    pub wlan_standard: WirelessStandard,

    /// Frequency in GHz (2.4, 5, 6, or 60)
    #[serde(rename = "frequencyGHz")]
    pub frequency_ghz: f64,

    /// Channel width in MHz
    #[serde(rename = "channelWidthMHz")]
    pub channel_width_mhz: i32,

    /// Current channel number
    #[serde(default)]
    pub channel: Option<i32>,
}

/// Device physical interfaces.
#[derive(Clone, Debug, Deserialize, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct PhysicalInterfaces {
    /// List of physical ports
    #[serde(default)]
    pub ports: Vec<Port>,

    /// List of wireless radios
    #[serde(default)]
    pub radios: Vec<Radio>,
}

/// Device uplink connection information.
#[derive(Clone, Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct DeviceUplink {
    /// ID of the parent device in the network topology
    pub device_id: String,
}

/// Device switching feature details.
#[derive(Clone, Debug, Deserialize, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct SwitchingFeature {
    // Currently empty, but structured for future expansion
}

/// Device access point feature details.
#[derive(Clone, Debug, Deserialize, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct AccessPointFeature {
    // Currently empty, but structured for future expansion
}

/// Device features and capabilities.
#[derive(Clone, Debug, Deserialize, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct DeviceFeatures {
    /// Switching feature details (if supported)
    #[serde(default)]
    pub switching: Option<SwitchingFeature>,

    /// Access point feature details (if supported)
    #[serde(default)]
    pub access_point: Option<AccessPointFeature>,
}

/// Detailed device information from the device details endpoint.
/// Endpoint: GET /v1/sites/{siteId}/devices/{deviceId}
#[derive(Clone, Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct DeviceDetails {
    /// Unique device identifier (UUID)
    pub id: String,

    /// MAC address of the device
    pub mac_address: MacAddress,

    /// IP address of the device
    pub ip_address: IpAddress,

    /// Display name of the device
    pub name: String,

    /// Device model identifier (e.g., "UHDIW", "U6-Pro")
    pub model: String,

    /// Whether the device is supported
    pub supported: bool,

    /// Current device state (ONLINE, OFFLINE, etc.)
    pub state: String,

    /// Current firmware version
    #[serde(default)]
    pub firmware_version: Option<String>,

    /// Whether firmware can be updated
    pub firmware_updatable: bool,

    /// When the device was adopted (ISO 8601 timestamp)
    #[serde(default)]
    pub adopted_at: Option<String>,

    /// When the device was provisioned (ISO 8601 timestamp)
    #[serde(default)]
    pub provisioned_at: Option<String>,

    /// Configuration ID
    pub configuration_id: String,

    /// Device uplink connection info (optional)
    #[serde(default)]
    pub uplink: Option<DeviceUplink>,

    /// Device features and capabilities
    #[serde(default)]
    pub features: DeviceFeatures,

    /// Physical interfaces (ports and radios)
    #[serde(default)]
    pub interfaces: PhysicalInterfaces,
}

impl DeviceDetails {
    /// Check if the device is currently online.
    pub fn is_online(&self) -> bool {
        self.state == "ONLINE"
    }

    /// Check if the device is currently offline.
    pub fn is_offline(&self) -> bool {
        self.state == "OFFLINE"
    }

    /// Get the number of available ports.
    pub fn port_count(&self) -> usize {
        self.interfaces.ports.len()
    }

    /// Get the number of available radios.
    pub fn radio_count(&self) -> usize {
        self.interfaces.radios.len()
    }

    /// Check if the device has switching capability.
    pub fn has_switching(&self) -> bool {
        self.features.switching.is_some()
    }

    /// Check if the device has access point capability.
    pub fn has_access_point(&self) -> bool {
        self.features.access_point.is_some()
    }

    /// Get all ports that are currently UP.
    pub fn active_ports(&self) -> Vec<&Port> {
        self.interfaces
            .ports
            .iter()
            .filter(|p| p.state == InterfaceState::Up)
            .collect()
    }

    /// Get all ports that are currently DOWN.
    pub fn inactive_ports(&self) -> Vec<&Port> {
        self.interfaces
            .ports
            .iter()
            .filter(|p| p.state == InterfaceState::Down)
            .collect()
    }

    /// Check if this device is a switch (has switching capability).
    pub fn is_switch(&self) -> bool {
        self.has_switching()
    }

    /// Get all ports with PoE capability.
    pub fn poe_ports(&self) -> Vec<&Port> {
        self.interfaces
            .ports
            .iter()
            .filter(|p| p.has_poe())
            .collect()
    }

    /// Get all ports with PoE enabled.
    pub fn poe_enabled_ports(&self) -> Vec<&Port> {
        self.interfaces
            .ports
            .iter()
            .filter(|p| p.is_poe_enabled())
            .collect()
    }

    /// Get all ports actively delivering PoE power.
    pub fn poe_active_ports(&self) -> Vec<&Port> {
        self.interfaces
            .ports
            .iter()
            .filter(|p| p.is_poe_active())
            .collect()
    }

    /// Get the count of PoE-capable ports.
    pub fn poe_port_count(&self) -> usize {
        self.poe_ports().len()
    }
}

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

    #[test]
    fn test_radio_deserialization() {
        let radio_json = json!({
            "wlanStandard": "802.11a",
            "frequencyGHz": 2.4,
            "channelWidthMHz": 40,
            "channel": 36
        });

        match serde_json::from_value::<Radio>(radio_json) {
            Ok(radio) => assert_eq!(radio.frequency_ghz, 2.4),
            Err(e) => panic!("Failed to deserialize radio: {}", e),
        }
    }

    #[test]
    fn test_device_details_deserialization() {
        let radio_json = json!({
            "wlanStandard": "802.11a",
            "frequencyGHz": 2.4,
            "channelWidthMHz": 40,
            "channel": 36
        });

        match serde_json::from_value::<Radio>(radio_json) {
            Ok(radio) => assert_eq!(radio.frequency_ghz, 2.4),
            Err(e) => panic!("Failed to deserialize radio in device test: {}", e),
        }

        let json_data = json!({
            "id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
            "macAddress": "94:2a:6f:26:c6:ca",
            "ipAddress": "192.168.1.55",
            "name": "IW HD",
            "model": "UHDIW",
            "supported": true,
            "state": "ONLINE",
            "firmwareVersion": "6.6.55",
            "firmwareUpdatable": true,
            "adoptedAt": "2019-08-24T14:15:22Z",
            "provisionedAt": "2019-08-24T14:15:22Z",
            "configurationId": "7596498d2f367dc2",
            "uplink": {
                "deviceId": "4de4adb9-21ee-47e3-aeb4-8cf8ed6c109a"
            },
            "features": {
                "switching": null,
                "accessPoint": null
            },
            "interfaces": {
                "ports": [
                    {
                        "idx": 1,
                        "state": "UP",
                        "connector": "RJ45",
                        "maxSpeedMbps": 10000,
                        "speedMbps": 1000,
                        "poe": {
                            "standard": "802.3bt",
                            "type": 3,
                            "enabled": true,
                            "state": "UP"
                        }
                    }
                ],
                "radios": [
                    {
                        "wlanStandard": "802.11a",
                        "frequencyGHz": 2.4,
                        "channelWidthMHz": 40,
                        "channel": 36
                    }
                ]
            }
        });

        let device: DeviceDetails = serde_json::from_value(json_data).unwrap();

        assert_eq!(device.id, "497f6eca-6276-4993-bfeb-53cbbbba6f08");
        assert_eq!(device.mac_address.as_str(), "94:2a:6f:26:c6:ca");
        assert_eq!(device.ip_address.to_string(), "192.168.1.55");
        assert_eq!(device.name, "IW HD");
        assert_eq!(device.model, "UHDIW");
        assert!(device.supported);
        assert_eq!(device.state, "ONLINE");
        assert_eq!(device.firmware_version, Some("6.6.55".to_string()));
        assert!(device.firmware_updatable);
        assert_eq!(device.port_count(), 1);
        assert_eq!(device.radio_count(), 1);
        assert!(device.is_online());
    }

    #[test]
    fn test_device_details_ports() {
        let json_data = json!({
            "id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
            "macAddress": "94:2a:6f:26:c6:ca",
            "ipAddress": "192.168.1.55",
            "name": "Test Switch",
            "model": "USW-24",
            "supported": true,
            "state": "ONLINE",
            "firmwareUpdatable": false,
            "configurationId": "config123",
            "uplink": {
                "deviceId": "uplink-device"
            },
            "features": {},
            "interfaces": {
                "ports": [
                    {
                        "idx": 1,
                        "state": "UP",
                        "connector": "RJ45",
                        "maxSpeedMbps": 1000,
                        "speedMbps": 1000
                    },
                    {
                        "idx": 2,
                        "state": "DOWN",
                        "connector": "RJ45",
                        "maxSpeedMbps": 1000
                    }
                ],
                "radios": []
            }
        });

        let device: DeviceDetails = serde_json::from_value(json_data).unwrap();

        assert_eq!(device.port_count(), 2);
        assert_eq!(device.radio_count(), 0);
        assert_eq!(device.active_ports().len(), 1);
        assert_eq!(device.inactive_ports().len(), 1);
    }

    #[test]
    fn test_device_details_features() {
        let json_data = json!({
            "id": "device-with-features",
            "macAddress": "94:2a:6f:26:c6:ca",
            "ipAddress": "192.168.1.55",
            "name": "Multi-function Device",
            "model": "UDM-Pro",
            "supported": true,
            "state": "ONLINE",
            "firmwareUpdatable": false,
            "configurationId": "config123",
            "uplink": {
                "deviceId": "uplink-device"
            },
            "features": {
                "switching": {},
                "accessPoint": {}
            },
            "interfaces": {
                "ports": [],
                "radios": []
            }
        });

        let device: DeviceDetails = serde_json::from_value(json_data).unwrap();

        assert!(device.has_switching());
        assert!(device.has_access_point());
    }

    #[test]
    fn test_wireless_standard_deserialization() {
        let standards = vec![
            ("\"802.11a\"", WirelessStandard::Standard802_11a),
            ("\"802.11b\"", WirelessStandard::Standard802_11b),
            ("\"802.11g\"", WirelessStandard::Standard802_11g),
            ("\"802.11n\"", WirelessStandard::Standard802_11n),
            ("\"802.11ac\"", WirelessStandard::Standard802_11ac),
            ("\"802.11ax\"", WirelessStandard::Standard802_11ax),
            ("\"802.11be\"", WirelessStandard::Standard802_11be),
        ];

        for (json_str, expected) in standards {
            let standard: WirelessStandard = serde_json::from_str(json_str).unwrap();
            assert_eq!(standard, expected);
        }
    }

    #[test]
    fn test_device_details_real_world_response() {
        // Test with anonymized response based on real device structure
        // Uses RFC 5737 test IP (198.51.100.x) and RFC 7042 example MAC (00:00:5e:00:53:xx)
        let json_data = json!({
            "id": "device-0001-0000-0000-000000000001",
            "macAddress": "00:00:5e:00:53:01",
            "ipAddress": "198.51.100.1",
            "name": "Test Device UX7",
            "model": "Express 7",
            "supported": true,
            "state": "ONLINE",
            "firmwareVersion": "5.0.10",
            "firmwareUpdatable": false,
            "provisionedAt": "2025-12-30T21:27:06Z",
            "configurationId": "config-0001",
            "uplink": {
                "deviceId": "uplink-device-0001"
            },
            "features": {
                "switching": {},
                "accessPoint": {}
            },
            "interfaces": {
                "ports": [
                    {
                        "idx": 1,
                        "state": "UP",
                        "connector": "RJ45",
                        "maxSpeedMbps": 2500,
                        "speedMbps": 1000
                    },
                    {
                        "idx": 2,
                        "state": "UP",
                        "connector": "RJ45",
                        "maxSpeedMbps": 10000,
                        "speedMbps": 1000
                    }
                ],
                "radios": [
                    {
                        "wlanStandard": "802.11be",
                        "frequencyGHz": 2.4,
                        "channelWidthMHz": 20,
                        "channel": 1
                    },
                    {
                        "wlanStandard": "802.11be",
                        "frequencyGHz": 5,
                        "channelWidthMHz": 80,
                        "channel": 48
                    },
                    {
                        "wlanStandard": "802.11be",
                        "frequencyGHz": 6,
                        "channelWidthMHz": 160,
                        "channel": 37
                    }
                ]
            }
        });

        let device: DeviceDetails =
            serde_json::from_value(json_data).expect("Failed to deserialize real-world response");

        assert_eq!(device.id, "device-0001-0000-0000-000000000001");
        assert_eq!(device.name, "Test Device UX7");
        assert_eq!(device.model, "Express 7");
        assert!(device.supported);
        assert_eq!(device.state, "ONLINE");
        assert!(device.is_online());
        assert_eq!(device.port_count(), 2);
        assert_eq!(device.radio_count(), 3);
        assert!(device.has_switching());
        assert!(device.has_access_point());

        // Verify ports
        assert_eq!(device.active_ports().len(), 2);
        assert_eq!(device.inactive_ports().len(), 0);

        let port1 = &device.interfaces.ports[0];
        assert_eq!(port1.idx, 1);
        assert_eq!(port1.max_speed_mbps, 2500);
        assert_eq!(port1.speed_mbps, Some(1000));

        let port2 = &device.interfaces.ports[1];
        assert_eq!(port2.idx, 2);
        assert_eq!(port2.max_speed_mbps, 10000);

        // Verify radios
        let radio1 = &device.interfaces.radios[0];
        assert_eq!(radio1.wlan_standard, WirelessStandard::Standard802_11be);
        assert_eq!(radio1.frequency_ghz, 2.4);
        assert_eq!(radio1.channel, Some(1));

        let radio2 = &device.interfaces.radios[1];
        assert_eq!(radio2.frequency_ghz, 5.0);
        assert_eq!(radio2.channel, Some(48));

        let radio3 = &device.interfaces.radios[2];
        assert_eq!(radio3.frequency_ghz, 6.0);
        assert_eq!(radio3.channel, Some(37));
    }

    #[test]
    fn test_device_details_without_optional_fields() {
        // Test with minimal required fields (no adoptedAt, no uplink)
        let json_data = json!({
            "id": "minimal-device",
            "macAddress": "1c:0b:8b:3e:5c:16",
            "ipAddress": "192.168.1.100",
            "name": "Minimal Device",
            "model": "Test",
            "supported": true,
            "state": "ONLINE",
            "firmwareUpdatable": false,
            "configurationId": "config123",
            "features": {},
            "interfaces": {
                "ports": [],
                "radios": []
            }
        });

        let device: DeviceDetails =
            serde_json::from_value(json_data).expect("Failed to deserialize minimal response");

        assert_eq!(device.id, "minimal-device");
        assert_eq!(device.name, "Minimal Device");
        assert!(device.is_online());
        assert!(device.adopted_at.is_none());
        assert!(device.uplink.is_none());
        assert_eq!(device.port_count(), 0);
        assert_eq!(device.radio_count(), 0);
    }

    #[test]
    fn test_port_connector_deserialization() {
        let connectors = vec![
            ("\"RJ45\"", PortConnector::Rj45),
            ("\"SFP\"", PortConnector::Sfp),
            ("\"SFPPLUS\"", PortConnector::SfpPlus),
            ("\"SFP28\"", PortConnector::Sfp28),
            ("\"QSFP28\"", PortConnector::Qsfp28),
        ];

        for (json_str, expected) in connectors {
            let connector: PortConnector = serde_json::from_str(json_str).unwrap();
            assert_eq!(connector, expected);
        }
    }

    #[test]
    fn test_interface_state_deserialization() {
        let up: InterfaceState = serde_json::from_str("\"UP\"").unwrap();
        let down: InterfaceState = serde_json::from_str("\"DOWN\"").unwrap();
        let unknown: InterfaceState = serde_json::from_str("\"UNKNOWN\"").unwrap();

        assert_eq!(up, InterfaceState::Up);
        assert_eq!(down, InterfaceState::Down);
        assert_eq!(unknown, InterfaceState::Unknown);
    }

    #[test]
    fn test_device_details_with_exact_api_response() {
        // Test with exact response from API docs
        let json_data = json!({
            "id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
            "macAddress": "94:2a:6f:26:c6:ca",
            "ipAddress": "192.168.1.55",
            "name": "IW HD",
            "model": "UHDIW",
            "supported": true,
            "state": "ONLINE",
            "firmwareVersion": "6.6.55",
            "firmwareUpdatable": true,
            "adoptedAt": "2019-08-24T14:15:22Z",
            "provisionedAt": "2019-08-24T14:15:22Z",
            "configurationId": "7596498d2f367dc2",
            "uplink": {
                "deviceId": "4de4adb9-21ee-47e3-aeb4-8cf8ed6c109a"
            },
            "features": {
                "switching": null,
                "accessPoint": null
            },
            "interfaces": {
                "ports": [
                    {
                        "idx": 1,
                        "state": "UP",
                        "connector": "RJ45",
                        "maxSpeedMbps": 10000,
                        "speedMbps": 1000,
                        "poe": {
                            "standard": "802.3bt",
                            "type": 3,
                            "enabled": true,
                            "state": "UP"
                        }
                    }
                ],
                "radios": [
                    {
                        "wlanStandard": "802.11a",
                        "frequencyGHz": 2.4,
                        "channelWidthMHz": 40,
                        "channel": 36
                    }
                ]
            }
        });

        let device: DeviceDetails =
            serde_json::from_value(json_data).expect("Failed to deserialize exact API response");

        // Verify all fields deserialized correctly
        assert_eq!(device.id, "497f6eca-6276-4993-bfeb-53cbbbba6f08");
        assert_eq!(device.mac_address.as_str(), "94:2a:6f:26:c6:ca");
        assert_eq!(device.ip_address.to_string(), "192.168.1.55");
        assert_eq!(device.name, "IW HD");
        assert_eq!(device.model, "UHDIW");
        assert!(device.supported);
        assert_eq!(device.state, "ONLINE");
        assert_eq!(device.firmware_version, Some("6.6.55".to_string()));
        assert!(device.firmware_updatable);
        assert!(device.is_online());

        // Verify ports and radios
        assert_eq!(device.port_count(), 1);
        assert_eq!(device.radio_count(), 1);

        let port = &device.interfaces.ports[0];
        assert_eq!(port.idx, 1);
        assert_eq!(port.state, InterfaceState::Up);
        assert_eq!(port.connector, PortConnector::Rj45);
        assert_eq!(port.max_speed_mbps, 10000);
        assert_eq!(port.speed_mbps, Some(1000));
        assert!(port.poe.is_some());
        let poe = port.poe.as_ref().unwrap();
        assert_eq!(poe.standard, Some("802.3bt".to_string()));
        assert_eq!(poe.r#type, Some(3));
        assert_eq!(poe.enabled, Some(true));

        let radio = &device.interfaces.radios[0];
        assert_eq!(radio.wlan_standard, WirelessStandard::Standard802_11a);
        assert_eq!(radio.frequency_ghz, 2.4);
        assert_eq!(radio.channel_width_mhz, 40);
        assert_eq!(radio.channel, Some(36));
    }

    #[test]
    fn test_poe_methods() {
        // Test PoE with enabled and UP state
        let poe_active = PoE {
            standard: Some("802.3bt".to_string()),
            r#type: Some(3),
            enabled: Some(true),
            state: Some("UP".to_string()),
        };
        assert!(poe_active.is_enabled());
        assert!(poe_active.is_active());

        // Test PoE with enabled but DOWN state
        let poe_enabled_down = PoE {
            standard: Some("802.3at".to_string()),
            r#type: Some(2),
            enabled: Some(true),
            state: Some("DOWN".to_string()),
        };
        assert!(poe_enabled_down.is_enabled());
        assert!(!poe_enabled_down.is_active());

        // Test PoE disabled
        let poe_disabled = PoE {
            standard: Some("802.3af".to_string()),
            r#type: Some(1),
            enabled: Some(false),
            state: None,
        };
        assert!(!poe_disabled.is_enabled());
        assert!(!poe_disabled.is_active());

        // Test PoE with None values
        let poe_none = PoE {
            standard: None,
            r#type: None,
            enabled: None,
            state: None,
        };
        assert!(!poe_none.is_enabled());
        assert!(!poe_none.is_active());
    }

    #[test]
    fn test_port_poe_methods() {
        // Port with active PoE
        let port_with_poe = Port {
            idx: 1,
            state: InterfaceState::Up,
            connector: PortConnector::Rj45,
            max_speed_mbps: 1000,
            speed_mbps: Some(1000),
            poe: Some(PoE {
                standard: Some("802.3bt".to_string()),
                r#type: Some(3),
                enabled: Some(true),
                state: Some("UP".to_string()),
            }),
        };
        assert!(port_with_poe.has_poe());
        assert!(port_with_poe.is_poe_enabled());
        assert!(port_with_poe.is_poe_active());

        // Port with disabled PoE
        let port_poe_disabled = Port {
            idx: 2,
            state: InterfaceState::Up,
            connector: PortConnector::Rj45,
            max_speed_mbps: 1000,
            speed_mbps: Some(1000),
            poe: Some(PoE {
                standard: Some("802.3at".to_string()),
                r#type: Some(2),
                enabled: Some(false),
                state: None,
            }),
        };
        assert!(port_poe_disabled.has_poe());
        assert!(!port_poe_disabled.is_poe_enabled());
        assert!(!port_poe_disabled.is_poe_active());

        // Port without PoE
        let port_no_poe = Port {
            idx: 3,
            state: InterfaceState::Up,
            connector: PortConnector::Sfp,
            max_speed_mbps: 10000,
            speed_mbps: Some(10000),
            poe: None,
        };
        assert!(!port_no_poe.has_poe());
        assert!(!port_no_poe.is_poe_enabled());
        assert!(!port_no_poe.is_poe_active());
    }

    #[test]
    fn test_port_state_methods() {
        let port_up = Port {
            idx: 1,
            state: InterfaceState::Up,
            connector: PortConnector::Rj45,
            max_speed_mbps: 1000,
            speed_mbps: Some(1000),
            poe: None,
        };
        assert!(port_up.is_up());
        assert!(!port_up.is_down());
        assert_eq!(port_up.speed_gbps(), Some(1.0));

        let port_down = Port {
            idx: 2,
            state: InterfaceState::Down,
            connector: PortConnector::Rj45,
            max_speed_mbps: 1000,
            speed_mbps: None,
            poe: None,
        };
        assert!(!port_down.is_up());
        assert!(port_down.is_down());
        assert_eq!(port_down.speed_gbps(), None);

        // Test 10G port speed
        let port_10g = Port {
            idx: 3,
            state: InterfaceState::Up,
            connector: PortConnector::SfpPlus,
            max_speed_mbps: 10000,
            speed_mbps: Some(10000),
            poe: None,
        };
        assert_eq!(port_10g.speed_gbps(), Some(10.0));
    }

    #[test]
    fn test_device_details_switch_methods() {
        // Create a switch device with multiple ports
        let json_data = json!({
            "id": "switch-device-id",
            "macAddress": "aa:bb:cc:dd:ee:ff",
            "ipAddress": "192.168.1.100",
            "name": "USW-24-PoE",
            "model": "USW-24-PoE",
            "supported": true,
            "state": "ONLINE",
            "firmwareUpdatable": false,
            "configurationId": "config123",
            "features": {
                "switching": {},
                "accessPoint": null
            },
            "interfaces": {
                "ports": [
                    {
                        "idx": 1,
                        "state": "UP",
                        "connector": "RJ45",
                        "maxSpeedMbps": 1000,
                        "speedMbps": 1000,
                        "poe": {
                            "standard": "802.3at",
                            "type": 2,
                            "enabled": true,
                            "state": "UP"
                        }
                    },
                    {
                        "idx": 2,
                        "state": "UP",
                        "connector": "RJ45",
                        "maxSpeedMbps": 1000,
                        "speedMbps": 1000,
                        "poe": {
                            "standard": "802.3at",
                            "type": 2,
                            "enabled": true,
                            "state": "DOWN"
                        }
                    },
                    {
                        "idx": 3,
                        "state": "DOWN",
                        "connector": "RJ45",
                        "maxSpeedMbps": 1000,
                        "poe": {
                            "standard": "802.3at",
                            "type": 2,
                            "enabled": false,
                            "state": null
                        }
                    },
                    {
                        "idx": 25,
                        "state": "UP",
                        "connector": "SFP",
                        "maxSpeedMbps": 1000,
                        "speedMbps": 1000
                    }
                ],
                "radios": []
            }
        });

        let device: DeviceDetails = serde_json::from_value(json_data).unwrap();

        // Test is_switch
        assert!(device.is_switch());
        assert!(device.has_switching());
        assert!(!device.has_access_point());

        // Test port counts
        assert_eq!(device.port_count(), 4);
        assert_eq!(device.active_ports().len(), 3);
        assert_eq!(device.inactive_ports().len(), 1);

        // Test PoE port methods
        assert_eq!(device.poe_port_count(), 3); // 3 ports have PoE capability
        assert_eq!(device.poe_ports().len(), 3);
        assert_eq!(device.poe_enabled_ports().len(), 2); // 2 ports have PoE enabled
        assert_eq!(device.poe_active_ports().len(), 1); // Only 1 port is actively delivering power

        // Verify the active PoE port is port 1
        let active_poe = device.poe_active_ports();
        assert_eq!(active_poe[0].idx, 1);
    }

    #[test]
    fn test_device_details_access_point_not_switch() {
        let json_data = json!({
            "id": "ap-device-id",
            "macAddress": "aa:bb:cc:dd:ee:ff",
            "ipAddress": "192.168.1.101",
            "name": "U6-Pro",
            "model": "U6-Pro",
            "supported": true,
            "state": "ONLINE",
            "firmwareUpdatable": false,
            "configurationId": "config123",
            "features": {
                "switching": null,
                "accessPoint": {}
            },
            "interfaces": {
                "ports": [
                    {
                        "idx": 1,
                        "state": "UP",
                        "connector": "RJ45",
                        "maxSpeedMbps": 2500,
                        "speedMbps": 1000
                    }
                ],
                "radios": [
                    {
                        "wlanStandard": "802.11ax",
                        "frequencyGHz": 5,
                        "channelWidthMHz": 80,
                        "channel": 36
                    }
                ]
            }
        });

        let device: DeviceDetails = serde_json::from_value(json_data).unwrap();

        assert!(!device.is_switch());
        assert!(!device.has_switching());
        assert!(device.has_access_point());
        assert_eq!(device.poe_port_count(), 0);
        assert_eq!(device.radio_count(), 1);
    }
}