コード例 #1
0
        public AzureDeviceEntity GetDevice(RunPSCommand PSCallback, string deviceId)
        {
            Collection <string> results = PSCallback?.Invoke($"az iot hub device-identity show --device-id {deviceId} --hub-name {Name}");

            if (results != null && results.Count != 0)
            {
                var deviceEntity = new AzureDeviceEntity();
                deviceEntity.Id = deviceId;

                for (int i = 0; i < results.Count; i++)
                {
                    string result = results[i];
                    if (result.Contains("\"iotEdge\": true"))
                    {
                        deviceEntity.IotEdge = true;
                        continue;
                    }

                    if (result.Contains("primaryKey"))
                    {
                        result = result.Substring(result.IndexOf(":"));
                        result = result.Substring(result.IndexOf("\"") + 1);
                        result = result.Substring(0, result.IndexOf("\""));
                        deviceEntity.PrimaryKey = result;
                    }
                }

                if (deviceEntity.IotEdge)
                {
                    deviceEntity.Modules = new List <AzureModuleEntity>();

                    Collection <string> results2 = PSCallback?.Invoke($"az iot hub module-identity list --device-id {deviceId} --hub-name {Name}");
                    if (results2 != null && results2.Count != 0)
                    {
                        for (int i = 0; i < results2.Count; i++)
                        {
                            string result = results2[i];
                            if (result.Contains("moduleId"))
                            {
                                var entity = new AzureModuleEntity();
                                result          = result.Substring(result.IndexOf(":"));
                                result          = result.Substring(result.IndexOf("\"") + 1);
                                result          = result.Substring(0, result.IndexOf("\""));
                                entity.Id       = result;
                                entity.DeviceId = deviceEntity.Id;
                                deviceEntity.Modules.Add(entity);
                            }
                        }
                    }
                }

                return(deviceEntity);
            }

            return(null);
        }
コード例 #2
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);
        }
コード例 #3
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);
        }