using DataPROWin7.DataModel; using DTS.Common.Events; using Prism.Ioc; using Prism.Events; using System; using System.Collections.Generic; using System.Linq; using System.Windows.Threading; using System.Windows; using System.Threading; namespace DTS.Common.DataModel.Common { public class TestSetupCollection { public static List TestSetupIds { get { lock (TestSetupListLock) { return _testSetupList; } } } public static object TestSetupListLock { get; private set; } = new object(); private static DateTime _lastUpdateTime = DateTime.MinValue; private static List _actualTestTemplates = new List(); private const int MIN_TESTS_UPDATEINTERVAL_MINUTES = 30; private static uint _crc = 0xFFFF; private static List _testSetupList = null; public static TestTemplate[] TestSetups { get { lock (TestSetupListLock) { return _actualTestTemplates.ToArray(); } } } /// /// will update the test setup list if it's more than MIN_TESTS_UPDATEINTERVAL_MINUTES OLD, /// OR the CRC has changed for the /// otherwise uses it's existing copy /// public static void UpdateTestSetupListIfStale() { if (DateTime.Now.Subtract(_lastUpdateTime).TotalMinutes > MIN_TESTS_UPDATEINTERVAL_MINUTES) { UpdateTestSetupList(); } else if (GetCRC() != _crc) { UpdateTestSetupList(); } } public static void UpdateTestSetupList(TestTemplate test) { //fixes an error when cmdline import and servicelocator is null IEventAggregator eventAggregator = null; try { eventAggregator = ContainerLocator.Container.Resolve(); } catch (Exception) { return; } if (test.IsDirty) { if (!test.IsComplete) { lock (TestSetupListLock) { if (TestSetupList.Contains(test.Name)) { _testSetupList.Remove(test.Name); return; } } } } else if (!test.IsComplete) { return; } lock (TestSetupListLock) { if (!TestSetupList.Contains(test.Name)) { _testSetupList.Add(test.Name); } } } public static void UpdateTestSetupList(Action onCompleteAction = null) { var allTemplates = TestTemplateList.TestTemplatesList.AllTemplates; var testSetupList = new List(); var count = allTemplates.Length; var currentItem = 0; //fixes an error when cmdline import and servicelocator is null IEventAggregator eventAggregator = null; try { if (Application.Current.Dispatcher.CheckAccess()) { eventAggregator = ContainerLocator.Container.Resolve(); } } catch (Exception ex) { return; } eventAggregator?.GetEvent().Publish(new ProgressBarEventArg() { ProgressBarPercentage = 0D, SetPercentage = true }); bool bCache = false; foreach (var t in allTemplates) { eventAggregator?.GetEvent().Publish(new ProgressBarEventArg() { SetPercentage = true, ProgressBarPercentage = 100D * currentItem / count }); currentItem++; //calculating iscomplete is expensive on a large test setup list with lots of sensors //for now just calculate on demand and put all tests in the list testSetupList.Add(t.Name); } eventAggregator?.GetEvent().Publish(new ProgressBarEventArg() { SetPercentage = true, ProgressBarPercentage = 100D }); var crc = GetCRC(); lock (TestSetupListLock) { _lastUpdateTime = DateTime.Now; _testSetupList = testSetupList; _actualTestTemplates = new List(1000); _actualTestTemplates.AddRange(allTemplates); _crc = crc; } onCompleteAction?.Invoke(); } public static string[] TestSetupList { get { lock (TestSetupListLock) { if (null != _testSetupList && _testSetupList.Any()) { return _testSetupList.ToArray(); } } var allTemplates = TestTemplateList.TestTemplatesList.AllTemplates; lock (TestSetupListLock) { _lastUpdateTime = DateTime.Now; _testSetupList = new List(); _actualTestTemplates = new List(); _actualTestTemplates.AddRange(allTemplates); var total = allTemplates.Length; int currentDone = 0; var eventAggregator = ContainerLocator.Container.Resolve(); foreach (var t in allTemplates) { eventAggregator.GetEvent().Publish(new ProgressBarEventArg() { SetPercentage = true, ProgressBarPercentage = 100D * currentDone / total }); currentDone++; if (!t.IsComplete) { continue; } _testSetupList.Add(t.Name); } eventAggregator.GetEvent().Publish(new ProgressBarEventArg() { SetPercentage = true, ProgressBarPercentage = 100D }); _crc = GetCRC(); return _testSetupList.ToArray(); } } } private static uint GetCRC() { var list = new List(); using (var cmd = Storage.DbOperations.GetSQLCommand(true)) { try { cmd.CommandType = System.Data.CommandType.Text; cmd.CommandText = "SELECT [TestSetupName], [Dirty], [Complete], [LastModified] FROM [TestSetups]"; var reader = cmd.ExecuteReader(); while (reader.Read()) { var name = (string)reader["TestSetupName"]; list.AddRange(System.Text.Encoding.ASCII.GetBytes(name)); var isDirty = (bool)reader["Dirty"]; list.AddRange(BitConverter.GetBytes(isDirty)); var complete = (bool)reader["Complete"]; list.AddRange(BitConverter.GetBytes(complete)); var lastModified = (DateTime)reader["LastModified"]; list.AddRange(BitConverter.GetBytes(lastModified.ToBinary())); } } finally { cmd.Connection.Dispose(); } } var crc = new DTS.Utilities.Crc32(); return crc.Get(list); } } }