aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Emulations/EmulatorBase.cs
blob: ca40cce7a2673ab5aae0d662e309fc7aaba432f2 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Logging;
using Tango.PMR.Common;
using Tango.Core.Commands;
using Tango.Transport;
using Tango.Core;

namespace Tango.Emulations
{
    /// <summary>
    /// Represents an <see cref="IEmulator"/> base class.
    /// </summary>
    /// <seealso cref="Tango.SharedUI.ExtendedObject" />
    /// <seealso cref="Tango.Emulations.IEmulator" />
    public abstract class EmulatorBase : ExtendedObject, IEmulator
    {
        #region Properties

        private bool _isStarted;
        /// <summary>
        /// Gets or sets a value indicating whether the emulator is started.
        /// </summary>
        public bool IsStarted
        {
            get { return _isStarted; }
            set { _isStarted = value; RaisePropertyChanged(nameof(IsStarted)); InvalidateRelayCommands(); }
        }

        private ITransporter _transporter;
        /// <summary>
        /// Gets or sets the transporter.
        /// </summary>
        public ITransporter Transporter
        {
            get { return _transporter; }
            set
            {
                _transporter = value;
                SetTransporter(_transporter);
            }
        }

        #endregion

        #region Commands

        /// <summary>
        /// Gets or sets the start command.
        /// </summary>
        public RelayCommand StartCommand { get; set; }

        /// <summary>
        /// Gets or sets the stop command.
        /// </summary>
        public RelayCommand StopCommand { get; set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="EmulatorBase"/> class.
        /// </summary>
        public EmulatorBase()
        {
            StartCommand = new RelayCommand(async () => { await Start(); }, (x) => !IsStarted);
            StopCommand = new RelayCommand(async () => { await Stop(); }, (x) => IsStarted);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="EmulatorBase"/> class.
        /// </summary>
        /// <param name="transporter">The transporter.</param>
        public EmulatorBase(ITransporter transporter) : this()
        {
            Transporter = transporter;
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Sets the transporter.
        /// </summary>
        /// <param name="transporter">The transporter.</param>
        private void SetTransporter(ITransporter transporter)
        {
            if (_transporter != null)
            {
                _transporter.StateChanged -= OnTransporterStateChanged;
            }

            _transporter = transporter;
            _transporter.RequestReceived += OnTransporterRequestReceived;

            _transporter.StateChanged += OnTransporterStateChanged;
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Stops this instance.
        /// </summary>
        public virtual async Task Stop()
        {
            if (IsStarted)
            {
                await _transporter.Disconnect();
                IsStarted = false;
            }
        }

        /// <summary>
        /// Starts this instance.
        /// </summary>
        public virtual async Task Start()
        {
            if (!IsStarted)
            {
                await _transporter.Connect();
                IsStarted = true;
            }
        }

        #endregion

        #region Virtual Methods

        /// <summary>
        /// Called when transporter state has changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        protected virtual void OnTransporterStateChanged(object sender, TransportComponentState e)
        {
            LogManager.Log("Transporter state changed: " + e.ToString());
        }

        #endregion

        #region Abstract Methods

        /// <summary>
        /// Called on new request message.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="container">The container.</param>
        protected abstract void OnTransporterRequestReceived(object sender, RequestReceivedEventArgs e);

        #endregion

        #region Dispose

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            Stop().Wait();
            if (Transporter != null) Transporter.Dispose();
        }

        #endregion
    }
}