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 { /// /// This class represent all events stored on the DAS. It is populated during /// . /// public class DownloadReport : IDownloadReport { /// /// This class represent one event/test. /// public class EventInfo : IEventInfo { /// /// Information about each module that was part of the event. Addressable by /// ModuleArrayIndex of the corresponding module. /// public IDASModule[] Modules { get; set; } /// /// The event number this information is regarding. /// public int EventNumber { get; set; } /// /// The GUID of the corresponding event. /// public Guid TestGUID { get; set; } /// /// Fault flags (if any) /// public ushort FaultFlags { get; set; } /// /// fault flags extended (if any) /// public ushort FaultFlagsEx { get; set; } /// /// Arm Attempts (if any) /// public byte ArmAttempts { get; set; } /// /// The timestamp of this event. /// public DateTime TestTime { get; set; } /// /// The ID of this event. /// public string TestID { get; set; } /// /// A text description that was stored. /// public string Description { get; set; } /// /// True if this event has already been downloaded. /// public bool HasBeenDownloaded { get; set; } /// /// True if this event received a trigger. /// public bool WasTriggered { get; set; } /// /// clears any fault flags /// 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; } } /// /// This class represent one event/test. /// public class UARTEventInfo : IUARTEventInfo { /// /// From which event do we want to download data? /// public ushort EventNumber { get; set; } /// /// Is data present? /// public bool DataPresent { get; set; } /// /// Has data already been downloaded? /// public bool DataDownloaded { get; set; } /// /// How much data is there? /// public ulong TotalByteCount { get; set; } /// /// Where in the data did the trigger occur? /// public ulong TriggerByteCount { get; set; } /// /// Where are the faults? /// public ulong FaultByteCount { get; set; } /// /// When did the UART stream start? /// public ulong StartTimestamp { get; set; } /// /// When did the UART stream end? /// public ulong EndTimestamp { get; set; } /// /// What was the baud rate during UART recording? /// public uint BaudRate { get; set; } } /// /// An array of all events stored on this DAS. /// public IEventInfo[] Events { get; set; } /// /// An array of all UART events stored on this DAS. /// 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); } } } }