/// <summary>
        /// Convert twin to registration information.
        /// </summary>
        /// <param name="twin"></param>
        /// <param name="onlyServerState"></param>
        /// <returns></returns>
        public static BaseRegistration ToRegistration(DeviceTwinModel twin,
                                                      bool onlyServerState = false)
        {
            if (twin == null)
            {
                return(null);
            }
            var type = twin.Tags.GetValueOrDefault <string>(nameof(DeviceType), null);

            if (string.IsNullOrEmpty(type) && twin.Properties.Reported != null)
            {
                type = twin.Properties.Reported.GetValueOrDefault <string>(TwinProperty.kType, null);
            }
            switch (type?.ToLowerInvariant() ?? "")
            {
            case "endpoint":
                return(EndpointRegistration.FromTwin(twin, onlyServerState));

            case "application":
                return(ApplicationRegistration.FromTwin(twin));

            case "supervisor":
                return(SupervisorRegistration.FromTwin(twin, onlyServerState));
            }
            return(null);
        }
 /// <summary>
 /// Flag twin as synchronized - i.e. it matches the other.
 /// </summary>
 /// <param name="registration"></param>
 /// <param name="other"></param>
 internal static bool IsInSyncWith(this SupervisorRegistration registration,
                                   SupervisorRegistration other)
 {
     return
         (other != null &&
          registration.SiteId == other.SiteId &&
          registration.LogLevel == other.LogLevel);
 }
Example #3
0
 /// <summary>
 /// Returns activation filter model
 /// </summary>
 /// <param name="registration"></param>
 /// <returns></returns>
 private static EndpointActivationFilterModel ToFilterModel(this SupervisorRegistration registration)
 {
     return(registration.IsNullFilter() ? null : new EndpointActivationFilterModel {
         SecurityMode = registration.SecurityModeFilter,
         SecurityPolicies = registration.SecurityPoliciesFilter.DecodeAsList(),
         TrustLists = registration.TrustListsFilter.DecodeAsList()
     });
 }
Example #4
0
        /// <summary>
        /// Create patch twin model to upload
        /// </summary>
        /// <param name="existing"></param>
        /// <param name="update"></param>
        public static DeviceTwinModel Patch(this SupervisorRegistration existing,
                                            SupervisorRegistration update)
        {
            var twin = new DeviceTwinModel {
                Etag       = existing?.Etag,
                Tags       = new Dictionary <string, JToken>(),
                Properties = new TwinPropertiesModel {
                    Desired = new Dictionary <string, JToken>()
                }
            };

            // Tags

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

            if (update?.SiteOrGatewayId != existing?.SiteOrGatewayId)
            {
                twin.Tags.Add(nameof(SupervisorRegistration.SiteOrGatewayId),
                              update?.SiteOrGatewayId);
            }

            // Settings

            var certUpdate = update?.Certificate?.DecodeAsByteArray()?.SequenceEqualsSafe(
                existing?.Certificate.DecodeAsByteArray());

            if (!(certUpdate ?? true))
            {
                twin.Properties.Desired.Add(nameof(SupervisorRegistration.Certificate),
                                            update?.Certificate == null ?
                                            null : JToken.FromObject(update.Certificate));
                twin.Tags.Add(nameof(SupervisorRegistration.Thumbprint),
                              update?.Certificate?.DecodeAsByteArray()?.ToSha1Hash());
            }

            if (update?.LogLevel != existing?.LogLevel)
            {
                twin.Properties.Desired.Add(nameof(SupervisorRegistration.LogLevel),
                                            update?.LogLevel == null ?
                                            null : JToken.FromObject(update.LogLevel));
            }

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

            twin.Tags.Add(nameof(SupervisorRegistration.DeviceType), update?.DeviceType);
            twin.Id       = update?.DeviceId ?? existing?.DeviceId;
            twin.ModuleId = update?.ModuleId ?? existing?.ModuleId;
            return(twin);
        }
