// Map API model to service model
        public DeviceModel ToServiceModel(string id = "")
        {
            this.Id = id;
            Enum.TryParse(this.Type, true, out DeviceModel.DeviceModelType type);

            var result = new DeviceModel
            {
                ETag                 = this.ETag,
                Id                   = this.Id,
                Version              = this.Version,
                Name                 = this.Name,
                Description          = this.Description,
                Type                 = type,
                Protocol             = (IoTHubProtocol)Enum.Parse(typeof(IoTHubProtocol), this.Protocol, true),
                Simulation           = DeviceModelSimulation.ToServiceModel(this.Simulation),
                Properties           = new Dictionary <string, object>(this.Properties),
                Telemetry            = this.Telemetry.Select(DeviceModelTelemetry.ToServiceModel).ToList(),
                CloudToDeviceMethods = null
            };

            // Map the list of CloudToDeviceMethods
            if (this.CloudToDeviceMethods != null && this.CloudToDeviceMethods.Count > 0)
            {
                result.CloudToDeviceMethods = new Dictionary <string, Script>();
                foreach (KeyValuePair <string, DeviceModelSimulationScript> method in this.CloudToDeviceMethods)
                {
                    var fieldValue = method.Value.ToServiceModel();
                    result.CloudToDeviceMethods.Add(method.Key, fieldValue);
                }
            }

            return(result);
        }
        // Map service model to API model
        public static DeviceModelApiModel FromServiceModel(DeviceModel value)
        {
            if (value == null)
            {
                return(null);
            }

            var result = new DeviceModelApiModel
            {
                Id          = value.Id,
                Version     = value.Version,
                Name        = value.Name,
                Description = value.Description,
                Protocol    = value.Protocol.ToString(),
                Simulation  = DeviceModelSimulation.FromServiceModel(value.Simulation)
            };

            foreach (var property in value.Properties)
            {
                result.Properties.Add(property.Key, property.Value);
            }

            foreach (var message in value.Telemetry)
            {
                result.Telemetry.Add(DeviceModelTelemetry.FromServiceModel(message));
            }

            foreach (var method in value.CloudToDeviceMethods)
            {
                result.CloudToDeviceMethods.Add(method.Key, DeviceModelSimulationScript.FromServiceModel(method.Value));
            }

            return(result);
        }
Exemple #3
0
        // Map API model to service model
        public static StateSimulation ToServiceModel(DeviceModelSimulation model)
        {
            if (model == null)
            {
                return(null);
            }

            return(new StateSimulation
            {
                InitialState = model.InitialState,
                Interval = TimeSpan.Parse(model.Interval),
                Scripts = model.Scripts.Select(script => script.ToServiceModel()).Where(x => x != null).ToList()
            });
        }