6
6
7
7
namespace ZigBeeNet
8
8
{
9
- public class ZigBeeEndpointAddress : IZigBeeAddress
9
+ public struct ZigBeeEndpointAddress : IZigBeeAddress , IComparable < ZigBeeEndpointAddress >
10
10
{
11
- public byte Endpoint { get ; private set ; }
11
+ public static ZigBeeEndpointAddress Zero { get ; } = new ZigBeeEndpointAddress ( 0 , 0 ) ;
12
+ public static ZigBeeEndpointAddress BROADCAST_RX_ON { get ; } = new ZigBeeEndpointAddress ( ZigBeeBroadcastDestination . BROADCAST_RX_ON ) ;
13
+ public static ZigBeeEndpointAddress BROADCAST_ALL_DEVICES { get ; } = new ZigBeeEndpointAddress ( ZigBeeBroadcastDestination . BROADCAST_ALL_DEVICES ) ;
14
+ public static ZigBeeEndpointAddress BROADCAST_LOW_POWER_ROUTERS { get ; } = new ZigBeeEndpointAddress ( ZigBeeBroadcastDestination . BROADCAST_LOW_POWER_ROUTERS ) ;
15
+ public static ZigBeeEndpointAddress BROADCAST_ROUTERS_AND_COORD { get ; } = new ZigBeeEndpointAddress ( ZigBeeBroadcastDestination . BROADCAST_ROUTERS_AND_COORD ) ;
12
16
13
- public ushort Address { get ; set ; }
17
+ public ushort Address { get ; }
18
+ public byte Endpoint { get ; }
14
19
15
- public bool IsGroup
16
- {
17
- get { return false ; }
18
- }
20
+ public bool IsGroup => false ;
19
21
20
22
/// <summary>
21
23
/// Constructor for ZDO ZigBee devices where only the address is defined
@@ -26,8 +28,14 @@ public bool IsGroup
26
28
/// </summary>
27
29
public ZigBeeEndpointAddress ( ushort address )
28
30
{
29
- this . Address = address ;
30
- this . Endpoint = 0 ;
31
+ Address = address ;
32
+ Endpoint = 0 ;
33
+ }
34
+
35
+ public ZigBeeEndpointAddress ( ZigBeeBroadcastDestination address )
36
+ {
37
+ Address = ( ushort ) address ;
38
+ Endpoint = 0 ;
31
39
}
32
40
33
41
/// <summary>
@@ -40,83 +48,50 @@ public ZigBeeEndpointAddress(ushort address)
40
48
/// </summary>
41
49
public ZigBeeEndpointAddress ( ushort address , byte endpoint )
42
50
{
43
- this . Address = address ;
44
- this . Endpoint = endpoint ;
51
+ Address = address ;
52
+ Endpoint = endpoint ;
45
53
}
46
54
47
55
public ZigBeeEndpointAddress ( string address )
48
56
{
49
- if ( address . Contains ( "/" ) )
50
- {
51
- var splits = address . Split ( '/' ) ;
52
- if ( splits . Length > 2 )
53
- {
54
- throw new ArgumentException ( nameof ( address ) ) ;
55
- }
56
- this . Address = ushort . Parse ( splits [ 0 ] ) ;
57
- this . Endpoint = byte . Parse ( splits [ 1 ] ) ;
58
- }
57
+ if ( TryParse ( address , out ( ushort a , byte e ) result ) )
58
+ ( Address , Endpoint ) = result ;
59
59
else
60
- {
61
- this . Address = ushort . Parse ( address ) ;
62
- this . Endpoint = 0 ;
63
- }
60
+ throw new ArgumentException ( nameof ( address ) ) ;
64
61
}
65
62
66
- public override int GetHashCode ( )
63
+ private static bool TryParse ( string address , out ( ushort addr , byte endpt ) result )
67
64
{
68
- byte [ ] hash = new byte [ 3 ] ;
69
-
70
- hash [ 0 ] = Address . GetByte ( 0 ) ;
71
- hash [ 1 ] = Address . GetByte ( 1 ) ;
72
- hash [ 2 ] = Endpoint ;
73
-
74
- return Hash . CalcHashCode ( hash ) ;
75
- }
76
-
77
- public override bool Equals ( object obj )
78
- {
79
- if ( obj == null )
80
- {
81
- return false ;
82
- }
83
-
84
- if ( ! typeof ( ZigBeeEndpointAddress ) . IsAssignableFrom ( obj . GetType ( ) ) )
85
- {
65
+ result = ( 0 , 0 ) ;
66
+ if ( string . IsNullOrWhiteSpace ( address ) )
86
67
return false ;
87
- }
88
-
89
- ZigBeeEndpointAddress other = ( ZigBeeEndpointAddress ) obj ;
90
-
91
- return ( other . Address == Address && other . Endpoint == Endpoint ) ;
68
+ string [ ] splits = address . Split ( '/' ) ;
69
+ return ( splits . Length == 1 && ushort . TryParse ( address , out result . addr ) )
70
+ || ( splits . Length == 2 && ushort . TryParse ( splits [ 0 ] , out result . addr ) && byte . TryParse ( splits [ 1 ] , out result . endpt ) ) ;
92
71
}
93
72
94
- public int CompareTo ( IZigBeeAddress that )
73
+ public static bool TryParse ( string address , out ZigBeeEndpointAddress result )
95
74
{
96
- if ( this == that )
75
+ if ( TryParse ( address , out ( ushort addr , byte endpt ) r ) )
97
76
{
98
- return 0 ;
77
+ result = new ZigBeeEndpointAddress ( r . addr , r . endpt ) ;
78
+ return true ;
99
79
}
100
-
101
- ZigBeeEndpointAddress thatAddr = ( ZigBeeEndpointAddress ) that ;
102
-
103
- if ( thatAddr . Address == Address && thatAddr . Endpoint == Endpoint )
104
- {
105
- return 0 ;
106
- }
107
-
108
- if ( thatAddr . Address == Address )
109
- {
110
- return Endpoint - thatAddr . Endpoint ;
111
- }
112
-
113
- return Address - thatAddr . Address ;
80
+ result = default ;
81
+ return false ;
114
82
}
115
83
116
- public override string ToString ( )
84
+ public override int GetHashCode ( ) => Endpoint << 16 | Address ;
85
+
86
+ public override bool Equals ( object obj )
117
87
{
118
- return Address + "/" + Endpoint ;
88
+ return ! ( obj is null )
89
+ && ( obj is ZigBeeEndpointAddress objAddr )
90
+ && objAddr . Address == Address && objAddr . Endpoint == Endpoint ;
119
91
}
120
92
93
+ public int CompareTo ( ZigBeeEndpointAddress other ) => ( Address == other . Address ) ? ( int ) Endpoint - ( int ) other . Endpoint : ( int ) Address - ( int ) other . Address ;
94
+
95
+ public override string ToString ( ) => Address + "/" + Endpoint ;
121
96
}
122
97
}
0 commit comments