aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/RemoteNotifications/DefaultRemoteNotificationsService.cs
blob: 5931f56ab8eac4bcbf8872f34bee9f2d01565a0e (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tango.Core.DI;
using Tango.Core.Threading;
using Tango.Integration.ExternalBridge;
using Tango.PPC.Common.ExternalBridge;
using Tango.PPC.Common.Notifications;
using Tango.PPC.Shared.Notifications;

namespace Tango.PPC.Common.RemoteNotifications
{
    [TangoCreateWhenRegistered]
    public class DefaultRemoteNotificationsService : IRemoteNotificationsService
    {
        private INotificationProvider _notification;

        public bool Enabled { get; set; } = true;

        public DefaultRemoteNotificationsService(IPPCExternalBridgeService externalBridge, INotificationProvider notificationProvider)
        {
            _notification = notificationProvider;
            externalBridge.RegisterRequestHandler(this);
        }

        [ExternalBridgeRequestHandlerMethod(typeof(RemoteMessageBoxRequest), RequestHandlerLoggingMode.LogRequestName)]
        public async Task OnRemoteMessageBoxRequest(RemoteMessageBoxRequest request, String token, ExternalBridgeReceiver receiver)
        {
            bool result = false;
            bool completed = false;

            if (request.Timeout != null)
            {
                ThreadFactory.StartNew(() =>
                {
                    Thread.Sleep(request.Timeout.Value);

                    if (!completed)
                    {
                        completed = true;
                        _notification.CurrentMessageBox.Close(false);
                        receiver.SendErrorResponse(new TimeoutException("The user did not respond within the given timeout."), token);
                    }
                });
            }

            switch (request.Type)
            {
                case RemoteMessageBoxType.Info:
                    await _notification.ShowInfo(request.Message);
                    break;
                case RemoteMessageBoxType.Warning:
                    await _notification.ShowWarning(request.Message);
                    break;
                case RemoteMessageBoxType.Question:
                    result = await _notification.ShowQuestion(request.Message);
                    break;
                case RemoteMessageBoxType.Error:
                    await _notification.ShowError(request.Message);
                    break;
                default:
                    break;
            }

            if (completed) return;

            completed = true;

            await receiver.SendGenericResponse(new RemoteMessageBoxResponse() { Result = result }, token);
        }

        public void OnReceiverDisconnected(ExternalBridgeReceiver receiver)
        {

        }
    }
}