diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2024-11-14 14:30:42 +0200 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2024-11-14 14:30:42 +0200 |
| commit | 7d9ab3b87aed944167d77244b00fbdd4224725e2 (patch) | |
| tree | 9a2a56059a0332182a7ecf4aa4237eae132f97f7 /Software/Visual_Studio/PPC/Tango.PPC.Common/SMS | |
| parent | c19568d8dc2677fe8dd57d55a737535c718f52f5 (diff) | |
| download | Tango-7d9ab3b87aed944167d77244b00fbdd4224725e2.tar.gz Tango-7d9ab3b87aed944167d77244b00fbdd4224725e2.zip | |
Added support for SMS notifications on TS & X4.
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common/SMS')
3 files changed, 147 insertions, 0 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/SMS/DefaultSMSNotificationProvider.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/SMS/DefaultSMSNotificationProvider.cs new file mode 100644 index 000000000..0b810bb52 --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/SMS/DefaultSMSNotificationProvider.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.Core; +using Tango.Core.DI; +using Tango.PPC.Common.Connection; +using Tango.PPC.Common.Notifications; +using Tango.PPC.Common.Threading; +using Tango.PPC.Common.Web; +using Tango.Settings; + +namespace Tango.PPC.Common.SMS +{ + [TangoCreateWhenRegistered] + public class DefaultSMSNotificationProvider : ExtendedObject, ISMSNotificationProvider + { + private PPCWebClient _webClient; + private IMachineProvider _machineProvider; + private PPCSettings _settings; + + public DefaultSMSNotificationProvider(PPCWebClient ppcWebClient, IMachineProvider machineProvider) + { + _webClient = ppcWebClient; + _machineProvider = machineProvider; + _settings = SettingsManager.Default.GetOrCreate<PPCSettings>(); + _machineProvider.MachineOperator.PrintingCompleted += MachineOperator_PrintingCompleted; + _machineProvider.MachineOperator.PrintingFailed += MachineOperator_PrintingFailed; + } + + private async void MachineOperator_PrintingFailed(object sender, Integration.Operation.PrintingFailedEventArgs e) + { + if (_settings.SMSNotificationSettings.Enable && _settings.SMSNotificationSettings.SendWhenJobFails) + { + if (e.Job.Designation == BL.Enumerations.JobDesignations.Default) + { + try + { + await SendMessage($"Job '{e.Job.Name}' failed at position {Math.Round(e.JobHandler.Status.ProgressMinusSettingUp, 1)}\n{e.Exception.Message}."); + } + catch { } + } + } + } + + private async void MachineOperator_PrintingCompleted(object sender, Integration.Operation.PrintingEventArgs e) + { + if (_settings.SMSNotificationSettings.Enable && _settings.SMSNotificationSettings.SendWhenJobCompletes) + { + if (e.Job.Designation == BL.Enumerations.JobDesignations.Default) + { + try + { + await SendMessage($"Job '{e.Job.Name}' has completed successfully."); + } + catch { } + } + } + } + + public Task SendMessage(string message) + { + return SendMessage(ParsePhoneNumbers(_settings.SMSNotificationSettings.Numbers), message); + } + + public async Task SendMessage(List<string> numbers, string message) + { + try + { + LogManager.Log($"Sending SMS notification..."); + if (numbers.Count > 0) + { + await _webClient.SendSMS(new SendSMSRequest() { PhoneNumbers = numbers, Message = message }); + LogManager.Log($"SMS notification sent successfully..."); + } + else + { + throw new ArgumentOutOfRangeException("No phone numbers defined."); + } + } + catch (Exception ex) + { + LogManager.Log(ex, "Error sending SMS."); + throw ex; + } + } + + public static List<String> ParsePhoneNumbers(String phoneNumbers) + { + if (phoneNumbers.Contains(",")) + { + return phoneNumbers.Split(',').Select(x => x.Trim(' ', '\n')).Where(x => x.IsNotNullOrEmpty()).ToList(); + } + else if (phoneNumbers.IsNotNullOrEmpty()) + { + return new List<string>() { phoneNumbers }; + } + else + { + return new List<string>(); + } + } + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/SMS/ISMSNotificationProvider.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/SMS/ISMSNotificationProvider.cs new file mode 100644 index 000000000..927a3e602 --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/SMS/ISMSNotificationProvider.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.PPC.Common.SMS +{ + public interface ISMSNotificationProvider + { + Task SendMessage(List<String> numbers, String message); + Task SendMessage(String message); + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/SMS/SMSNotificationSettings.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/SMS/SMSNotificationSettings.cs new file mode 100644 index 000000000..77d31974d --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/SMS/SMSNotificationSettings.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.PPC.Common.SMS +{ + public class SMSNotificationSettings + { + public bool Enable { get; set; } + + public String Numbers { get; set; } + + public bool SendWhenJobCompletes { get; set; } + + public bool SendWhenJobFails { get; set; } + + public int MinimumNotificationMeters { get; set; } + + public SMSNotificationSettings() + { + SendWhenJobCompletes = true; + SendWhenJobFails = true; + MinimumNotificationMeters = 100; + } + } +} |
