Exemplo n.º 1
0
        private static async Task <MessageResponse> OnFruitMessageReceived(Message msg, object ctx)
        {
            AzureModule module    = (AzureModule)ctx;
            var         msgBytes  = msg.GetBytes();
            var         msgString = Encoding.UTF8.GetString(msgBytes);

            Log.WriteLine("fruit msg received: '{0}'", msgString);
            var fruitMsg = JsonConvert.DeserializeObject <FruitMessage>(msgString);
            await module.ProcessFruitMessage(fruitMsg);

            return(MessageResponse.Completed);
        }
Exemplo n.º 2
0
        private async Task <MethodResponse> SetFruit(MethodRequest req, object context)
        {
            string data = Encoding.UTF8.GetString(req.Data);

            Log.WriteLine("Direct Method SetFruit {0}", data);
            var         fruitMsg = JsonConvert.DeserializeObject <FruitMessage>(data);
            AzureModule module   = (AzureModule)context;
            await module.ProcessFruitMessage(fruitMsg);

            // Acknowlege the direct method call with a 200 success message
            string result = "{\"result\":\"Executed direct method: " + req.Name + "\"}";

            return(new MethodResponse(Encoding.UTF8.GetBytes(result), 200));
        }
Exemplo n.º 3
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);
        }