Ejemplo n.º 1
0
        private static async Task UpdateStatisticsButtons()
        {
            keyboard.SetLcdButton(0, lastButtonIsPressed ? LcdColor.Red : LcdColor.Green, "Button: " + lastButtonPressed);
            keyboard.SetLcdButton(1, LcdColor.Green, "JoyX:", joyX, "");
            keyboard.SetLcdButton(2, LcdColor.Green, "JoyY:", joyY, "");
            keyboard.SetLcdButton(3, LcdColor.Green, "JoyZ:", joyZ, "");
            keyboard.SetLcdButton(4, LcdColor.Green, "T-Bar:", tBar, "");

            for (int i = 0; i < rotaries.Length; i++)
            {
                keyboard.SetLcdButton(8 + i, LcdColor.Green, $"Rotary {i + 1}: {rotaries[i]}");
            }

            await keyboard.UpdateAllDisplaysAsync();
        }
        /// <summary>
        /// Command handler
        /// </summary>
        private async Task Consumer_Received(object sender, BasicDeliverEventArgs e)
        {
            try
            {
                if (e.RoutingKey == RoutingAddressMap.LampCommandRoutingKey)
                {
                    var msg = Deserialize <LampCommandMessage>(e.Body);
                    if (msg.Command == LampCommandType.ClearAll)
                    {
                        keyboard.ClearAllPushButtonsAndLedButtons();
                        await keyboard.UpdateLampsAsync();

                        await keyboard.UpdateAllDisplaysAsync();
                    }
                    else
                    {
                        bool on = msg.Command == LampCommandType.SetOn;
                        foreach (int keyIndex in msg.KeyIndexes)
                        {
                            keyboard.SetPushButtonLamp(keyIndex, on);
                        }
                        await keyboard.UpdateLampsAsync().ConfigureAwait(false);
                    }
                }
                else if (e.RoutingKey == RoutingAddressMap.QuickKeyCommandRoutingKey)
                {
                    var msg = Deserialize <QuickKeyMessage>(e.Body);
                    foreach (var button in msg.Buttons)
                    {
                        keyboard.SetLcdButton(button.Index, button.Color.Convert(), button.Text);
                    }

                    //Update buttons
                    List <int> rowsToUpdate = msg.Buttons.Select(b => b.Index / 8).Distinct().ToList();
                    if (msg.Buttons.Count == 1)
                    {
                        await keyboard.UpdateOneDisplayAsync(msg.Buttons[0].Index).ConfigureAwait(false);
                    }
                    else if (rowsToUpdate.Count == 1)
                    {
                        await keyboard.UpdateDisplayRowAsync(rowsToUpdate[0]).ConfigureAwait(false);
                    }
                    else
                    {
                        await keyboard.UpdateAllDisplaysAsync().ConfigureAwait(false);
                    }

                    //Need to update lamps to update LCD button colors
                    await keyboard.UpdateLampsAsync().ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{ex.GetType().Name} occurred while processing message: {ex.Message}");
            }
            finally
            {
                //Ack our processed message
                rabbitChannel.BasicAck(e.DeliveryTag, false);
            }
        }