Example #5
0
 /// <summary>
 /// Returns if no activation filter specified
 /// </summary>
 /// <param name="registration"></param>
 /// <returns></returns>
 private static bool IsNullFilter(this SupervisorRegistration registration)
 {
     if (registration.SecurityModeFilter == null &&
         (registration.TrustListsFilter == null || registration.TrustListsFilter.Count == 0) &&
         (registration.SecurityPoliciesFilter == null || registration.SecurityPoliciesFilter.Count == 0))
     {
         return(true);
     }
     return(false);
 }
        public void TestEqualIsEqualWithDeviceModel()
        {
            var r1 = CreateRegistration();
            var m  = SupervisorRegistration.Patch(null, r1);
            var r2 = BaseRegistration.ToRegistration(m);

            Assert.Equal(r1, r2);
            Assert.Equal(r1.GetHashCode(), r2.GetHashCode());
            Assert.True(r1 == r2);
            Assert.False(r1 != r2);
        }
        public void TestEqualIsEqualWithServiceModelConversion()
        {
            var r1 = CreateRegistration();
            var m  = r1.ToServiceModel();
            var r2 = SupervisorRegistration.FromServiceModel(m);

            Assert.Equal(r1, r2);
            Assert.Equal(r1.GetHashCode(), r2.GetHashCode());
            Assert.True(r1 == r2);
            Assert.False(r1 != r2);
        }
 /// <summary>
 /// Convert to service model
 /// </summary>
 /// <param name="registration"></param>
 /// <returns></returns>
 public static SupervisorModel ToServiceModel(this SupervisorRegistration registration)
 {
     return(new SupervisorModel {
         Id = SupervisorModelEx.CreateSupervisorId(registration.DeviceId, registration.ModuleId),
         SiteId = registration.SiteId,
         Certificate = registration.Certificate?.DecodeAsByteArray(),
         LogLevel = registration.LogLevel,
         Connected = registration.IsConnected() ? true : (bool?)null,
         OutOfSync = registration.IsConnected() && !registration._isInSync ? true : (bool?)null
     });
 }
        public void TestEqualIsNotEqualWithDeviceModel()
        {
            var r1 = CreateRegistration();
            var m  = SupervisorRegistration.Patch(null, r1);

            m.Properties.Desired["AddressRangesToScan"] = null;
            var r2 = BaseRegistration.ToRegistration(m);

            Assert.NotEqual(r1, r2);
            Assert.NotEqual(r1.GetHashCode(), r2.GetHashCode());
            Assert.True(r1 != r2);
            Assert.False(r1 == r2);
        }
        public void TestEqualIsNotEqualWithServiceModelConversionWhenDisabled()
        {
            var fix = new Fixture();

            var r1 = CreateRegistration();
            var m  = r1.ToServiceModel();
            var r2 = SupervisorRegistration.FromServiceModel(m, true);

            Assert.NotEqual(r1, r2);
            Assert.NotEqual(r1.GetHashCode(), r2.GetHashCode());
            Assert.True(r1 != r2);
            Assert.False(r1 == r2);
        }
        public void TestEqualIsNotEqualWithServiceModelConversion()
        {
            var r1 = CreateRegistration();
            var m  = r1.ToServiceModel();

            m.DiscoveryConfig.AddressRangesToScan = "";
            var r2 = SupervisorRegistration.FromServiceModel(m);

            Assert.NotEqual(r1, r2);
            Assert.NotEqual(r1.GetHashCode(), r2.GetHashCode());
            Assert.True(r1 != r2);
            Assert.False(r1 == r2);
        }
