Skip to content

Commit

Permalink
Merge pull request #179 from mcrossley/master
Browse files Browse the repository at this point in the history
v3.26.0 merge
  • Loading branch information
mcrossley authored Aug 9, 2023
2 parents e8015e7 + 7740e57 commit 71d9750
Show file tree
Hide file tree
Showing 22 changed files with 1,654 additions and 1,262 deletions.
27 changes: 21 additions & 6 deletions CumulusMX/Alarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public bool Triggered
public double LatchHours { get; set; }
public string EmailMsg { get; set; }
public string Units { get; set; }
public string LastError { get; set; }
public string LastMessage { get; set; }
public int TriggerThreshold { get; set; }

AlarmTypes type;
Expand All @@ -72,6 +72,12 @@ public void CheckAlarm(double value)
}
}

public void ClearAlarm()
{
if (Latch && triggered && DateTime.Now > triggeredTime.AddHours(LatchHours))
doTriggered(false);
}

private void doTriggered(bool value)
{
if (value)
Expand All @@ -91,9 +97,9 @@ private void doTriggered(bool value)
{
// Construct the message - preamble, plus values
var msg = cumulus.Trans.AlarmEmailPreamble + "\r\n" + string.Format(EmailMsg, Value, Units);
if (!string.IsNullOrEmpty(LastError))
if (!string.IsNullOrEmpty(LastMessage))
{
msg += "\r\nLast error: " + LastError;
msg += "\r\nLast message: " + LastMessage;
}
_ = Task.Run(async () =>
{
Expand All @@ -105,7 +111,7 @@ private void doTriggered(bool value)

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

if (await cumulus.emailer.SendEmail(cumulus.AlarmDestEmail, cumulus.AlarmFromEmail, cumulus.Trans.AlarmEmailSubject, msg, cumulus.AlarmEmailHtml))
if (await cumulus.emailer.SendEmail(cumulus.AlarmDestEmail, cumulus.AlarmFromEmail, cumulus.Trans.AlarmEmailSubject, msg, cumulus.AlarmEmailHtml, cumulus.AlarmEmailUseBcc))
{
break;
}
Expand Down Expand Up @@ -239,6 +245,15 @@ public bool DownTriggered
}
}

public new void ClearAlarm()
{
if (Latch && upTriggered && DateTime.Now > UpTriggeredTime.AddHours(LatchHours))
doUpTriggered(false);

if (Latch && downTriggered && DateTime.Now > DownTriggeredTime.AddHours(LatchHours))
doDownTriggered(false);
}

private void doUpTriggered(bool value)
{
if (value)
Expand All @@ -262,7 +277,7 @@ private void doUpTriggered(bool value)

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

if (await cumulus.emailer.SendEmail(cumulus.AlarmDestEmail, cumulus.AlarmFromEmail, cumulus.Trans.AlarmEmailSubject, msg, cumulus.AlarmEmailHtml))
if (await cumulus.emailer.SendEmail(cumulus.AlarmDestEmail, cumulus.AlarmFromEmail, cumulus.Trans.AlarmEmailSubject, msg, cumulus.AlarmEmailHtml, cumulus.AlarmEmailUseBcc))
{
break;
}
Expand Down Expand Up @@ -336,7 +351,7 @@ private void doDownTriggered(bool value)

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

if (await cumulus.emailer.SendEmail(cumulus.AlarmDestEmail, cumulus.AlarmFromEmail, cumulus.Trans.AlarmEmailSubject, msg, cumulus.AlarmEmailHtml))
if (await cumulus.emailer.SendEmail(cumulus.AlarmDestEmail, cumulus.AlarmFromEmail, cumulus.Trans.AlarmEmailSubject, msg, cumulus.AlarmEmailHtml, cumulus.AlarmEmailUseBcc))
{
break;
}
Expand Down
99 changes: 77 additions & 22 deletions CumulusMX/AlarmSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,19 @@ public string GetAlarmSettings()
Action = cumulus.SensorAlarm.Action,
ActionParams = cumulus.SensorAlarm.ActionParams
},
newRecord = new JsonAlarmValues()
{
Enabled = cumulus.NewRecordAlarm.Enabled,
SoundEnabled = cumulus.NewRecordAlarm.Sound,
Sound = cumulus.NewRecordAlarm.SoundFile,
Notify = cumulus.NewRecordAlarm.Notify,
Email = cumulus.NewRecordAlarm.Email,
Latches = cumulus.NewRecordAlarm.Latch,
LatchHrs = cumulus.NewRecordAlarm.LatchHours,
Threshold = cumulus.NewRecordAlarm.TriggerThreshold,
Action = cumulus.NewRecordAlarm.Action,
ActionParams = cumulus.NewRecordAlarm.ActionParams
},
dataStopped = new JsonAlarmValues()
{
Enabled = cumulus.DataStoppedAlarm.Enabled,
Expand Down Expand Up @@ -238,16 +251,16 @@ public string GetAlarmSettings()
},
httpUpload = new JsonAlarmValues()
{
Enabled = cumulus.HttpUploadAlarm.Enabled,
SoundEnabled = cumulus.HttpUploadAlarm.Sound,
Sound = cumulus.HttpUploadAlarm.SoundFile,
Notify = cumulus.HttpUploadAlarm.Notify,
Email = cumulus.HttpUploadAlarm.Email,
Latches = cumulus.HttpUploadAlarm.Latch,
LatchHrs = cumulus.HttpUploadAlarm.LatchHours,
Threshold = cumulus.HttpUploadAlarm.TriggerThreshold,
Action = cumulus.HttpUploadAlarm.Action,
ActionParams = cumulus.HttpUploadAlarm.ActionParams
Enabled = cumulus.ThirdPartyAlarm.Enabled,
SoundEnabled = cumulus.ThirdPartyAlarm.Sound,
Sound = cumulus.ThirdPartyAlarm.SoundFile,
Notify = cumulus.ThirdPartyAlarm.Notify,
Email = cumulus.ThirdPartyAlarm.Email,
Latches = cumulus.ThirdPartyAlarm.Latch,
LatchHrs = cumulus.ThirdPartyAlarm.LatchHours,
Threshold = cumulus.ThirdPartyAlarm.TriggerThreshold,
Action = cumulus.ThirdPartyAlarm.Action,
ActionParams = cumulus.ThirdPartyAlarm.ActionParams
},
mySqlUpload = new JsonAlarmValues()
{
Expand All @@ -261,14 +274,28 @@ public string GetAlarmSettings()
Threshold = cumulus.MySqlUploadAlarm.TriggerThreshold,
Action = cumulus.MySqlUploadAlarm.Action,
ActionParams = cumulus.MySqlUploadAlarm.ActionParams
},
ftpUpload = new JsonAlarmValues()
{
Enabled = cumulus.FtpAlarm.Enabled,
SoundEnabled = cumulus.FtpAlarm.Sound,
Sound = cumulus.FtpAlarm.SoundFile,
Notify = cumulus.FtpAlarm.Notify,
Email = cumulus.FtpAlarm.Email,
Latches = cumulus.FtpAlarm.Latch,
LatchHrs = cumulus.FtpAlarm.LatchHours,
Threshold = cumulus.FtpAlarm.TriggerThreshold,
Action = cumulus.FtpAlarm.Action,
ActionParams = cumulus.FtpAlarm.ActionParams
}
};

var email = new JsonAlarmEmail()
{
fromEmail = cumulus.AlarmFromEmail,
destEmail = cumulus.AlarmDestEmail.Join(";"),
useHtml = cumulus.AlarmEmailHtml
useHtml = cumulus.AlarmEmailHtml,
useBcc = cumulus.AlarmEmailUseBcc
};

var retObject = new JsonAlarmSettings()
Expand Down Expand Up @@ -448,6 +475,18 @@ public string UpdateAlarmSettings(IHttpContext context)
cumulus.HighWindAlarm.Action = settings.windAbove.Action.Trim();
cumulus.HighWindAlarm.ActionParams = settings.windAbove.ActionParams.Trim();

cumulus.NewRecordAlarm.Enabled = settings.newRecord.Enabled;
cumulus.NewRecordAlarm.Sound = settings.newRecord.SoundEnabled;
cumulus.NewRecordAlarm.SoundFile = settings.newRecord.Sound.Trim();
cumulus.NewRecordAlarm.Notify = settings.newRecord.Notify;
cumulus.NewRecordAlarm.Email = settings.newRecord.Email;
cumulus.NewRecordAlarm.Latch = settings.newRecord.Latches;
cumulus.NewRecordAlarm.LatchHours = settings.newRecord.LatchHrs;
cumulus.NewRecordAlarm.TriggerThreshold = settings.newRecord.Threshold;
emailRequired = emailRequired || (cumulus.NewRecordAlarm.Email && cumulus.NewRecordAlarm.Enabled);
cumulus.NewRecordAlarm.Action = settings.newRecord.Action.Trim();
cumulus.NewRecordAlarm.ActionParams = settings.newRecord.ActionParams.Trim();

cumulus.SensorAlarm.Enabled = settings.contactLost.Enabled;
cumulus.SensorAlarm.Sound = settings.contactLost.SoundEnabled;
cumulus.SensorAlarm.SoundFile = settings.contactLost.Sound.Trim();
Expand Down Expand Up @@ -507,17 +546,17 @@ public string UpdateAlarmSettings(IHttpContext context)
cumulus.UpgradeAlarm.Action = settings.upgrade.Action.Trim();
cumulus.UpgradeAlarm.ActionParams = settings.upgrade.ActionParams.Trim();

cumulus.HttpUploadAlarm.Enabled = settings.httpUpload.Enabled;
cumulus.HttpUploadAlarm.Sound = settings.httpUpload.SoundEnabled;
cumulus.HttpUploadAlarm.SoundFile = settings.httpUpload.Sound.Trim();
cumulus.HttpUploadAlarm.Notify = settings.httpUpload.Notify;
cumulus.HttpUploadAlarm.Email = settings.httpUpload.Email;
cumulus.HttpUploadAlarm.Latch = settings.httpUpload.Latches;
cumulus.HttpUploadAlarm.LatchHours = settings.httpUpload.LatchHrs;
cumulus.HttpUploadAlarm.TriggerThreshold = settings.httpUpload.Threshold;
emailRequired = emailRequired || (cumulus.HttpUploadAlarm.Email && cumulus.HttpUploadAlarm.Enabled);
cumulus.HttpUploadAlarm.Action = settings.httpUpload.Action.Trim();
cumulus.HttpUploadAlarm.ActionParams = settings.httpUpload.ActionParams.Trim();
cumulus.ThirdPartyAlarm.Enabled = settings.httpUpload.Enabled;
cumulus.ThirdPartyAlarm.Sound = settings.httpUpload.SoundEnabled;
cumulus.ThirdPartyAlarm.SoundFile = settings.httpUpload.Sound.Trim();
cumulus.ThirdPartyAlarm.Notify = settings.httpUpload.Notify;
cumulus.ThirdPartyAlarm.Email = settings.httpUpload.Email;
cumulus.ThirdPartyAlarm.Latch = settings.httpUpload.Latches;
cumulus.ThirdPartyAlarm.LatchHours = settings.httpUpload.LatchHrs;
cumulus.ThirdPartyAlarm.TriggerThreshold = settings.httpUpload.Threshold;
emailRequired = emailRequired || (cumulus.ThirdPartyAlarm.Email && cumulus.ThirdPartyAlarm.Enabled);
cumulus.ThirdPartyAlarm.Action = settings.httpUpload.Action.Trim();
cumulus.ThirdPartyAlarm.ActionParams = settings.httpUpload.ActionParams.Trim();

cumulus.MySqlUploadAlarm.Enabled = settings.mySqlUpload.Enabled;
cumulus.MySqlUploadAlarm.Sound = settings.mySqlUpload.SoundEnabled;
Expand All @@ -531,6 +570,18 @@ public string UpdateAlarmSettings(IHttpContext context)
cumulus.MySqlUploadAlarm.Action = settings.mySqlUpload.Action.Trim();
cumulus.MySqlUploadAlarm.ActionParams = settings.mySqlUpload.ActionParams.Trim();

cumulus.FtpAlarm.Enabled = settings.ftpUpload.Enabled;
cumulus.FtpAlarm.Sound = settings.ftpUpload.SoundEnabled;
cumulus.FtpAlarm.SoundFile = settings.ftpUpload.Sound.Trim();
cumulus.FtpAlarm.Notify = settings.ftpUpload.Notify;
cumulus.FtpAlarm.Email = settings.ftpUpload.Email;
cumulus.FtpAlarm.Latch = settings.ftpUpload.Latches;
cumulus.FtpAlarm.LatchHours = settings.ftpUpload.LatchHrs;
cumulus.FtpAlarm.TriggerThreshold = settings.ftpUpload.Threshold;
emailRequired = emailRequired || (cumulus.FtpAlarm.Email && cumulus.FtpAlarm.Enabled);
cumulus.FtpAlarm.Action = settings.ftpUpload.Action.Trim();
cumulus.FtpAlarm.ActionParams = settings.ftpUpload.ActionParams.Trim();

// validate the from email
if (emailRequired && !EmailSender.CheckEmailAddress(result.email.fromEmail.Trim()))
{
Expand Down Expand Up @@ -559,6 +610,7 @@ public string UpdateAlarmSettings(IHttpContext context)
}
cumulus.AlarmDestEmail = emails;
cumulus.AlarmEmailHtml = result.email.useHtml;
cumulus.AlarmEmailUseBcc = result.email.useBcc;

// Save the settings
cumulus.WriteIniFile();
Expand Down Expand Up @@ -648,6 +700,7 @@ public class JsonAlarmSettingsData
public JsonAlarmValues rainRateAbove { get; set; }
public JsonAlarmValues gustAbove { get; set; }
public JsonAlarmValues windAbove { get; set; }
public JsonAlarmValues newRecord { get; set; }
public JsonAlarmValues contactLost { get; set; }
public JsonAlarmValues dataStopped { get; set; }
public JsonAlarmValues batteryLow { get; set; }
Expand All @@ -656,6 +709,7 @@ public class JsonAlarmSettingsData
public JsonAlarmValues httpUpload { get; set; }
public JsonAlarmValues mySqlUpload { get; set; }
public JsonAlarmValues isRaining { get; set; }
public JsonAlarmValues ftpUpload { get; set; }
}

