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

            Log.WriteLine("Orientation msg received: '{0}'", msgString);
            var orientationMsg = JsonConvert.DeserializeObject <OrientationMessage>(msgString);
            await module.ProcessOrientationMessage(orientationMsg);

            return(MessageResponse.Completed);
        }
Example #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));
        }
Example #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, string> FruitColors = new Dictionary <string, string>()
            {
                { "apple", "red" },
                { "pear", "yellow" },
                { "pen", "green" },
                { "grapes", "blue" }
            };
            AzureConnection connection = null;
            GPIODevice      gpio       = 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("GPIO Main CreateAzureConnectionAsync exception {0}", e.ToString());
                }
            }),
                Task.Run(() =>
            {
                try
                {
                    gpio = new GPIODevice();
                    gpio.InitOutputPins(Options);
                    if (Options.Test)
                    {
                        Log.WriteLine("initiating pin test");
                        if (Options.TestTime.HasValue)
                        {
                            gpio.Test(Options.TestTime.Value, TimeSpan.FromSeconds(2));
                        }
                        else
                        {
                            gpio.Test(TimeSpan.FromSeconds(15), TimeSpan.FromSeconds(2));
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.WriteLine("GPIO InitOutputPins exception {0}", e.ToString());
                }
            }
                         )
                );

            try
            {
                AzureModule m = (AzureModule)connection.Module;
                Orientation currentOrientation = Orientation.RightSideUp;
                EventHandler <ConfigurationType> ConfigurationChangedHandler = async(object sender, ConfigurationType newConfiguration) =>
                {
                    var module = (AzureModule)sender;
                    Log.WriteLine("updating gpio pin config with {0}", newConfiguration.ToString());
                    await gpio.UpdatePinConfigurationAsync(newConfiguration.GpioPins);
                };
                m.ConfigurationChanged += ConfigurationChangedHandler;
                try
                {
                    EventHandler <string> FruitChangedHandler = async(object sender, string fruit) =>
                    {
                        Log.WriteLine("fruit changed to {0}", fruit.ToLower());
                        var    module = (AzureModule)sender;
                        string color  = null;
                        if (FruitColors.ContainsKey(fruit.ToLower()))
                        {
                            color = FruitColors[fruit.ToLower()];
                        }
                        await Task.Run(() => gpio.ActivePin = color);
                    };
                    m.FruitChanged += FruitChangedHandler;
                    try
                    {
                        EventHandler <Orientation> OrientationChangedHandler = async(object sender, EdgeModuleSamples.Common.Orientation o) =>
                        {
                            Log.WriteLine("OrientationChanged sent {0}", o.ToString());
                            //var module = (AzureModule)sender;
                            Log.WriteLine("Current Orientation {0}", currentOrientation.ToString());
                            if (o != currentOrientation)
                            {
                                currentOrientation = o;
                                Log.WriteLine("Orientation changing to {0}", o.ToString());
                                await Task.Run(() => gpio.InvertOutputPins());
                            }
                            else
                            {
                                Log.WriteLine("Orientation already correct -- skipping", o.ToString());
                                await Task.CompletedTask;
                            }
                        };
                        m.OrientationChanged += OrientationChangedHandler;
                        try
                        {
                            await Task.Run(async() =>
                            {
                                try
                                {
                                    Log.WriteLine("initializing gpio pin config with {0}", m.Configuration.GpioPins);
                                    await gpio.UpdatePinConfigurationAsync(m.Configuration.GpioPins);
                                }
                                catch (Exception e)
                                {
                                    Log.WriteLine("GPIO UpdatePinConfig Lambda exception {0}", e.ToString());
                                }
                            });

                            await connection.NotifyModuleLoadAsync();

                            Log.WriteLine("Initialization Complete. have connection and device pins.  Active Pin is {0}", gpio.ActivePin == null ? "(null)" : gpio.ActivePin);

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

            return(0);
        }