public static void Main(string[] args) { // Register Device with Azure registryManager = RegistryManager.CreateFromConnectionString(connectionString); RegisterDeviceAsync().Wait(); Console.WriteLine("Simulated device\n"); deviceClient = DeviceClient.Create(iotHubUri, new DeviceAuthenticationWithRegistrySymmetricKey(deviceId, deviceKey)); SendDeviceToCloudMessagesAsync(); Console.ReadLine(); }
static void Main( string[] args ) { registryManager = RegistryManager.CreateFromConnectionString( connectionString ); AddDeviceAsync( ).Wait( ); Console.ReadLine( ); }
private static async Task PrepareTestAsync() { kRegistryManager = AZD.RegistryManager.CreateFromConnectionString(kIoTHubConnectionString); kDeviceConnectionList = new List <string>(); kDevicesList = new List <AZD.Device>(); do { var devicesList = (await kRegistryManager.GetDevicesAsync(kDevicePageSize)).ToList(); kDevicesList.AddRange(devicesList); if (kDevicesList.Count == kTotalDevices) { break; } } while (true); for (int idx = 0; idx < kDevicesList.Count; ++idx) { try { var device = kDevicesList[idx]; var connString = device.Authentication.SymmetricKey.PrimaryKey; connString = string.Format(kDeviceConnectionString, device.Id, connString); Console.WriteLine(connString); kDeviceConnectionList.Add(connString); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
/// <summary> /// Simulator a device /// </summary> /// <param name="args">Usage: sumulator {deviceid} {min} {max}</param> static void Main(string[] args) { #region added if (args.Length < 3) { Error("Usage: sumulator {deviceid} {max} {min}"); Wait(); return; } deviceId = args[0]; max = int.Parse(args[2]); min = int.Parse(args[1]); run = true; connectionString = ConfigurationManager.AppSettings["iotHubConnectionString"]; iotHubUri = ConfigurationManager.AppSettings["iotHubUri"]; registryManager = RegistryManager.CreateFromConnectionString(connectionString); #endregion AddDeviceAsync().Wait(); SendDeviceToCloudMessagesAsync(); ReceiveCommandAsync(); Wait("Press [ENTER] to exit..."); run = false; RemoveDeviceAsync().Wait(); }
private static async Task AddDevicesAsync() { kRegistryManager = AZD.RegistryManager.CreateFromConnectionString(kIoTHubConnectionString); kDeviceConnectionList = new List <DeviceClient>(); for (int idx = 0; idx < kTotalDevices; ++idx) { var deviceIdString = $"dev{idx}"; try { var device = await kRegistryManager.AddDeviceAsync(new AZD.Device(deviceIdString)); var connString = device.Authentication.SymmetricKey.PrimaryKey; connString = string.Format(kDeviceConnectionString, deviceIdString, connString); Console.WriteLine(connString); kDeviceConnectionList.Add(DeviceClient.CreateFromConnectionString(connString)); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
public async static Task<string> AddDeviceAsync(string deviceId) { Device device; bool isNew = true; if(registryManager == null) registryManager = RegistryManager.CreateFromConnectionString(connectionString); try { device = await registryManager.AddDeviceAsync(new Device(deviceId)); } catch (DeviceAlreadyExistsException) { device = await registryManager.GetDeviceAsync(deviceId); isNew = false; } if(isNew) Debug.WriteLine(string.Format("Generated device key: {0}", device.Authentication.SymmetricKey.PrimaryKey)); else Debug.WriteLine(string.Format("Using existing device key: {0}", device.Authentication.SymmetricKey.PrimaryKey)); return device.Authentication.SymmetricKey.PrimaryKey; }
public DevicesProcessor(string iotHubConnenctionString, int devicesCount, string protocolGatewayName) { this.listOfDevices = new List<DeviceEntity>(); this.iotHubConnectionString = iotHubConnenctionString; this.maxCountOfDevices = devicesCount; this.protocolGatewayHostName = protocolGatewayName; this.registryManager = RegistryManager.CreateFromConnectionString(iotHubConnectionString); }
public DeviceUpdateForm(RegistryManager manager, int maxDevices, string deviceID) { InitializeComponent(); this.registryManager = manager; this.devicesMaxCount = maxDevices; this.selectedDeviceID = deviceID; updateControls(deviceID); }
//------------------------------------------------------------------------------------------------------------------------ #endregion #region Functions //------------------------------------------------------------------------------------------------------------------------ public static string AddDevice(string devname, string connectionstring) { //create registermanager registryManager = RegistryManager.CreateFromConnectionString(connectionstring); //add device to registry identity -- get devkey var res = AddDeviceAsync(devname).Result; return res; }
public SASTokenForm(RegistryManager registryManager, int maxDevices, string iotHubHostName) { InitializeComponent(); this.registryManager = registryManager; this.devicesMaxCount = maxDevices; this.iotHubHostName = iotHubHostName; initControls(); }
public async Task OpenAsync() { serviceClient = Microsoft.Azure.Devices.ServiceClient.CreateFromConnectionString(serviceConnectionString); await serviceClient.OpenAsync(); registryManager = Microsoft.Azure.Devices.RegistryManager.CreateFromConnectionString(ownerConnectionString); await registryManager.OpenAsync(); }
public IOTHubDeviceExplorer(string iotHubConnenctionString, int devicesCount, string protocolGatewayName) { this.iotHubConnectionString = iotHubConnenctionString; this.maxCountOfDevices = devicesCount; this.protocolGatewayHostName = protocolGatewayName; this.registryManager = RegistryManager.CreateFromConnectionString(iotHubConnectionString); this.serviceClient = ServiceClient.CreateFromConnectionString(iotHubConnectionString); }
public static void Main(string[] args) { Console.WriteLine("Please enter your IoT Hub connection string:"); string connectionString = Console.ReadLine(); Console.WriteLine("Please enter your device Id:"); string deviceId = Console.ReadLine(); registryManager = RegistryManager.CreateFromConnectionString(connectionString); AddDeviceAsync(deviceId).Wait(); Console.ReadLine(); }
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() }; } }
private static void Main(string[] args) { _registryManager = RegistryManager.CreateFromConnectionString(_connectionString); #region Menu bool exit = false; while (!exit) { Console.WriteLine("(A)dd a device."); Console.WriteLine("(L)ist devices."); Console.WriteLine("(R)emove a device."); Console.WriteLine("E(x)it."); Console.Write("Select an option: "); var choice = Console.ReadLine(); Console.WriteLine(); string deviceId; switch (choice) { case "A": case "a": deviceId = PromptForDeviceId(); AddDeviceAsync(deviceId).Wait(); break; case "L": case "l": ViewDevicesAsync().Wait(); break; case "R": case "r": deviceId = PromptForDeviceId(); RemoveDeviceAsync(deviceId).Wait(); break; case "X": case "x": exit = true; break; default: break; } } #endregion }
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 CreateDeviceForm(string iotHubConnectionString, int devicesMaxCount) { InitializeComponent(); this.iotHubConnectionString = iotHubConnectionString; this.registryManager = RegistryManager.CreateFromConnectionString(iotHubConnectionString); this.devicesMaxCount = devicesMaxCount; generateIDCheckBox.Checked = false; generateDeviceID = false; generateKeysCheckBox.Checked = true; generateDeviceKeys = true; autoGenerateDeviceKeys(); }
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() { }); } }
static string connectionString = ""; // Example: HostName=PierceHub.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=SPHff77vetNDgrZahijsTMhGqZd0MllkgG0JyLzfz2E= public static void Main(string[] args) { Console.WriteLine("Device Provisioning Tool"); Console.WriteLine("Creating a unique device identity for device: coffeeMaker"); registryManager = RegistryManager.CreateFromConnectionString(connectionString); AddDeviceAsync().Wait(); Console.WriteLine("Creating a unique Shared Access Signature for your account:"); var signature = new SharedAccessSignatureBuilder() { KeyName = policyName, Key = policyKey, Target = iotHubUrl, TimeToLive = TimeSpan.FromDays(365) }.ToSignature(); Console.WriteLine(signature); Console.ReadLine(); }
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); } }
private static async Task RemoveDevicesAsync() { kRegistryManager = AZD.RegistryManager.CreateFromConnectionString(kIoTHubConnectionString); do { var devicesList = (await kRegistryManager.GetDevicesAsync(kRemoveMaxCount)).ToList(); if (devicesList?.Count == 0) { break; } try { await kRegistryManager.RemoveDevices2Async(devicesList); } catch (Exception ex) { Console.WriteLine(ex.Message); } }while (true); }
static void Main(string[] args) { // initialize the configM microservice client sdk _configM = new ConfigM {ApiUrl = ConfigurationManager.AppSettings["ConfigM"]}; // lookup the manifest for the device microservice var deviceManifest = _configM.GetByName("DeviceM"); // initialize the device registery microservice client SDK _registryM = new DeviceM {ApiUrl = deviceManifest.lineitems[LineitemsKey.AdminAPI]}; // get a list of all devices from the registry _devices = _registryM.GetAll(); // initialize the IoT Hub registration manager _registryManager = RegistryManager.CreateFromConnectionString(ConfigurationManager.AppSettings["IoTHubConnStr"]); // register each device with IoT Hub foreach (var device in _devices.list) { AddDeviceAsync(device).Wait(); } }
public AzureSubscriptionService(string constr = "HostName=iotHub4jbagz366jxyq.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=ijus5i/Z0Pr0EFHFQuNcK4kpqI+34rPmgz+VbTHFZUw=") { this.connectionString = constr; registryManager = RegistryManager.CreateFromConnectionString(connectionString); }
public IotDeviceService(RegistryManager registryManager) { _registryManager = registryManager; }
static async Task<string> selfRegisterAndSetConnString(string DeviceId) { Task<string> deviceConnString = null; try { registryManager = RegistryManager.CreateFromConnectionString(iotHubConnString); Device newDevice = new Device(DeviceId); await registryManager.AddDeviceAsync(newDevice); newDevice = await registryManager.GetDeviceAsync(DeviceId); newDevice.Authentication.SymmetricKey.PrimaryKey = CryptoKeyGenerator.GenerateKey(32); newDevice.Authentication.SymmetricKey.SecondaryKey = CryptoKeyGenerator.GenerateKey(32); newDevice = await registryManager.UpdateDeviceAsync(newDevice); string deviceInfo = String.Format("ID={0}\nPrimaryKey={1}\nSecondaryKey={2}", newDevice.Id, newDevice.Authentication.SymmetricKey.PrimaryKey, newDevice.Authentication.SymmetricKey.SecondaryKey); deviceConnString = Task.FromResult(string.Format("HostName={0};DeviceId={1};SharedAccessKey={2}", iotHubName, newDevice.Id, newDevice.Authentication.SymmetricKey.PrimaryKey)); } catch (Exception ex) { Console.WriteLine("An error occured creating device:{0}", ex.ToString()); } return deviceConnString.Result; }
public IoTDeviceManager(string iotHubConnectionString) { registryManager = RegistryManager.CreateFromConnectionString(iotHubConnectionString); }
public DevicesController() { _manager = RegistryManager.CreateFromConnectionString(ConfigurationManager.ConnectionStrings["IoTHub"].ConnectionString); }
public IotHubService(IAppConfiguration configuration) { _registryManager = AzureDevices.RegistryManager.CreateFromConnectionString(configuration.IotHubConnectionString); }
private static void Main(string[] args) { Console.WriteLine("Enter IoT Hub connection string: "); connectionString = Console.ReadLine(); Console.WriteLine("Enter Service Fabric cluster address where your IoT project is deployed (or blank for local): "); clusterAddress = Console.ReadLine(); registryManager = RegistryManager.CreateFromConnectionString(connectionString); fabricClient = String.IsNullOrEmpty(clusterAddress) ? new FabricClient() : new FabricClient(clusterAddress); Task.Run( async () => { while (true) { try { devices = await registryManager.GetDevicesAsync(Int32.MaxValue); tenants = (await fabricClient.QueryManager.GetApplicationListAsync()) .Where(x => x.ApplicationTypeName == Names.TenantApplicationTypeName) .Select(x => x.ApplicationName.ToString().Replace(Names.TenantApplicationNamePrefix + "/", "")); Console.WriteLine(); Console.WriteLine("Devices IDs: "); foreach (Device device in devices) { Console.WriteLine(device.Id); } Console.WriteLine(); Console.WriteLine("Tenants: "); foreach (string tenant in tenants) { Console.WriteLine(tenant); } Console.WriteLine(); Console.WriteLine("Commands:"); Console.WriteLine("1: Register a device"); Console.WriteLine("2: Register random devices"); Console.WriteLine("3: Send data from a device"); Console.WriteLine("4: Send data from all devices"); Console.WriteLine("5: Exit"); string command = Console.ReadLine(); switch (command) { case "1": Console.WriteLine("Make up a device ID: "); string deviceId = Console.ReadLine(); await AddDeviceAsync(deviceId); break; case "2": Console.WriteLine("How many devices? "); int num = Int32.Parse(Console.ReadLine()); await AddRandomDevicesAsync(num); break; case "3": Console.WriteLine("Tenant: "); string tenant = Console.ReadLine(); Console.WriteLine("Device id: "); string deviceKey = Console.ReadLine(); await SendDeviceToCloudMessagesAsync(deviceKey, tenant); break; case "4": Console.WriteLine("Tenant: "); string tenantName = Console.ReadLine(); Console.WriteLine("Iterations: "); int iterations = Int32.Parse(Console.ReadLine()); await SendAllDevices(tenantName, iterations); break; case "5": return; default: break; } } catch (Exception ex) { Console.WriteLine("Oops, {0}", ex.Message); } } }) .GetAwaiter().GetResult(); }
public AzureSubscriptionService() { this.connectionStringForPortal = ConfigurationManager.AppSettings["AzureSubscriptionStr"]; this.connectionStringForStorage = ConfigurationManager.AppSettings["AzureStorageStr"]; registryManager = RegistryManager.CreateFromConnectionString(connectionStringForPortal); }
private void EnsureRegistryManagerInitialized() { if (registryManager == null) { lock (SyncRoot) { if (registryManager == null) { var settings = Configuration.GetMobileAppSettingsProvider().GetMobileAppSettings(); string iotHubconnectionString = settings["IoTHubConnectionString"]; //ConfigurationManager.AppSettings["IoTHubConnectionString"]; registryManager = RegistryManager.CreateFromConnectionString(iotHubconnectionString); } } } }