aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Integration/Services/ExternalBridgeTcpClient.cs
blob: 57948441806593aff985d9f425f863c627cc33b1 (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
112
113
114
115
116
using System;
using System.Collections.Generic;
using System.Linq;
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.Adapters;
using Tango.Transport.Transporters;

namespace Tango.Integration.Services
{
    /// <summary>
    /// Represents a secure external bridge TCP client.
    /// </summary>
    /// <seealso cref="Tango.Transport.Transporters.BasicTransporter" />
    /// <seealso cref="Tango.Integration.Services.IExternalBridgeSecureClient" />
    public class ExternalBridgeTcpClient : MachineOperator, IExternalBridgeSecureClient
    {
        private Machine _machine;
        /// <summary>
        /// Gets the machine database entity.
        /// </summary>
        public Machine Machine
        {
            get
            {
                return _machine;
            }
        }

        private String _serialNumber;
        /// <summary>
        /// Gets the machine serial number.
        /// </summary>
        public String SerialNumber
        {
            get { return _serialNumber; }
            set
            {
                _serialNumber = value;
                RaisePropertyChangedAuto();

                _machine = ObservablesEntitiesAdapter.Instance.Machines.SingleOrDefault(x => x.SerialNumber == _serialNumber);
                RaisePropertyChanged(nameof(Machine));
            }
        }

        private String _ipAddress;
        /// <summary>
        /// Gets or sets the machine IP address.
        /// </summary>
        public String IPAddress
        {
            get { return _ipAddress; }
            set { _ipAddress = value; RaisePropertyChangedAuto(); }
        }

        /// <summary>
        /// Gets a value indicating whether this client requires authentication.
        /// </summary>
        public bool RequiresAuthentication => true;

        /// <summary>
        /// Connects the transport component.
        /// </summary>
        /// <returns></returns>
        public override async Task Connect()
        {
            await Disconnect();
            Adapter = new TcpTransportAdapter(IPAddress, SettingsManager.Default.GetOrCreate<IntegrationSettings>().ExternalBridgeServicePort);
            await base.Connect();
        }

        /// <summary>
        /// Authenticates with the service using the specified password.
        /// </summary>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public async Task<bool> Authenticate(String password)
        {
            var response = await SendRequest<ExternalClientLoginRequest, ExternalClientLoginResponse>(new ExternalClientLoginRequest()
            {
                Password = password
            });
            return response.Message.Authenticated;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ExternalBridgeTcpClient"/> class.
        /// </summary>
        /// <param name="serialNumber">The machine serial number.</param>
        /// <param name="ipAddress">The machine IP address.</param>
        public ExternalBridgeTcpClient(String serialNumber, String ipAddress)
        {
            SerialNumber = serialNumber;
            IPAddress = ipAddress;
            UseKeepAlive = true;
            EnableDiagnostics = true;
        }

        /// <summary>
        /// Returns a <see cref="System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String" /> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return SerialNumber;
        }
    }
}