public async void GetDeviceListFromAzure() { registryManager = RegistryManager.CreateFromConnectionString(connectionString); IQuery query = registryManager.CreateQuery("select * from devices"); List <Device> localDevices = new List <Device>(); Device currentDevice; try { while (query.HasMoreResults) { IEnumerable <Twin> devices = await query.GetNextAsTwinAsync(); devices.ToList().ForEach(x => { currentDevice = new Device() { Id = x.DeviceId, Status = (AzureIOT.Models.Status)x.Status, LastActive = x.LastActivityTime ?? DateTime.MinValue, CloudToDeviceMessages = x.CloudToDeviceMessageCount ?? x.CloudToDeviceMessageCount.Value, AuthType = (AzureIOT.Models.AuthTypes)(x.AuthenticationType ?? x.AuthenticationType), PrimaryThumbprint = x.X509Thumbprint.PrimaryThumbprint, SecondaryThumbprint = x.X509Thumbprint.SecondaryThumbprint }; localDevices.Add(currentDevice); currentDevice = null; }); } this.deviceResponse = new Response <List <Device> >() { Success = true, ResponseObject = localDevices }; } catch (Exception ex) { List <AzureException> azureException = new List <AzureException>() { new AzureException() { Exception = ex, Message = ex.Message } }; this.deviceResponse = new Response <List <Device> >() { Success = false, Exceptions = azureException.ToArray() }; } }
public void GetDeviceListFromAzure() { registryManager = RegistryManager.CreateFromConnectionString(connectionStringForPortal); List <Device> localDevices = new List <Device>(); IEnumerable <Microsoft.Azure.Devices.Device> azureDevice = new List <Microsoft.Azure.Devices.Device>();// = registryManager.GetDevicesAsync(100); Task task = Task.Run(() => { azureDevice = registryManager.GetDevicesAsync(100).Result; }); task.Wait(); Device currentDevice; try { foreach (var x in azureDevice) { currentDevice = new Device() { Id = x.Id, Status = (AzureIOT.Models.Status)x.Status, LastActive = x.ConnectionStateUpdatedTime, CloudToDeviceMessages = x.CloudToDeviceMessageCount, //AuthType = (AzureIOT.Models.AuthTypes)(x. ?? x.AuthenticationType), PrimaryThumbprint = x.Authentication != null ? x.Authentication.X509Thumbprint.PrimaryThumbprint : null, SecondaryThumbprint = x.Authentication != null ? x.Authentication.X509Thumbprint.SecondaryThumbprint : null, }; localDevices.Add(currentDevice); currentDevice = null; } this.deviceResponse = new Response <List <Device> >() { Success = true, ResponseObject = localDevices }; } catch (Exception ex) { List <AzureException> azureException = new List <AzureException>() { new AzureException() { Exception = ex, Message = ex.Message } }; this.deviceResponse = new Response <List <Device> >() { Success = false, Exceptions = azureException.ToArray() }; } }
public Device DeviceDetail(string DeviceId) { registryManager = RegistryManager.CreateFromConnectionString(connectionStringForPortal); List <Device> localDevices = new List <Device>(); IEnumerable <Microsoft.Azure.Devices.Device> azureDevice = new List <Microsoft.Azure.Devices.Device>();// = registryManager.GetDevicesAsync(100); Task task = Task.Run(() => { azureDevice = registryManager.GetDevicesAsync(100).Result; }); task.Wait(); try { Device currentDevice = new Device(); foreach (var x in azureDevice) { if (x.Id != DeviceId) { continue; } currentDevice = new Device() { Id = x.Id, Status = (AzureIOT.Models.Status)x.Status, LastActive = x.LastActivityTime, ConnectionStatus = (AzureIOT.Models.ConnectStatus)x.ConnectionState, CloudToDeviceMessages = x.CloudToDeviceMessageCount, //AuthType = (AzureIOT.Models.AuthTypes)(x. ?? x.AuthenticationType), PrimaryThumbprint = x.Authentication != null ? x.Authentication.X509Thumbprint.PrimaryThumbprint : null, SecondaryThumbprint = x.Authentication != null ? x.Authentication.X509Thumbprint.SecondaryThumbprint : null, }; } return(currentDevice); } catch (DeviceAlreadyExistsException ex) { List <AzureException> exception = new List <AzureException>() { new AzureException() { Exception = ex, Message = ex.Message } }; return(new Device() { }); } }
public DashboardFacts GetDashboardFacts() { DashboardFacts facts = new DashboardFacts(); registryManager = RegistryManager.CreateFromConnectionString(connectionStringForPortal); List <Device> localDevices = new List <Device>(); IEnumerable <Microsoft.Azure.Devices.Device> azureDevice = new List <Microsoft.Azure.Devices.Device>();// = registryManager.GetDevicesAsync(100); Task task = Task.Run(() => { azureDevice = registryManager.GetDevicesAsync(100).Result; }); task.Wait(); try { facts.TotalDevices = azureDevice.Count(); foreach (var x in azureDevice) { if (x.ConnectionState == DeviceConnectionState.Connected) { facts.ConnectedDevices++; } else { facts.OfflineDevices++; } facts.TotalCloudToDeviceMessages = x.CloudToDeviceMessageCount; } return(facts); } catch (DeviceAlreadyExistsException ex) { List <AzureException> exception = new List <AzureException>() { new AzureException() { Exception = ex, Message = ex.Message } }; return(facts); } }
public AzureSubscriptionService() { this.connectionStringForPortal = ConfigurationManager.AppSettings["AzureSubscriptionStr"]; this.connectionStringForStorage = ConfigurationManager.AppSettings["AzureStorageStr"]; registryManager = RegistryManager.CreateFromConnectionString(connectionStringForPortal); }
public AzureSubscriptionService(string constr = "HostName=iotHub4jbagz366jxyq.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=ijus5i/Z0Pr0EFHFQuNcK4kpqI+34rPmgz+VbTHFZUw=") { this.connectionString = constr; registryManager = RegistryManager.CreateFromConnectionString(connectionString); }