Skip to content

Commit

Permalink
Merge pull request #183 from mcrossley/master
Browse files Browse the repository at this point in the history
v3.28.0
  • Loading branch information
mcrossley authored Dec 12, 2023
2 parents 627026b + 2f45739 commit 13d9ce8
Show file tree
Hide file tree
Showing 51 changed files with 3,073 additions and 1,902 deletions.
69 changes: 48 additions & 21 deletions CumulusMX/Alarm.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System;
using System.Runtime.Serialization;
using System.Threading.Tasks;

namespace CumulusMX
{
public class Alarm
{
public Cumulus cumulus { get; set; }
public readonly Cumulus cumulus;

public string Name { get; }
public string Id { get; }

public string Name { get; set; }
public virtual bool Enabled
{
get => enabled;
Expand Down Expand Up @@ -51,16 +54,18 @@ public bool Triggered
public string LastMessage { get; set; }
public int TriggerThreshold { get; set; }

AlarmTypes type;
private readonly AlarmTypes type;
private protected bool enabled;
bool triggered;
int triggerCount = 0;
DateTime triggeredTime;

public Alarm(string AlarmName, AlarmTypes AlarmType)
public Alarm(string id, AlarmTypes AlarmType, Cumulus cumul, string units = null)
{
Name = AlarmName;
Id = id;
cumulus = cumul;
type = AlarmType;
Units = units;
}

public void CheckAlarm(double value)
Expand Down Expand Up @@ -90,7 +95,7 @@ private void doTriggered(bool value)
// If we were not set before, so we need to send an email?
if (!triggered)
{
cumulus.LogMessage($"Alarm ({Name}): Triggered, value = {Value}");
cumulus.LogMessage($"Alarm ({Name}): Triggered, value = {value}, threshold = {Value}" + (string.IsNullOrEmpty(LastMessage) ? "" : $", Message = {LastMessage}"));

if (Email && cumulus.SmtpOptions.Enabled && cumulus.emailer != null)
{
Expand Down Expand Up @@ -150,15 +155,15 @@ private void doTriggered(bool value)
// We are latching, but the latch period has expired, clear the trigger
triggered = false;
triggerCount = 0;
cumulus.LogMessage($"Alarm ({Name}): Trigger cleared");
cumulus.LogMessage($"Alarm ({Name}): Trigger cleared, value = {value}");
}
}
else
{
// No latch, just clear the trigger
triggered = false;
triggerCount = 0;
cumulus.LogMessage($"Alarm ({Name}): Trigger cleared");
cumulus.LogMessage($"Alarm ({Name}): Trigger cleared, value = {value}");
}
}
}
Expand All @@ -167,8 +172,16 @@ private void doTriggered(bool value)

public class AlarmChange : Alarm
{
public AlarmChange(string AlarmName) : base(AlarmName, AlarmTypes.Change)
public string IdUp { get; }
public string IdDown { get; }

public string NameUp { get; set; }
public string NameDown { get; set; }

public AlarmChange(string idUp, string idDwn, Cumulus cumul, string units = null) : base("", AlarmTypes.Change, cumul, units)
{
IdUp = idUp;
IdDown = idDwn;
}

public override bool Enabled
Expand Down Expand Up @@ -260,7 +273,7 @@ private void doUpTriggered(bool value)
// If we were not set before, so we need to send an email etc?
if (!upTriggered)
{
cumulus.LogMessage($"Alarm ({Name}): Up triggered, value = {Value}");
cumulus.LogMessage($"Alarm ({NameUp}): Up triggered, value = {value}, threshold = {Value}" + (string.IsNullOrEmpty(LastMessage) ? "" : $", Message = {LastMessage}"));

if (Email && cumulus.SmtpOptions.Enabled && cumulus.emailer != null)
{
Expand All @@ -274,7 +287,7 @@ private void doUpTriggered(bool value)
// delay for 0, 60, 120 seconds
System.Threading.Thread.Sleep(i * 60000);

cumulus.LogMessage($"Alarm ({Name}): Sending email - attempt {i + 1}");
cumulus.LogMessage($"Alarm ({NameUp}): Sending email - attempt {i + 1}");

if (await cumulus.emailer.SendEmail(cumulus.AlarmDestEmail, cumulus.AlarmFromEmail, cumulus.Trans.AlarmEmailSubject, msg, cumulus.AlarmEmailHtml, cumulus.AlarmEmailUseBcc))
{
Expand All @@ -292,12 +305,12 @@ private void doUpTriggered(bool value)
var parser = new TokenParser(cumulus.TokenParserOnToken);
parser.InputText = ActionParams;
var args = parser.ToStringFromString();
cumulus.LogMessage($"Alarm ({Name}): Starting external program: '{Action}', with parameters: {args}");
cumulus.LogMessage($"Alarm ({NameUp}): Starting external program: '{Action}', with parameters: {args}");
Utils.RunExternalTask(Action, args, false);
}
catch (Exception ex)
{
cumulus.LogErrorMessage($"Alarm: Error executing external program '{Action}': {ex.Message}");
cumulus.LogErrorMessage($"Alarm ({NameUp}): Error executing external program '{Action}': {ex.Message}");
}
}
}
Expand All @@ -315,14 +328,14 @@ private void doUpTriggered(bool value)
{
// We are latching, but the latch period has expired, clear the trigger
upTriggered = false;
cumulus.LogMessage($"Alarm ({Name}): Up trigger cleared");
cumulus.LogMessage($"Alarm ({NameUp}): Up trigger cleared, value = {value}");
}
}
else
{
// No latch, just clear the trigger
upTriggered = false;
cumulus.LogMessage($"Alarm ({Name}): Up trigger cleared");
cumulus.LogMessage($"Alarm ({NameUp}): Up trigger cleared, value = {value}");
}
}
}
Expand All @@ -334,7 +347,7 @@ private void doDownTriggered(bool value)
// If we were not set before, so we need to send an email?
if (!downTriggered && Enabled)
{
cumulus.LogMessage($"Alarm ({Name}): Down triggered, value = {Value}");
cumulus.LogMessage($"Alarm ({NameDown}): Down triggered, value = {value}, threshold = {Value}" + (string.IsNullOrEmpty(LastMessage) ? "" : $", Message = {LastMessage}"));

if (Email && cumulus.SmtpOptions.Enabled && cumulus.emailer != null)
{
Expand All @@ -348,7 +361,7 @@ private void doDownTriggered(bool value)
// delay for 0, 60, 120 seconds
System.Threading.Thread.Sleep(i * 60000);

cumulus.LogMessage($"Alarm ({Name}): Sending email - attempt {i + 1}");
cumulus.LogMessage($"Alarm ({NameDown}): Sending email - attempt {i + 1}");

if (await cumulus.emailer.SendEmail(cumulus.AlarmDestEmail, cumulus.AlarmFromEmail, cumulus.Trans.AlarmEmailSubject, msg, cumulus.AlarmEmailHtml, cumulus.AlarmEmailUseBcc))
{
Expand All @@ -366,12 +379,12 @@ private void doDownTriggered(bool value)
var parser = new TokenParser(cumulus.TokenParserOnToken);
parser.InputText = ActionParams;
var args = parser.ToStringFromString();
cumulus.LogMessage($"Alarm ({Name}): Starting external program: '{Action}', with parameters: {args}");
cumulus.LogMessage($"Alarm ({NameDown}): Starting external program: '{Action}', with parameters: {args}");
Utils.RunExternalTask(Action, args, false);
}
catch (Exception ex)
{
cumulus.LogErrorMessage($"Alarm: Error executing external program '{Action}': {ex.Message}");
cumulus.LogErrorMessage($"Alarm ({NameDown}): Error executing external program '{Action}': {ex.Message}");
}
}
}
Expand All @@ -389,14 +402,14 @@ private void doDownTriggered(bool value)
{
// We are latching, but the latch period has expired, clear the trigger
downTriggered = false;
cumulus.LogMessage($"Alarm ({Name}): Down trigger cleared");
cumulus.LogMessage($"Alarm ({NameDown}): Down trigger cleared, value = {value}");
}
}
else
{
// No latch, just clear the trigger
downTriggered = false;
cumulus.LogMessage($"Alarm ({Name}): Down trigger cleared");
cumulus.LogMessage($"Alarm ({NameDown}): Down trigger cleared, value = {value}");
}
}
}
Expand All @@ -412,4 +425,18 @@ public enum AlarmTypes
Change,
Trigger
}

[DataContract]
public class DashboardAlarms
{
public DashboardAlarms(string Id, bool Triggered)
{
id = Id;
triggered = Triggered;
}
[DataMember]
public string id { get; set; }
[DataMember]
public bool triggered { get; set; }
}
}
Loading

0 comments on commit 13d9ce8

Please sign in to comment.