Beispiel #1
0
        private static ushort GetPacketSize(ILifxPayload payload)
        {
            // TODO: is there a nicer way to get the payload size? Serializing it twice feels a bit circular.
            // maybe just put it on the interface?
            const int headerSize = 36;

            using (var tempPayloadStream = new MemoryStream())
            {
                payload.WriteToStream(new BinaryWriter(tempPayloadStream));
                return((ushort)(headerSize + tempPayloadStream.ToArray().Length));
            }
        }
Beispiel #2
0
        public static LifxMessage CreateBroadcast(ILifxPayload payload, uint sourceIdentifier, bool responseRequired, bool ackRequired, byte sequenceNum)
        {
            ushort size = GetPacketSize(payload);

            return(new LifxMessage(
                       new LifxHeader(
                           new Frame(size, 0x400, true, true, 0, sourceIdentifier),
                           new FrameAddress(new byte[8], responseRequired, ackRequired, sequenceNum),
                           new ProtocolHeader(payload.MessageType)),
                       payload,
                       null));
        }
Beispiel #3
0
        // TODO: split into CreateCommand/CreateQuery to take away responseRequired/ackRequired parameters?
        //  do we ever want/need to set responseRequired?
        //  maybe pull sourceIdentifier/sequenceNum generation inside a class?
        public static LifxMessage CreateTargeted(ILifxPayload payload, uint sourceIdentifier, bool responseRequired, bool ackRequired, byte sequenceNum, byte[] targetMac, IUdpClient respondClient)
        {
            ushort size = GetPacketSize(payload);

            return(new LifxMessage(
                       new LifxHeader(
                           new Frame(size, 0x400, true, false, 0, sourceIdentifier),
                           new FrameAddress(targetMac, responseRequired, ackRequired, sequenceNum),
                           new ProtocolHeader(payload.MessageType)),
                       payload,
                       respondClient));
        }
Beispiel #4
0
        private static LifxMessage FromPayloadBytes(LifxHeader header, byte[] payload, IUdpClient respondClient)
        {
            ILifxPayload parsedPayload = null;

            switch (header.ProtocolHeader.MessageType)
            {
            case MessageType.DeviceAcknowledgement:
                parsedPayload = AcknowledgementResponse.FromBytes();
                break;

            case MessageType.DeviceStateLabel:
                parsedPayload = StateLabelResponse.FromBytes(payload);
                break;

            case MessageType.LightState:
                parsedPayload = LightStateResponse.FromBytes(payload);
                break;

            case MessageType.LightStatePower:
                parsedPayload = LightPowerResponse.FromBytes(payload);
                break;

            case MessageType.DeviceStateVersion:
                parsedPayload = StateVersionResponse.FromBytes(payload);
                break;

            case MessageType.DeviceStateHostFirmware:
                parsedPayload = StateHostFirmwareResponse.FromBytes(payload);
                break;

            case MessageType.DeviceStateService:
                parsedPayload = StateServiceResponse.FromBytes(payload);
                break;

            default:
                parsedPayload = UnknownResponse.FromBytes(payload);
                break;
            }

            return(new LifxMessage(header, parsedPayload, respondClient));
        }
Beispiel #5
0
 internal LifxMessage(LifxHeader header, ILifxPayload payload, IUdpClient respondClient)
 {
     Header        = header;
     Payload       = payload;
     RespondClient = respondClient;
 }