Example #12
0
 /// <summary>
 /// Convert to service model
 /// </summary>
 /// <param name="registration"></param>
 /// <returns></returns>
 public static SupervisorModel ToServiceModel(this SupervisorRegistration registration)
 {
     return(new SupervisorModel {
         Discovery = registration.Discovery != DiscoveryMode.Off ?
                     registration.Discovery : (DiscoveryMode?)null,
         Id = registration.SupervisorId,
         SiteId = registration.SiteId,
         Certificate = registration.Certificate?.DecodeAsByteArray(),
         LogLevel = registration.LogLevel,
         DiscoveryConfig = registration.ToConfigModel(),
         Connected = registration.IsConnected() ? true : (bool?)null,
         OutOfSync = registration.IsConnected() && !registration._isInSync ? true : (bool?)null
     });
 }
 /// <summary>
 /// Convert to service model
 /// </summary>
 /// <param name="registration"></param>
 /// <returns></returns>
 public static SupervisorModel ToServiceModel(this SupervisorRegistration registration)
 {
     if (registration == null)
     {
         return(null);
     }
     return(new SupervisorModel {
         Id = SupervisorModelEx.CreateSupervisorId(registration.DeviceId, registration.ModuleId),
         SiteId = registration.SiteId,
         LogLevel = registration.LogLevel,
         Connected = registration.IsConnected() ? true : (bool?)null,
         OutOfSync = registration.IsConnected() && !registration._isInSync ? true : (bool?)null
     });
 }
        public void TestEqualIsEqualWithDeviceModelWhenDisabled()
        {
            var fix = new Fixture();

            var r1 = CreateRegistration();
            var r2 = SupervisorRegistration.FromServiceModel(
                r1.ToServiceModel(), true);
            var m1 = SupervisorRegistration.Patch(r1, r2);
            var r3 = SupervisorRegistration.FromServiceModel(
                r2.ToServiceModel(), false);
            var m2 = SupervisorRegistration.Patch(r2, r3);

            Assert.True((bool)m1.Tags[nameof(BaseRegistration.IsDisabled)]);
            Assert.NotNull((DateTime?)m1.Tags[nameof(BaseRegistration.NotSeenSince)]);
            Assert.Null((bool?)m2.Tags[nameof(BaseRegistration.IsDisabled)]);
            Assert.Null((DateTime?)m2.Tags[nameof(BaseRegistration.NotSeenSince)]);
        }
Example #15
0
 /// <summary>
 /// Returns config model
 /// </summary>
 /// <param name="registration"></param>
 /// <returns></returns>
 private static DiscoveryConfigModel ToConfigModel(this SupervisorRegistration registration)
 {
     return(registration.IsNullConfig() ? null : new DiscoveryConfigModel {
         AddressRangesToScan = registration.AddressRangesToScan,
         PortRangesToScan = registration.PortRangesToScan,
         MaxNetworkProbes = registration.MaxNetworkProbes,
         NetworkProbeTimeout = registration.NetworkProbeTimeout,
         MaxPortProbes = registration.MaxPortProbes,
         MinPortProbesPercent = registration.MinPortProbesPercent,
         PortProbeTimeout = registration.PortProbeTimeout,
         IdleTimeBetweenScans = registration.IdleTimeBetweenScans,
         Callbacks = registration.DiscoveryCallbacks?.DecodeAsList(),
         DiscoveryUrls = registration.DiscoveryUrls?.DecodeAsList(),
         Locales = registration.Locales?.DecodeAsList(),
         ActivationFilter = registration.ToFilterModel()
     });
 }
        /// <summary>
        /// Create patch twin model to upload
        /// </summary>
        /// <param name="existing"></param>
        /// <param name="update"></param>
        /// <param name="serializer"></param>
        public static DeviceTwinModel Patch(this SupervisorRegistration existing,
                                            SupervisorRegistration update, IJsonSerializer serializer)
        {
            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(SupervisorRegistration.IsDisabled), (update?.IsDisabled ?? false) ?
                              true : (bool?)null);
                twin.Tags.Add(nameof(SupervisorRegistration.NotSeenSince), (update?.IsDisabled ?? false) ?
                              DateTime.UtcNow : (DateTime?)null);
            }

            if (update?.SiteOrGatewayId != existing?.SiteOrGatewayId)
            {
                twin.Tags.Add(nameof(SupervisorRegistration.SiteOrGatewayId),
                              update?.SiteOrGatewayId);
            }

            // Settings

            if (update?.LogLevel != existing?.LogLevel)
            {
                twin.Properties.Desired.Add(nameof(SupervisorRegistration.LogLevel),
                                            update?.LogLevel == null ?
                                            null : serializer.FromObject(update.LogLevel.ToString()));
            }

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

            twin.Tags.Add(nameof(SupervisorRegistration.DeviceType), update?.DeviceType);
            twin.Id       = update?.DeviceId ?? existing?.DeviceId;
            twin.ModuleId = update?.ModuleId ?? existing?.ModuleId;
            return(twin);
        }
