AlarmEventLogEntry.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Text;
  3. namespace Wayne.Lib.Log
  4. {
  5. /// <summary>
  6. /// Describes an alarm generated on a device.
  7. /// </summary>
  8. public class AlarmEventLogEntry: EventLogEntry
  9. {
  10. private string alarmText = null;
  11. /// <summary>
  12. /// The alarm type id.
  13. /// </summary>
  14. public AlarmId AlarmId { get; private set; }
  15. /// <summary>
  16. /// The Station number where the alarm was generated.
  17. /// </summary>
  18. public string SiteId { get; private set; }
  19. /// <summary>
  20. /// The device id on the device that generated the alarm.
  21. /// </summary>
  22. public int DeviceId { get; private set; }
  23. /// <summary>
  24. /// The date-time the alarm was generated.
  25. /// </summary>
  26. public DateTime Created { get; private set; }
  27. /// <summary>
  28. /// The Device type.
  29. /// </summary>
  30. public AlarmDeviceType DeviceType { get; private set; }
  31. /// <summary>
  32. /// The alarm text.
  33. /// </summary>
  34. public string AlarmText
  35. {
  36. get { return alarmText ?? AlarmId.ToString(); }
  37. set { alarmText = value; }
  38. }
  39. /// <summary>
  40. /// Ctor, the default alarm text is used.
  41. /// </summary>
  42. /// <param name="alarmId">The id of the alarm</param>
  43. /// <param name="deviceType">The devicetype from where the alarm was generated.</param>
  44. /// <param name="siteId">The station number</param>
  45. /// <param name="deviceId">The device id where the alarm was generated.</param>
  46. public AlarmEventLogEntry(AlarmId alarmId, AlarmDeviceType deviceType, string siteId, int deviceId)
  47. : this(alarmId, deviceType, siteId, deviceId, null)
  48. {
  49. }
  50. /// <summary>
  51. /// Ctor
  52. /// </summary>
  53. /// <param name="alarmId">The id of the alarm</param>
  54. /// <param name="deviceType">The devicetype from where the alarm was generated.</param>
  55. /// <param name="siteId">The station number</param>
  56. /// <param name="deviceId">The device id where the alarm was generated.</param>
  57. /// <param name="alarmText">To override the default Alarm text</param>
  58. public AlarmEventLogEntry(AlarmId alarmId, AlarmDeviceType deviceType, string siteId, int deviceId, string alarmText)
  59. : base(new IdentifiableEntity(IdentifiableEntity.NoId, "AlarmEvent", string.Empty, null), alarmId)
  60. {
  61. AlarmId = alarmId;
  62. SiteId = siteId;
  63. DeviceId = deviceId;
  64. Created = DateTime.Now;
  65. DeviceType = deviceType;
  66. AlarmText = alarmText;
  67. }
  68. /// <summary>
  69. /// Used to generate Alarm Event logfiles.
  70. /// </summary>
  71. /// <param name="separator">The separator to use to seperate log entries</param>
  72. /// <returns>A formatted representation of the log entry</returns>
  73. public string GetLogLine(string separator)
  74. {
  75. var output = new StringBuilder();
  76. //output.Append(SiteId.Substring(Math.Max(0, SiteId.Length-4), 4).PadLeft(4, '0'));
  77. output.Append(Prepare(SiteId, 4, '0'));
  78. output.Append(separator);
  79. output.Append(Created.ToString("yyyy-MM-dd HH:mm:ss"));
  80. output.Append(separator);
  81. output.Append(Prepare(DeviceType, 1, '0'));
  82. output.Append(separator);
  83. output.Append(Prepare(DeviceId, 2, '0'));
  84. output.Append(separator);
  85. output.Append(Prepare(AlarmId, 4, '0'));
  86. output.Append(separator);
  87. output.Append(Prepare(AlarmText, 40, ' ', PadDirection.Right));
  88. return output.ToString();
  89. }
  90. /// <summary>
  91. /// Overrides object.ToString()
  92. /// </summary>
  93. /// <returns>A readable representation of the log entry.</returns>
  94. public override string ToString()
  95. {
  96. var sb = new StringBuilder();
  97. sb.Append("Alarm event ");
  98. sb.Append(AlarmId.ToString("D"));
  99. sb.Append(" signalled from device type ");
  100. sb.Append(DeviceType.ToString());
  101. sb.Append(", station ");
  102. sb.Append(SiteId.PadLeft(4, '0'));
  103. sb.Append(", device ");
  104. sb.Append(DeviceId.ToString("D").PadLeft(2, '0'));
  105. sb.Append(" at ");
  106. sb.Append(Created.ToString("yyyy-MM-dd HH:mm:ss"));
  107. return sb.ToString();
  108. }
  109. private enum PadDirection
  110. {
  111. Left,
  112. Right
  113. }
  114. private static string Prepare(string original, int length, char paddingCharacter, PadDirection padDirection)
  115. {
  116. return padDirection == PadDirection.Left
  117. ? original.PadLeft(length, paddingCharacter).Substring(Math.Max(0, original.Length - length))
  118. : original.PadRight(length, paddingCharacter).Substring(Math.Max(0, original.Length - length));
  119. }
  120. private static string Prepare(string original, int length, char paddingCharacter)
  121. {
  122. return Prepare(original, length, paddingCharacter, PadDirection.Left);
  123. }
  124. private static string Prepare(int original, int length, char paddingCharacter)
  125. {
  126. return Prepare(original.ToString("D"), length, paddingCharacter);
  127. }
  128. private static string Prepare(Enum original, int length, char paddingCharacter)
  129. {
  130. return Prepare(original.ToString("D"), length, paddingCharacter);
  131. }
  132. }
  133. }