/// <summary>
 /// Returns true if this registration matches the endpoint
 /// model provided.
 /// </summary>
 /// <param name="endpoint"></param>
 /// <returns></returns>
 public bool Matches(EndpointModel endpoint)
 {
     return(endpoint != null &&
            EndpointUrl == endpoint.Url &&
            AlternativeUrls.DecodeAsList().ToHashSetSafe().SetEqualsSafe(
                endpoint.AlternativeUrls) &&
            CredentialType == (endpoint.User?.Type ?? Models.CredentialType.None) &&
            JToken.DeepEquals(Credential, endpoint.User?.Value) &&
            SecurityMode == (endpoint.SecurityMode ?? Models.SecurityMode.Best) &&
            SecurityPolicy == endpoint.SecurityPolicy &&
            endpoint.ClientCertificate.SequenceEqualsSafe(
                ClientCertificate.DecodeAsByteArray()) &&
            endpoint.ServerThumbprint.SequenceEqualsSafe(
                ServerThumbprint.DecodeAsByteArray()));
 }
 /// <summary>
 /// Flag endpoint as synchronized - i.e. it matches the other.
 /// </summary>
 /// <param name="other"></param>
 internal void MarkAsInSyncWith(EndpointRegistration other)
 {
     _isInSync =
         other != null &&
         EndpointUrl == other.EndpointUrl &&
         AlternativeUrls.DecodeAsList().ToHashSetSafe().SetEqualsSafe(
             other.AlternativeUrls.DecodeAsList()) &&
         CredentialType == other.CredentialType &&
         JToken.DeepEquals(Credential, other.Credential) &&
         SecurityPolicy == other.SecurityPolicy &&
         SecurityMode == other.SecurityMode &&
         ClientCertificate.DecodeAsByteArray().SequenceEqualsSafe(
             other.ClientCertificate.DecodeAsByteArray()) &&
         ServerThumbprint.DecodeAsByteArray().SequenceEqualsSafe(
             other.ServerThumbprint.DecodeAsByteArray());
 }
 /// <summary>
 /// Convert to service model
 /// </summary>
 /// <returns></returns>
 public EndpointInfoModel ToServiceModel()
 {
     return(new EndpointInfoModel {
         ApplicationId = ApplicationId,
         Registration = new EndpointRegistrationModel {
             Id = DeviceId,
             SiteId = string.IsNullOrEmpty(SiteId) ?
                      null : SiteId,
             SupervisorId = string.IsNullOrEmpty(SupervisorId) ?
                            null : SupervisorId,
             Certificate = Certificate.DecodeAsByteArray(),
             AuthenticationMethods = AuthenticationMethods?.DecodeAsList(j =>
                                                                         j.ToObject <AuthenticationMethodModel>()),
             SecurityLevel = SecurityLevel,
             EndpointUrl = string.IsNullOrEmpty(EndpointRegistrationUrl) ?
                           (string.IsNullOrEmpty(EndpointUrl) ?
                            EndpointUrlLC : EndpointUrl) : EndpointRegistrationUrl,
             Endpoint = new EndpointModel {
                 Url = string.IsNullOrEmpty(EndpointUrl) ?
                       EndpointUrlLC : EndpointUrl,
                 AlternativeUrls = AlternativeUrls?.DecodeAsList().ToHashSetSafe(),
                 User = CredentialType == null ? null :
                        new CredentialModel {
                     Value = Credential,
                     Type = CredentialType == Models.CredentialType.None ?
                            null : CredentialType
                 },
                 SecurityMode = SecurityMode == Models.SecurityMode.Best ?
                                null : SecurityMode,
                 SecurityPolicy = string.IsNullOrEmpty(SecurityPolicy) ?
                                  null : SecurityPolicy,
                 ClientCertificate = ClientCertificate.DecodeAsByteArray(),
                 ServerThumbprint = ServerThumbprint.DecodeAsByteArray()
             }
         },
         ActivationState = ActivationState,
         NotSeenSince = NotSeenSince,
         EndpointState = ActivationState == EndpointActivationState.ActivatedAndConnected ?
                         State : (EndpointConnectivityState?)null,
         OutOfSync = Connected && !_isInSync ? true : (bool?)null
     });
 }
        /// <inheritdoc/>
        public override bool Equals(object obj)
        {
            var registration = obj as EndpointRegistration;

            return(base.Equals(registration) &&
                   (Activated ?? false) == (registration.Activated ?? false) &&
                   EndpointUrlLC == registration.EndpointUrlLC &&
                   SupervisorId == registration.SupervisorId &&
                   JToken.DeepEquals(Credential, registration.Credential) &&
                   State == registration.State &&
                   CredentialType == registration.CredentialType &&
                   SecurityLevel == registration.SecurityLevel &&
                   SecurityPolicy == registration.SecurityPolicy &&
                   SecurityMode == registration.SecurityMode &&
                   AuthenticationMethods.DecodeAsList().SetEqualsSafe(
                       AuthenticationMethods.DecodeAsList(), JToken.DeepEquals) &&
                   ClientCertificate.DecodeAsByteArray().SequenceEqualsSafe(
                       registration.ClientCertificate.DecodeAsByteArray()) &&
                   ServerThumbprint.DecodeAsByteArray().SequenceEqualsSafe(
                       registration.ServerThumbprint.DecodeAsByteArray()));
        }
        /// <inheritdoc/>
        public override int GetHashCode()
        {
            var hashCode = base.GetHashCode();

            hashCode = hashCode * -1521134295 +
                       EqualityComparer <string> .Default.GetHashCode(EndpointUrlLC);

            hashCode = hashCode * -1521134295 +
                       EqualityComparer <string> .Default.GetHashCode(ApplicationId);

            hashCode = hashCode * -1521134295 +
                       EqualityComparer <bool> .Default.GetHashCode(Activated ?? false);

            hashCode = hashCode * -1521134295 +
                       JToken.EqualityComparer.GetHashCode(Credential);
            hashCode = hashCode * -1521134295 +
                       EqualityComparer <int?> .Default.GetHashCode(SecurityLevel);

            hashCode = hashCode * -1521134295 +
                       EqualityComparer <CredentialType?> .Default.GetHashCode(CredentialType);

            hashCode = hashCode * -1521134295 +
                       EqualityComparer <EndpointConnectivityState?> .Default.GetHashCode(State);

            hashCode = hashCode * -1521134295 +
                       EqualityComparer <SecurityMode?> .Default.GetHashCode(SecurityMode);

            hashCode = hashCode * -1521134295 +
                       EqualityComparer <string> .Default.GetHashCode(SecurityPolicy);

            hashCode = hashCode * -1521134295 +
                       EqualityComparer <string> .Default.GetHashCode(
                ClientCertificate.DecodeAsByteArray().ToSha1Hash());

            hashCode = hashCode * -1521134295 +
                       EqualityComparer <string> .Default.GetHashCode(
                ServerThumbprint.DecodeAsByteArray().ToSha1Hash());

            return(hashCode);
        }