aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeUsbClient.cs
blob: d7246fe7b62825ab83b4755ef02a193237ff8137 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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.ExternalBridge
{
    /// <summary>
    /// Represents an external bridge USB client.
    /// </summary>
    /// <remarks>
    /// This isn't really an external bridge client, but merely a transporter for communicating with a Tango machine embedded device.
    /// </remarks>
    /// <seealso cref="Tango.Transport.Transporters.BasicTransporter" />
    /// <seealso cref="Tango.Integration.ExternalBridge.IExternalBridgeClient" />
    public class ExternalBridgeUsbClient : MachineOperator, IExternalBridgeClient
    {
        private String _serialNumber;
        /// <summary>
        /// Gets the machine serial number.
        /// </summary>
        public String SerialNumber
        {
            get { return _serialNumber; }
            set
            {
                _serialNumber = value;
                RaisePropertyChangedAuto();
            }
        }

        private String _comPort;
        /// <summary>
        /// Gets or sets the serial COM port.
        /// </summary>
        public String ComPort
        {
            get { return _comPort; }
            set { _comPort = value; RaisePropertyChangedAuto(); }
        }

        private UsbSerialBaudRates _baudRate;
        /// <summary>
        /// Gets or sets the baud rate.
        /// </summary>
        public UsbSerialBaudRates BaudRate
        {
            get { return _baudRate; }
            set { _baudRate = value; RaisePropertyChangedAuto(); }
        }

        private String _Device;
        /// <summary>
        /// Gets or sets the USB device name.
        /// </summary>
        public String Device
        {
            get { return _Device; }
            set { _Device = value; RaisePropertyChangedAuto(); }
        }

        /// <summary>
        /// Gets or sets a value indicating whether transport compression is required by the remote machine.
        /// </summary>
        public bool CompressionEnabled { get; set; }

        /// <summary>
        /// Connects the transport component.
        /// </summary>
        /// <returns></returns>
        public override async Task Connect()
        {
            if (Status != MachineStatuses.Upgrading)
            {
                await Disconnect();
                Adapter = new UsbTransportAdapter(ComPort) { BaudRate = BaudRate };
            }
            await base.Connect();
        }

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

        /// <summary>
        /// Initializes a new instance of the <see cref="ExternalBridgeUsbClient"/> class.
        /// </summary>
        /// <param name="comPort">The COM port.</param>
        /// <param name="device">The device.</param>
        public ExternalBridgeUsbClient(String comPort, String device,UsbSerialBaudRates baudRate = UsbSerialBaudRates.BR_115200)
        {
            ComponentName = $"External Bridge USB Client {_component_counter++}";
            ComPort = comPort;
            Device = device;
            BaudRate = baudRate;
            UseKeepAlive = false;
            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 Device;
        }

        /// <summary>
        /// Gets the database machine associated with this client.
        /// </summary>
        public Machine Machine { get; private set; }

        /// <summary>
        /// Sets the database machine.
        /// </summary>
        /// <param name="machine">The machine.</param>
        public void SetMachine(Machine machine)
        {
            Machine = machine;
        }
    }
}