- Add controller name and TLS support to email notifications

This commit is contained in:
Ryan Wagoner 2019-12-14 23:27:29 -05:00
parent 336e02e8e8
commit c3aa88621d
9 changed files with 40 additions and 33 deletions

View file

@ -1,6 +1,5 @@
using OmniLinkBridge.Modules;
using OmniLinkBridge.OmniLink;
using log4net;
using log4net;
using OmniLinkBridge.Modules;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
@ -10,7 +9,7 @@ namespace OmniLinkBridge
{
public class CoreServer
{
private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private OmniLinkII omnilink;
private readonly List<IModule> modules = new List<IModule>();

View file

@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
namespace OmniLinkBridge
{

View file

@ -16,6 +16,7 @@ namespace OmniLinkBridge
public static int controller_port;
public static string controller_key1;
public static string controller_key2;
public static string controller_name;
// Time Sync
public static bool time_sync;
@ -61,6 +62,7 @@ namespace OmniLinkBridge
// Email Notifications
public static string mail_server;
public static bool mail_tls;
public static int mail_port;
public static string mail_username;
public static string mail_password;

View file

@ -8,20 +8,23 @@ namespace OmniLinkBridge.Notifications
{
public class EmailNotification : INotification
{
private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public void Notify(string source, string description, NotificationPriority priority)
{
foreach (MailAddress address in Global.mail_to)
{
MailMessage mail = new MailMessage();
mail.From = Global.mail_from;
MailMessage mail = new MailMessage
{
From = Global.mail_from,
Subject = $"{Global.controller_name} - {source}",
Body = $"{source}: {description}"
};
mail.To.Add(address);
mail.Subject = "OmniLinkBridge - " + source;
mail.Body = source + ": " + description;
using (SmtpClient smtp = new SmtpClient(Global.mail_server, Global.mail_port))
{
smtp.EnableSsl = Global.mail_tls;
if (!string.IsNullOrEmpty(Global.mail_username))
{
smtp.UseDefaultCredentials = false;
@ -34,7 +37,7 @@ namespace OmniLinkBridge.Notifications
}
catch (Exception ex)
{
log.Error("An error occurred sending notification", ex);
log.Error("An error occurred sending email notification", ex);
}
}
}

View file

@ -8,7 +8,7 @@ namespace OmniLinkBridge.Notifications
{
public static class Notification
{
private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static readonly List<INotification> providers = new List<INotification>()
{

View file

@ -8,35 +8,36 @@ namespace OmniLinkBridge.Notifications
{
public class ProwlNotification : INotification
{
private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static Uri URI = new Uri("https://api.prowlapp.com/publicapi/add");
private static readonly Uri URI = new Uri("https://api.prowlapp.com/publicapi/add");
public void Notify(string source, string description, NotificationPriority priority)
{
foreach (string key in Global.prowl_key)
{
List<string> parameters = new List<string>();
parameters.Add("apikey=" + key);
parameters.Add("priority= " + (int)priority);
parameters.Add("application=OmniLinkBridge");
parameters.Add("event=" + source);
parameters.Add("description=" + description);
List<string> parameters = new List<string>
{
"apikey=" + key,
"priority= " + (int)priority,
"application=" + Global.controller_name,
"event=" + source,
"description=" + description
};
using (WebClient client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
client.UploadStringAsync(URI, string.Join("&", parameters.ToArray()));
client.UploadStringCompleted += client_UploadStringCompleted;
client.UploadStringCompleted += Client_UploadStringCompleted;
}
}
}
private void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
private void Client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
if (e.Error != null)
log.Error("An error occurred sending notification", e.Error);
log.Error("An error occurred sending prowl notification", e.Error);
}
}
}

View file

@ -8,9 +8,9 @@ namespace OmniLinkBridge.Notifications
{
public class PushoverNotification : INotification
{
private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static Uri URI = new Uri("https://api.pushover.net/1/messages.json");
private static readonly Uri URI = new Uri("https://api.pushover.net/1/messages.json");
public void Notify(string source, string description, NotificationPriority priority)
{
@ -20,22 +20,22 @@ namespace OmniLinkBridge.Notifications
{ "token", Global.pushover_token },
{ "user", key },
{ "priority", ((int)priority).ToString() },
{ "title", source },
{ "title", $"{Global.controller_name} - {source}" },
{ "message", description }
};
using (WebClient client = new WebClient())
{
client.UploadValues(URI, parameters);
client.UploadStringCompleted += client_UploadStringCompleted;
client.UploadStringCompleted += Client_UploadStringCompleted;
}
}
}
private void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
private void Client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
if (e.Error != null)
log.Error("An error occurred sending notification", e.Error);
log.Error("An error occurred sending pushover notification", e.Error);
}
}
}

View file

@ -3,6 +3,8 @@ controller_address =
controller_port = 4369
controller_key1 = 00-00-00-00-00-00-00-00
controller_key2 = 00-00-00-00-00-00-00-00
# Used in notifications
controller_name = OmniLinkBridge
# Controller Time Sync (yes/no)
# time_interval is interval in minutes to check controller time
@ -62,6 +64,8 @@ notify_message = no
# Email Notifications
# mail_username and mail_password optional for authenticated mail
mail_server =
mail_tls = no
# When TLS is enabled the port is usually 587
mail_port = 25
mail_username =
mail_password =

View file

@ -5,7 +5,6 @@ using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Reflection;
@ -24,6 +23,7 @@ namespace OmniLinkBridge
Global.controller_port = ValidatePort(settings, "controller_port");
Global.controller_key1 = settings["controller_key1"];
Global.controller_key2 = settings["controller_key2"];
Global.controller_name = settings["controller_name"] ?? "OmniLinkBridge";
// Controller Time Sync
Global.time_sync = ValidateYesNo(settings, "time_sync");
@ -72,6 +72,7 @@ namespace OmniLinkBridge
// Email Notifications
Global.mail_server = settings["mail_server"];
Global.mail_tls = ValidateYesNo(settings, "mail_tls");
Global.mail_port = ValidatePort(settings, "mail_port");
Global.mail_username = settings["mail_username"];
Global.mail_password = settings["mail_password"];