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(); _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) { if (e.JobHandler.Status.ProgressMinusSettingUp > _settings.SMSNotificationSettings.MinimumNotificationMeters) { try { await SendMessage($"{_machineProvider.Machine.Type.ToDescription()} [{_machineProvider.Machine.SerialNumber}].\nJob '{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) { if (e.JobHandler.Status.ProgressMinusSettingUp > _settings.SMSNotificationSettings.MinimumNotificationMeters) { try { await SendMessage($"{_machineProvider.Machine.Type.ToDescription()} [{_machineProvider.Machine.SerialNumber}].\nJob '{e.Job.Name}' has completed successfully."); } catch { } } } } } public Task SendMessage(string message) { return SendMessage(ParsePhoneNumbers(_settings.SMSNotificationSettings.Numbers), message); } public async Task SendMessage(List 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 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() { phoneNumbers }; } else { return new List(); } } } }