Files
DP44/Common/DTS.Common.DataModel/.svn/pristine/d3/d3aa92c18ff7aefafc8fe2772e906d17b6792be9.svn-base

224 lines
8.0 KiB
Plaintext
Raw Normal View History

2026-04-17 14:55:32 -04:00
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<string> TestSetupIds
{
get
{
lock (TestSetupListLock)
{
return _testSetupList;
}
}
}
public static object TestSetupListLock { get; private set; } = new object();
private static DateTime _lastUpdateTime = DateTime.MinValue;
private static List<TestTemplate> _actualTestTemplates = new List<TestTemplate>();
private const int MIN_TESTS_UPDATEINTERVAL_MINUTES = 30;
private static uint _crc = 0xFFFF;
private static List<string> _testSetupList = null;
public static TestTemplate[] TestSetups
{
get
{
lock (TestSetupListLock)
{
return _actualTestTemplates.ToArray();
}
}
}
/// <summary>
/// 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
/// </summary>
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<IEventAggregator>();
}
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<string>();
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<IEventAggregator>();
}
}
catch (Exception ex)
{
return;
}
eventAggregator?.GetEvent<ProgressBarEvent>().Publish(new ProgressBarEventArg()
{ ProgressBarPercentage = 0D, SetPercentage = true });
bool bCache = false;
foreach (var t in allTemplates)
{
eventAggregator?.GetEvent<ProgressBarEvent>().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<ProgressBarEvent>().Publish(new ProgressBarEventArg()
{ SetPercentage = true, ProgressBarPercentage = 100D });
var crc = GetCRC();
lock (TestSetupListLock)
{
_lastUpdateTime = DateTime.Now;
_testSetupList = testSetupList;
_actualTestTemplates = new List<TestTemplate>(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<string>();
_actualTestTemplates = new List<TestTemplate>();
_actualTestTemplates.AddRange(allTemplates);
var total = allTemplates.Length;
int currentDone = 0;
var eventAggregator = ContainerLocator.Container.Resolve<IEventAggregator>();
foreach (var t in allTemplates)
{
eventAggregator.GetEvent<ProgressBarEvent>().Publish(new ProgressBarEventArg()
{ SetPercentage = true, ProgressBarPercentage = 100D * currentDone / total });
currentDone++;
if (!t.IsComplete) { continue; }
_testSetupList.Add(t.Name);
}
eventAggregator.GetEvent<ProgressBarEvent>().Publish(new ProgressBarEventArg()
{ SetPercentage = true, ProgressBarPercentage = 100D });
_crc = GetCRC();
return _testSetupList.ToArray();
}
}
}
private static uint GetCRC()
{
var list = new List<byte>();
using (var cmd = Storage.DbOperations.GetSQLCommand())
{
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);
}
}
}