blob: eb86211522831f52117060e161b02e63d7a6e4e8 (
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Emulations.Emulators;
using Tango.Logging;
using Tango.SharedUI;
using Tango.SharedUI.Commands;
using Tango.Transport.Adapters;
using Tango.Transport.Servers;
using Tango.Transport.Transporters;
namespace Tango.MachineEM.UI.ViewModels
{
public class MainViewVM : ExtendedObject
{
private TcpServer TcpServer;
#region Properties
private MachineEmulator _emulator;
/// <summary>
/// Gets or sets the machine emulator.
/// </summary>
public MachineEmulator Emulator
{
get { return _emulator; }
set { _emulator = value; RaisePropertyChanged(nameof(Emulator)); }
}
private String _log;
/// <summary>
/// Gets or sets the log.
/// </summary>
public String Log
{
get { return _log; }
set { _log = value; RaisePropertyChanged(nameof(Log)); }
}
#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="MainViewVM"/> class.
/// </summary>
public MainViewVM()
{
MainViewLogger logger = new MainViewLogger();
logger.NewLog += (output) =>
{
Log += output + Environment.NewLine;
};
LogManager.RegisterLogger(logger);
Emulator = new MachineEmulator(new ProtoTransporter());
StartCommand = new RelayCommand(Start, (x) => !Emulator.IsStarted);
StopCommand = new RelayCommand(Stop,(x) => Emulator.IsStarted);
}
#endregion
#region Event Handlers
/// <summary>
/// Handles the ClientConnected event of the TcpServer control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="ClientConnectedEventArgs"/> instance containing the event data.</param>
private void TcpServer_ClientConnected(object sender, ClientConnectedEventArgs e)
{
Emulator.Transporter.Adapters.Add(new TcpTransportAdapter(e.Socket));
}
#endregion
#region Command Handlers
/// <summary>
/// Stops the TCP server and emulator.
/// </summary>
private async void Start()
{
TcpServer = new TcpServer(9999);
TcpServer.ClientConnected += TcpServer_ClientConnected;
TcpServer.Start();
await Emulator.Start();
InvalidateRelayCommands();
}
/// <summary>
/// Starts the TCP server/USB and emulator.
/// </summary>
private async void Stop()
{
TcpServer.Stop();
await Emulator.Stop();
InvalidateRelayCommands();
}
#endregion
#region Custom Logger
public class MainViewLogger : ILogger
{
public bool Enabled { get; set; }
public bool Immediate { get; set; }
public event Action<String> NewLog;
public MainViewLogger()
{
Enabled = true;
Immediate = true;
}
public void OnError(LogItemBase output)
{
NewLog?.Invoke(output.ToString());
}
public void OnTrace(LogItemBase output)
{
NewLog?.Invoke(output.ToString());
}
}
#endregion
}
}
|