public async Task <bool> UpdateDevicePropertiesAsync(string eventData, string tenant) { bool updateStatus = false; DevicePropertyServiceModel existingDevProperties = null; ValueServiceModel deviceCacheValue = null; CosmosOperations docClient = null; try { this.cosmosDbcoll = $"{CollectionKey}-{tenant}"; this.cosmosDb = Environment.GetEnvironmentVariable("DevicePropertiesCacheDatabaseId", EnvironmentVariableTarget.Process); this.whitelist = Environment.GetEnvironmentVariable("WhiteList", EnvironmentVariableTarget.Process); this.cosmosDbRus = Convert.ToInt32(Environment.GetEnvironmentVariable("CosmosDBRus", EnvironmentVariableTarget.Process)); this.cosmosConnectionString = Environment.GetEnvironmentVariable("CosmosDbConnectionString", EnvironmentVariableTarget.Process); docClient = await CosmosOperations.GetClientAsync(); updateStatus = await docClient.CreateCollectionIfNotExistsAsync(this.cosmosDb, this.cosmosDbcoll, this.cosmosDbRus, CosmosOperation.DeviceCache); // CosmosHelper cosmosHelper = new CosmosHelper(cosmosConnectionString, cosmosDbcoll, cosmosDb, tenant, cosmosDbRus, CosmosOperation.devicecache); try { deviceCacheValue = await docClient.GetDocumentAsync(this.GenerateCollectionLink(this.cosmosDb, this.cosmosDbcoll), CollectionId, Key); existingDevProperties = JsonConvert.DeserializeObject <DevicePropertyServiceModel>(deviceCacheValue.Data); } catch (ResourceNotFoundException) { // Do nothing } catch (Exception exception) { throw new ApplicationException("Get Device Properties failed", exception); } DeviceTwinNameOperations devTwinOps = new DeviceTwinNameOperations(); DeviceTwinName deviceTwinName = devTwinOps.GetValidNames(this.whitelist, existingDevProperties, eventData); if (deviceTwinName != null) { DevicePropertyServiceModel devicePropertyModel = new DevicePropertyServiceModel { Tags = deviceTwinName.Tags, Reported = deviceTwinName.ReportedProperties, }; ValueServiceModel valueSvcModel = new ValueServiceModel(new KeyValueDocument(CollectionId, Key, JsonConvert.SerializeObject(devicePropertyModel))); if (devicePropertyModel != null && existingDevProperties == null) { // Create the document if (deviceCacheValue == null) { await docClient.SaveDocumentAsync(CollectionId, Key, valueSvcModel, this.GenerateCollectionLink(this.cosmosDb, this.cosmosDbcoll)); updateStatus = true; } } else { // update only if the Tags or the reported properties are different from ones stored in cosmos if (existingDevProperties != null && (!existingDevProperties.Tags.SetEquals(devicePropertyModel.Tags) || !existingDevProperties.Reported.SetEquals(devicePropertyModel.Reported))) { await docClient.SaveDocumentAsync(CollectionId, Key, valueSvcModel, this.GenerateCollectionLink(this.cosmosDb, this.cosmosDbcoll)); updateStatus = true; } } } else { updateStatus = true; } } catch (Exception exception) { throw new ApplicationException($"Update Device Properties failed {exception}, tenant: {tenant}"); } return(updateStatus); }
public async Task <ValueServiceModel> SaveDocumentAsync(string collectionId, string key, ValueServiceModel input, string collectionLink) { try { var response = await this.client.UpsertDocumentAsync( collectionLink, new KeyValueDocument(collectionId, key, input.Data), IfMatch(input.ETag)); return(new ValueServiceModel(response)); } catch (Exception ex) { throw new ApplicationException("Update failed while saving the resource", ex); } }