static void Main() { int SSDPTimeOut = 10000; int SSDPRequestCount = 1; Console.WriteLine("Beware that these devices usually have rate limits on SSDP requests with very long wait times. You might have to power cycle your device to zero the rate limit delay."); Console.WriteLine("Press any key start a SSDP search request..."); Console.ReadKey(); Console.WriteLine($"Sending SSDP search request with {SSDPTimeOut}ms timeout and request count of {SSDPRequestCount} ..."); Console.WriteLine(); List <YeeLightDevice> devices = DeviceLocator.DiscoverDevices(SSDPTimeOut, SSDPRequestCount); if (devices.Count > 0) { Console.WriteLine($"Found {devices.Count} device(s): "); foreach (YeeLightDevice device in devices) { var(ipAddress, port) = device.GetLightIPAddressAndPort(); Console.WriteLine($"IP: {ipAddress} | Port: {port}"); } } else { Console.WriteLine("No devices have been found."); } Console.WriteLine(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }
public override bool Initialize() { if (!IsInitialized) { try { lights.Clear(); var IPListString = Global.Configuration.VarRegistry.GetVariable <string>($"{DeviceName}_IP"); var lightIPList = new List <IPAddress>(); //Auto discover a device if the IP is empty and auto-discovery is enabled if (string.IsNullOrWhiteSpace(IPListString) && Global.Configuration.VarRegistry.GetVariable <bool>($"{DeviceName}_auto_discovery")) { var devices = DeviceLocator.DiscoverDevices(10000, 2); if (!devices.Any()) { throw new Exception("Auto-discovery is enabled but no devices have been located."); } lightIPList.AddRange(devices.Select(v => v.GetLightIPAddressAndPort().ipAddress)); } else { lightIPList = IPListString.Split(new[] { ',' }).Select(x => IPAddress.Parse(x.Replace(" ", ""))).ToList(); if (lightIPList.Count == 0) { throw new Exception("Device IP list is empty."); } } for (int i = 0; i < lightIPList.Count; i++) { IPAddress ipaddr = lightIPList[i]; try { ConnectNewDevice(ipaddr); } catch (Exception exc) { LogError($"Encountered an error while connecting to the {i}. light. Exception: {exc}"); } } IsInitialized = lights.All(x => x.IsConnected()); } catch (Exception exc) { LogError($"Encountered an error while initializing. Exception: {exc}"); IsInitialized = false; return(false); } } return(IsInitialized); }
public void Connect(IPAddress lightIP, DoWorkEventArgs token = null) { if (light.IsConnected()) { return; } //Can't reconnect using same device class instance light = new YeeLightAPI.YeeLightDevice(); //Auto discover a device if the IP is 0.0.0.0 and auto-discovery is enabled if (lightIP.Equals(IPAddress.Parse("0.0.0.0")) && default_registry.GetVariable <bool>($"{devicename}_auto_discovery")) { var devices = DeviceLocator.DiscoverDevices(10000, 2); if (devices.Any()) { light = devices.First(); lightIP = light.GetLightIPAddressAndPort().ipAddress; default_registry.SetVariable($"{devicename}_IP", lightIP.ToString()); } else { throw new Exception("IP address is set to 0.0.0.0 and auto-discovery is enabled but no devices have been located."); } } //If it isn't then set the IP to the provided IP address else { light.SetLightIPAddressAndPort(lightIP, YeeLightAPI.YeeLightConstants.Constants.DefaultCommandPort); } IPAddress localIP; int localMusicModeListenPort = GetFreeTCPPort(); // This can be any free port using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0)) { socket.Connect(lightIP, lightListenPort); localIP = ((IPEndPoint)socket.LocalEndPoint).Address; } light.Connect(); light.SetMusicMode(localIP, (ushort)localMusicModeListenPort, true); isConnected = light.IsConnected(); }