示例#1
0
        private void ConnectNewDevice(IPAddress lightIP)
        {
            if (lights.Any(x => x.IsConnected() && x.GetLightIPAddressAndPort().ipAddress == lightIP))
            {
                return;
            }

            var light = new YeeLightAPI.YeeLightDevice();

            light.SetLightIPAddressAndPort(lightIP, YeeLightAPI.YeeLightConstants.Constants.DefaultCommandPort);

            LightConnectAndEnableMusicMode(light);

            lights.Add(light);
        }
示例#2
0
        private void LightConnectAndEnableMusicMode(YeeLightAPI.YeeLightDevice light)
        {
            int localMusicModeListenPort = GetFreeTCPPort(); // This can be any free port

            IPAddress localIP;

            using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
            {
                var lightIP = light.GetLightIPAddressAndPort().ipAddress;
                socket.Connect(lightIP, lightListenPort);
                localIP = ((IPEndPoint)socket.LocalEndPoint).Address;
            }

            light.Connect();
            light.SetMusicMode(localIP, (ushort)localMusicModeListenPort, true);
        }