Example #17
0
 /// <summary>
 /// Returns if no discovery config specified
 /// </summary>
 /// <param name="registration"></param>
 /// <returns></returns>
 private static bool IsNullConfig(this SupervisorRegistration registration)
 {
     if (string.IsNullOrEmpty(registration.AddressRangesToScan) &&
         string.IsNullOrEmpty(registration.PortRangesToScan) &&
         registration.MaxNetworkProbes == null &&
         registration.NetworkProbeTimeout == null &&
         registration.MaxPortProbes == null &&
         registration.MinPortProbesPercent == null &&
         registration.PortProbeTimeout == null &&
         (registration.DiscoveryCallbacks == null || registration.DiscoveryCallbacks.Count == 0) &&
         (registration.DiscoveryUrls == null || registration.DiscoveryUrls.Count == 0) &&
         (registration.Locales == null || registration.Locales.Count == 0) &&
         registration.IdleTimeBetweenScans == null)
     {
         return(true);
     }
     return(false);
 }
Example #18
0
        /// <summary>
        /// Decode tags and property into registration object
        /// </summary>
        /// <param name="twin"></param>
        /// <param name="properties"></param>
        /// <returns></returns>
        public static SupervisorRegistration ToSupervisorRegistration(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 SupervisorRegistration {
                // Device

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

                // Tags

                IsDisabled =
                    tags.GetValueOrDefault <bool>(nameof(SupervisorRegistration.IsDisabled), null),
                NotSeenSince =
                    tags.GetValueOrDefault <DateTime>(nameof(SupervisorRegistration.NotSeenSince), null),
                Thumbprint =
                    tags.GetValueOrDefault <string>(nameof(SupervisorRegistration.Thumbprint), null),

                // Properties

                Certificate =
                    properties.GetValueOrDefault <Dictionary <string, string> >(nameof(SupervisorRegistration.Certificate), null),
                LogLevel =
                    properties.GetValueOrDefault <TraceLogLevel>(nameof(SupervisorRegistration.LogLevel), null),

                SiteId =
                    properties.GetValueOrDefault <string>(TwinProperty.SiteId, null),
                Connected = connected ??
                            properties.GetValueOrDefault(TwinProperty.Connected, false),
                Type =
                    properties.GetValueOrDefault <string>(TwinProperty.Type, null)
            };

            return(registration);
        }
Example #19
0
 /// <summary>
 /// Flag twin as synchronized - i.e. it matches the other.
 /// </summary>
 /// <param name="other"></param>
 internal void MarkAsInSyncWith(SupervisorRegistration other)
 {
     _isInSync =
         other != null &&
         SiteId == other.SiteId &&
         LogLevel == other.LogLevel &&
         Discovery == other.Discovery &&
         AddressRangesToScan == other.AddressRangesToScan &&
         PortRangesToScan == other.PortRangesToScan &&
         MaxNetworkProbes == other.MaxNetworkProbes &&
         NetworkProbeTimeout == other.NetworkProbeTimeout &&
         MaxPortProbes == other.MaxPortProbes &&
         MinPortProbesPercent == other.MinPortProbesPercent &&
         PortProbeTimeout == other.PortProbeTimeout &&
         IdleTimeBetweenScans == other.IdleTimeBetweenScans &&
         DiscoveryUrls.DecodeAsList().SequenceEqualsSafe(
             other.DiscoveryUrls.DecodeAsList()) &&
         Locales.DecodeAsList().SequenceEqualsSafe(
             other.Locales.DecodeAsList());
 }
