77using System . Linq ;
88using System . Net ;
99using System . Net . Sockets ;
10- using System . Text ;
11- using System . Threading ;
12- using System . Threading . Tasks ;
1310
1411namespace Aurora . Devices . UDP
1512{
16- public class UDPDevice : Device
13+ public class UdpDevice : IDevice
1714 {
18- private const string deviceName = "UDP" ;
19- private bool isInitialized = false ;
15+ public string DeviceName => "UDP" ;
16+ public bool IsInitialized { get ; private set ; } = false ;
2017
2118 private VariableRegistry defaultRegistry = null ;
2219
@@ -31,67 +28,60 @@ public class UDPDevice : Device
3128 private Color lastColor ;
3229 private long lastUpdateTime = long . MaxValue ;
3330
34- public string GetDeviceDetails ( )
35- {
36- return deviceName + ( isInitialized ? ": Initialized" : ": Not initialized" ) ;
37- }
31+ public string DeviceDetails => DeviceName + ( IsInitialized ? ": Initialized" : ": Not initialized" ) ;
32+ public string DeviceUpdatePerformance => ( IsInitialized ? lastUpdateTime + " ms" : "" ) ;
3833
39- public string GetDeviceName ( )
34+ public VariableRegistry RegisteredVariables
4035 {
41- return deviceName ;
36+ get
37+ {
38+ if ( defaultRegistry == null )
39+ {
40+ var devKeysEnumAsEnumerable = Enum . GetValues ( typeof ( DeviceKeys ) ) . Cast < DeviceKeys > ( ) ;
41+
42+ defaultRegistry = new VariableRegistry ( ) ;
43+ defaultRegistry . Register ( $ "{ DeviceName } _devicekey", DeviceKeys . Peripheral , "Key Color to Use" ,
44+ devKeysEnumAsEnumerable . Max ( ) , devKeysEnumAsEnumerable . Min ( ) ) ;
45+ defaultRegistry . Register ( $ "{ DeviceName } _led_count", 300 , "LED Count" ) ;
46+ defaultRegistry . Register ( $ "{ DeviceName } _ip", "" , "IP Adresses (Comma separated)" ) ;
47+ defaultRegistry . Register ( $ "{ DeviceName } _port", 19446 , "UDP Port" ) ;
48+ }
49+
50+ return defaultRegistry ;
51+ }
4252 }
4353
54+
4455 public bool Initialize ( )
4556 {
46- if ( isInitialized ) return true ;
57+ if ( IsInitialized ) return true ;
4758
48- deviceKey = Global . Configuration . VarRegistry . GetVariable < DeviceKeys > ( $ "{ deviceName } _devicekey") ;
49- ledCount = Global . Configuration . VarRegistry . GetVariable < int > ( $ "{ deviceName } _led_count") ;
59+ deviceKey = Global . Configuration . VarRegistry . GetVariable < DeviceKeys > ( $ "{ DeviceName } _devicekey") ;
60+ ledCount = Global . Configuration . VarRegistry . GetVariable < int > ( $ "{ DeviceName } _led_count") ;
5061
51- var udpPort = Global . Configuration . VarRegistry . GetVariable < int > ( $ "{ deviceName } _port") ;
52- var ips = Global . Configuration . VarRegistry . GetVariable < string > ( $ "{ deviceName } _ip") . Split ( ',' ) ;
62+ var udpPort = Global . Configuration . VarRegistry . GetVariable < int > ( $ "{ DeviceName } _port") ;
63+ var ips = Global . Configuration . VarRegistry . GetVariable < string > ( $ "{ DeviceName } _ip") . Split ( ',' ) ;
5364
5465 endpoints = new List < IPEndPoint > ( ) ;
5566 foreach ( var ip in ips )
5667 {
5768 try
5869 {
5970 endpoints . Add ( new IPEndPoint ( IPAddress . Parse ( ip ) , udpPort ) ) ;
60- } catch ( FormatException ) { } // Don't crash on malformed IPs
71+ }
72+ catch ( FormatException )
73+ {
74+ } // Don't crash on malformed IPs
6175 }
6276
6377 udpClient = new UdpClient ( ) ;
6478 lastColor = Color . Black ;
6579 redrawWatch . Start ( ) ;
6680
67- isInitialized = true ;
81+ IsInitialized = true ;
6882 return true ;
6983 }
7084
71- public bool IsConnected ( )
72- {
73- return isInitialized ; // UDP has no connections
74- }
75-
76- public bool IsInitialized ( )
77- {
78- return isInitialized ;
79- }
80-
81- public bool IsKeyboardConnected ( )
82- {
83- return ( isInitialized && deviceKey != DeviceKeys . Peripheral ) ;
84- }
85-
86- public bool IsPeripheralConnected ( )
87- {
88- return ( isInitialized && deviceKey == DeviceKeys . Peripheral ) ;
89- }
90-
91- public bool Reconnect ( )
92- {
93- return true ; // UDP has no connections
94- }
9585
9686 public void Reset ( )
9787 {
@@ -106,7 +96,7 @@ public void Shutdown()
10696 udpClient = null ;
10797 redrawWatch . Stop ( ) ;
10898
109- isInitialized = false ;
99+ IsInitialized = false ;
110100 }
111101
112102 public bool UpdateDevice ( DeviceColorComposition colorComposition , DoWorkEventArgs e , bool forced = false )
@@ -116,26 +106,28 @@ public bool UpdateDevice(DeviceColorComposition colorComposition, DoWorkEventArg
116106
117107 public bool UpdateDevice ( Dictionary < DeviceKeys , Color > keyColors , DoWorkEventArgs e , bool forced = false )
118108 {
119- if ( ! isInitialized ) return false ;
109+ if ( ! IsInitialized ) return false ;
120110
121111 if ( keyColors . ContainsKey ( deviceKey ) )
122112 {
123113 updateStopwatch . Restart ( ) ;
124114
125115 var c = keyColors [ deviceKey ] ;
126- if ( redrawWatch . ElapsedMilliseconds < 1000 ) // Only send the color when it changes or a full second has passed
116+ if ( redrawWatch . ElapsedMilliseconds < 1000
117+ ) // Only send the color when it changes or a full second has passed
127118 {
128119 if ( c == lastColor && ! forced ) return true ;
129120 }
130121 else
131122 {
132123 redrawWatch . Restart ( ) ;
133124 }
125+
134126 lastColor = c ;
135127
136128 // Build a payload by repeating the color ledCount times
137129 var payload = new byte [ 3 * ledCount ] ;
138- for ( int i = 0 ; i < ledCount * 3 ; i += 3 )
130+ for ( var i = 0 ; i < ledCount * 3 ; i += 3 )
139131 {
140132 payload [ i + 0 ] = c . R ;
141133 payload [ i + 1 ] = c . G ;
@@ -151,27 +143,8 @@ public bool UpdateDevice(Dictionary<DeviceKeys, Color> keyColors, DoWorkEventArg
151143 updateStopwatch . Stop ( ) ;
152144 lastUpdateTime = updateStopwatch . ElapsedMilliseconds ;
153145 }
154- return true ;
155- }
156146
157- public string GetDeviceUpdatePerformance ( )
158- {
159- return ( isInitialized ? lastUpdateTime + " ms" : "" ) ;
160- }
161-
162- public VariableRegistry GetRegisteredVariables ( )
163- {
164- if ( defaultRegistry == null )
165- {
166- var devKeysEnumAsEnumerable = Enum . GetValues ( typeof ( DeviceKeys ) ) . Cast < DeviceKeys > ( ) ;
167-
168- defaultRegistry = new VariableRegistry ( ) ;
169- defaultRegistry . Register ( $ "{ deviceName } _devicekey", DeviceKeys . Peripheral , "Key Color to Use" , devKeysEnumAsEnumerable . Max ( ) , devKeysEnumAsEnumerable . Min ( ) ) ;
170- defaultRegistry . Register ( $ "{ deviceName } _led_count", 300 , "LED Count" ) ;
171- defaultRegistry . Register ( $ "{ deviceName } _ip", "" , "IP Adresses (Comma separated)" ) ;
172- defaultRegistry . Register ( $ "{ deviceName } _port", 19446 , "UDP Port" ) ;
173- }
174- return defaultRegistry ;
147+ return true ;
175148 }
176149 }
177- }
150+ }
0 commit comments