public async Task<SIoT.Device> AddDeviceWithTagsAsync(string deviceId, string jsonTwin) { var twin = new Twin(deviceId); twin.Tags = new TwinCollection(jsonTwin); var result = await _registryManager.AddDeviceWithTwinAsync(new Device(deviceId), twin).ConfigureAwait(false); if (result != null && result.IsSuccessful) return await GetDeviceAsync(deviceId).ConfigureAwait(false); throw new Exception("An error has occurred during the device creation or the twin updates."); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req, ILogger log) { string deviceName = req.Query["deviceName"]; string deviceNumber = req.Query["deviceNumber"]; log.LogInformation($"DeviceCreation Request for {deviceName} #{deviceNumber}"); var device = new Device(deviceName); var creationTwin = new Twin { Tags = new TwinCollection(@"{ deviceName: '" + deviceName + "' }"), Properties = new TwinProperties() { Desired = new TwinCollection(@"{ deviceNumber: '" + deviceNumber + "', led: {r: 100, g: 50, b: 0 }}") } }; var registryOperation = await registryManager.AddDeviceWithTwinAsync(device, creationTwin); var createdDevice = await registryManager.GetDeviceAsync(deviceName); return((ActionResult) new OkObjectResult($"HostName=iotworkshopfr.azure-devices.net;DeviceId={deviceName};SharedAccessKey={createdDevice.Authentication.SymmetricKey.PrimaryKey}")); }
public async Task AddDeviceWithTwinWithDeviceCapabilities() { string deviceId = "some-device-" + Guid.NewGuid().ToString(); using (RegistryManager registryManager = RegistryManager.CreateFromConnectionString(Configuration.IoTHub.ConnectionString)) { var twin = new Twin { Tags = new TwinCollection(@"{ companyId: 1234 }"), }; var iotEdgeDevice = new Device(deviceId) { Capabilities = new DeviceCapabilities { IotEdge = true } }; await registryManager.AddDeviceWithTwinAsync(iotEdgeDevice, twin).ConfigureAwait(false); Device actual = await registryManager.GetDeviceAsync(deviceId).ConfigureAwait(false); await registryManager.RemoveDeviceAsync(deviceId).ConfigureAwait(false); Assert.IsTrue(actual.Capabilities != null && actual.Capabilities.IotEdge); } }
public async Task <SIoT.Device> AddDeviceWithTagsAsync(string deviceId, string jsonTwin) { Twin twin = new Twin(deviceId); twin.Tags = new TwinCollection(jsonTwin); BulkRegistryOperationResult result = await _registryManager.AddDeviceWithTwinAsync(new Device(deviceId), twin); if (result != null && result.IsSuccessful) { return(await GetDeviceAsync(deviceId)); } else { throw new Exception(JsonConvert.SerializeObject(result.Errors, Formatting.Indented)); } }
private static async Task <bool> HandleDevice(ExportImportDevice device, RegistryManager registryManager, TraceWriter log) { if (device.ImportMode == ImportMode.Delete) { try { await registryManager.RemoveDeviceAsync(device.Id); log.Info($"Device {device.Id} deleted"); return(true); } catch (Exception ex) { log.Error($"Failed to delete device {device.Id}", ex); } } else { // For now we try to create the device without checking if it already exists // add additional logic to update an existing device try { var newDevice = new Device(device.Id) { Status = DeviceStatus.Enabled }; var twin = new Twin { Tags = device.Tags }; if (device.Properties != null) { twin.Properties = new TwinProperties(); if (device.Properties.DesiredProperties != null) { twin.Properties.Desired = new TwinCollection(device.Properties.DesiredProperties.ToJson()); } if (device.Properties.ReportedProperties != null) { twin.Properties.Reported = new TwinCollection(device.Properties.ReportedProperties.ToJson()); } } await registryManager.AddDeviceWithTwinAsync(newDevice, twin); log.Info($"Device {device.Id} created"); return(true); } catch (DeviceAlreadyExistsException) { log.Info($"Device {device.Id} already exists, skipping"); } } return(true); }