Beispiel #1
0
        public override async Task AzureModuleInitAsync <C>(C c)
        {
            AzureConnection c1 = c as AzureConnection;
            await base.AzureModuleInitAsync(c1);

            await _moduleClient.SetInputMessageHandlerAsync(Keys.InputFruit, OnFruitMessageReceived, this);

            await _moduleClient.SetMethodHandlerAsync(Keys.SetFruit, SetFruit, this);

            await _moduleClient.SetInputMessageHandlerAsync(Keys.InputOrientation, OnOrientationMessageReceived, this);

            await _moduleClient.SetMethodHandlerAsync(Keys.SetOrientation, SetOrientation, this);

            await base.AzureModuleInitEndAsync();
        }
Beispiel #2
0
        static async Task <int> MainAsync(string[] args)
        {
            Log.WriteLine("Starting async...");
            var Options = new AppOptions();

            Options.Parse(args);
            Log.Enabled = !Options.Quiet;
            Log.Verbose = Options.Verbose;
            Log.WriteLine("arg parse complete...");
            // TODO: Options.List
            Dictionary <string, RGBColor> FruitColors = new Dictionary <string, RGBColor>()
            {
                { "apple", Colors.Red },
                { "pear", Colors.Yellow },
                { "pen", Colors.Green },
                { "grapes", Colors.Blue },
                { "other", Colors.Black }
            };
            AzureConnection connection = null;
            UARTDevice      uart       = null;
            await Task.WhenAll(
                Task.Run(async() => {
                try {
                    if (!Options.Test)
                    {
                        Log.WriteLine("starting connection creation");
                        connection = await AzureConnection.CreateAzureConnectionAsync();
                    }
                }
                catch (Exception e)
                {
                    Log.WriteLine("Main CreateAzureConnectionAsync exception {0}", e.ToString());
                }
            }),
                Task.Run(async() =>
            {
                try
                {
                    Log.WriteLine("creating UART device {0}", Options.DeviceName != null ? Options.DeviceName : "(default)");
                    uart = await UARTDevice.CreateUARTDeviceAsync(Options.DeviceName);
                    await uart.InitAsync();
                    Log.WriteLine("uart initialzed");
                    if (Options.Test)
                    {
                        Log.WriteLine("initiating test");
                        if (Options.TestMessage != null && Options.TestMessage.Length > 1)
                        {
                            await uart.TestAsync(Options.TestMessage);
                        }
                        else
                        {
                            await uart.TestAsync("Test");
                        }
                        Environment.Exit(2);
                    }
                }
                catch (Exception e)
                {
                    Log.WriteLine("UART exception {0}", e.ToString());
                }
            }
                         )
                );

            try
            {
                string      currentFruit       = null;
                Orientation currentOrientation = Orientation.Unknown;
                AzureModule m = (AzureModule)connection.Module;
                EventHandler <ConfigurationType> ConfigurationChangedHandler = async(object sender, ConfigurationType newConfiguration) =>
                {
                    var module = (AzureModule)sender;
                    Log.WriteLine("updating UART_lcd config with {0}", newConfiguration.ToString());
                    //await gpio.UpdatePinConfigurationAsync(newConfiguration.GpioPins);
                    await Task.CompletedTask;
                };
                m.ConfigurationChanged += ConfigurationChangedHandler;
                try
                {
                    EventHandler <string> FruitChangedHandler = async(object sender, string fruit) =>
                    {
                        Log.WriteLine("fruit changed sent {0}", fruit.ToLower());
                        Log.WriteLine("current fruit {0}", currentFruit);
                        if (fruit.ToLower() != currentFruit)
                        {
                            currentFruit = fruit.ToLower();
                            Log.WriteLine("setting fruit to {0}", fruit.ToLower());
                            LCDMessage msg;
                            msg.bgcolor = currentOrientation == Orientation.UpsideDown ? Colors.White : FruitColors[currentFruit.ToLower()];
                            msg.clear   = true;
                            msg.msg     = currentFruit;
                            uart.QueueMessage(msg);
                        }
                        else
                        {
                            Log.WriteLine("fruit already correct -- skipping");
                            await Task.CompletedTask;
                        }
                    };
                    m.FruitChanged += FruitChangedHandler;
                    try
                    {
                        EventHandler <Orientation> OrientationChangedHandler = async(object sender, Orientation o) =>
                        {
                            Log.WriteLine("orientation changed sent {0}", o.ToString());
                            Log.WriteLine("current orientation {0}", currentOrientation);
                            if (o != currentOrientation)
                            {
                                currentOrientation = o;
                                Log.WriteLine("setting orientation to {0}", o.ToString());
                                LCDMessage msg;

                                msg.bgcolor = o == Orientation.UpsideDown ? Colors.White : FruitColors[currentFruit != null ? currentFruit.ToLower() : "other"];
                                msg.clear   = false;
                                msg.msg     = null;
                                uart.QueueMessage(msg);
                            }
                            else
                            {
                                Log.WriteLine("fruit already correct -- skipping");
                                await Task.CompletedTask;
                            }
                        };
                        m.OrientationChanged += OrientationChangedHandler;
                        try
                        {
                            await uart.SetBackgroundAsync(Colors.White);

                            await uart.WriteStringAsync("Loaded");

                            await connection.NotifyModuleLoadAsync();

                            Log.WriteLine("Initialization Complete. have connection and device.  ");

                            Task.WaitAll(Task.Run(() =>
                            {
                                try
                                {
                                    // TODO: implement ctl-c handler and cancellation token
                                    for (; ;)
                                    {
                                        Log.WriteLine("{0} wait spin", Environment.TickCount);
                                        Thread.Sleep(TimeSpan.FromSeconds(30));
                                    }
                                }
                                catch (Exception e)
                                {
                                    Log.WriteLine("wait spin exception {0}", e.ToString());
                                }
                            }));
                        }
                        finally
                        {
                            m.OrientationChanged -= OrientationChangedHandler;
                        }
                    }
                    finally
                    {
                        m.FruitChanged -= FruitChangedHandler;
                    }
                }
                finally
                {
                    m.ConfigurationChanged -= ConfigurationChangedHandler;
                }
            } finally
            {
                uart.Dispose();
                if (connection != null)
                {
                    connection.Dispose();
                }
            }
            return(0);
        }