using System; using System.Text; namespace Wayne.Lib.Log { /// /// Describes an alarm generated on a device. /// public class AlarmEventLogEntry: EventLogEntry { private string alarmText = null; /// /// The alarm type id. /// public AlarmId AlarmId { get; private set; } /// /// The Station number where the alarm was generated. /// public string SiteId { get; private set; } /// /// The device id on the device that generated the alarm. /// public int DeviceId { get; private set; } /// /// The date-time the alarm was generated. /// public DateTime Created { get; private set; } /// /// The Device type. /// public AlarmDeviceType DeviceType { get; private set; } /// /// The alarm text. /// public string AlarmText { get { return alarmText ?? AlarmId.ToString(); } set { alarmText = value; } } /// /// Ctor, the default alarm text is used. /// /// The id of the alarm /// The devicetype from where the alarm was generated. /// The station number /// The device id where the alarm was generated. public AlarmEventLogEntry(AlarmId alarmId, AlarmDeviceType deviceType, string siteId, int deviceId) : this(alarmId, deviceType, siteId, deviceId, null) { } /// /// Ctor /// /// The id of the alarm /// The devicetype from where the alarm was generated. /// The station number /// The device id where the alarm was generated. /// To override the default Alarm text public AlarmEventLogEntry(AlarmId alarmId, AlarmDeviceType deviceType, string siteId, int deviceId, string alarmText) : base(new IdentifiableEntity(IdentifiableEntity.NoId, "AlarmEvent", string.Empty, null), alarmId) { AlarmId = alarmId; SiteId = siteId; DeviceId = deviceId; Created = DateTime.Now; DeviceType = deviceType; AlarmText = alarmText; } /// /// Used to generate Alarm Event logfiles. /// /// The separator to use to seperate log entries /// A formatted representation of the log entry public string GetLogLine(string separator) { var output = new StringBuilder(); //output.Append(SiteId.Substring(Math.Max(0, SiteId.Length-4), 4).PadLeft(4, '0')); output.Append(Prepare(SiteId, 4, '0')); output.Append(separator); output.Append(Created.ToString("yyyy-MM-dd HH:mm:ss")); output.Append(separator); output.Append(Prepare(DeviceType, 1, '0')); output.Append(separator); output.Append(Prepare(DeviceId, 2, '0')); output.Append(separator); output.Append(Prepare(AlarmId, 4, '0')); output.Append(separator); output.Append(Prepare(AlarmText, 40, ' ', PadDirection.Right)); return output.ToString(); } /// /// Overrides object.ToString() /// /// A readable representation of the log entry. public override string ToString() { var sb = new StringBuilder(); sb.Append("Alarm event "); sb.Append(AlarmId.ToString("D")); sb.Append(" signalled from device type "); sb.Append(DeviceType.ToString()); sb.Append(", station "); sb.Append(SiteId.PadLeft(4, '0')); sb.Append(", device "); sb.Append(DeviceId.ToString("D").PadLeft(2, '0')); sb.Append(" at "); sb.Append(Created.ToString("yyyy-MM-dd HH:mm:ss")); return sb.ToString(); } private enum PadDirection { Left, Right } private static string Prepare(string original, int length, char paddingCharacter, PadDirection padDirection) { return padDirection == PadDirection.Left ? original.PadLeft(length, paddingCharacter).Substring(Math.Max(0, original.Length - length)) : original.PadRight(length, paddingCharacter).Substring(Math.Max(0, original.Length - length)); } private static string Prepare(string original, int length, char paddingCharacter) { return Prepare(original, length, paddingCharacter, PadDirection.Left); } private static string Prepare(int original, int length, char paddingCharacter) { return Prepare(original.ToString("D"), length, paddingCharacter); } private static string Prepare(Enum original, int length, char paddingCharacter) { return Prepare(original.ToString("D"), length, paddingCharacter); } } }