private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                string      statusText = Strings.ModulesStatus + "\r\n";
                AzureIoTHub hub        = new AzureIoTHub(_viewModel.DisplayName);
                List <KeyValuePair <string, string> > modulesStatus = hub.GetDeviceModulesStatusAsync(_viewModel.AzureCreateId).Result;
                if ((modulesStatus != null) && (modulesStatus.Count > 0))
                {
                    foreach (KeyValuePair <string, string> moduleStatus in modulesStatus)
                    {
                        statusText += (moduleStatus.Key + ": " + moduleStatus.Value + "\r\n");
                    }
                }
                else
                {
                    statusText += Strings.NotAvailable;
                }

                ModulesStatus.Dispatcher.Invoke(() => ModulesStatus.Text = statusText, DispatcherPriority.Background);
            }
            catch (Exception)
            {
                // do nothing
            }
        }
コード例 #2
0
        public void CreateAzureIoTEdgeDevice(AzureIoTHub azureIoTHub, string azureCreateId, bool installIIoTModules)
        {
            PowerShell PS = PowerShell.Create();

            //PS.Streams.Warning.DataAdded += PSWarningStreamHandler;
            //PS.Streams.Error.DataAdded += PSErrorStreamHandler;
            //PS.Streams.Information.DataAdded += PSInfoStreamHandler;

            if (!SetupPrerequisits())
            {
                Console.WriteLine("Error: " + Strings.PreRequisitsFailed);
                return;
            }

            // first set the Azure subscription for the selected IoT Hub
            PS.AddScript($"Az account set --subscription '{azureIoTHub.SubscriptionName}'");
            Collection <PSObject> results = PS.Invoke();

            PS.Streams.ClearStreams();
            PS.Commands.Clear();

            // check if device exists already
            var deviceEntity = azureIoTHub.GetDevice(Program.RunPSCommand, azureCreateId);

            if (deviceEntity != null)
            {
                char decision = 'a';
                while (decision != 'y' && decision != 'n')
                {
                    Console.WriteLine();
                    Console.Write(Strings.DeletedDevice + " [y/n]: ");
                    decision = Console.ReadKey().KeyChar;
                }
                Console.WriteLine();

                if (decision == 'y')
                {
                    azureIoTHub.DeleteDevice(Program.RunPSCommand, azureCreateId);
                }
                else
                {
                    return;
                }
            }

            try
            {
                // create the device
                azureIoTHub.CreateIoTEdgeDevice(Program.RunPSCommand, azureCreateId);

                // retrieve the newly created device
                deviceEntity = azureIoTHub.GetDevice(Program.RunPSCommand, azureCreateId);
                if (deviceEntity != null)
                {
                    if (!InstallIoTEdge(deviceEntity, azureIoTHub, installIIoTModules))
                    {
                        // installation failed so delete the device again
                        azureIoTHub.DeleteDevice(Program.RunPSCommand, azureCreateId);
                    }
                }
                else
                {
                    Console.WriteLine("Error: " + Strings.CreateFailed);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);

                try
                {
                    // installation failed so delete the device again (if neccessary)
                    azureIoTHub.DeleteDevice(Program.RunPSCommand, azureCreateId);
                }
                catch (Exception ex2)
                {
                    Console.WriteLine("Error: " + ex2.Message);
                }
            }
        }
