Beispiel #1
0
        public async Task <List <BroadlinkDevice> > DiscoverAsync()
        {
            var discoveredDevices = new List <BroadlinkDevice>();

            byte[] discoveryPacket = PacketGenerator.GenerateDiscoveryPacket(LocalIPEndPoint.Address, (short)LocalIPEndPoint.Port);

            IPEndPoint ep = new IPEndPoint(IPAddress.Broadcast, 80);

            using (var client = new UdpClient(LocalIPEndPoint))
            {
                Task.Run(async() =>
                {
                    while (true)
                    {
                        var result = await client.ReceiveAsync();
                        if (result != null)
                        {
                            var response = result.Buffer;
                            if (response != null)
                            {
                                var macArray = new byte[7];
                                Array.Copy(response, 0x3a, macArray, 0, 7);

                                var discoveredDevice             = CreateBroadlinkDevice(BitConverter.ToInt16(response, 0x34));
                                discoveredDevice.LocalIPEndPoint = LocalIPEndPoint;
                                discoveredDevice.EndPoint        = result.RemoteEndPoint;
                                discoveredDevice.MacAddress      = macArray;

                                discoveredDevices.Add(discoveredDevice);
                            }
                        }
                    }
                });

                await client.SendAsync(discoveryPacket, discoveryPacket.Length, ep);

                Debug.WriteLine("Message sent to the broadcast address");

                await Task.Delay(3000);

                return(discoveredDevices);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Read the data for a remote control command.
        /// </summary>
        /// <returns>Byte array containing the packet for <see cref="SendRemoteCommandAsync(byte[])" /></returns>
        public async Task <byte[]> ReadLearningDataAsync()
        {
            var packet            = PacketGenerator.GenerateReadLearningModePacket(this);
            var encryptedResponse = await SendAndWaitForResponseAsync(packet);

            var errorCode = BitConverter.ToInt16(encryptedResponse, 0x22);

            if (errorCode != 0)
            {
                throw new Exception($"Error {errorCode} in learning response");
            }

            var encryptedPayload = encryptedResponse.Slice(0x38);

            var payload      = encryptedPayload.Decrypt(EncryptionKey);
            var learningData = payload.Slice(0x04);

            return(learningData);
        }
Beispiel #3
0
        /// <summary>
        /// Obtain the device id and encryption key needed for other calls
        /// </summary>
        /// <remarks>Run this method before making any other calls</remarks>
        /// <returns></returns>
        public async Task AuthorizeAsync()
        {
            byte[] authorizationPacket = PacketGenerator.GenerateAuthorizationPacket(this);

            var encryptedResponse = await SendAndWaitForResponseAsync(authorizationPacket);

            var encryptedPayload = encryptedResponse.Slice(0x38);
            var payload          = encryptedPayload.Decrypt();

            var deviceId = new byte[4];

            Array.Copy(payload, 0x00, deviceId, 0, deviceId.Length);

            var encryptionKey = new byte[16];

            Array.Copy(payload, 0x04, encryptionKey, 0, encryptionKey.Length);

            DeviceId      = deviceId;
            EncryptionKey = encryptionKey;
        }
Beispiel #4
0
        /// <summary>
        /// Get the temperature
        /// </summary>
        /// <returns>temperature in degrees Celsius</returns>
        public async Task <float> GetTemperatureAsync()
        {
            var packet            = PacketGenerator.GenerateReadTemperaturePacket(this);
            var encryptedResponse = await SendAndWaitForResponseAsync(packet);

            var errorCode = BitConverter.ToInt16(encryptedResponse, 0x22);

            if (errorCode != 0)
            {
                throw new Exception($"Error {errorCode} in temperature response");
            }

            var encryptedPayload = encryptedResponse.Slice(0x38);

            var payload         = encryptedPayload.Decrypt(EncryptionKey);
            var temperatureData = payload.Slice(0x04, 0x05);
            var temperature     = temperatureData[0] + (float)temperatureData[1] / 10;

            return(temperature);
        }
Beispiel #5
0
        /// <summary>
        /// Execute a remote control command
        /// </summary>
        /// <param name="data">Packet obtained using <see cref="ReadLearningDataAsync()" /></param>
        /// <returns></returns>
        public async Task SendRemoteCommandAsync(byte[] data)
        {
            var packet = PacketGenerator.GenerateSendDataPacket(this, data);

            await SendAsync(packet);
        }
Beispiel #6
0
        /// <summary>
        /// Start the remote control command learning mode.
        /// </summary>
        /// <returns></returns>
        public async Task EnterLearningModeAsync()
        {
            var packet = PacketGenerator.GenerateStartLearningModePacket(this);

            await SendAsync(packet);
        }