public static async Task <TwinAllOperationsInitializer> CreateAsync(RegistryManager registryManager, ModuleClient moduleClient, ITwinTestResultHandler resultHandler, TwinEventStorage storage) { try { TwinState 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 TwinState { ReportedPropertyUpdateCounter = 0, DesiredPropertyUpdateCounter = 0, TwinETag = twin.ETag, LastTimeOffline = DateTime.MinValue }; } else { Logger.LogInformation("Existing storage detected. Initializing reported / desired property update counters."); initializedState = new TwinState { ReportedPropertyUpdateCounter = GetNewPropertyCounter(reportedPropertyUpdates), DesiredPropertyUpdateCounter = GetNewPropertyCounter(desiredPropertyUpdates), TwinETag = twin.ETag, LastTimeOffline = DateTime.MinValue, LastTimeOfEdgeRestart = 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 <TwinEdgeOperationsInitializer> CreateAsync(RegistryManager registryManager, ModuleClient moduleClient, ITwinTestResultHandler reporter) { try { Twin twin = await registryManager.GetTwinAsync(Settings.Current.DeviceId, Settings.Current.TargetModuleId); // reset reported properties await TwinTesterUtil.ResetTwinReportedPropertiesAsync(moduleClient, twin); return(new TwinEdgeOperationsInitializer(registryManager, moduleClient, reporter, 0)); } catch (Exception e) { throw new Exception($"Shutting down module. Initialization failure: {e}"); } }