-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathEmailNotifier.cs
425 lines (368 loc) · 17.7 KB
/
EmailNotifier.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//******************************************************************************************************
// EmailNotifier.cs - Gbtc
//
// Copyright © 2016, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
// file except in compliance with the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 05/16/2016 - J. Ritchie Carroll
// Generated original version of source code.
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using GSF;
using GSF.Net.Smtp;
using GSF.TimeSeries;
using GSF.TimeSeries.Adapters;
namespace DynamicCalculator
{
/// <summary>
/// The EmailNotifier is an action adapter which takes multiple input measurements and defines
/// a boolean expression such that when the expression is true an e-mail is triggered.
/// </summary>
[Description("E-Mail Notifier: Sends an e-mail based on a custom boolean expression")]
public class EmailNotifier : DynamicCalculator
{
#region [ Members ]
// Constants
private const string DefaultExpressionText = "x > 0";
private const int DefaultFramesPerSecond = 30;
private const double DefaultLagTime = 5.0D;
private const double DefaultLeadTime = 5.0D;
private const bool DefaultMultiTriggerPrevention = false;
// Fields
private readonly Mail m_mailClient;
private long m_expressionSuccesses;
private long m_expressionFailures;
private long m_totalEmailOperations;
private bool m_triggerDetected;
#endregion
#region [ Constructors ]
/// <summary>
/// Creates a new instance of the <see cref="EmailNotifier"/>.
/// </summary>
public EmailNotifier() =>
m_mailClient = new Mail();
#endregion
#region [ Properties ]
/// <summary>
/// Gets or sets flag that determines if the trigger continuously being met will send multiple emails or should be prevented.
/// </summary>
[ConnectionStringParameter]
[Description("Defines flag that determines if the trigger continuously being met will send multiple emails or should be prevented.")]
[DefaultValue(DefaultMultiTriggerPrevention)]
public bool MultiTriggerPrevention
{
get;
set;
}
/// <summary>
/// Gets or sets the textual representation of the boolean expression.
/// </summary>
[ConnectionStringParameter]
[Description("Define the boolean expression used to determine if an e-mail should be sent.")]
[DefaultValue(DefaultExpressionText)]
public new string ExpressionText // Redeclared to provide a more relevant description and example value for this adapter
{
get => base.ExpressionText;
set => base.ExpressionText = value;
}
/// <summary>
/// Gets or sets the e-mail address of the message sender.
/// </summary>
/// <exception cref="ArgumentNullException">Value being assigned is a null or empty string.</exception>
[ConnectionStringParameter]
[Description("Define the e-mail address of the message sender.")]
public string From
{
get => m_mailClient.From;
set => m_mailClient.From = value;
}
/// <summary>
/// Gets or sets the comma-separated or semicolon-separated e-mail address list of the message recipients.
/// </summary>
/// <exception cref="ArgumentNullException">Value being assigned is a null or empty string.</exception>
[ConnectionStringParameter]
[Description("Define the comma-separated or semicolon-separated e-mail address list of the e-mail message recipients.")]
[DefaultValue("")]
public string ToRecipients
{
get => m_mailClient.ToRecipients;
set => m_mailClient.ToRecipients = value;
}
/// <summary>
/// Gets or sets the comma-separated or semicolon-separated e-mail address list of the message carbon copy (CC) recipients.
/// </summary>
[ConnectionStringParameter]
[Description("Define the comma-separated or semicolon-separated e-mail address list of the e-mail message carbon copy (CC) recipients.")]
[DefaultValue("")]
public string CcRecipients
{
get => m_mailClient.CcRecipients;
set => m_mailClient.CcRecipients = value;
}
/// <summary>
/// Gets or sets the comma-separated or semicolon-separated e-mail address list of the message blank carbon copy (BCC) recipients.
/// </summary>
[ConnectionStringParameter]
[Description("Define the comma-separated or semicolon-separated e-mail address list of the e-mail message blank carbon copy (BCC) recipients.")]
[DefaultValue("")]
public string BccRecipients
{
get => m_mailClient.BccRecipients;
set => m_mailClient.BccRecipients = value;
}
/// <summary>
/// Gets or sets the subject of the message.
/// </summary>
[ConnectionStringParameter]
[Description("Define the subject of the e-mail message.")]
public string Subject
{
get => m_mailClient.Subject;
set => m_mailClient.Subject = value;
}
/// <summary>
/// Gets or sets the body of the message.
/// </summary>
[ConnectionStringParameter]
[Description("Define the body of the e-mail message.")]
public string Body { get; set; }
/// <summary>
/// Gets or sets the name or IP address of the SMTP server to be used for sending the message.
/// </summary>
/// <exception cref="ArgumentNullException">Value being assigned is a null or empty string.</exception>
[ConnectionStringParameter]
[Description("Define the name or IP address of the SMTP server to be used for sending the e-mail message.")]
public string SmtpServer
{
get => m_mailClient.SmtpServer;
set => m_mailClient.SmtpServer = value;
}
/// <summary>
/// Gets or sets a boolean value that indicating whether the message body is to be formatted as HTML.
/// </summary>
[ConnectionStringParameter]
[Description("Define the boolean value that indicating whether the message body is to be formatted as HTML.")]
[DefaultValue(false)]
public bool IsBodyHtml
{
get => m_mailClient.IsBodyHtml;
set => m_mailClient.IsBodyHtml = value;
}
/// <summary>
/// Gets or sets the username used to authenticate to the SMTP server.
/// </summary>
[ConnectionStringParameter]
[Description("Define the username used to authenticate to the SMTP server.")]
[DefaultValue("")]
public string Username
{
get => m_mailClient.Username;
set => m_mailClient.Username = value;
}
/// <summary>
/// Gets or sets the password used to authenticate to the SMTP server.
/// </summary>
[ConnectionStringParameter]
[Description("Define the password used to authenticate to the SMTP server.")]
[DefaultValue("")]
public string Password
{
get => m_mailClient.Password;
set => m_mailClient.Password = value;
}
/// <summary>
/// Gets or sets the flag that determines whether to use SSL when communicating with the SMTP server.
/// </summary>
[ConnectionStringParameter]
[Description("Define the flag that determines whether to use SSL when communicating with the SMTP server.")]
[DefaultValue(false)]
public bool EnableSSL
{
get => m_mailClient.EnableSSL;
set => m_mailClient.EnableSSL = value;
}
/// <summary>
/// Gets or sets the number of frames per second.
/// </summary>
/// <remarks>
/// Valid frame rates for a <see cref="ConcentratorBase"/> are greater than 0 frames per second.
/// </remarks>
[ConnectionStringParameter]
[Description("Defines the number of frames per second expected by the adapter.")]
[DefaultValue(DefaultFramesPerSecond)]
public new int FramesPerSecond // Redeclared to provide a default value since property is not commonly used
{
get => base.FramesPerSecond;
set => base.FramesPerSecond = value;
}
/// <summary>
/// Gets or sets the allowed past time deviation tolerance, in seconds (can be sub-second).
/// </summary>
/// <remarks>
/// <para>Defines the time sensitivity to past measurement timestamps.</para>
/// <para>The number of seconds allowed before assuming a measurement timestamp is too old.</para>
/// <para>This becomes the amount of delay introduced by the concentrator to allow time for data to flow into the system.</para>
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">LagTime must be greater than zero, but it can be less than one.</exception>
[ConnectionStringParameter]
[Description("Defines the allowed past time deviation tolerance, in seconds (can be sub-second).")]
[DefaultValue(DefaultLagTime)]
public new double LagTime // Redeclared to provide a default value since property is not commonly used
{
get => base.LagTime;
set => base.LagTime = value;
}
/// <summary>
/// Gets or sets the allowed future time deviation tolerance, in seconds (can be sub-second).
/// </summary>
/// <remarks>
/// <para>Defines the time sensitivity to future measurement timestamps.</para>
/// <para>The number of seconds allowed before assuming a measurement timestamp is too advanced.</para>
/// <para>This becomes the tolerated +/- accuracy of the local clock to real-time.</para>
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">LeadTime must be greater than zero, but it can be less than one.</exception>
[ConnectionStringParameter]
[Description("Defines the allowed future time deviation tolerance, in seconds (can be sub-second).")]
[DefaultValue(DefaultLeadTime)]
public new double LeadTime // Redeclared to provide a default value since property is not commonly used
{
get => base.LeadTime;
set => base.LeadTime = value;
}
#region [ Hidden Properties ]
/// <summary>
/// Gets or sets output measurements that the action adapter will produce, if any.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public override IMeasurement[] OutputMeasurements // Redeclared to hide property - not relevant to this adapter
{
get => base.OutputMeasurements;
set => base.OutputMeasurements = value;
}
/// <summary>
/// Gets or sets the source of the timestamps of the calculated values.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public new TimestampSource TimestampSource // Redeclared to hide property - not relevant to this adapter
{
get => base.TimestampSource;
set => base.TimestampSource = value;
}
#endregion
/// <summary>
/// Gets flag that determines if the implementation of the <see cref="DynamicCalculator"/> requires an output measurement.
/// </summary>
protected override bool ExpectsOutputMeasurement => false;
/// <summary>
/// Returns the detailed status of the data input source.
/// </summary>
public override string Status
{
get
{
StringBuilder status = new();
status.Append(base.Status);
status.AppendLine();
status.AppendLine($" Expression Successes: {m_expressionSuccesses:N0}");
status.AppendLine($" Expression Failures: {m_expressionFailures:N0}");
status.AppendLine($" Total E-mail Operations: {m_totalEmailOperations:N0}");
return status.ToString();
}
}
#endregion
#region [ Methods ]
/// <summary>
/// Initializes <see cref="EmailNotifier"/>.
/// </summary>
public override void Initialize()
{
const string MissingRequiredMailSetting = "Missing required e-mail setting: \"{0}\"";
Dictionary<string, string> settings = Settings;
if (!settings.TryGetValue(nameof(ExpressionText), out _))
settings[nameof(ExpressionText)] = DefaultExpressionText;
if (!settings.TryGetValue(nameof(FramesPerSecond), out _))
settings[nameof(FramesPerSecond)] = DefaultFramesPerSecond.ToString();
if (!settings.TryGetValue(nameof(LagTime), out _))
settings[nameof(LagTime)] = DefaultLagTime.ToString();
if (!settings.TryGetValue(nameof(LeadTime), out _))
settings[nameof(LeadTime)] = DefaultLeadTime.ToString();
base.Initialize();
// Load required mail settings
if (settings.TryGetValue(nameof(From), out string setting) && !string.IsNullOrWhiteSpace(setting))
From = setting;
else
throw new ArgumentException(string.Format(MissingRequiredMailSetting, nameof(From)));
if (settings.TryGetValue(nameof(Subject), out setting) && !string.IsNullOrWhiteSpace(setting))
Subject = setting;
else
throw new ArgumentException(string.Format(MissingRequiredMailSetting, nameof(Subject)));
if (settings.TryGetValue(nameof(Body), out setting) && !string.IsNullOrWhiteSpace(setting))
Body = setting;
else
throw new ArgumentException(string.Format(MissingRequiredMailSetting, nameof(Body)));
if (settings.TryGetValue(nameof(SmtpServer), out setting) && !string.IsNullOrWhiteSpace(setting))
SmtpServer = setting;
else
throw new ArgumentException(string.Format(MissingRequiredMailSetting, nameof(SmtpServer)));
// Load optional mail settings
if (settings.TryGetValue(nameof(ToRecipients), out setting) && !string.IsNullOrWhiteSpace(setting))
ToRecipients = setting;
if (settings.TryGetValue(nameof(CcRecipients), out setting) && !string.IsNullOrWhiteSpace(setting))
CcRecipients = setting;
if (settings.TryGetValue(nameof(BccRecipients), out setting) && !string.IsNullOrWhiteSpace(setting))
BccRecipients = setting;
if (string.IsNullOrWhiteSpace(ToRecipients) && string.IsNullOrWhiteSpace(CcRecipients) && string.IsNullOrWhiteSpace(BccRecipients))
throw new ArgumentException($"At least one destination e-mail address for one of {nameof(ToRecipients)}, {nameof(CcRecipients)} or {nameof(BccRecipients)} must be defined");
if (settings.TryGetValue(nameof(IsBodyHtml), out setting) && !string.IsNullOrWhiteSpace(setting))
IsBodyHtml = setting.ParseBoolean();
if (settings.TryGetValue(nameof(Username), out setting) && !string.IsNullOrWhiteSpace(setting))
Username = setting;
if (settings.TryGetValue(nameof(Password), out setting) && !string.IsNullOrWhiteSpace(setting))
Password = setting;
if (settings.TryGetValue(nameof(EnableSSL), out setting) && !string.IsNullOrWhiteSpace(setting))
EnableSSL = setting.ParseBoolean();
if (settings.TryGetValue(nameof(MultiTriggerPrevention), out setting) && !string.IsNullOrWhiteSpace(setting))
MultiTriggerPrevention = setting.ParseBoolean();
m_triggerDetected = false;
}
/// <summary>
/// Handler for the values calculated by the <see cref="DynamicCalculator"/>.
/// </summary>
/// <param name="value">The value calculated by the <see cref="DynamicCalculator"/>.</param>
protected override void HandleCalculatedValue(object value)
{
if (value.ToString().ParseBoolean())
{
if (m_triggerDetected && MultiTriggerPrevention)
return;
m_triggerDetected = true;
m_expressionSuccesses++;
m_mailClient.Body = Body.Interpolate(Variables);
m_mailClient.Send();
m_totalEmailOperations++;
}
else
{
m_triggerDetected = false;
m_expressionFailures++;
}
}
#endregion
}
}