Esempio n. 1
0
 public DesiredPropertyUpdater(RegistryManager registryManager, ITwinTestResultHandler resultHandler, TwinTestState twinTestState)
 {
     this.registryManager = registryManager;
     this.resultHandler   = resultHandler;
     this.twinTestState   = twinTestState;
     this.desiredPropertyUpdateCounter = twinTestState.DesiredPropertyUpdateCounter;
 }
 public DesiredPropertiesValidator(RegistryManager registryManager, ModuleClient moduleClient, TwinEventStorage storage, ITwinTestResultHandler resultHandler, TwinTestState twinTestState)
 {
     this.registryManager = registryManager;
     this.moduleClient    = moduleClient;
     this.storage         = storage;
     this.resultHandler   = resultHandler;
     this.twinTestState   = twinTestState;
 }
        bool SkipCallbackValidationDueToEdgeHubRestart(TwinTestState twinTestState, DateTime twinUpdateTime)
        {
            DateTime edgeHubRestartTolerancePeriodLowerBound =
                twinTestState.EdgeHubLastStopped == DateTime.MinValue ? DateTime.MinValue : twinTestState.EdgeHubLastStopped.Add(-Settings.Current.EdgeHubRestartFailureTolerance);
            DateTime edgeHubRestartTolerancePeriodUpperBound =
                twinTestState.EdgeHubLastStarted == DateTime.MinValue ? DateTime.MinValue : twinTestState.EdgeHubLastStarted.Add(Settings.Current.EdgeHubRestartFailureTolerance);

            return(edgeHubRestartTolerancePeriodLowerBound <= twinUpdateTime && twinUpdateTime <= edgeHubRestartTolerancePeriodUpperBound);
        }
Esempio n. 4
0
        bool DoesExceedFailureThreshold(TwinTestState twinState, string reportedPropertyKey, DateTime reportedPropertyUpdatedAt)
        {
            DateTime comparisonPoint        = reportedPropertyUpdatedAt > twinState.LastNetworkOffline ? reportedPropertyUpdatedAt : twinState.LastNetworkOffline;
            bool     exceedFailureThreshold = DateTime.UtcNow - comparisonPoint > Settings.Current.TwinUpdateFailureThreshold;

            if (exceedFailureThreshold)
            {
                Logger.LogInformation(
                    $"Reported Property [{reportedPropertyKey}] exceed failure threshold: updated at={reportedPropertyUpdatedAt}, " +
                    $"last offline={twinState.LastNetworkOffline}, threshold={Settings.Current.TwinUpdateFailureThreshold}");
            }

            return(exceedFailureThreshold);
        }
