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

            await _moduleClient.SetInputMessageHandlerAsync(Keys.ModuleLoadedInputRoute, ModuleLoadedMessageHandler, this);

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

            await base.AzureModuleInitEndAsync();

            Log.WriteLine("I2C AzureModuleInitAsync complete.");
        }
Ejemplo n.º 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...");
            AzureConnection connection = null;
            I2CMpuDevice    mpu        = null;
            // TODO: Options.List
            await Task.WhenAll(
                Task.Run(async() => {
                try {
                    if (!Options.Test)
                    {
                        Log.WriteLine("starting connection creation");
                        connection = await AzureConnection.CreateAzureConnectionAsync();
                    }
                }
                catch (Exception e)
                {
                    Log.WriteLine("I2c Main CreateAzureConnectionAsync exception {0}", e.ToString());
                }
            }),
                Task.Run(async() =>
            {
                try
                {
                    Log.WriteLine("creating mpu device {0}", Options.DeviceName != null ? Options.DeviceName : "(default)");
                    mpu = await I2CMpuDevice.CreateMpuDeviceAsync(Options.DeviceName);
                    mpu.InitAsync().Wait();
                    if (Options.Test)
                    {
                        Log.WriteLine("initiating test");
                        if (Options.Test)
                        {
                            mpu.Test(Options.TestTime.Value);
                        }
                        else
                        {
                            mpu.Test(TimeSpan.FromSeconds(15));
                        }
                        Environment.Exit(2);
                    }
                }
                catch (Exception e)
                {
                    Log.WriteLine("I2c exception {0}", e.ToString());
                    Environment.Exit(2);
                }
            }
                         )
                );

            try
            {
                EventHandler <string> ModuleLoadedHandler = async(Object sender, string moduleName) =>
                {
                    try
                    {
                        Log.WriteLine("module loaded.   resending state");
                        await connection.NotifyNewModuleOfCurrentStateAsync();
                    }
                    catch (Exception e)
                    {
                        Log.WriteLineError("failed to notify state {0}", e.ToString());
                        Environment.Exit(2);
                    }
                };
                if (connection != null)
                {
                    ((AzureModule)(connection.Module)).ModuleLoaded += ModuleLoadedHandler;
                }

                EdgeModuleSamples.Common.Device.MpuDevice.OrientationEventHandler OrientationChangedHandler = (device, change) =>
                {
                    connection.UpdateObject(new KeyValuePair <string, object>(Keys.Orientation, change.newOrientation));
                };
                mpu.OrientationChanged += OrientationChangedHandler;
                try
                {
                    Log.WriteLine("Initialization Complete. have connection and device.  ");

                    Task.WaitAll(Task.Run(async() =>
                    {
                        try
                        {
                            await mpu.BeginOrientationMonitoringAsync();
                        }
                        catch (Exception e)
                        {
                            Log.WriteLine("I2c wait spin exception {0}", e.ToString());
                        }
                    }));
                } finally
                {
                    mpu.OrientationChanged -= OrientationChangedHandler;
                    if (connection != null)
                    {
                        ((AzureModule)(connection.Module)).ModuleLoaded -= ModuleLoadedHandler;
                    }
                }
            } finally
            {
                if (connection != null)
                {
                    connection.Dispose();
                }
                mpu.Dispose();
            }
            return(0);
        }