예제 #1
0
        // Map service model to API model
        public static SimulationApiModel FromServiceModel(
            Simulation value)
        {
            if (value == null)
            {
                return(null);
            }

            var result = new SimulationApiModel
            {
                ETag                            = value.ETag,
                Id                              = value.Id,
                Name                            = value.Name,
                Description                     = value.Description,
                Enabled                         = value.Enabled,
                Running                         = value.ShouldBeRunning,
                ActiveNow                       = value.IsActiveNow,
                DeleteDevicesOnce               = value.DeleteDevicesOnce,
                DevicesDeletionComplete         = value.DevicesDeletionComplete,
                DeleteDevicesWhenSimulationEnds = value.DeleteDevicesWhenSimulationEnds,
                IotHubs                         = new List <SimulationIotHub>(),
                ReplayFileId                    = value.ReplayFileId,
                ReplayFileIndefinitely          = value.ReplayFileRunIndefinitely
            };

            foreach (var iotHubConnectionString in value.IotHubConnectionStrings)
            {
                var iotHub = new SimulationIotHub {
                    ConnectionString = iotHubConnectionString
                };
                result.IotHubs.Add(iotHub);
            }

            // Ignore the date if the simulation doesn't have a start time
            if (value.StartTime.HasValue && !value.StartTime.Value.Equals(DateTimeOffset.MinValue))
            {
                result.StartTime = value.StartTime?.ToString(DATE_FORMAT);
            }

            // Ignore the date if the simulation doesn't have an end time
            if (value.EndTime.HasValue && !value.EndTime.Value.Equals(DateTimeOffset.MaxValue))
            {
                result.EndTime = value.EndTime?.ToString(DATE_FORMAT);
            }

            // Ignore the date if the simulation doesn't have an end time
            if (value.StoppedTime.HasValue && !value.StoppedTime.Value.Equals(DateTimeOffset.MaxValue))
            {
                result.StoppedTime = value.StoppedTime?.ToString(DATE_FORMAT);
            }

            result.DeviceModels = SimulationDeviceModelRef.FromServiceModel(value.DeviceModels);
            result.Statistics   = GetSimulationStatistics(value);
            result.RateLimits   = SimulationRateLimits.FromServiceModel(value.RateLimits);

            result.created  = value.Created;
            result.modified = value.Modified;

            return(result);
        }
예제 #2
0
        // 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);
        }
예제 #3
0
        // 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);
        }
        // Map service model to API model
        public static SimulationApiModel FromServiceModel(
            Simulation value,
            IServicesConfig servicesConfig,
            IDeploymentConfig deploymentConfig,
            IIotHubConnectionStringManager connectionStringManager,
            ISimulationRunner simulationRunner,
            IRateLimiting rateReporter)
        {
            if (value == null)
            {
                return(null);
            }

            var result = new SimulationApiModel
            {
                ETag        = value.ETag,
                Id          = value.Id,
                Name        = value.Name,
                Description = value.Description,
                Enabled     = value.Enabled,
                Running     = value.ShouldBeRunning,
                StartTime   = value.StartTime.ToString(),
                EndTime     = value.EndTime.ToString(),
                StoppedTime = value.StoppedTime.ToString(),
                IotHubs     = new List <SimulationIotHub>()
            };

            foreach (var iotHubConnectionString in value.IotHubConnectionStrings)
            {
                var iotHub = new SimulationIotHub {
                    ConnectionString = iotHubConnectionString
                };
                result.IotHubs.Add(iotHub);
            }

            // Ignore the date if the simulation doesn't have a start time
            if (value.StartTime.HasValue && !value.StartTime.Value.Equals(DateTimeOffset.MinValue))
            {
                result.StartTime = value.StartTime?.ToString(DATE_FORMAT);
            }

            // Ignore the date if the simulation doesn't have an end time
            if (value.EndTime.HasValue && !value.EndTime.Value.Equals(DateTimeOffset.MaxValue))
            {
                result.EndTime = value.EndTime?.ToString(DATE_FORMAT);
            }

            // Ignore the date if the simulation doesn't have an end time
            if (value.StoppedTime.HasValue && !value.StoppedTime.Value.Equals(DateTimeOffset.MaxValue))
            {
                result.StoppedTime = value.StoppedTime?.ToString(DATE_FORMAT);
            }

            result.DeviceModels = SimulationDeviceModelRef.FromServiceModel(value.DeviceModels);
            result.Statistics   = SimulationStatistics.FromServiceModel(value.Statistics);
            result.created      = value.Created;
            result.modified     = value.Modified;

            result.AppendHubPropertiesAndStatistics(servicesConfig, deploymentConfig, connectionStringManager, simulationRunner, rateReporter);

            return(result);
        }
 // Map API model to service model
 public static string ToServiceModel(SimulationIotHub iotHub)
 {
     return(iotHub != null && !IsDefaultHub(iotHub.ConnectionString)
         ? iotHub.ConnectionString
         : ServicesConfig.USE_DEFAULT_IOTHUB);
 }