Example #1
0
        internal LIFXProtocolPacket GetPacket()
        {
            UInt16 payloadLength = (UInt16)payloadToBytes().Length;
            UInt16 size          = (UInt16)(LIFXProtocolFrame.BASE_SIZE + payloadLength);

            var packet = new LIFXProtocolPacket();

            packet.Frame = new LIFXProtocolFrame
            {
                Size   = size,
                Source = _source,
                Tagged = _isTagged
            };

            packet.FrameAddress = new LIFXProtocolFrameAddress
            {
                Target      = LIFXProtocolFrameAddress.EMPTY_TARGET,
                AckRequired = true,
                ResRequired = true,
                Sequence    = 0
            };

            packet.ProtocolHeader = new LIFXProtocolProtocolHeader
            {
                Type = (UInt16)_type
            };

            packet.Payload = this.payloadToBytes();

            return(packet);
        }
Example #2
0
        private void HandleDeviceStateVersion(LIFXProtocolPacket packet, string hostname)
        {
            string id = BitConverter.ToString(packet.FrameAddress.Target);

            var device = Devices.FirstOrDefault(x => x.MacAddressString == id);

            if (device != null)
            {
                device.Hostname = hostname;
            }
            else
            {
                if (packet.Frame.Source != _discoverSourceID)
                {
                    return;
                }

                device = new LightBulb()
                {
                    Hostname   = hostname,
                    Service    = packet.Payload[0],
                    Port       = BitConverter.ToUInt32(packet.Payload, 1),
                    MacAddress = packet.FrameAddress.Target
                };

                Devices.Add(device);
            }
        }
Example #3
0
        private async Task <LIFXProtocolPacket> SendReceiveAsync(UInt32 source, string hostName, LIFXProtocolPacket packet, int timeout = 30000)
        {
            try
            {
                var tokenSource = new CancellationTokenSource(timeout);
                var token       = tokenSource.Token;

                var tcs            = new TaskCompletionSource <LIFXProtocolPacket>();
                var successfullAdd = _tcsDictionary.TryAdd(source, tcs);
                if (!successfullAdd)
                {
                    throw new Exception($"Failed to add {source} to dictionary");
                }

                await _networkClient.SendMessage(hostName, packet);

                var result = await tcs.Task.WithCancellation(token);

                if (_tcsDictionary.ContainsKey(source))
                {
                    TaskCompletionSource <LIFXProtocolPacket> value = null;
                    _tcsDictionary.TryRemove(source, out value);
                }

                return(result);
            }
            finally
            {
                if (_tcsDictionary.ContainsKey(source))
                {
                    TaskCompletionSource <LIFXProtocolPacket> value = null;
                    _tcsDictionary.TryRemove(source, out value);
                }
            }
        }