/// <summary> /// We only support update twin /// </summary> /// <param name="device"></param> /// <param name="devicePropertyDelegate"></param> /// <returns></returns> public async Task <DeviceServiceModel> CreateOrUpdateAsync(DeviceServiceModel device, DevicePropertyDelegate devicePropertyDelegate) { // validate device module var azureDevice = await this.registry.GetDeviceAsync(device.Id); if (azureDevice == null) { azureDevice = await this.registry.AddDeviceAsync(device.ToAzureModel()); } Twin azureTwin; if (device.Twin == null) { azureTwin = await this.registry.GetTwinAsync(device.Id); } else { azureTwin = await this.registry.UpdateTwinAsync(device.Id, device.Twin.ToAzureModel(), device.Twin.ETag); // Update the deviceGroupFilter cache, no need to wait var model = new DevicePropertyServiceModel(); var tagRoot = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(device.Twin.Tags)) as JToken; if (tagRoot != null) { model.Tags = new HashSet <string>(tagRoot.GetAllLeavesPath()); } var reportedRoot = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(device.Twin.ReportedProperties)) as JToken; if (reportedRoot != null) { model.Reported = new HashSet <string>(reportedRoot.GetAllLeavesPath()); } var unused = devicePropertyDelegate(model); } return(new DeviceServiceModel(azureDevice, azureTwin, this.ioTHubHostName)); }
public async Task <DeviceServiceModel> UpdateAsync(DeviceServiceModel device, DevicePropertyDelegate devicePropertyDelegate) { // validate device module var azureDevice = await this.tenantConnectionHelper.GetRegistry().GetDeviceAsync(device.Id); if (azureDevice == null) { throw new ResourceNotFoundException($"Device {device.Id} could not be found on this tenant's IoT Hub. You must create the device first before calling the update method."); } Twin azureTwin; if (device.Twin == null) { azureTwin = await this.tenantConnectionHelper.GetRegistry().GetTwinAsync(device.Id); } else { azureTwin = await this.tenantConnectionHelper.GetRegistry().UpdateTwinAsync(device.Id, device.Twin.ToAzureModel(), device.Twin.ETag); // Update the deviceGroupFilter cache, no need to wait var model = new DevicePropertyServiceModel(); if (JsonConvert.DeserializeObject(JsonConvert.SerializeObject(device.Twin.Tags)) is JToken tagRoot) { model.Tags = new HashSet <string>(tagRoot.GetAllLeavesPath()); } if (JsonConvert.DeserializeObject(JsonConvert.SerializeObject(device.Twin.ReportedProperties)) is JToken reportedRoot) { model.Reported = new HashSet <string>(reportedRoot.GetAllLeavesPath()); } _ = devicePropertyDelegate(model); } await this.asaManager.BeginDeviceGroupsConversionAsync(); return(new DeviceServiceModel(azureDevice, azureTwin, this.tenantConnectionHelper.GetIotHubName())); }
public async Task <DeviceRegistryApiModel> PutAsync(string id, [FromBody] DeviceRegistryApiModel device) { DevicePropertyDelegate updateListDelegate = new DevicePropertyDelegate(this.deviceProperties.UpdateListAsync); return(new DeviceRegistryApiModel(await this.devices.CreateOrUpdateAsync(device.ToServiceModel(), updateListDelegate))); }