コード例 #3
0
        private bool InstallIoTEdge(AzureDeviceEntity deviceEntity, AzureIoTHub iotHub, bool installIIoTModules)
        {
            if (deviceEntity != null)
            {
                PowerShell PS = PowerShell.Create();
                PS.Streams.Warning.DataAdded     += PSWarningStreamHandler;
                PS.Streams.Error.DataAdded       += PSErrorStreamHandler;
                PS.Streams.Information.DataAdded += PSInfoStreamHandler;

                if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                {
                    try
                    {
                        var newProcessInfo = new ProcessStartInfo
                        {
                            FileName = Environment.SystemDirectory + "\\WindowsPowerShell\\v1.0\\powershell.exe"
                        };

                        Console.WriteLine(Strings.Uninstall);
                        newProcessInfo.Arguments = "Invoke-WebRequest -useb aka.ms/iotedge-win | Invoke-Expression; Uninstall-IoTEdge -Force";
                        var process = Process.Start(newProcessInfo);
                        process.WaitForExit();
                        if (process.ExitCode != 0)
                        {
                            Console.WriteLine("Error: " + Strings.UninstallFailed);
                            return(false);
                        }

                        Console.WriteLine(Strings.Install);
                        newProcessInfo.Arguments = $"Invoke-WebRequest -useb aka.ms/iotedge-win | Invoke-Expression; Install-IoTEdge -ContainerOs Windows -Manual -DeviceConnectionString 'HostName={iotHub.Name}.azure-devices.net;DeviceId={deviceEntity.Id};SharedAccessKey={deviceEntity.PrimaryKey}' -SkipBatteryCheck";
                        process = Process.Start(newProcessInfo);
                        process.WaitForExit();
                        if (process.ExitCode != 0)
                        {
                            Console.WriteLine("Error: " + Strings.InstallFailed);
                            return(false);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error: " + ex.Message);
                        return(false);
                    }
                }
                else if (Environment.OSVersion.Platform == PlatformID.Unix)
                {
                    "sudo apt-get update".Bash();
                    "sudo apt-get --assume-yes install iotedge".Bash();
                    $"sudo sed -i 's/<ADD DEVICE CONNECTION STRING HERE>/HostName={iotHub.Name}.azure-devices.net;DeviceId={deviceEntity.Id};SharedAccessKey={deviceEntity.PrimaryKey}/g' /etc/iotedge/config.yaml".Bash();
                    "sudo systemctl restart iotedge".Bash();
                }
                else
                {
                    Console.WriteLine(Strings.OSNotSupported);
                    return(false);
                }

                if (installIIoTModules)
                {
                    Console.WriteLine(Strings.Deployment);

                    if (!AzureIoT.CreateDriveMappingDirectory())
                    {
                        Console.WriteLine("Error: " + Strings.DeployFailed);
                        return(false);
                    }

                    if (!AzureIoT.LoadDeploymentManifest())
                    {
                        Console.WriteLine("Error: " + Strings.DeployFailed);
                        return(false);
                    }

                    if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                    {
                        PS.AddScript($"Az iot edge set-modules --device-id {deviceEntity.Id} --hub-name {iotHub.Name} --content ./{AzureIoT.DeploymentManifestNameWindows}");
                    }
                    else if (Environment.OSVersion.Platform == PlatformID.Unix)
                    {
                        PS.AddScript($"Az iot edge set-modules --device-id {deviceEntity.Id} --hub-name {iotHub.Name} --content ./{AzureIoT.DeploymentManifestNameLinux}");
                    }
                    else
                    {
                        Console.WriteLine(Strings.OSNotSupported);
                        return(false);
                    }

                    Collection <PSObject> results = PS.Invoke();
                    PS.Streams.ClearStreams();
                    PS.Commands.Clear();
                    if (results.Count == 0)
                    {
                        Console.WriteLine("Error: " + Strings.DeployFailed);
                        return(false);
                    }
                }

                Console.WriteLine();
                Console.WriteLine(Strings.Completed);
                Console.WriteLine(Strings.Reboot);
                return(true);
            }

            return(false);
        }
コード例 #4
0
        private bool InstallIoTEdge(AzureDeviceEntity deviceEntity, AzureIoTHub iotHub)
        {
            if (deviceEntity != null)
            {
                PowerShell PS = PowerShell.Create();
                PS.Streams.Warning.DataAdded     += PSWarningStreamHandler;
                PS.Streams.Error.DataAdded       += PSErrorStreamHandler;
                PS.Streams.Information.DataAdded += PSInfoStreamHandler;

                OutputLB += (Strings.Uninstall + "\n");
                try
                {
                    PS.AddScript("Invoke-WebRequest -useb aka.ms/iotedge-win | Invoke-Expression; Uninstall-IoTEdge -Force");
                    Collection <PSObject> results1 = PS.Invoke();
                    PS.Streams.ClearStreams();
                    PS.Commands.Clear();
                    if (results1.Count == 0)
                    {
                        MessageBox.Show(Strings.UninstallFailed, Strings.AboutSubtitle, MessageBoxButton.OK, MessageBoxImage.Error);
                        return(false);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, Strings.AboutSubtitle, MessageBoxButton.OK, MessageBoxImage.Error);
                    PS.Streams.ClearStreams();
                    PS.Commands.Clear();
                    return(false);
                }

                OutputLB += (Strings.Install + "\n");
                PS.AddScript($"Invoke-WebRequest -useb aka.ms/iotedge-win | Invoke-Expression; Install-IoTEdge -ContainerOs Windows -Manual -DeviceConnectionString 'HostName={iotHub.Name}.azure-devices.net;DeviceId={deviceEntity.Id};SharedAccessKey={deviceEntity.PrimaryKey}' -SkipBatteryCheck");
                Collection <PSObject> results = PS.Invoke();
                PS.Streams.ClearStreams();
                PS.Commands.Clear();
                if (results.Count == 0)
                {
                    MessageBox.Show(Strings.InstallFailed, Strings.AboutSubtitle, MessageBoxButton.OK, MessageBoxImage.Error);
                    return(false);
                }

                bool installIIoTModues = false;
                _parentPage.CheckBox.Dispatcher.Invoke(() => installIIoTModues = (_parentPage.CheckBox.IsChecked == true), DispatcherPriority.Send);
                if (installIIoTModues)
                {
                    OutputLB += (Strings.Deployment + "\n");

                    if (!AzureIoT.CreateDriveMappingDirectory())
                    {
                        MessageBox.Show(Strings.DeployFailed, Strings.AboutSubtitle, MessageBoxButton.OK, MessageBoxImage.Error);
                        return(false);
                    }

                    if (!AzureIoT.LoadDeploymentManifest())
                    {
                        MessageBox.Show(Strings.DeployFailed, Strings.AboutSubtitle, MessageBoxButton.OK, MessageBoxImage.Error);
                        return(false);
                    }

                    PS.AddScript($"Az iot edge set-modules --device-id {deviceEntity.Id} --hub-name {iotHub.Name} --content ./{AzureIoT.DeploymentManifestNameWindows}");
                    results = PS.Invoke();
                    PS.Streams.ClearStreams();
                    PS.Commands.Clear();
                    if (results.Count == 0)
                    {
                        MessageBox.Show(Strings.DeployFailed, Strings.AboutSubtitle, MessageBoxButton.OK, MessageBoxImage.Error);
                        return(false);
                    }
                }

                OutputLB += (Strings.Completed + "\n" + Strings.Reboot + "\n");
                return(true);
            }

            return(false);
        }
        private bool InstallIoTEdge(Device deviceEntity, AzureIoTHub iotHub)
        {
            PowerShell PS = PowerShell.Create();

            PS.Streams.Warning.DataAdded     += PSWarningStreamHandler;
            PS.Streams.Error.DataAdded       += PSErrorStreamHandler;
            PS.Streams.Information.DataAdded += PSInfoStreamHandler;

            OutputLB += (Strings.Uninstall + "\n");
            try
            {
                PS.AddScript("Invoke-WebRequest -useb aka.ms/iotedge-win | Invoke-Expression; Uninstall-IoTEdge -Force");
                Collection <PSObject> results1 = PS.Invoke();
                PS.Streams.ClearStreams();
                PS.Commands.Clear();
                if (results1.Count == 0)
                {
                    MessageBox.Show(Strings.UninstallFailed, Strings.AboutSubtitle, MessageBoxButton.OK, MessageBoxImage.Error);
                    return(false);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Strings.AboutSubtitle, MessageBoxButton.OK, MessageBoxImage.Error);
                PS.Streams.ClearStreams();
                PS.Commands.Clear();
                return(false);
            }

            OutputLB += (Strings.Install + "\n");

            bool   useLCOW    = false;
            bool   isSaaS     = false;
            string idScope    = string.Empty;
            string deviceID   = string.Empty;
            string primaryKey = string.Empty;

            _parentPage.Dispatcher.Invoke(() =>
            {
                useLCOW    = (_parentPage.UseLCoW.IsChecked == true);
                isSaaS     = _parentPage._isSaaSSetup;
                idScope    = _parentPage.DPS_IDScope.Text;
                deviceID   = _parentPage.DPS_DeviceID.Text;
                primaryKey = _parentPage.DPS_PrimaryKey.Text;
            });

            string os = "Windows";

            if (useLCOW)
            {
                os = "Linux";
            }

            if (isSaaS)
            {
                PS.AddScript($"Invoke-WebRequest -useb aka.ms/iotedge-win | Invoke-Expression; Install-IoTEdge -ContainerOs {os} -Dps -ScopeId {idScope} -RegistrationId {deviceID} -SymmetricKey {primaryKey} -SkipBatteryCheck");
            }
            else
            {
                if (deviceEntity != null)
                {
                    PS.AddScript($"Invoke-WebRequest -useb aka.ms/iotedge-win | Invoke-Expression; Install-IoTEdge -ContainerOs {os} -Manual -DeviceConnectionString 'HostName={iotHub.Name}.azure-devices.net;DeviceId={deviceEntity.Id};SharedAccessKey={deviceEntity.Authentication.SymmetricKey.PrimaryKey}' -SkipBatteryCheck");
                }
                else
                {
                    MessageBox.Show(Strings.InstallFailed, Strings.AboutSubtitle, MessageBoxButton.OK, MessageBoxImage.Error);
                    return(false);
                }
            }

            Collection <PSObject> results = PS.Invoke();

            PS.Streams.ClearStreams();
            PS.Commands.Clear();
            if (results.Count == 0)
            {
                MessageBox.Show(Strings.InstallFailed, Strings.AboutSubtitle, MessageBoxButton.OK, MessageBoxImage.Error);
                return(false);
            }

            if (!Tools.CreateDriveMappingDirectory())
            {
                MessageBox.Show(Strings.DeployFailed, Strings.AboutSubtitle, MessageBoxButton.OK, MessageBoxImage.Error);
                return(false);
            }

            OutputLB += (Strings.Completed + "\n" + Strings.Reboot + "\n");
            return(true);
        }
コード例 #6
0
        public void CreateAzureIoTEdgeDevice(string azureCreateId)
        {
            if (!SetupPrerequisits())
            {
                Console.WriteLine("Error: " + Strings.PreRequisitsFailed);
                return;
            }

            string connectionString = string.Empty;

            while ((connectionString == string.Empty) &&
                   !connectionString.StartsWith("HostName=") &&
                   !connectionString.Contains(".azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey="))
            {
                Console.WriteLine("\n" + Strings.IoTHubs + ":");
                Console.WriteLine(Strings.IoTHubsHint);
                connectionString = Console.ReadLine();
            }

            AzureIoTHub azureIoTHub = new AzureIoTHub(connectionString);

            // check if device exists already
            var deviceEntity = azureIoTHub.GetDeviceAsync(azureCreateId).Result;

            if (deviceEntity != null)
            {
                char decision = 'a';
                while (decision != 'y' && decision != 'n')
                {
                    Console.WriteLine();
                    Console.Write(Strings.DeletedDevice + " [y/n]: ");
                    decision = Console.ReadKey().KeyChar;
                }
                Console.WriteLine();

                if (decision == 'y')
                {
                    azureIoTHub.DeleteDeviceAsync(azureCreateId).Wait();
                }
                else
                {
                    return;
                }
            }

            try
            {
                // create the device
                string os = "Windows";
                if (Environment.OSVersion.Platform == PlatformID.Unix)
                {
                    os = "Linux";
                }

                azureIoTHub.CreateIoTEdgeDeviceAsync(azureCreateId, os).Wait();

                // retrieve the newly created device
                deviceEntity = azureIoTHub.GetDeviceAsync(azureCreateId).Result;
                if (deviceEntity != null)
                {
                    if (!InstallIoTEdge(deviceEntity, azureIoTHub))
                    {
                        // installation failed so delete the device again
                        azureIoTHub.DeleteDeviceAsync(azureCreateId).Wait();
                    }
                }
                else
                {
                    Console.WriteLine("Error: " + Strings.CreateFailed);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);

                try
                {
                    // installation failed so delete the device again (if neccessary)
                    azureIoTHub.DeleteDeviceAsync(azureCreateId).Wait();
                }
                catch (Exception ex2)
                {
                    Console.WriteLine("Error: " + ex2.Message);
                }
            }
        }
コード例 #7
0
        private bool InstallIoTEdge(Device deviceEntity, AzureIoTHub iotHub)
        {
            if (deviceEntity != null)
            {
                if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                {
                    try
                    {
                        var newProcessInfo = new ProcessStartInfo
                        {
                            FileName = Environment.SystemDirectory + "\\WindowsPowerShell\\v1.0\\powershell.exe"
                        };

                        Console.WriteLine(Strings.Uninstall);
                        newProcessInfo.Arguments = "Invoke-WebRequest -useb aka.ms/iotedge-win | Invoke-Expression; Uninstall-IoTEdge -Force";
                        var process = Process.Start(newProcessInfo);
                        process.WaitForExit();
                        if (process.ExitCode != 0)
                        {
                            Console.WriteLine("Error: " + Strings.UninstallFailed);
                            return(false);
                        }

                        Console.WriteLine(Strings.Install);
                        newProcessInfo.Arguments = $"Invoke-WebRequest -useb aka.ms/iotedge-win | Invoke-Expression; Install-IoTEdge -ContainerOs Windows -Manual -DeviceConnectionString 'HostName={iotHub.Name}.azure-devices.net;DeviceId={deviceEntity.Id};SharedAccessKey={deviceEntity.Authentication.SymmetricKey.PrimaryKey}' -SkipBatteryCheck";
                        process = Process.Start(newProcessInfo);
                        process.WaitForExit();
                        if (process.ExitCode != 0)
                        {
                            Console.WriteLine("Error: " + Strings.InstallFailed);
                            return(false);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error: " + ex.Message);
                        return(false);
                    }
                }
                else if (Environment.OSVersion.Platform == PlatformID.Unix)
                {
                    "sudo apt-get update".Bash();
                    "sudo apt-get remove --purge iotedge".Bash();
                    "sudo apt-get --assume-yes install iotedge".Bash();
                    $"sudo sed -i 's/<ADD DEVICE CONNECTION STRING HERE>/HostName={iotHub.Name}.azure-devices.net;DeviceId={deviceEntity.Id};SharedAccessKey={deviceEntity.Authentication.SymmetricKey.PrimaryKey}/g' /etc/iotedge/config.yaml".Bash();
                    "sudo systemctl restart iotedge".Bash();
                }
                else
                {
                    Console.WriteLine(Strings.OSNotSupported);
                    return(false);
                }

                if (!Tools.CreateDriveMappingDirectory())
                {
                    Console.WriteLine("Error: " + Strings.DeployFailed);
                    return(false);
                }

                Console.WriteLine();
                Console.WriteLine(Strings.Completed);
                Console.WriteLine(Strings.Reboot);
                return(true);
            }

            return(false);
        }