using System.Collections.Generic;
namespace DTS.Common.Classes.TMAT
{
///
/// these are all the patterns that represent global keys
///
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
}
///
/// these are all the patterns that represent channel keys
///
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,
}
///
/// Helper attribute, allows us to associate patterns to enums to make processing file easier
///
[System.AttributeUsage(System.AttributeTargets.Field)]
public class TMTKey : System.Attribute
{
///
/// the pattern to look for in a line to replace
///
public string Key { get; set; }
public TMTKey(string key)
{
Key = key;
}
///
/// returns the pattern to look for in a line for a given property
///
///
///
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;
}
///
/// returns the pattern to look for in a line for a given channel property
///
///
///
///
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
{
///
/// updates the fields in the file with a given value
///
///
///
void UpdateValue(TMTGlobalKeys key, string value);
///
/// updates the files in the file with the given value
///
///
///
///
void UpdateValue(TMTChannelKeys key, string value, int channelNumber);
///
/// returns all lines in the TMT file
///
///
string[] GetAllLines();
}
public class TMTTemplate :ITMTTemplate
{
///
/// reads all lines from the file in the given location and prepare to update values
///
///
public TMTTemplate(string templateLocation)
{
if (System.IO.File.Exists(templateLocation))
{
_allLines.AddRange(System.IO.File.ReadAllLines(templateLocation));
}
}
///
/// constructs a new template with the given lines of content
///
///
public TMTTemplate(string[] lines)
{
_allLines.AddRange(lines);
}
///
/// updates the fields in the file with a given value
///
///
///
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);
}
}
}
///
/// updates the files in the file with the given value
///
///
///
///
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);
}
}
}
///
/// all lines in the TMT file
///
private List _allLines = new List();
///
/// returns all lines in the TMT file
///
///
public string[] GetAllLines()
{
return _allLines.ToArray();
}
}
}