aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeSignalRClient.cs
blob: e2aae9eb8c27bf6cdff709d21d2cc569931eafa7 (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Authentication;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.Entities;
using Tango.Integration.Operation;
using Tango.PMR.Integration;
using Tango.Settings;
using Tango.Transport;
using Tango.Transport.Adapters;

namespace Tango.Integration.ExternalBridge
{
    public class ExternalBridgeSignalRClient : ExternalBridgeTcpClient
    {
        public ExternalBridgeSignalRClient(String url, String hub, String serialNumber)
        {
            ComponentName = $"External Bridge SignalR Client {_component_counter++}";

            SerialNumber = serialNumber;
            IPAddress = hub;
            Machine = ObservablesStaticCollections.Instance.Machines.SingleOrDefault(x => x.SerialNumber == serialNumber);
            Adapter = new SignalRTransportAdapter(url, hub, SignalRTransportAdapterMode.CreateSession, serialNumber);

            KeepAliveTimeout = TimeSpan.FromSeconds(5);
            KeepAliveRetries = 2;
            UseKeepAlive = false;
        }

        public override async Task Connect(ExternalBridgeLoginRequest login)
        {
            if (State != TransportComponentState.Connected)
            {
                try
                {
                    await Adapter.Connect();

                    State = TransportComponentState.Connected;
                    StartThreads();

                    LogManager.Log("External Bridge SignalR Client Connected...");


                    LogRequestSent(login);

                    var response = await SendRequest<ExternalBridgeLoginRequest, ExternalBridgeLoginResponse>(login);

                    SessionLogger.CreateSession();

                    DeviceInformation = response.Message.DeviceInformation;
                    if (!response.Message.Authenticated)
                    {
                        await Adapter.Disconnect();
                        throw new AuthenticationException(response.Container.ErrorMessage);
                    }
                }
                catch (Exception ex)
                {
                    LogRequestFailed(login, ex);
                    try
                    {
                        await Adapter.Disconnect();
                    }
                    catch { }

                    throw ex;
                }

                OnEnableDiagnosticsChanged(EnableDiagnostics);
                OnEnableEmbeddedDebuggingChanged(EnableEmbeddedDebugging);
                OnEnableEventsNotification(EnableEventsNotification);
                OnEnableApplicationLogsChanged(EnableApplicationLogs);
                OnEnableMachineStatusUpdatesChanged(EnableMachineStatusUpdates);
                //TODO: Uncomment this only when Machine Studio enables automatic thread loading (ExternalBridgeTCPClient).
                //OnEnableAutomaticThreadLoadingChanged(EnableAutomaticThreadLoading);
            }
        }
    }
}