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();
        }
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...");
            // TODO: Options.List
            AzureConnection connection = null;
            SPIMpuDevice    mpu        = 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("SPI Main CreateAzureConnectionAsync exception {0}", e.ToString());
                }
            }),
                Task.Run(async() =>
            {
                try
                {
                    Log.WriteLine("creating mpu device {0}", Options.DeviceName != null ? Options.DeviceName : "(default)");
                    mpu = await SPIMpuDevice.CreateMpuDeviceAsync(Options.DeviceName);
                    //var settings = new SPIDevice.
                    mpu.InitAsync().Wait();
                    if (Options.Test)
                    {
                        Log.WriteLine("initiating test");
                        if (Options.TestTime.HasValue)
                        {
                            mpu.Test(Options.TestTime.Value);
                        }
                        else
                        {
                            mpu.Test(TimeSpan.FromSeconds(15));
                        }
                        Environment.Exit(2);
                    }
                }
                catch (Exception e)
                {
                    Log.WriteLine("SPI exception {0}", e.ToString());
                }
            }
                         )
                );

            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("SPI wait spin exception {0}", e.ToString());
                        }
                    }));
                } finally
                {
                    mpu.OrientationChanged -= OrientationChangedHandler;
                    if (connection != null)
                    {
                        ((AzureModule)(connection.Module)).ModuleLoaded -= ModuleLoadedHandler;
                    }
                }
            } finally
            {
                mpu.Dispose();
                if (connection != null)
                {
                    connection.Dispose();
                }
            }
            return(0);
        }