Example #20
0
 /// <summary>
 /// Flag twin as synchronized - i.e. it matches the other.
 /// </summary>
 /// <param name="registration"></param>
 /// <param name="other"></param>
 internal static bool IsInSyncWith(this SupervisorRegistration registration,
                                   SupervisorRegistration other)
 {
     return
         (other != null &&
          registration.SiteId == other.SiteId &&
          registration.LogLevel == other.LogLevel &&
          registration.Discovery == other.Discovery &&
          registration.AddressRangesToScan == other.AddressRangesToScan &&
          registration.PortRangesToScan == other.PortRangesToScan &&
          registration.MaxNetworkProbes == other.MaxNetworkProbes &&
          registration.NetworkProbeTimeout == other.NetworkProbeTimeout &&
          registration.MaxPortProbes == other.MaxPortProbes &&
          registration.MinPortProbesPercent == other.MinPortProbesPercent &&
          registration.PortProbeTimeout == other.PortProbeTimeout &&
          registration.IdleTimeBetweenScans == other.IdleTimeBetweenScans &&
          registration.DiscoveryUrls.DecodeAsList().SequenceEqualsSafe(
              other.DiscoveryUrls.DecodeAsList()) &&
          registration.Locales.DecodeAsList().SequenceEqualsSafe(
              other.Locales.DecodeAsList()));
 }
        /// <summary>
        /// Decode tags and property into registration object
        /// </summary>
        /// <param name="twin"></param>
        /// <param name="properties"></param>
        /// <returns></returns>
        public static SupervisorRegistration ToSupervisorRegistration(this DeviceTwinModel twin,
                                                                      Dictionary <string, VariantValue> properties)
        {
            if (twin == null)
            {
                return(null);
            }

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

            var registration = new SupervisorRegistration {
                // Device

                DeviceId  = twin.Id,
                ModuleId  = twin.ModuleId,
                Etag      = twin.Etag,
                Connected = twin.IsConnected() ?? false,

                // Tags

                IsDisabled =
                    tags.GetValueOrDefault <bool>(nameof(SupervisorRegistration.IsDisabled), null),
                NotSeenSince =
                    tags.GetValueOrDefault <DateTime>(nameof(SupervisorRegistration.NotSeenSince), null),

                // Properties

                LogLevel =
                    properties.GetValueOrDefault <TraceLogLevel>(nameof(SupervisorRegistration.LogLevel), null),

                SiteId =
                    properties.GetValueOrDefault <string>(TwinProperty.SiteId, null),
                Version =
                    properties.GetValueOrDefault <string>(TwinProperty.Version, null),
                Type =
                    properties.GetValueOrDefault <string>(TwinProperty.Type, null)
            };

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

            // Tags

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

            if (update?.SiteOrSupervisorId != existing?.SiteOrSupervisorId)
            {
                twin.Tags.Add(nameof(SupervisorRegistration.SiteOrSupervisorId),
                              update?.SiteOrSupervisorId);
            }

            var cbUpdate = update?.DiscoveryCallbacks?.DecodeAsList()?.SetEqualsSafe(
                existing?.DiscoveryCallbacks?.DecodeAsList(),
                (callback1, callback2) => callback1.IsSameAs(callback2));

            if (!(cbUpdate ?? true))
            {
                twin.Tags.Add(nameof(SupervisorRegistration.DiscoveryCallbacks),
                              update?.DiscoveryCallbacks == null ?
                              null : JToken.FromObject(update.DiscoveryCallbacks));
            }

            var policiesUpdate = update?.SecurityPoliciesFilter.DecodeAsList().SequenceEqualsSafe(
                existing?.SecurityPoliciesFilter?.DecodeAsList());

            if (!(policiesUpdate ?? true))
            {
                twin.Tags.Add(nameof(SupervisorRegistration.SecurityPoliciesFilter),
                              update?.SecurityPoliciesFilter == null ?
                              null : JToken.FromObject(update.SecurityPoliciesFilter));
            }

            var trustListUpdate = update?.TrustListsFilter.DecodeAsList().SequenceEqualsSafe(
                existing?.TrustListsFilter?.DecodeAsList());

            if (!(trustListUpdate ?? true))
            {
                twin.Tags.Add(nameof(SupervisorRegistration.TrustListsFilter),
                              update?.TrustListsFilter == null ?
                              null : JToken.FromObject(update.TrustListsFilter));
            }

            if (update?.SecurityModeFilter != existing?.SecurityModeFilter)
            {
                twin.Tags.Add(nameof(SupervisorRegistration.SecurityModeFilter),
                              JToken.FromObject(update?.SecurityModeFilter));
            }

            // Settings

            var urlUpdate = update?.DiscoveryUrls.DecodeAsList().SequenceEqualsSafe(
                existing?.DiscoveryUrls?.DecodeAsList());

            if (!(urlUpdate ?? true))
            {
                twin.Properties.Desired.Add(nameof(SupervisorRegistration.DiscoveryUrls),
                                            update?.DiscoveryUrls == null ?
                                            null : JToken.FromObject(update.DiscoveryUrls));
            }

            var certUpdate = update?.Certificate?.DecodeAsByteArray()?.SequenceEqualsSafe(
                existing?.Certificate.DecodeAsByteArray());

            if (!(certUpdate ?? true))
            {
                twin.Properties.Desired.Add(nameof(SupervisorRegistration.Certificate),
                                            update?.Certificate == null ?
                                            null : JToken.FromObject(update.Certificate));
                twin.Tags.Add(nameof(SupervisorRegistration.Thumbprint),
                              update?.Certificate?.DecodeAsByteArray()?.ToSha1Hash());
            }

            var localesUpdate = update?.Locales?.DecodeAsList()?.SequenceEqualsSafe(
                existing?.Locales?.DecodeAsList());

            if (!(localesUpdate ?? true))
            {
                twin.Properties.Desired.Add(nameof(SupervisorRegistration.Locales),
                                            update?.Locales == null ?
                                            null : JToken.FromObject(update.Locales));
            }

            if (update?.Discovery != existing?.Discovery)
            {
                twin.Properties.Desired.Add(nameof(SupervisorRegistration.Discovery),
                                            JToken.FromObject(update?.Discovery));
            }

            if (update?.AddressRangesToScan != existing?.AddressRangesToScan)
            {
                twin.Properties.Desired.Add(nameof(SupervisorRegistration.AddressRangesToScan),
                                            update?.AddressRangesToScan);
            }

            if (update?.NetworkProbeTimeout != existing?.NetworkProbeTimeout)
            {
                twin.Properties.Desired.Add(nameof(SupervisorRegistration.NetworkProbeTimeout),
                                            update?.NetworkProbeTimeout);
            }

            if (update?.LogLevel != existing?.LogLevel)
            {
                twin.Properties.Desired.Add(nameof(SupervisorRegistration.LogLevel),
                                            update?.LogLevel == null ?
                                            null : JToken.FromObject(update.LogLevel));
            }

            if (update?.MaxNetworkProbes != existing?.MaxNetworkProbes)
            {
                twin.Properties.Desired.Add(nameof(SupervisorRegistration.MaxNetworkProbes),
                                            update?.MaxNetworkProbes);
            }

            if (update?.PortRangesToScan != existing?.PortRangesToScan)
            {
                twin.Properties.Desired.Add(nameof(SupervisorRegistration.PortRangesToScan),
                                            update?.PortRangesToScan);
            }

            if (update?.PortProbeTimeout != existing?.PortProbeTimeout)
            {
                twin.Properties.Desired.Add(nameof(SupervisorRegistration.PortProbeTimeout),
                                            update?.PortProbeTimeout);
            }

            if (update?.MaxPortProbes != existing?.MaxPortProbes)
            {
                twin.Properties.Desired.Add(nameof(SupervisorRegistration.MaxPortProbes),
                                            update?.MaxPortProbes);
            }

            if (update?.IdleTimeBetweenScans != existing?.IdleTimeBetweenScans)
            {
                twin.Properties.Desired.Add(nameof(SupervisorRegistration.IdleTimeBetweenScans),
                                            update?.IdleTimeBetweenScans);
            }

            if (update?.MinPortProbesPercent != existing?.MinPortProbesPercent)
            {
                twin.Properties.Desired.Add(nameof(SupervisorRegistration.MinPortProbesPercent),
                                            update?.MinPortProbesPercent);
            }

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

            twin.Tags.Add(nameof(SupervisorRegistration.DeviceType), update?.DeviceType);
            twin.Id       = update?.DeviceId ?? existing?.DeviceId;
            twin.ModuleId = update?.ModuleId ?? existing?.ModuleId;
            return(twin);
        }
 /// <summary>
 /// Create device twin
 /// </summary>
 /// <param name="registration"></param>
 /// <param name="serializer"></param>
 /// <returns></returns>
 public static DeviceTwinModel ToDeviceTwin(
     this SupervisorRegistration registration, IJsonSerializer serializer)
 {
     return(Patch(null, registration, serializer));
 }
