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
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008
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;
        }
    }
}