Exemple #1
0
        // The following is based on https://github.com/PhilWheat/LIFX-Control
        public void DiscoverBulbs(string ip = "")
        {
            // Send discovery packet
            GetServicePayload payload = new GetServicePayload();

            byte[] sendData = Utils.StringToByteArray(PacketFactory.GetPacket(new byte[8], payload));
            if (ip != "")
            {
                _localIp = ip;
            }

            var a = new UdpClient();

            a.Connect(_sendingEndPoint);
            a.Send(sendData, sendData.Length);
            a.Close();

            //_sendingSocket.SendTo(sendData, _sendingEndPoint);

            // Listen for replies
            IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

            _receivingUdpClient = new UdpClient(56700);

            byte[] receivebytes;

            // Pause for a second to allow for slow bulb responses - not uncommmon :/
            Thread.Sleep(1000);
            Bulbs = new List <Bulb>();
            // Now loop through received packets
            while (_receivingUdpClient.Available > 0)
            {
                // Get the outstanding bytes
                receivebytes = _receivingUdpClient.Receive(ref remoteIpEndPoint);

                // Get the MAC address of the bulb replying
                var macAddress = Utils.ByteArrayToString(receivebytes).Substring(16, 12);
                if (macAddress != "000000000000")
                {
                    var newBulb = new Bulb()
                    {
                        MacAddress = macAddress, IpAddress = remoteIpEndPoint.Address.ToString()
                    };

                    // Create a new Bulb object
                    if (Bulbs.Count(x => x.MacAddress == macAddress) == 0)
                    {
                        Bulbs.Add(newBulb);
                    }
                }
            }

            // Now, find the labels of all the bubs we detected
            GetLabelPayload labelPayload = new GetLabelPayload();
            // and also the version of each bulb
            //GetVersionPayload versionPayload = new GetVersionPayload();
            // and zones if any
            GetColourZonesPayload ColourZonesPayload = new GetColourZonesPayload();

            foreach (var bulb in Bulbs)
            {
                a = new UdpClient();
                a.Connect(_sendingEndPoint);
                // Send label request to a specific bulb
                sendData = Utils.StringToByteArray(PacketFactory.GetPacket(Utils.StringToByteArray(bulb.MacAddress + "0000"), labelPayload));
                a.Send(sendData, sendData.Length);
                // Send version request to a specific bulb
                //sendData = Utils.StringToByteArray(PacketFactory.GetPacket(Utils.StringToByteArray(bulb.MacAddress + "0000"), versionPayload));
                //a.Send(sendData, sendData.Length);
                a.Close();

                //_sendingSocket.SendTo(sendData, _sendingEndPoint);

                Thread.Sleep(1000);

                while (_receivingUdpClient.Available > 0)
                {
                    receivebytes = _receivingUdpClient.Receive(ref remoteIpEndPoint);
                    if (receivebytes[0] == 0x44)
                    {
                        // Parse the received label and mark it against the bulb
                        var label1 = Utils.HexToAscii(Utils.ByteArrayToString(receivebytes).Substring(36 * 2));
                        bulb.Label = label1.Substring(0, label1.IndexOf('\0'));
                    }

                    /* if (receivebytes[0] == 48)
                     * {
                     *  // set the proper version of bulb
                     *  bulb.Version = receivebytes[40];
                     * } */
                }
            }
            // seperating the 2 seems more reliable
            foreach (var bulb in Bulbs)
            {
                a = new UdpClient();
                a.Connect(_sendingEndPoint);
                // Send zone request
                sendData = Utils.StringToByteArray(PacketFactory.GetPacket(Utils.StringToByteArray(bulb.MacAddress + "0000"), ColourZonesPayload));
                a.Send(sendData, sendData.Length);
                a.Close();

                //_sendingSocket.SendTo(sendData, _sendingEndPoint);

                Thread.Sleep(1000);

                while (_receivingUdpClient.Available > 0)
                {
                    receivebytes = _receivingUdpClient.Receive(ref remoteIpEndPoint);
                    if (receivebytes[0] == 46)
                    {
                        // set the zones count of bulb
                        bulb.Zones = receivebytes[36];
                    }
                }
            }

            _receivingUdpClient.Close();
        }
        // The following is based on https://github.com/PhilWheat/LIFX-Control
        public void DiscoverBulbs(string ip = "")
        {
            // Send discovery packet
            GetServicePayload payload = new GetServicePayload();

            byte[] sendData = Utils.StringToByteArray(PacketFactory.GetPacket(new byte[8], payload));
            if (ip != "")
            {
                _localIp = ip;
            }

            var a = new UdpClient();

            a.Connect(_sendingEndPoint);
            a.Send(sendData, sendData.Length);
            a.Close();

            //_sendingSocket.SendTo(sendData, _sendingEndPoint);

            // Listen for replies
            IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

            _receivingUdpClient = new UdpClient(56700);

            byte[] receivebytes;

            // Pause for a second to allow for slow bulb responses - not uncommmon :/
            Thread.Sleep(1000);
            Bulbs = new List <Bulb>();
            // Now loop through received packets
            while (_receivingUdpClient.Available > 0)
            {
                // Get the outstanding bytes
                receivebytes = _receivingUdpClient.Receive(ref remoteIpEndPoint);

                // Get the MAC address of the bulb replying
                var macAddress = Utils.ByteArrayToString(receivebytes).Substring(16, 12);
                if (macAddress != "000000000000")
                {
                    var newBulb = new Bulb()
                    {
                        MacAddress = macAddress, IpAddress = remoteIpEndPoint.Address.ToString()
                    };

                    // Create a new Bulb object
                    if (Bulbs.Count(x => x.MacAddress == macAddress) == 0)
                    {
                        Bulbs.Add(newBulb);
                    }
                }
            }

            // Now, find the labels of all the bubs we detected
            GetLabelPayload labelPayload = new GetLabelPayload();

            foreach (var bulb in Bulbs)
            {
                // Send label request to a specific bulb
                sendData = Utils.StringToByteArray(PacketFactory.GetPacket(Utils.StringToByteArray(bulb.MacAddress + "0000"), labelPayload));

                a = new UdpClient();
                a.Connect(_sendingEndPoint);
                a.Send(sendData, sendData.Length);
                a.Close();

                //_sendingSocket.SendTo(sendData, _sendingEndPoint);

                Thread.Sleep(1000);

                while (_receivingUdpClient.Available > 0)
                {
                    receivebytes = _receivingUdpClient.Receive(ref remoteIpEndPoint);
                    if (receivebytes[0] == 0x44)
                    {
                        // Parse the received label and mark it against the bulb
                        var label1 = Utils.HexToAscii(Utils.ByteArrayToString(receivebytes).Substring(36 * 2));
                        bulb.Label = label1.Substring(0, label1.IndexOf('\0'));
                    }
                }
            }

            _receivingUdpClient.Close();

            if (Mouse != null)
            {
                foreach (var led in Mouse.Leds)
                {
                    Bulbs.Add(new Bulb()
                    {
                        Label = $"Corsair Mouse {led.Id.ToString()}", BulbType = BulbType.CorsairMouse, IpAddress = led.Id.ToString()
                    });
                }
            }

            if (Keyboard != null)
            {
                foreach (var led in Keyboard.Leds)
                {
                    Bulbs.Add(new Bulb()
                    {
                        Label = $"Corsair Keyboard {led.Id.ToString()}", BulbType = BulbType.CorsairKeyboard, IpAddress = led.Id.ToString()
                    });
                }
            }

            if (auraSDK != null && auraSDK.Motherboards.Length > 0)
            {
                for (var i = 0; i < auraSDK.Motherboards[0].LedCount; i++)
                {
                    Bulbs.Add(new Bulb()
                    {
                        BulbType = BulbType.Asus, Label = $"Asus {i}", IpAddress = i.ToString()
                    });
                }
            }
        }
        // The following is based on https://github.com/PhilWheat/LIFX-Control
        public void DiscoverBulbs()
        {
            // Send discovery packet
            GetServicePayload payload = new GetServicePayload();

            byte[] sendData = Utils.StringToByteArray(PacketFactory.GetPacket(new byte[8], payload));

            var a = new UdpClient();

            a.Connect(_sendingEndPoint);
            a.Send(sendData, sendData.Length);
            a.Close();

            //_sendingSocket.SendTo(sendData, _sendingEndPoint);

            // Listen for replies
            IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

            _receivingUdpClient = new UdpClient(56700);

            byte[] receivebytes;

            // Pause for a second to allow for slow bulb responses - not uncommmon :/
            Thread.Sleep(1000);
            Bulbs = new List <Bulb>();
            // Now loop through received packets
            while (_receivingUdpClient.Available > 0)
            {
                // Get the outstanding bytes
                receivebytes = _receivingUdpClient.Receive(ref remoteIpEndPoint);

                // Get the MAC address of the bulb replying
                var macAddress = Utils.ByteArrayToString(receivebytes).Substring(16, 12);
                if (macAddress != "000000000000")
                {
                    var newBulb = new Bulb()
                    {
                        MacAddress = macAddress
                    };

                    // Create a new Bulb object
                    if (Bulbs.Count(x => x.MacAddress == macAddress) == 0)
                    {
                        Bulbs.Add(newBulb);
                    }
                }
            }

            // Now, find the labels of all the bubs we detected
            GetLabelPayload labelPayload = new GetLabelPayload();

            foreach (var bulb in Bulbs)
            {
                // Send label request to a specific bulb
                sendData = Utils.StringToByteArray(PacketFactory.GetPacket(Utils.StringToByteArray(bulb.MacAddress + "0000"), labelPayload));

                a = new UdpClient();
                a.Connect(_sendingEndPoint);
                a.Send(sendData, sendData.Length);
                a.Close();

                //_sendingSocket.SendTo(sendData, _sendingEndPoint);

                Thread.Sleep(1000);

                while (_receivingUdpClient.Available > 0)
                {
                    receivebytes = _receivingUdpClient.Receive(ref remoteIpEndPoint);
                    if (receivebytes[0] == 0x44)
                    {
                        // Parse the received label and mark it against the bulb
                        var label1 = Utils.HexToAscii(Utils.ByteArrayToString(receivebytes).Substring(36 * 2));
                        bulb.Label = label1.Substring(0, label1.IndexOf('\0'));
                    }
                }
            }

            _receivingUdpClient.Close();
        }