Exemplo n.º 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);
        }
Exemplo n.º 2
0
        public static List <AzureIoTHub> GetIotHubList(
            ShowProgress progressCallback,
            ShowError errorCallback,
            RunPSCommand PSCallback)
        {
            List <AzureIoTHub> hubList = new List <AzureIoTHub>();

            try
            {
                if (MSAHelper.Subscriptions.Count == 0)
                {
                    // no subscritions means no IoT Hubs
                    return(hubList);
                }

                double progressPerSubscription = 85.0f / MSAHelper.Subscriptions.Count;
                for (int k = 0; k < MSAHelper.Subscriptions.Count; k++)
                {
                    List <Task> tasks            = new List <Task>();
                    string      subscriptionName = MSAHelper.Subscriptions[k];

                    PSCallback?.Invoke("az account set --subscription '" + subscriptionName + "'");

                    Collection <string> hubListResults = PSCallback?.Invoke("az iot hub list");
                    if (hubListResults != null && hubListResults.Count != 0)
                    {
                        for (int i = 0; i < hubListResults.Count; i++)
                        {
                            string hubName = hubListResults[i];
                            if (hubName.Contains("\"name\""))
                            {
                                hubName = hubName.Substring(hubName.IndexOf(":"));
                                hubName = hubName.Substring(hubName.IndexOf("\"") + 1);
                                hubName = hubName.Substring(0, hubName.IndexOf("\""));

                                // filter
                                if (hubName == "$fallback" || hubName == "S1" || hubName == "F1" || hubName == "B1")
                                {
                                    continue;
                                }

                                tasks.Add(Task.Run(() =>
                                {
                                    Collection <string> results2 = PSCallback("az iot hub show-connection-string --name '" + hubName + "'");
                                    if (results2 != null && results2.Count != 0)
                                    {
                                        for (int j = 0; j < results2.Count; j++)
                                        {
                                            string connectionString = results2[j];
                                            if (connectionString.Contains("\"connectionString\""))
                                            {
                                                // we have access
                                                lock (hubListLock)
                                                {
                                                    hubList.Add(new AzureIoTHub(hubName, subscriptionName));
                                                }
                                            }
                                        }
                                    }
                                }));
                            }
                        }
                    }

                    Task.WhenAll(tasks).Wait();
                    tasks.Clear();
                    progressCallback?.Invoke(progressPerSubscription, false);
                }
            }
            catch (Exception ex)
            {
                errorCallback?.Invoke(ex.Message);
            }

            return(hubList);
        }
Exemplo n.º 3
0
        public bool DeleteDevice(RunPSCommand PSCallback, string deviceId)
        {
            Collection <string> results = PSCallback?.Invoke($"az iot hub device-identity delete --device-id {deviceId} --hub-name {Name}");

            return(results != null);
        }
Exemplo n.º 4
0
        public bool CreateIoTEdgeDevice(RunPSCommand PSCallback, string deviceId)
        {
            Collection <string> results = PSCallback?.Invoke($"az iot hub device-identity create --device-id {deviceId} --hub-name {Name} --edge-enabled");

            return(results != null && results.Count != 0);
        }
        public static bool SignIn(
            ShowProgress progressCallback,
            ShowError errorCallback,
            RunPSCommand PSCallback)
        {
            if (CurrentState == SigninStates.SignedIn)
            {
                return(true);
            }

            try
            {
                Collection <string> results = PSCallback?.Invoke("az");
                if (results == null || results.Count == 0)
                {
                    errorCallback?.Invoke(Strings.AzureCLI);
                    if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                    {
                        Process.Start(new ProcessStartInfo("https://aka.ms/installazurecliwindows"));
                    }
                    else if (Environment.OSVersion.Platform == PlatformID.Unix)
                    {
                        "sudo apt-get update".Bash();
                        "sudo apt --assume-yes install curl".Bash();
                        "curl -sL -N https://aka.ms/InstallAzureCLIDeb | sudo bash".Bash();
                    }
                    else
                    {
                        errorCallback?.Invoke(Strings.OSNotSupported);
                    }

                    return(false);
                }

                progressCallback?.Invoke(5, true);

                results = PSCallback?.Invoke("az login");
                if (results == null || results.Count == 0)
                {
                    errorCallback?.Invoke(Strings.LoginFailedAlertMessage);
                    return(false);
                }

                // enumerate subscriptions
                Subscriptions.Clear();
                for (int i = 0; i < results.Count; i++)
                {
                    string json = results[i].ToString();
                    if (json.Contains("\"name\""))
                    {
                        json = json.Substring(json.IndexOf(":"));
                        json = json.Substring(json.IndexOf("\"") + 1);
                        json = json.Substring(0, json.IndexOf("\""));
                        if (!json.Contains("@"))
                        {
                            Subscriptions.Add(json);
                        }
                    }
                }

                progressCallback?.Invoke(10, true);

                // install iot extension, if required
                PSCallback?.Invoke("az extension add --name azure-cli-iot-ext");

                progressCallback?.Invoke(15, true);

                CurrentState = SigninStates.SignedIn;

                return(true);
            }
            catch (Exception ex)
            {
                errorCallback?.Invoke(Strings.LoginFailedAlertMessage + ": " + ex.Message);
                return(false);
            }
        }