private async Task <bool> UpdateSecret(Sensor sensor, SensorUpdate update) { ApiKey key = null; if (!string.IsNullOrEmpty(update.Secret)) { key = await this.m_keys.GetAsync(update.Secret).ConfigureAwait(false); } if (key != null) { return(false); /* Key already exists */ } var old = await this.m_keys.GetAsync(sensor.Secret).ConfigureAwait(false); var oldSecret = sensor.Secret; sensor.Secret = string.IsNullOrEmpty(update.Secret) ? NextStringWithSymbols(this.m_rng, SensorSecretLength) : update.Secret; await Task.WhenAll( this.m_mqtt.PublishCommandAsync(CommandType.FlushSensor, sensor.InternalId.ToString()), this.m_mqtt.PublishCommandAsync(CommandType.FlushKey, oldSecret) ).ConfigureAwait(false); var cache = this.m_cache.RemoveAsync(this.GenerateCacheKey(null, 0, 10, true)); await Task.WhenAll( this.m_keys.UpdateAsync(old.Key, sensor.Secret), this.m_sensors.UpdateSecretAsync(sensor.InternalId, sensor.Secret), cache ).ConfigureAwait(false); return(true); }
public async Task CreateAsync(Sensor sensor, CancellationToken ct = default) { var now = DateTime.UtcNow; sensor.CreatedAt = now; sensor.UpdatedAt = now; sensor.InternalId = ObjectId.GenerateNewId(); await this.m_sensors.InsertOneAsync(sensor, default, ct).ConfigureAwait(false);
private static void ApplyUpdate(Sensor sensor, SensorUpdate update) { if (!string.IsNullOrEmpty(update.Name)) { sensor.Name = update.Name; } if (!string.IsNullOrEmpty(update.Description)) { sensor.Description = update.Description; } if (update.StorageEnabled.HasValue) { sensor.StorageEnabled = update.StorageEnabled.Value; } }
public async Task <IActionResult> Post([FromBody] Sensor sensor) { sensor.Owner = this.m_currentUserId; if (string.IsNullOrWhiteSpace(sensor.Secret)) { sensor.Secret = NextStringWithSymbols(this.m_rng, SensorSecretLength); } var cache = this.m_cache.RemoveAsync(this.GenerateCacheKey(null, 0, 10, true)); await this.m_keys.CreateSensorKeyAsync(sensor).ConfigureAwait(false); await this.m_sensors.CreateAsync(sensor).ConfigureAwait(false); await Task.WhenAll( this.m_mqtt.PublishCommandAsync(CommandType.AddSensor, sensor.InternalId.ToString()), this.m_mqtt.PublishCommandAsync(CommandType.AddKey, sensor.Secret) ).ConfigureAwait(false); await cache.ConfigureAwait(false); return(this.CreatedAtAction(nameof(this.Get), new { Id = sensor.InternalId }, new Response <Sensor>(sensor))); }