Beispiel #1
0
        /// <summary>
        /// Decode tags and property into registration object
        /// </summary>
        /// <param name="twin"></param>
        /// <param name="properties"></param>
        /// <returns></returns>
        public static GatewayRegistration ToGatewayRegistration(this DeviceTwinModel twin,
                                                                Dictionary <string, VariantValue> properties)
        {
            if (twin == null)
            {
                return(null);
            }

            var tags = twin.Tags ?? new Dictionary <string, VariantValue>();

            var registration = new GatewayRegistration {
                // Device

                DeviceId = twin.Id,
                Etag     = twin.Etag,

                // Connected
                Connected = twin.IsConnected() ?? false,

                // Tags

                IsDisabled =
                    tags.GetValueOrDefault <bool>(nameof(GatewayRegistration.IsDisabled), null),
                NotSeenSince =
                    tags.GetValueOrDefault <DateTime>(nameof(GatewayRegistration.NotSeenSince), null),
                Type =
                    tags.GetValueOrDefault <string>(TwinProperty.Type, null),
                SiteId =
                    tags.GetValueOrDefault <string>(TwinProperty.SiteId, null),

                // Properties
            };

            return(registration);
        }
Beispiel #2
0
        /// <summary>
        /// Create patch twin model to upload
        /// </summary>
        /// <param name="existing"></param>
        /// <param name="update"></param>
        public static DeviceTwinModel Patch(this GatewayRegistration existing,
                                            GatewayRegistration update)
        {
            var twin = new DeviceTwinModel {
                Etag       = existing?.Etag,
                Tags       = new Dictionary <string, VariantValue>(),
                Properties = new TwinPropertiesModel {
                    Desired = new Dictionary <string, VariantValue>()
                }
            };

            // Tags

            if (update?.IsDisabled != null && update.IsDisabled != existing?.IsDisabled)
            {
                twin.Tags.Add(nameof(GatewayRegistration.IsDisabled), (update?.IsDisabled ?? false) ?
                              true : (bool?)null);
                twin.Tags.Add(nameof(GatewayRegistration.NotSeenSince), (update?.IsDisabled ?? false) ?
                              DateTime.UtcNow : (DateTime?)null);
            }

            if (update?.SiteId != existing?.SiteId)
            {
                twin.Tags.Add(TwinProperty.SiteId, update?.SiteId);
            }

            twin.Tags.Add(nameof(GatewayRegistration.DeviceType), update?.DeviceType);
            twin.Id = update?.DeviceId ?? existing?.DeviceId;
            return(twin);
        }
 /// <summary>
 /// Convert to service model
 /// </summary>
 /// <param name="registration"></param>
 /// <returns></returns>
 public static GatewayModel ToServiceModel(this GatewayRegistration registration)
 {
     return(new GatewayModel {
         Id = registration.DeviceId,
         SiteId = registration.SiteId,
         Connected = registration.IsConnected() ? true : (bool?)null
     });
 }
Beispiel #4
0
 /// <summary>
 /// Create device twin
 /// </summary>
 /// <param name="registration"></param>
 /// <returns></returns>
 public static DeviceTwinModel ToDeviceTwin(this GatewayRegistration registration)
 {
     return(Patch(null, registration));
 }