Example #1
0
        public LifxDataPacket(LifxCommand messageToPackage)
        {
            mPacketData = new byte[messageToPackage.GetRawMessage().Length + 36];
            Size = (ushort)(messageToPackage.GetRawMessage().Length + 36);
            Protocol = STANDARD_PROTOCOL;

            PacketType = messageToPackage.PacketType;
            Payload = messageToPackage.GetRawMessage();
        }
Example #2
0
        private UdpClient CreateClient(LifxCommand command, IPEndPoint endPoint)
        {
            if (command.IsBroadcastCommand)
            {
                mSendCommandClient = new UdpClient();

                mSendCommandClient.EnableBroadcast = true;
                mSendCommandClient.Client.SetSocketOption(
                SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

                mSendCommandClient.Connect(new IPEndPoint(IPAddress.Broadcast, LIFX_PORT));
                return mSendCommandClient;
            }
            else
            {
                mSendCommandClient = new UdpClient();

                mSendCommandClient.Client.SetSocketOption(
                SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

                mSendCommandClient.Connect(endPoint);
                return mSendCommandClient;

            }
        }
Example #3
0
 private UdpClient GetConnectedClient(LifxCommand command, IPEndPoint endPoint)
 {
     if (mSendCommandClient == null)
     {
         return CreateClient(command, endPoint);
     }
     else
     { 
         if (command.IsBroadcastCommand)
         {
             if (mSendCommandClient.Client.EnableBroadcast)
             {
                 return mSendCommandClient;
             }
             else
             {
                 mSendCommandClient.Close();
                 return CreateClient(command, endPoint);
             }
         }
         else
         {
             if (mSendCommandClient.Client.EnableBroadcast)
             {
                 mSendCommandClient.Close();
                 return CreateClient(command, endPoint); 
                 
             }
             else
             {
                 return mSendCommandClient;
             }
         }
     }
 }
Example #4
0
        /// <summary>
        /// Sends command to a bulb
        /// </summary>
        /// <param name="command"></param>
        /// <param name="bulb">The bulb to send the command to.</param>
        /// <returns>Returns the response message. If the command does not trigger a response it will reurn null. </returns>
        public LifxReceivedMessage SendCommand(LifxCommand command, string macAddress, string panController, IPEndPoint endPoint)
        {
            if (!IsInitialized)
                throw new InvalidOperationException("The communicator needs to be initialized before sending a command.");


            UdpClient client = GetConnectedClient(command, endPoint);


            LifxDataPacket packet = new LifxDataPacket(command);
            packet.TargetMac = LifxHelper.StringToByteArray(macAddress);
            packet.PanControllerMac = LifxHelper.StringToByteArray(panController);

            client.Send(packet.PacketData, packet.PacketData.Length);

            DateTime commandSentTime = DateTime.Now;

            if (command.ReturnMessage == null)
                return null;

            while ((DateTime.Now - commandSentTime).TotalMilliseconds < mTimeoutMilliseconds)
            {
                if (mIncomingQueue.Count != 0)
                {
                    IncomingMessage mess = mIncomingQueue.Dequeue();
                    LifxDataPacket receivedPacket = mess.Data;


                    if (receivedPacket.PacketType == LifxPANGatewayStateMessage.PACKET_TYPE) 
                    { 
                        //Panhandler identified
                        LifxPANGatewayStateMessage panGateway = new LifxPANGatewayStateMessage();
                        panGateway.ReceivedData = receivedPacket;

                        AddDiscoveredPanHandler(new LifxPanController(
                               LifxHelper.ByteArrayToString(receivedPacket.TargetMac),
                               mess.BulbAddress));

                    }
                    else if (receivedPacket.PacketType == LifxLightStatusMessage.PACKET_TYPE && command.IsDiscoveryCommand)
                    {
                        //Panhandler identified
                        LifxLightStatusMessage panGateway = new LifxLightStatusMessage();
                        panGateway.ReceivedData = receivedPacket;

                        AddDiscoveredBulb(
                            LifxHelper.ByteArrayToString(receivedPacket.TargetMac),   
                            LifxHelper.ByteArrayToString(receivedPacket.PanControllerMac));
                    }
                    else if (receivedPacket.PacketType == command.ReturnMessage.PacketType)
                    {
                       
                        command.ReturnMessage.ReceivedData = receivedPacket;
                        mIncomingQueue.Clear();
                        return command.ReturnMessage;
                    }
                }
                Thread.Sleep(30);
            }

            if (command.IsDiscoveryCommand)
                return null;

            if (command.RetryCount > 0)
            {
                command.RetryCount -= 1;

                //Recurssion
                return SendCommand(command, macAddress, panController, endPoint);
            }
            else
                throw new TimeoutException("Did not get a reply from bulb in a timely fashion");

        }
Example #5
0
 public LifxReceivedMessage SendCommand(LifxCommand command, LifxPanController panController)
 {
     return SendCommand(command, "", panController.MacAddress, panController.IpEndpoint);
 }
Example #6
0
 public LifxReceivedMessage SendCommand(LifxCommand command, LifxBulb bulb)
 {
     return SendCommand(command, bulb.MacAddress, bulb.PanHandler, bulb.IpEndpoint);
 }