Esempio n. 1
0
        public LifxBulb(LifxPanController panController, string ipAddress, string macAddress)
        {
            PanController           = panController;
            PanControllerMACAddress = panController.MACAddress;
            IPAddress  = ipAddress;
            MACAddress = macAddress;

            Label  = "";
            Tags   = 0;
            IsOn   = LifxPowerState.Off;
            Colour = new LifxColour()
            {
                Luminosity = 65525
            };
        }
Esempio n. 2
0
        /// <summary>
        /// The listner clients message recieved event handler.
        /// This is where the message will be converted into a LifxMessage and will invoke the MessageRecieved event handler if anything is subscribed to it
        /// </summary>
        /// <param name="sender">The socket that recieved the packet</param>
        /// <param name="args">Where the packet information is contained</param>
        private void lifxCommunicatorClient_MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
        {
            if (IsDisposed)
            {
                return;
            }

            uint bufferArraySize = args.GetDataReader().UnconsumedBufferLength;

            Byte[] receiveBytes = new Byte[bufferArraySize];
            args.GetDataReader().ReadBytes(receiveBytes);

            LifxDataPacket packet          = new LifxDataPacket(receiveBytes);
            LifxMessage    receivedMessage = LifxHelper.PacketToMessage(packet);

            if (receivedMessage != null) // Check to make sure the packet we recieved was sent from a LIFX bulb, others can be sending on the same port
            {
                // If the packet type is a pan gateway we need to handle this a bit differently
                if (receivedMessage.PacketType == MessagePacketType.PanGateway)
                {
                    // This locks the variable and lets any senders will waiting for a pan gateway know that we have received the message and you can stop retrying
                    lock (commandsAwaitingResponse)
                    {
                        // Ensure that there are senders awaiting a message
                        if (commandsAwaitingResponse.ContainsKey(receivedMessage.PacketType))
                        {
                            commandsAwaitingResponse[receivedMessage.PacketType] = receivedMessage; // Sets the reply (from bulb) message to this message
                            messageResumer[receivedMessage.PacketType].Release();                   // Stops the senders from retrying to ask for pan gateway messages
                        }
                    }

                    // Sets up the pan handler class and assigns the bulb to it
                    LifxPanController foundPanHandler = new LifxPanController()
                    {
                        MACAddress = LifxHelper.ByteArrayToString(receivedMessage.ReceivedData.PanControllerMac),
                        IPAddress  = args.RemoteAddress.DisplayName
                    };
                    foundPanHandler.Bulbs.Add(new LifxBulb(foundPanHandler, args.RemoteAddress.DisplayName, LifxHelper.ByteArrayToString(receivedMessage.ReceivedData.PanControllerMac)));

                    // Invoke the PanControllerFound event handler
                    PanControllerFound.Invoke(this, foundPanHandler);
                    return;
                }

                // If the incoming packet isn't a pan controller, just lock the variable
                // and lets any senders that may be waiting for a return message know that we have received the message and you can stop retrying
                lock (commandsAwaitingResponse)
                {
                    // If there is someone waiting for a recieved message, i.e. the command is *meant* to receive something from the bulb
                    // let it know we have found it (Release) and assign the message to the varaible
                    // Otherwise we have received a LifxMessage we didn't ask for. Ensure that there is someone subscribed to the event handler and Invoke with the message
                    if (commandsAwaitingResponse.ContainsKey(receivedMessage.PacketType))
                    {
                        commandsAwaitingResponse[receivedMessage.PacketType] = receivedMessage;
                        messageResumer[receivedMessage.PacketType].Release();
                    }
                    else
                    {
                        if (MessageRecieved != null)
                        {
                            MessageRecieved.Invoke(this, receivedMessage);
                        }
                    }
                }
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Tells the comminucator to send a command to the pan controller specified
 /// </summary>
 /// <param name="command">The command to send to the pan controller</param>
 /// <param name="bulb">The pan controller to send the command to</param>
 /// <returns>Returns a message if the command is expecting one, otherwise will return null <see cref="LifxCommand.ExpectedReturnMessagePacketType"/></returns>
 public async Task <LifxMessage> SendCommand(LifxCommand command, LifxPanController panController)
 {
     return(await SendCommand(command, "", panController.MACAddress, panController.IPAddress));
 }