Esempio n. 5
0
        public static async Task <TwinAllOperationsInitializer> CreateAsync(RegistryManager registryManager, ModuleClient moduleClient, ITwinTestResultHandler resultHandler, TwinEventStorage storage)
        {
            try
            {
                TwinTestState initializedState;
                Twin          twin = await registryManager.GetTwinAsync(Settings.Current.DeviceId, Settings.Current.TargetModuleId);

                Dictionary <string, DateTime> reportedPropertyUpdates = await storage.GetAllReportedPropertiesUpdatedAsync();

                Dictionary <string, DateTime> desiredPropertyUpdates = await storage.GetAllDesiredPropertiesUpdatedAsync();

                if (reportedPropertyUpdates.Count == 0 &&
                    desiredPropertyUpdates.Count == 0 &&
                    (await storage.GetAllDesiredPropertiesReceivedAsync()).Count == 0)
                {
                    Logger.LogInformation("No existing storage detected. Initializing new module twin for fresh run.");

                    // reset desired properties
                    Twin desiredPropertyResetTwin = await registryManager.ReplaceTwinAsync(Settings.Current.DeviceId, Settings.Current.TargetModuleId, new Twin(), twin.ETag);

                    await TwinTesterUtil.ResetTwinReportedPropertiesAsync(moduleClient, desiredPropertyResetTwin);

                    await Task.Delay(TimeSpan.FromSeconds(10)); // give enough time for reported properties reset to reach cloud

                    twin = await registryManager.GetTwinAsync(Settings.Current.DeviceId, Settings.Current.TargetModuleId);

                    initializedState = new TwinTestState(twin.ETag);
                }
                else
                {
                    Logger.LogInformation("Existing storage detected. Initializing reported / desired property update counters.");
                    initializedState = new TwinTestState(
                        GetNewPropertyCounter(reportedPropertyUpdates),
                        GetNewPropertyCounter(desiredPropertyUpdates),
                        twin.ETag,
                        DateTime.MinValue,
                        DateTime.MinValue,
                        DateTime.MinValue);
                }

                Logger.LogInformation($"Start state of module twin: {JsonConvert.SerializeObject(twin, Formatting.Indented)}");
                return(new TwinAllOperationsInitializer(registryManager, moduleClient, resultHandler, storage, initializedState));
            }
            catch (Exception e)
            {
                throw new Exception($"Shutting down module. Initialization failure: {e}");
            }
        }
        public static async Task <TwinCloudOperationsInitializer> CreateAsync(RegistryManager registryManager, ITwinTestResultHandler resultHandler)
        {
            try
            {
                TwinTestState initializedState;
                Twin          twin = await registryManager.GetTwinAsync(Settings.Current.DeviceId, Settings.Current.TargetModuleId);

                initializedState = new TwinTestState(twin.ETag);

                Logger.LogInformation($"Start state of module twin: {JsonConvert.SerializeObject(twin, Formatting.Indented)}");
                return(new TwinCloudOperationsInitializer(registryManager, resultHandler, initializedState));
            }
            catch (Exception e)
            {
                throw new Exception($"Shutting down module. Initialization failure: {e}");
            }
        }
Esempio n. 7
0
        TwinAllOperationsInitializer(RegistryManager registryManager, ModuleClient moduleClient, ITwinTestResultHandler resultHandler, TwinEventStorage storage, TwinTestState twinTestState)
        {
            this.registryManager             = registryManager;
            this.twinTestState               = twinTestState;
            this.reportedPropertyUpdater     = new ReportedPropertyUpdater(moduleClient, resultHandler, twinTestState.ReportedPropertyUpdateCounter);
            this.desiredPropertyUpdater      = new DesiredPropertyUpdater(registryManager, resultHandler, twinTestState);
            this.desiredPropertyReceiver     = new DesiredPropertyReceiver(moduleClient, resultHandler);
            this.reportedPropertiesValidator = new ReportedPropertiesValidator(registryManager, moduleClient, storage, resultHandler, twinTestState);
            this.desiredPropertiesValidator  = new DesiredPropertiesValidator(registryManager, moduleClient, storage, resultHandler, twinTestState);

            moduleClient.SetConnectionStatusChangesHandler((status, reason) =>
            {
                Logger.LogInformation($"Detected change in connection status:{Environment.NewLine}Changed Status: {status} Reason: {reason}");
                if (status == ConnectionStatus.Disconnected_Retrying)
                {
                    this.twinTestState.EdgeHubLastStopped = DateTime.UtcNow;
                }
                else if (status == ConnectionStatus.Connected)
                {
                    this.twinTestState.EdgeHubLastStarted = DateTime.UtcNow;
                }
            });
        }
        bool ExceedFailureThreshold(TwinTestState twinTestState, DateTime twinUpdateTime)
        {
            DateTime comparisonPoint = twinUpdateTime > twinTestState.LastNetworkOffline ? twinUpdateTime : twinTestState.LastNetworkOffline;

            return(DateTime.UtcNow - comparisonPoint > Settings.Current.TwinUpdateFailureThreshold);
        }
 TwinCloudOperationsInitializer(RegistryManager registryManager, ITwinTestResultHandler resultHandler, TwinTestState twinTestState)
 {
     this.desiredPropertyUpdater = new DesiredPropertyUpdater(registryManager, resultHandler, twinTestState);
 }