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 | |
| 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')
9 files changed, 209 insertions, 0 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCSettings.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCSettings.cs index 98f8f27fd..da39b40ec 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCSettings.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCSettings.cs @@ -11,6 +11,7 @@ using Tango.Logging; using Tango.PMR.Integration; using Tango.PMR.Printing; using Tango.PPC.Common.Lubrication; +using Tango.PPC.Common.SMS; using Tango.Settings; using Tango.Transport.Adapters; using Tango.Web; @@ -413,6 +414,11 @@ namespace Tango.PPC.Common public bool EnableJerricanChangePopup { get; set; } /// <summary> + /// Gets or sets the SMS notification settings. + /// </summary> + public SMSNotificationSettings SMSNotificationSettings { get; set; } + + /// <summary> /// Initializes a new instance of the <see cref="PPCSettings"/> class. /// </summary> public PPCSettings() @@ -481,6 +487,8 @@ namespace Tango.PPC.Common ScreenSaverStartDuration = 5; EnableScreenSaver = true; EnableRemoteJobUpload = true; + + SMSNotificationSettings = new SMSNotificationSettings(); } } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCViewModel.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCViewModel.cs index 7064a48e1..436b82d01 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCViewModel.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCViewModel.cs @@ -21,6 +21,7 @@ using Tango.PPC.Common.RemoteAssistance; using Tango.PPC.Common.RemoteDesktop; using Tango.PPC.Common.RemoteJobUpload; using Tango.PPC.Common.Resume; +using Tango.PPC.Common.SMS; using Tango.PPC.Common.Storage; using Tango.PPC.Common.Synchronization; using Tango.PPC.Common.ThreadLoading; @@ -149,6 +150,9 @@ namespace Tango.PPC.Common [TangoInject] public IJobResumeManager JobResumeManager { get; set; } + [TangoInject] + public ISMSNotificationProvider SMSNotificationProvider { get; set; } + private PPCSettings _settings; /// <summary> /// Gets the main PPC settings. 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; + } + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj b/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj index 08ad6ec1c..f3e7d4e5e 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj @@ -232,6 +232,9 @@ <Compile Include="Resume\JobResumeDroppedEventArgs.cs" /> <Compile Include="Resume\JobResumeModel.cs" /> <Compile Include="Resume\JobResumeUpdatedEventArgs.cs" /> + <Compile Include="SMS\DefaultSMSNotificationProvider.cs" /> + <Compile Include="SMS\ISMSNotificationProvider.cs" /> + <Compile Include="SMS\SMSNotificationSettings.cs" /> <Compile Include="SQL\DefaultRemoteSqlService.cs" /> <Compile Include="SQL\IRemoteSqlService.cs" /> <Compile Include="Statistics\DefaultStatisticsService.cs" /> @@ -261,6 +264,8 @@ <Compile Include="Publish\SequenceItem.cs" /> <Compile Include="Publish\SynchronizationOptions.cs" /> <Compile Include="UWF\AlternativeUnifiedWriteFilterManager.cs" /> + <Compile Include="Web\SendSMSResponse.cs" /> + <Compile Include="Web\SendSMSRequest.cs" /> <Compile Include="Web\CheckForUpdateRequest.cs" /> <Compile Include="Web\CheckForUpdateResponse.cs" /> <Compile Include="MachineUpdate\DbCompareResult.cs" /> diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/PPCWebClientBase.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/PPCWebClientBase.cs index 35150a9d6..0363285f8 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/PPCWebClientBase.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/PPCWebClientBase.cs @@ -122,6 +122,15 @@ namespace Tango.PPC.Common.Web } /// <summary> + /// Executes the SendSMS action and returns Tango.PPC.Common.Web.SendSMSResponse. + /// </summary> + /// <returns></returns> + public Task<Tango.PPC.Common.Web.SendSMSResponse> SendSMS(Tango.PPC.Common.Web.SendSMSRequest request) + { + return Post<Tango.PPC.Common.Web.SendSMSRequest, Tango.PPC.Common.Web.SendSMSResponse>("SendSMS", request); + } + + /// <summary> /// Executes the GetLatestVersion action and returns Tango.PPC.Common.Web.LatestVersionResponse. /// </summary> /// <returns></returns> diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/SendSMSRequest.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/SendSMSRequest.cs new file mode 100644 index 000000000..ff07e9540 --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/SendSMSRequest.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.BL.Entities; +using Tango.Transport.Web; + +namespace Tango.PPC.Common.Web +{ + public class SendSMSRequest : WebRequestMessage + { + public List<String> PhoneNumbers { get; set; } + + public String Message { get; set; } + + public SendSMSRequest() + { + PhoneNumbers = new List<string>(); + } + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/SendSMSResponse.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/SendSMSResponse.cs new file mode 100644 index 000000000..28c983911 --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/SendSMSResponse.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.Transport.Web; + +namespace Tango.PPC.Common.Web +{ + public class SendSMSResponse : WebResponseMessage + { + + } +} |
