aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/SMS/DefaultSMSNotificationProvider.cs
blob: 7452ea987a56347906a6636cd660dd4e45ab4229 (plain)
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
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)
                {
                    if (e.JobHandler.Status.ProgressMinusSettingUp > _settings.SMSNotificationSettings.MinimumNotificationMeters)
                    {
                        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)
                {
                    if (e.JobHandler.Status.ProgressMinusSettingUp > _settings.SMSNotificationSettings.MinimumNotificationMeters)
                    {
                        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>();
            }
        }
    }
}