@@ -35,58 +35,56 @@ public class YeeLightDevice : DefaultDevice
3535
3636 public override bool Initialize ( )
3737 {
38- if ( ! IsInitialized )
38+ if ( IsInitialized ) return IsInitialized ;
39+ try
3940 {
40- try
41- {
42- lights . Clear ( ) ;
41+ lights . Clear ( ) ;
4342
44- var IPListString = Global . Configuration . VarRegistry . GetVariable < string > ( $ "{ DeviceName } _IP") ;
45- var lightIPList = new List < IPAddress > ( ) ;
43+ var ipListString = Global . Configuration . VarRegistry . GetVariable < string > ( $ "{ DeviceName } _IP") ;
44+ var lightIpList = new List < IPAddress > ( ) ;
4645
47- //Auto discover a device if the IP is empty and auto-discovery is enabled
48- if ( string . IsNullOrWhiteSpace ( IPListString ) && Global . Configuration . VarRegistry . GetVariable < bool > ( $ "{ DeviceName } _auto_discovery") )
46+ //Auto discover a device if the IP is empty and auto-discovery is enabled
47+ if ( string . IsNullOrWhiteSpace ( ipListString ) && Global . Configuration . VarRegistry . GetVariable < bool > ( $ "{ DeviceName } _auto_discovery") )
48+ {
49+ var devices = DeviceLocator . DiscoverDevices ( 10000 , 2 ) ;
50+ if ( ! devices . Any ( ) )
4951 {
50- var devices = DeviceLocator . DiscoverDevices ( 10000 , 2 ) ;
51- if ( ! devices . Any ( ) )
52- {
53- throw new Exception ( "Auto-discovery is enabled but no devices have been located." ) ;
54- }
55-
56- lightIPList . AddRange ( devices . Select ( v => v . GetLightIPAddressAndPort ( ) . ipAddress ) ) ;
52+ throw new Exception ( "Auto-discovery is enabled but no devices have been located." ) ;
5753 }
58- else
54+
55+ lightIpList . AddRange ( devices . Select ( v => v . GetLightIPAddressAndPort ( ) . ipAddress ) ) ;
56+ }
57+ else
58+ {
59+ lightIpList = ipListString . Split ( ',' ) . Select ( x => IPAddress . Parse ( x . Replace ( " " , "" ) ) ) . ToList ( ) ;
60+ if ( lightIpList . Count == 0 )
5961 {
60- lightIPList = IPListString . Split ( new [ ] { ',' } ) . Select ( x => IPAddress . Parse ( x . Replace ( " " , "" ) ) ) . ToList ( ) ;
61- if ( lightIPList . Count == 0 )
62- {
63- throw new Exception ( "Device IP list is empty." ) ;
64- }
62+ throw new Exception ( "Device IP list is empty." ) ;
6563 }
64+ }
6665
67- for ( int i = 0 ; i < lightIPList . Count ; i ++ )
66+ for ( var i = 0 ; i < lightIpList . Count ; i ++ )
67+ {
68+ var ipaddr = lightIpList [ i ] ;
69+ try
6870 {
69- IPAddress ipaddr = lightIPList [ i ] ;
70- try
71- {
72- ConnectNewDevice ( ipaddr ) ;
73- }
74- catch ( Exception exc )
75- {
76- LogError ( $ "Encountered an error while connecting to the { i } . light. Exception: { exc } ") ;
77- }
71+ ConnectNewDevice ( ipaddr ) ;
72+ }
73+ catch ( Exception exc )
74+ {
75+ LogError ( $ "Encountered an error while connecting to the { i } . light. Exception: { exc } ") ;
7876 }
79-
80- updateDelayStopWatch . Start ( ) ;
81- IsInitialized = lights . All ( x => x . IsConnected ( ) ) ;
8277 }
83- catch ( Exception exc )
84- {
85- LogError ( $ "Encountered an error while initializing. Exception: { exc } ") ;
86- IsInitialized = false ;
8778
88- return false ;
89- }
79+ updateDelayStopWatch . Start ( ) ;
80+ IsInitialized = lights . All ( x => x . IsConnected ( ) ) ;
81+ }
82+ catch ( Exception exc )
83+ {
84+ LogError ( $ "Encountered an error while initializing. Exception: { exc } ") ;
85+ IsInitialized = false ;
86+
87+ return false ;
9088 }
9189
9290 return IsInitialized ;
@@ -259,30 +257,32 @@ private void ConnectNewDevice(IPAddress lightIP)
259257 lights . Add ( light ) ;
260258 }
261259
260+ private int _connectionTries ;
262261 private void LightConnectAndEnableMusicMode ( YeeLightAPI . YeeLightDevice light )
263262 {
264- int localMusicModeListenPort = GetFreeTCPPort ( ) ; // This can be any free port
263+ var localMusicModeListenPort = GetFreeTCPPort ( ) ; // This can be any free port
265264
266- IPAddress localIP ;
267- using ( Socket socket = new Socket ( AddressFamily . InterNetwork , SocketType . Dgram , 0 ) )
268- {
269- var lightIP = light . GetLightIPAddressAndPort ( ) . ipAddress ;
270- socket . Connect ( lightIP , lightListenPort ) ;
271- localIP = ( ( IPEndPoint ) socket . LocalEndPoint ) . Address ;
272- }
265+ using var socket = new Socket ( AddressFamily . InterNetwork , SocketType . Dgram , 0 ) ;
266+ var lightIp = light . GetLightIPAddressAndPort ( ) . ipAddress ;
267+ socket . Connect ( lightIp , lightListenPort ) ;
268+ var localIp = ( ( IPEndPoint ) socket . LocalEndPoint ) . Address ;
273269
274270 light . Connect ( ) ;
275- light . SetMusicMode ( localIP , ( ushort ) localMusicModeListenPort , true ) ;
271+ _connectionTries = 1000 ;
272+ Thread . Sleep ( 500 ) ;
273+ while ( ! light . IsConnected ( ) && -- _connectionTries > 0 )
274+ {
275+ Thread . Sleep ( 500 ) ;
276+ }
277+ light . SetMusicMode ( localIp , ( ushort ) localMusicModeListenPort , true ) ;
276278 }
277279
278280 private int GetFreeTCPPort ( )
279281 {
280- int freePort ;
281-
282282 // When a TCPListener is created with 0 as port, the TCP/IP stack will asign it a free port
283- TcpListener listener = new TcpListener ( IPAddress . Loopback , 0 ) ; // Create a TcpListener on loopback with 0 as the port
283+ var listener = new TcpListener ( IPAddress . Loopback , 0 ) ; // Create a TcpListener on loopback with 0 as the port
284284 listener . Start ( ) ;
285- freePort = ( ( IPEndPoint ) listener . LocalEndpoint ) . Port ; // Gets the local port the TcpListener is listening on
285+ var freePort = ( ( IPEndPoint ) listener . LocalEndpoint ) . Port ;
286286 listener . Stop ( ) ;
287287 return freePort ;
288288 }
0 commit comments