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

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

            var registration = new EndpointRegistration {
                // Device

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

                // Tags
                IsDisabled =
                    tags.GetValueOrDefault(nameof(EndpointRegistration.IsDisabled), twin.IsDisabled()),
                NotSeenSince =
                    tags.GetValueOrDefault <DateTime>(nameof(EndpointRegistration.NotSeenSince), null),

                Thumbprint =
                    tags.GetValueOrDefault <string>(nameof(EndpointRegistration.Thumbprint), null),
                SupervisorId =
                    tags.GetValueOrDefault <string>(nameof(EndpointRegistration.SupervisorId), null),
                DiscovererId =
                    tags.GetValueOrDefault <string>(nameof(EndpointRegistration.DiscovererId),
                                                    tags.GetValueOrDefault <string>(nameof(EndpointRegistration.SupervisorId), null)),
                Activated =
                    tags.GetValueOrDefault <bool>(nameof(EndpointRegistration.Activated), null),
                ApplicationId =
                    tags.GetValueOrDefault <string>(nameof(EndpointRegistration.ApplicationId), null),
                SecurityLevel =
                    tags.GetValueOrDefault <int>(nameof(EndpointRegistration.SecurityLevel), null),
                AuthenticationMethods =
                    tags.GetValueOrDefault <Dictionary <string, JToken> >(nameof(EndpointRegistration.AuthenticationMethods), null),
                EndpointRegistrationUrl =
                    tags.GetValueOrDefault <string>(nameof(EndpointRegistration.EndpointRegistrationUrl), null),

                // Properties

                Connected = connected ??
                            properties.GetValueOrDefault(TwinProperty.Connected, false),
                Type =
                    properties.GetValueOrDefault <string>(TwinProperty.Type, null),
                State =
                    properties.GetValueOrDefault(nameof(EndpointRegistration.State), EndpointConnectivityState.Connecting),
                SiteId =
                    properties.GetValueOrDefault(TwinProperty.SiteId,
                                                 tags.GetValueOrDefault <string>(nameof(EndpointRegistration.SiteId), null)),
                EndpointUrl =
                    properties.GetValueOrDefault <string>(nameof(EndpointRegistration.EndpointUrl), null),
                AlternativeUrls =
                    properties.GetValueOrDefault <Dictionary <string, string> >(nameof(EndpointRegistration.AlternativeUrls), null),
                SecurityMode =
                    properties.GetValueOrDefault <SecurityMode>(nameof(EndpointRegistration.SecurityMode), null),
                SecurityPolicy =
                    properties.GetValueOrDefault <string>(nameof(EndpointRegistration.SecurityPolicy), null),
                Certificate =
                    properties.GetValueOrDefault <Dictionary <string, string> >(nameof(EndpointRegistration.Certificate), null),
            };

            return(registration);
        }
        /// <summary>
        /// Make sure to get the registration information from the right place.
        /// Reported (truth) properties take precedence over desired.
        /// </summary>
        /// <param name="twin"></param>
        /// <returns></returns>
        public static ApplicationRegistration ToApplicationRegistration(this DeviceTwinModel twin)
        {
            if (twin == null)
            {
                return(null);
            }
            var tags         = twin.Tags ?? new Dictionary <string, VariantValue>();
            var registration = new ApplicationRegistration {
                // Device

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

                // Tags

                IsDisabled =
                    tags.GetValueOrDefault(nameof(ApplicationRegistration.IsDisabled), twin.IsDisabled()),
                NotSeenSince =
                    tags.GetValueOrDefault <DateTime>(nameof(ApplicationRegistration.NotSeenSince), null),
                SiteId =
                    tags.GetValueOrDefault <string>(nameof(ApplicationRegistration.SiteId), null),

                ApplicationName =
                    tags.GetValueOrDefault <string>(nameof(ApplicationRegistration.ApplicationName), null),
                Locale =
                    tags.GetValueOrDefault <string>(nameof(ApplicationRegistration.Locale), null),
                LocalizedNames =
                    tags.GetValueOrDefault <Dictionary <string, string> >(nameof(ApplicationRegistration.LocalizedNames), null),
                ApplicationUri =
                    tags.GetValueOrDefault <string>(nameof(ApplicationRegistration.ApplicationUri), null),
                RecordId =
                    tags.GetValueOrDefault <uint>(nameof(ApplicationRegistration.RecordId), null),
                ProductUri =
                    tags.GetValueOrDefault <string>(nameof(ApplicationRegistration.ProductUri), null),
                DiscovererId =
                    tags.GetValueOrDefault <string>(nameof(ApplicationRegistration.DiscovererId),
                                                    tags.GetValueOrDefault <string>("SupervisorId", null)),
                DiscoveryProfileUri =
                    tags.GetValueOrDefault <string>(nameof(ApplicationRegistration.DiscoveryProfileUri), null),
                GatewayServerUri =
                    tags.GetValueOrDefault <string>(nameof(ApplicationRegistration.GatewayServerUri), null),
                ApplicationType =
                    tags.GetValueOrDefault <ApplicationType>(nameof(ApplicationType), null),
                Capabilities =
                    tags.GetValueOrDefault <Dictionary <string, bool> >(nameof(ApplicationRegistration.Capabilities), null),
                HostAddresses =
                    tags.GetValueOrDefault <Dictionary <string, string> >(nameof(ApplicationRegistration.HostAddresses), null),
                DiscoveryUrls =
                    tags.GetValueOrDefault <Dictionary <string, string> >(nameof(ApplicationRegistration.DiscoveryUrls), null),

                CreateTime =
                    tags.GetValueOrDefault <DateTime>(nameof(ApplicationRegistration.CreateTime), null),
                CreateAuthorityId =
                    tags.GetValueOrDefault <string>(nameof(ApplicationRegistration.CreateAuthorityId), null),
                UpdateTime =
                    tags.GetValueOrDefault <DateTime>(nameof(ApplicationRegistration.UpdateTime), null),
                UpdateAuthorityId =
                    tags.GetValueOrDefault <string>(nameof(ApplicationRegistration.UpdateAuthorityId), null),
            };

            return(registration);
        }