Files
DP44/Common/DTS.CommonCore/.svn/pristine/9c/9c5db544946426fed0a235f9679f260b84d04ca4.svn-base

186 lines
6.5 KiB
Plaintext
Raw Permalink Normal View History

2026-04-17 14:55:32 -04:00
using System.Collections.Generic;
namespace DTS.Common.Classes.TMAT
{
/// <summary>
/// these are all the patterns that represent global keys
/// </summary>
public enum TMTGlobalKeys
{
[TMTKey("{NAME OF PROGRAM}")] NameOfProgram,
[TMTKey("{TEST ID}")] TestId,
[TMTKey("{DAS SERIAL NUMBER}")] DASSerialNumber,
[TMTKey("{DAS INDEX}")] DASIndex,
[TMTKey("{DAS SAMPLE RATE}")] DASSampleRate,
[TMTKey("{TEST TIMESTAMP}")] TestTimeStamp,
[TMTKey("{DAS BIT RATE}")] DASBitRate,
[TMTKey("{STREAM TIME FORMAT}")] StreamTimeFormat,
//FB 26736 Keys for time & data channel Id
[TMTKey("{UDP STREAM TIME CHANNEL ID}")] UdpStreamTimeChannelId,
[TMTKey("{UDP STREAM DATA CHANNEL ID}")] UdpStreamDataChannelId,
//FB 29996 Key for file creation date
[TMTKey("{CREATE DATE}")] CreateDate
}
/// <summary>
/// these are all the patterns that represent channel keys
/// </summary>
public enum TMTChannelKeys
{
[TMTKey("{{CHANNEL {0} HARDWARE CHANNEL NUMBER}}")] HardwareChannelNumber,
[TMTKey("{{CHANNEL {0} NAME}}")] ChannelName,
[TMTKey("{{CHANNEL {0} COUPLING MODE}}")] CouplingMode,
[TMTKey("{{CHANNEL {0} BRIDGE RESISTANCE}}")] BridgeResistance,
[TMTKey("{{CHANNEL {0} AAF}}")] AAF,
[TMTKey("{{CHANNEL {0} OFFSET mV}}")] OffsetMV,
[TMTKey("{{CHANNEL {0} INPUT RANGE}}")] InputRangeMV,
[TMTKey("{{CHANNEL {0} MAX RANGE EU}}")] MaxRangeEU,
[TMTKey("{{CHANNEL {0} MIN RANGE EU}}")] MinRangeEU,
[TMTKey("{{CHANNEL {0} EU}}")] EU,
[TMTKey("{{CHANNEL {0} SCALEFACTOR EU}}")] ScaleFactorEU,
[TMTKey("{{CHANNEL {0} OFFSET EU}}")] OffsetEU,
}
/// <summary>
/// Helper attribute, allows us to associate patterns to enums to make processing file easier
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Field)]
public class TMTKey : System.Attribute
{
/// <summary>
/// the pattern to look for in a line to replace
/// </summary>
public string Key { get; set; }
public TMTKey(string key)
{
Key = key;
}
/// <summary>
/// returns the pattern to look for in a line for a given property
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static string GetKey(TMTGlobalKeys o)
{
var mi = o.GetType().GetMember(o.ToString());
if (mi.Length <= 0) return string.Empty;
return GetCustomAttribute(mi[0], typeof(TMTKey)) is TMTKey attr ? attr.Key : string.Empty;
}
/// <summary>
/// returns the pattern to look for in a line for a given channel property
/// </summary>
/// <param name="o"></param>
/// <param name="channelNumber"></param>
/// <returns></returns>
public static string GetKey(TMTChannelKeys o, int channelNumber)
{
var mi = o.GetType().GetMember(o.ToString());
if (mi.Length <= 0) return string.Empty;
if (GetCustomAttribute(mi[0], typeof(TMTKey)) is TMTKey attr)
{
return string.Format(attr.Key, channelNumber);
}
else
{
return string.Empty;
}
}
}
public interface ITMTTemplate
{
/// <summary>
/// updates the fields in the file with a given value
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
void UpdateValue(TMTGlobalKeys key, string value);
/// <summary>
/// updates the files in the file with the given value
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="channelNumber"></param>
void UpdateValue(TMTChannelKeys key, string value, int channelNumber);
/// <summary>
/// returns all lines in the TMT file
/// </summary>
/// <returns></returns>
string[] GetAllLines();
}
public class TMTTemplate :ITMTTemplate
{
/// <summary>
/// reads all lines from the file in the given location and prepare to update values
/// </summary>
/// <param name="templateLocation"></param>
public TMTTemplate(string templateLocation)
{
if (System.IO.File.Exists(templateLocation))
{
_allLines.AddRange(System.IO.File.ReadAllLines(templateLocation));
}
}
/// <summary>
/// constructs a new template with the given lines of content
/// </summary>
/// <param name="lines"></param>
public TMTTemplate(string[] lines)
{
_allLines.AddRange(lines);
}
/// <summary>
/// updates the fields in the file with a given value
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void UpdateValue(TMTGlobalKeys key, string value)
{
var pattern = TMTKey.GetKey(key);
for (int i = 0; i < _allLines.Count; i++)
{
if (_allLines[i].Contains(pattern))
{
_allLines[i] = _allLines[i].Replace(pattern, value);
}
}
}
/// <summary>
/// updates the files in the file with the given value
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="channelNumber"></param>
public void UpdateValue(TMTChannelKeys key, string value, int channelNumber)
{
var pattern = TMTKey.GetKey(key, channelNumber);
for (int i = 0; i < _allLines.Count; i++)
{
if (_allLines[i].Contains(pattern))
{
_allLines[i] = _allLines[i].Replace(pattern, value);
}
}
}
/// <summary>
/// all lines in the TMT file
/// </summary>
private List<string> _allLines = new List<string>();
/// <summary>
/// returns all lines in the TMT file
/// </summary>
/// <returns></returns>
public string[] GetAllLines()
{
return _allLines.ToArray();
}
}
}