-
Notifications
You must be signed in to change notification settings - Fork 147
/
TelemetryClient.cs
111 lines (93 loc) · 3.44 KB
/
TelemetryClient.cs
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
using System;
namespace TDDMicroExercises.TelemetrySystem
{
public class TelemetryClient
{
//
// The communication with the server is simulated in this implementation.
// Because the focus of the exercise is on the other class.
//
public const string DiagnosticMessage = "AT#UD";
private bool _onlineStatus;
private bool _diagnosticMessageJustSent = false;
private readonly Random _connectionEventsSimulator = new Random();
private readonly Random _randomMessageSimulator = new Random();
public virtual bool OnlineStatus
{
get { return _onlineStatus; }
}
public virtual void Connect(string telemetryServerConnectionString)
{
this.Connect(telemetryServerConnectionString, _connectionEventsSimulator.Next(1, 10));
}
public virtual void Connect(string telemetryServerConnectionString, int connectionEventsRandomNumberFrom1To10)
{
if (string.IsNullOrEmpty(telemetryServerConnectionString))
{
throw new ArgumentNullException();
}
// Fake the connection with 20% chances of success
bool success = connectionEventsRandomNumberFrom1To10 <= 2;
_onlineStatus = success;
}
public virtual void Disconnect()
{
_onlineStatus = false;
}
public virtual void Send(string message)
{
if (string.IsNullOrEmpty(message))
{
throw new ArgumentNullException();
}
// The simulation of Send() actually just remember if the last message sent was a diagnostic message.
// This information will be used to simulate the Receive(). Indeed there is no real server listening.
if (message == DiagnosticMessage)
{
_diagnosticMessageJustSent = true;
}
else
{
_diagnosticMessageJustSent = false;
}
}
public virtual string Receive()
{
return this.Receive(_randomMessageSimulator.Next(50, 110), _randomMessageSimulator.Next(40, 126));
}
public virtual string Receive(int messageRandomNumberFrom50To110, int messageRandomNumberFrom40To126)
{
string message;
if (_diagnosticMessageJustSent)
{
// Simulate the reception of the diagnostic message
message = "LAST TX rate................ 100 MBPS\r\n"
+ "HIGHEST TX rate............. 100 MBPS\r\n"
+ "LAST RX rate................ 100 MBPS\r\n"
+ "HIGHEST RX rate............. 100 MBPS\r\n"
+ "BIT RATE.................... 100000000\r\n"
+ "WORD LEN.................... 16\r\n"
+ "WORD/FRAME.................. 511\r\n"
+ "BITS/FRAME.................. 8192\r\n"
+ "MODULATION TYPE............. PCM/FM\r\n"
+ "TX Digital Los.............. 0.75\r\n"
+ "RX Digital Los.............. 0.10\r\n"
+ "BEP Test.................... -5\r\n"
+ "Local Rtrn Count............ 00\r\n"
+ "Remote Rtrn Count........... 00";
_diagnosticMessageJustSent = false;
}
else
{
// Simulate the reception of a response message returning a random message.
message = string.Empty;
int messageLength = messageRandomNumberFrom50To110;
for(int i = messageLength; i > 0; --i)
{
message += (char)messageRandomNumberFrom40To126;
}
}
return message;
}
}
}