Files
DP44/DataPRO/IService/Classes/Download/DownloadReport.cs
2026-04-17 14:55:32 -04:00

326 lines
10 KiB
C#

using DASFactoryDb.Download;
using DTS.Common.Interface.DASFactory;
using DTS.Common.Interface.DASFactory.Config;
using DTS.Common.Interface.DASFactory.Download;
using DTS.Common.Interface.DownloadEvent;
using DTS.Common.Utilities.Logging;
using System;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.IO;
using System.Text;
using System.Xml;
namespace DTS.DASLib.Service
{
/// <summary>
/// This class represent all events stored on the DAS. It is populated during
/// <see cref="DownloadService.QueryDownload" />.
/// </summary>
public class DownloadReport : IDownloadReport
{
/// <summary>
/// This class represent one event/test.
/// </summary>
public class EventInfo : IEventInfo
{
/// <summary>
/// Information about each module that was part of the event. Addressable by
/// ModuleArrayIndex of the corresponding module.
/// </summary>
public IDASModule[] Modules { get; set; }
/// <summary>
/// The event number this information is regarding.
/// </summary>
public int EventNumber { get; set; }
/// <summary>
/// The GUID of the corresponding event.
/// </summary>
public Guid TestGUID { get; set; }
/// <summary>
/// Fault flags (if any)
/// </summary>
public ushort FaultFlags { get; set; }
/// <summary>
/// fault flags extended (if any)
/// </summary>
public ushort FaultFlagsEx { get; set; }
/// <summary>
/// Arm Attempts (if any)
/// </summary>
public byte ArmAttempts { get; set; }
/// <summary>
/// The timestamp of this event.
/// </summary>
public DateTime TestTime { get; set; }
/// <summary>
/// The ID of this event.
/// </summary>
public string TestID { get; set; }
/// <summary>
/// A text description that was stored.
/// </summary>
public string Description { get; set; }
/// <summary>
/// True if this event has already been downloaded.
/// </summary>
public bool HasBeenDownloaded { get; set; }
/// <summary>
/// True if this event received a trigger.
/// </summary>
public bool WasTriggered { get; set; }
/// <summary>
/// clears any fault flags
/// </summary>
public void ClearFaults()
{
FaultFlags = 0;
FaultFlagsEx = 0;
}
public EventInfo() { }
public EventInfo(EventInfo copy)
{
Modules = copy.Modules;
EventNumber = copy.EventNumber;
TestGUID = copy.TestGUID;
FaultFlags = copy.FaultFlags;
FaultFlagsEx = copy.FaultFlagsEx;
ArmAttempts = copy.ArmAttempts;
TestTime = copy.TestTime;
TestID = copy.TestID;
Description = copy.Description;
HasBeenDownloaded = copy.HasBeenDownloaded;
WasTriggered = copy.WasTriggered;
}
}
/// <summary>
/// This class represent one event/test.
/// </summary>
public class UARTEventInfo : IUARTEventInfo
{
/// <summary>
/// From which event do we want to download data?
/// </summary>
public ushort EventNumber { get; set; }
/// <summary>
/// Is data present?
/// </summary>
public bool DataPresent { get; set; }
/// <summary>
/// Has data already been downloaded?
/// </summary>
public bool DataDownloaded { get; set; }
/// <summary>
/// How much data is there?
/// </summary>
public ulong TotalByteCount { get; set; }
/// <summary>
/// Where in the data did the trigger occur?
/// </summary>
public ulong TriggerByteCount { get; set; }
/// <summary>
/// Where are the faults?
/// </summary>
public ulong FaultByteCount { get; set; }
/// <summary>
/// When did the UART stream start?
/// </summary>
public ulong StartTimestamp { get; set; }
/// <summary>
/// When did the UART stream end?
/// </summary>
public ulong EndTimestamp { get; set; }
/// <summary>
/// What was the baud rate during UART recording?
/// </summary>
public uint BaudRate { get; set; }
}
/// <summary>
/// An array of all events stored on this DAS.
/// </summary>
public IEventInfo[] Events { get; set; }
/// <summary>
/// An array of all UART events stored on this DAS.
/// </summary>
public IUARTEventInfo[] UARTEvents { get; set; }
public static void SetEventDownloadStatus(IDASCommunication das, bool[] status, bool bStoreInDb)
{
if (null == das) { return; }
das.EventDownloadedStatus = status;
if (!bStoreInDb || !DASFactoryDb.DbWrapper.Connected) { return; }
try
{
Download.ClearExistingEventDownloadStatus(das.RecordId);
if (null == status) { return; }
foreach (var statum in status)
{
Download.InsertEventDownloadStatus(das.RecordId, statum);
}
}
catch (Exception ex)
{
APILogger.Log(ex);
}
}
public static void SetEventArmAttempts(IDASCommunication das, byte[] armAttempts, bool storeInDb)
{
if (null == das) { return; }
das.ArmAttempts = armAttempts;
if (!storeInDb || !DASFactoryDb.DbWrapper.Connected) { return; }
try
{
Download.ClearExistingEventArmAttempts(das.RecordId);
if (null == armAttempts) { return; }
foreach (byte armAttempt in armAttempts)
{
Download.InsertEventArmAttempts(das.RecordId, armAttempt);
}
}
catch (Exception ex)
{
APILogger.Log(ex);
}
}
public static void SetEventFaultFlags(IDASCommunication das, ushort[] faultFlags, bool storeInDb)
{
if (null == das) { return; }
das.FaultFlags = faultFlags;
if (!storeInDb || !DASFactoryDb.DbWrapper.Connected) { return; }
try
{
Download.ClearExistingFaultFlags(das.RecordId);
if (null == faultFlags) { return; }
foreach (var flag in faultFlags)
{
Download.InsertEventFaultFlags(das.RecordId, flag);
}
}
catch (Exception ex)
{
APILogger.Log(ex);
}
}
public static void SetEventInfo(IDASCommunication das, IDownloadReport eventInfo, bool storeInDB)
{
if (null == das) { return; }
das.EventInfo = eventInfo;
if (!storeInDB || !DASFactoryDb.DbWrapper.Connected) { return; }
try
{
Download.ClearExistingDownloadReports(das.RecordId);
if (null == eventInfo || null == eventInfo.Events) { return; }
foreach (var eventData in eventInfo.Events)
{
InsertEventInfo(das, eventData);
}
}
catch (Exception ex)
{
APILogger.Log(ex);
}
}
private static void InsertEventInfo(IDASCommunication das, IEventInfo eventInfo)
{
try
{
if (null == eventInfo || null == das)
{
return;
}
var xml = string.Empty;
using (var sw = new StringWriter())
{
using (var xw = XmlWriter.Create(sw,
new XmlWriterSettings() { ConformanceLevel = ConformanceLevel.Document }))
{
xw.WriteStartDocument();
xw.WriteStartElement("Modules");
foreach (var mod in eventInfo.Modules)
{
mod.WriteXml(xw);
}
xw.WriteEndElement();
xw.WriteEndDocument();
}
sw.Flush();
xml = sw.ToString();
}
Download.InsertEventInfo(das.RecordId,
eventInfo.EventNumber,
eventInfo.TestGUID,
eventInfo.FaultFlags,
eventInfo.ArmAttempts,
eventInfo.TestTime,
eventInfo.TestID,
eventInfo.Description,
eventInfo.HasBeenDownloaded,
eventInfo.WasTriggered,
xml
);
}
catch (Exception ex)
{
APILogger.Log(ex);
}
}
public static void SetEventGuids(IDASCommunication das, Guid[] guids, bool storeInDb)
{
if (null == das) { return; }
das.EventGuids = guids;
if (!storeInDb || !DASFactoryDb.DbWrapper.Connected) { return; }
try
{
Download.ClearExistingEventGuids(das.RecordId);
if (null == guids) { return; }
foreach (var guid in guids)
{
Download.EventGuidInsert(das.RecordId, guid);
}
}
catch (Exception ex)
{
APILogger.Log(ex);
}
}
}
}