Example #24
0
 /// <summary>
 /// Create device twin
 /// </summary>
 /// <param name="registration"></param>
 /// <returns></returns>
 public static DeviceTwinModel ToDeviceTwin(this SupervisorRegistration registration)
 {
     return(Patch(null, registration));
 }
Example #25
0
        /// <summary>
        /// Decode tags and property into registration object
        /// </summary>
        /// <param name="twin"></param>
        /// <param name="properties"></param>
        /// <returns></returns>
        public static SupervisorRegistration ToSupervisorRegistration(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 SupervisorRegistration {
                // Device

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

                // Tags

                IsDisabled =
                    tags.GetValueOrDefault <bool>(nameof(SupervisorRegistration.IsDisabled), null),
                NotSeenSince =
                    tags.GetValueOrDefault <DateTime>(nameof(SupervisorRegistration.NotSeenSince), null),
                Thumbprint =
                    tags.GetValueOrDefault <string>(nameof(SupervisorRegistration.Thumbprint), null),
                DiscoveryCallbacks =
                    tags.GetValueOrDefault <Dictionary <string, CallbackModel> >(nameof(SupervisorRegistration.DiscoveryCallbacks), null),
                SecurityModeFilter =
                    tags.GetValueOrDefault <SecurityMode>(nameof(SupervisorRegistration.SecurityModeFilter), null),
                SecurityPoliciesFilter =
                    tags.GetValueOrDefault <Dictionary <string, string> >(nameof(SupervisorRegistration.SecurityPoliciesFilter), null),
                TrustListsFilter =
                    tags.GetValueOrDefault <Dictionary <string, string> >(nameof(SupervisorRegistration.TrustListsFilter), null),

                // Properties

                Certificate =
                    properties.GetValueOrDefault <Dictionary <string, string> >(nameof(SupervisorRegistration.Certificate), null),
                LogLevel =
                    properties.GetValueOrDefault <SupervisorLogLevel>(nameof(SupervisorRegistration.LogLevel), null),
                Discovery =
                    properties.GetValueOrDefault(nameof(SupervisorRegistration.Discovery), DiscoveryMode.Off),
                AddressRangesToScan =
                    properties.GetValueOrDefault <string>(nameof(SupervisorRegistration.AddressRangesToScan), null),
                NetworkProbeTimeout =
                    properties.GetValueOrDefault <TimeSpan>(nameof(SupervisorRegistration.NetworkProbeTimeout), null),
                MaxNetworkProbes =
                    properties.GetValueOrDefault <int>(nameof(SupervisorRegistration.MaxNetworkProbes), null),
                PortRangesToScan =
                    properties.GetValueOrDefault <string>(nameof(SupervisorRegistration.PortRangesToScan), null),
                PortProbeTimeout =
                    properties.GetValueOrDefault <TimeSpan>(nameof(SupervisorRegistration.PortProbeTimeout), null),
                MaxPortProbes =
                    properties.GetValueOrDefault <int>(nameof(SupervisorRegistration.MaxPortProbes), null),
                MinPortProbesPercent =
                    properties.GetValueOrDefault <int>(nameof(SupervisorRegistration.MinPortProbesPercent), null),
                IdleTimeBetweenScans =
                    properties.GetValueOrDefault <TimeSpan>(nameof(SupervisorRegistration.IdleTimeBetweenScans), null),
                DiscoveryUrls =
                    properties.GetValueOrDefault <Dictionary <string, string> >(nameof(SupervisorRegistration.DiscoveryUrls), null),
                Locales =
                    properties.GetValueOrDefault <Dictionary <string, string> >(nameof(SupervisorRegistration.Locales), null),

                SiteId =
                    properties.GetValueOrDefault <string>(TwinProperty.kSiteId, null),
                Connected = connected ??
                            properties.GetValueOrDefault(TwinProperty.kConnected, false),
                Type =
                    properties.GetValueOrDefault <string>(TwinProperty.kType, null)
            };

            return(registration);
        }