public DiscoveredEventArgs(DiscoveredDevice discoveredDevice) { DiscoveredDevice = discoveredDevice; }
public static void StartDiscoveryTask() { if (discoveryThreadRunning || discoverTask != null) { return; } discoveryThreadRunning = true; discoverTask = new Task(() => { IPAddress SSDP_IP = new IPAddress(new byte[] { 239, 255, 255, 250 }); int SSDP_PORT = 1900; var client = new UdpClient(); try { client.Client.Bind(new IPEndPoint(IPAddress.Any, SSDP_PORT)); foreach (IPAddress IP in IPs) { client.JoinMulticastGroup(SSDP_IP, IP); } client.MulticastLoopback = true; while (discoveryThreadRunning) { log.Debug("Discover started"); NanoleafPlugin.DiscoverState = "Started"; var result = client.ReceiveAsync().GetAwaiter().GetResult(); NanoleafPlugin.DiscoverState = "Result Received"; string message = Encoding.Default.GetString(result.Buffer); if (message.Contains("nl-devicename")) { if (discoveredDevices.Any(d => d.IP.Equals(result.RemoteEndPoint.Address.ToString()))) { return; } var array = message.Replace("\r\n", "|").Split('|'); EDeviceType type = EDeviceType.UNKNOWN; switch (array.FirstOrDefault(s => s.StartsWith("NT"))?.Replace("NT: ", "")) { case "nanoleaf:nl22": case "Nanoleaf_aurora:light": type = EDeviceType.LightPanles; break; case "nanoleaf:nl29": type = EDeviceType.Canvas; break; case "nanoleaf:nl42": type = EDeviceType.Shapes; break; case "nanoleaf:nl45": type = EDeviceType.Essentials; break; case "nanoleaf:nl52": type = EDeviceType.Elements; break; case "nanoleaf:nl59": type = EDeviceType.Lines; break; } string name = array.FirstOrDefault(s => s.StartsWith("nl-devicename"))?.Replace("nl-devicename: ", ""); var device = new DiscoveredDevice(result.RemoteEndPoint.Address.ToString(), name, type);; discoveredDevices.Add(device); deviceDiscovered?.Invoke(null, new DiscoveredEventArgs(device)); } log.Debug("Discover passed"); NanoleafPlugin.DiscoverState = "Passed"; } } catch (Exception e) { log.Warn("The Socket is already in use." + Environment.NewLine + "there Are a feaw things to fix this issue." + Environment.NewLine + "Open the CMD.exe and perform the command \"netstat -a -n -o\"" + Environment.NewLine + "Now you see all open Ports" + Environment.NewLine + "find TCP [the IP address]:[port number] .... #[target_PID]# (ditto for UDP)" + Environment.NewLine + "Open TaskManager and Klick on Processes" + Environment.NewLine + "Enable \"PID\" column by going to: View > Select Columns > Check the box for PID" + Environment.NewLine + "Find the PID of interest and \"END PROCESS\"" + Environment.NewLine + Environment.NewLine + "Common Programs are Spotify or the SSPDSRF-Service" , e); } log.Debug("Discover stopped"); NanoleafPlugin.DiscoverState = "Stopped"; client.Close(); discoveryThreadRunning = false; }, token); discoverTask.Start(); }