public class JsonAlarmValues
Expand All @@ -678,6 +732,7 @@ public class JsonAlarmEmail
public string fromEmail { get; set; }
public string destEmail { get; set; }
public bool useHtml { get; set; }
public bool useBcc { get; set; }
}

public class JsonAlarmUnits
Expand Down
20 changes: 10 additions & 10 deletions CumulusMX/AstroLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace CumulusMX
{
internal class AstroLib
{
public static double BrasSolar(double el, double erv, double nfac)
public static int BrasSolar(double el, double erv, double nfac)
{
// el solar elevation deg from horizon
// erv distance from earth to sun in AU
Expand All @@ -25,10 +25,10 @@ public static double BrasSolar(double el, double erv, double nfac)
double al = 0.128 - (0.054 * Math.Log(m) / Math.Log(10));

// clear-sky solar radiation at earth surface on horizontal surface (W/m^2)
return i0 * Math.Exp(-nfac * al * m);
return (int) Math.Round(i0 * Math.Exp(-nfac * al * m));
}

public static double RyanStolzSolar(double el, double erv, double atc, double z)
public static int RyanStolzSolar(double el, double erv, double atc, double z)
{
// el solar elevation deg from horizon
// erv distance from earth to sun in AU
Expand All @@ -47,7 +47,7 @@ public static double RyanStolzSolar(double el, double erv, double atc, double z)

double rsToa = 1360 * sinal / (erv * erv); // RS on the top of atmosphere

return rsToa * Math.Pow(atc, rm); //RS on the ground
return (int) Math.Round(rsToa * Math.Pow(atc, rm)); //RS on the ground
}

private static double DegToRad(double angle)
Expand All @@ -60,8 +60,8 @@ private static double RadToDeg(double angle)
return angle * (180.0 / Math.PI);
}

public static double SolarMax(DateTime timestamp, double longitude, double latitude, double altitude,
out double solarelevation, SolarOptions options)
public static int SolarMax(DateTime timestamp, double longitude, double latitude, double altitude,
out double solarelevation, SolarOptions options)
{
double factor = 0;
if (options.SolarCalc == 0)
Expand All @@ -76,7 +76,7 @@ public static double SolarMax(DateTime timestamp, double longitude, double latit
}


private static double SolarMax(DateTime timestamp, double longitude, double latitude, double altitude,
private static int SolarMax(DateTime timestamp, double longitude, double latitude, double altitude,
out double solarelevation, double factor, int method)
{
DateTime utctime = timestamp.ToUniversalTime();
Expand Down Expand Up @@ -504,7 +504,7 @@ private static double calcObliquityCorrection(double t)
double e0 = calcMeanObliquityOfEcliptic(t);
double omega = 125.04 - 1934.136 * t;
return e0 + 0.00256 * Math.Cos(DegToRad(omega));
}
Expand Down Expand Up @@ -543,7 +543,7 @@ private static void CalculateSunPosition(DateTime dateTime, double latitude, dou
//var solarNoonLst = (720.0 - 4 * longitude - eqOfTime + zone * 60.0) / 1440.0;
//var sunriseTimeLst = solarNoonLst - haSunRise * 4 / 1440.0;
//var sunsetTimeLst = solarNoonLst + haSunRise * 4 / 1440.0;
//var sunlightDurationMins = 8 * haSunRise;
//var sunlightDurationMins = 8 * haSunRise;
var trueSolarTime = PutInRange(dateTime.TimeOfDay.TotalMinutes + eqOfTime + 4 * longitude - 60 * zone, 1440);
var hourAngle = trueSolarTime / 4.0 < 0 ? trueSolarTime / 4.0 + 180 : trueSolarTime / 4.0 - 180;
var solarZenithAngle = RadToDeg(Math.Acos(Math.Sin(DegToRad(latitude)) * Math.Sin(DegToRad(sunDec)) + Math.Cos(DegToRad(latitude)) * Math.Cos(DegToRad(sunDec)) * Math.Cos(DegToRad(hourAngle))));
Expand Down Expand Up @@ -636,7 +636,7 @@ public static void CalculateSunPosition(DateTime dateTime, double latitude, doub
Math.Sin(latitude * Deg2Rad) *
Math.Sin(declination) +
Math.Cos(latitude * Deg2Rad) *
Math.Cos(declination) *
Math.Cos(declination) *
Math.Cos(hourAngle)
);
Expand Down
Loading

0 comments on commit 71d9750

Please sign in to comment.