// Map API model to service model, keeping the original fields when needed public Simulation ToServiceModel( Simulation existingSimulation, IRateLimitingConfig defaultRateLimits, string id = "") { var now = DateTimeOffset.UtcNow; // ID can be empty, e.g. with POST requests this.Id = id; // Use the existing simulation fields if available, so that read-only values are not lost // e.g. the state of partitioning, device creation, etc. var result = new Simulation(); if (existingSimulation != null) { result = existingSimulation; } result.ETag = this.ETag; result.Id = this.Id; result.Name = this.Name; result.Description = this.Description; result.StartTime = DateHelper.ParseDateExpression(this.StartTime, now); result.EndTime = DateHelper.ParseDateExpression(this.EndTime, now); result.DeviceModels = this.DeviceModels?.Select(x => x.ToServiceModel()).ToList(); result.RateLimits = this.RateLimits.ToServiceModel(defaultRateLimits); result.ReplayFileId = this.ReplayFileId; result.ReplayFileRunIndefinitely = this.ReplayFileIndefinitely; // Overwrite the value only if the request included the field, i.e. don't // enable/disable the simulation if the user didn't explicitly ask to. if (this.Enabled.HasValue) { result.Enabled = this.Enabled.Value; } // Overwrite the value only if the request included the field, i.e. don't // delete all devices if the user didn't explicitly ask to. if (this.DeleteDevicesWhenSimulationEnds.HasValue) { result.DeleteDevicesWhenSimulationEnds = this.DeleteDevicesWhenSimulationEnds.Value; } foreach (var hub in this.IotHubs) { var connString = SimulationIotHub.ToServiceModel(hub); if (!result.IotHubConnectionStrings.Contains(connString)) { result.IotHubConnectionStrings.Add(connString); } } return(result); }
// Map API model to service model public Simulation ToServiceModel(string id = "") { this.Id = id; var now = DateTimeOffset.UtcNow; var result = new Simulation { ETag = this.ETag, Id = this.Id, // When unspecified, a simulation is enabled Enabled = this.Enabled ?? true, StartTime = DateHelper.ParseDateExpression(this.StartTime, now), EndTime = DateHelper.ParseDateExpression(this.EndTime, now), IotHubConnectionString = SimulationIotHub.ToServiceModel(this.IotHub), DeviceModels = this.DeviceModels?.Select(x => x.ToServiceModel()).ToList() }; return(result); }