Ejemplo n.º 1
0
 private LifxResponse ParseMessage(byte[] packet)
 {
     using (MemoryStream ms = new MemoryStream(packet))
     {
         var          header = new FrameHeader();
         BinaryReader br     = new BinaryReader(ms);
         //frame
         var size = br.ReadUInt16();
         if (packet.Length != size || size < 36)
         {
             throw new Exception("Invalid packet");
         }
         var a      = br.ReadUInt16();            //origin:2, reserved:1, addressable:1, protocol:12
         var source = br.ReadUInt32();
         //frame address
         byte[] target = br.ReadBytes(8);
         header.TargetMacAddress = target;
         ms.Seek(6, SeekOrigin.Current);        //skip reserved
         var b = br.ReadByte();                 //reserved:6, ack_required:1, res_required:1,
         header.Sequence = br.ReadByte();
         //protocol header
         var nanoseconds = br.ReadUInt64();
         header.AtTime = Utilities.Epoch.AddMilliseconds(nanoseconds * 0.000001);
         var type = (MessageType)br.ReadUInt16();
         ms.Seek(2, SeekOrigin.Current);                 //skip reserved
         byte[] payload = null;
         if (size > 36)
         {
             payload = br.ReadBytes(size - 36);
         }
         return(LifxResponse.Create(header, type, source, payload));
     }
 }
Ejemplo n.º 2
0
		private void ProcessDeviceDiscoveryMessage(HostName remoteAddress, string remotePort, LifxResponse msg)
		{
			if (DiscoveredBulbs.ContainsKey(remoteAddress.ToString()))  //already discovered
            {
				DiscoveredBulbs[remoteAddress.ToString()].LastSeen = DateTime.UtcNow; //Update datestamp
				return;
			}
			if (msg.Source != discoverSourceID || //did we request the discovery?
				_DiscoverCancellationSource == null ||
				_DiscoverCancellationSource.IsCancellationRequested) //did we cancel discovery?
				return;

			var device = new LightBulb()
			{
				HostName = remoteAddress,
				Service = msg.Payload[0],
				Port = BitConverter.ToUInt32(msg.Payload, 1),
				LastSeen = DateTime.UtcNow
			};
			DiscoveredBulbs[remoteAddress.ToString()] = device;
			devices.Add(device);
			if (DeviceDiscovered != null)
			{
				DeviceDiscovered(this, new DeviceDiscoveryEventArgs() { Device = device });
			}
		}
Ejemplo n.º 3
0
        internal static LifxResponse Create(FrameHeader header, MessageType type, uint source, byte[] payload)
        {
            LifxResponse response = null;

            switch (type)
            {
            case MessageType.DeviceAcknowledgement:
                response = new AcknowledgementResponse(payload);
                break;

            case MessageType.DeviceStateLabel:
                response = new StateLabelResponse(payload);
                break;

            case MessageType.LightState:
                response = new LightStateResponse(payload);
                break;

            case MessageType.LightStatePower:
                response = new LightPowerResponse(payload);
                break;

            case MessageType.DeviceStateVersion:
                response = new StateVersionResponse(payload);
                break;

            case MessageType.DeviceStateHostFirmware:
                response = new StateHostFirmwareResponse(payload);
                break;

            case MessageType.DeviceStateService:
                response = new StateServiceResponse(payload);
                break;

            default:
                response = new UnknownResponse(payload);
                break;
            }
            response.Header  = header;
            response.Type    = type;
            response.Payload = payload;
            response.Source  = source;
            return(response);
        }
Ejemplo n.º 4
0
        private void ProcessDeviceDiscoveryMessage(System.Net.IPAddress remoteAddress, int remotePort, LifxResponse msg)
        {
            string id = msg.Header.TargetMacAddressName;                 //remoteAddress.ToString()

            if (DiscoveredBulbs.ContainsKey(id))                         //already discovered
            {
                DiscoveredBulbs[id].LastSeen = DateTime.UtcNow;          //Update datestamp
                DiscoveredBulbs[id].HostName = remoteAddress.ToString(); //Update hostname in case IP changed

                return;
            }
            if (msg.Source != discoverSourceID ||                    //did we request the discovery?
                _DiscoverCancellationSource == null ||
                _DiscoverCancellationSource.IsCancellationRequested) //did we cancel discovery?
            {
                return;
            }

            var device = new LightBulb()
            {
                HostName   = remoteAddress.ToString(),
                Service    = msg.Payload[0],
                Port       = BitConverter.ToUInt32(msg.Payload, 1),
                LastSeen   = DateTime.UtcNow,
                MacAddress = msg.Header.TargetMacAddress
            };

            DiscoveredBulbs[id] = device;
            devices.Add(device);
            if (DeviceDiscovered != null)
            {
                DeviceDiscovered(this, new DeviceDiscoveryEventArgs()
                {
                    Device = device
                });
            }
        }
Ejemplo n.º 5
0
        private async void ProcessDeviceDiscoveryMessage(HostName remoteAddress, string remotePort, LifxResponse msg)
        {
            if (DiscoveredBulbs.ContainsKey(remoteAddress.ToString()))                //already discovered
            {
                DiscoveredBulbs[remoteAddress.ToString()].LastSeen = DateTime.UtcNow; //Update datestamp
                return;
            }
            if (msg.Source != discoverSourceID ||                    //did we request the discovery?
                _DiscoverCancellationSource == null ||
                _DiscoverCancellationSource.IsCancellationRequested) //did we cancel discovery?
            {
                return;
            }

            var device = new LightBulb
            {
                HostName = remoteAddress,
                Service  = msg.Payload[0],
                Port     = BitConverter.ToUInt32(msg.Payload, 1),
                LastSeen = DateTime.UtcNow
            };

            device.State = await GetLightStateAsync(device);

            DiscoveredBulbs[remoteAddress.ToString()] = device;

            device.State = await GetLightStateAsync(device);

            // device.IsOn = await GetLightPowerAsync(device);

            devices.Add(device);
            if (DeviceDiscovered != null)
            {
                DeviceDiscovered(this, new DeviceDiscoveryEventArgs {
                    Device = device
                });
            }
        }