Skip to content

Commit

Permalink
extract interface from TelemeteryClient
Browse files Browse the repository at this point in the history
make and change TelemeteryDiagnosticControls depend on abstracted
telemetery client
  • Loading branch information
donbing committed May 21, 2012
1 parent befaa29 commit 3cc4aea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
39 changes: 24 additions & 15 deletions TDDMicroExercises/CSharp/TelemetrySystem/TelemetryClient.cs
Expand Up @@ -2,18 +2,27 @@

namespace TDDMicroExercises.TelemetrySystem
{
public class TelemetryClient
{
public interface ITelemetryClient
{
bool OnlineStatus { get; }
void Connect(string telemetryServerConnectionString);
void Disconnect();
void Send(string message);
string Receive();
}

public class TelemetryClient : ITelemetryClient
{
public const string DiagnosticMessage = "AT#UD";

private bool _onlineStatus;
private string _diagnosticMessageResult = string.Empty;
private bool onlineStatus;
private string diagnosticMessageResult = string.Empty;

private readonly Random _connectionEventsSimulator = new Random(42);
private readonly Random connectionEventsSimulator = new Random(42);

public bool OnlineStatus
{
get { return _onlineStatus; }
get { return onlineStatus; }
}

public void Connect(string telemetryServerConnectionString)
Expand All @@ -24,15 +33,15 @@ public void Connect(string telemetryServerConnectionString)
}

// simulate the operation on a real modem
bool success = _connectionEventsSimulator.Next(1, 10) <= 8;
bool success = connectionEventsSimulator.Next(1, 10) <= 8;

_onlineStatus = success;
onlineStatus = success;

}

public void Disconnect()
{
_onlineStatus = false;
onlineStatus = false;
}

public void Send(string message)
Expand All @@ -45,7 +54,7 @@ public void Send(string message)
if (message == DiagnosticMessage)
{
// simulate a status report
_diagnosticMessageResult =
diagnosticMessageResult =
"LAST TX rate................ 100 MBPS\r\n"
+ "HIGHEST TX rate............. 100 MBPS\r\n"
+ "LAST RX rate................ 100 MBPS\r\n"
Expand All @@ -71,19 +80,19 @@ public string Receive()
{
string message;

if (string.IsNullOrEmpty(_diagnosticMessageResult) == false)
if (string.IsNullOrEmpty(diagnosticMessageResult) == false)
{
message = _diagnosticMessageResult;
_diagnosticMessageResult = string.Empty;
message = diagnosticMessageResult;
diagnosticMessageResult = string.Empty;
}
else
{
// simulate a received message
message = string.Empty;
int messageLenght = _connectionEventsSimulator.Next(50, 110);
int messageLenght = connectionEventsSimulator.Next(50, 110);
for(int i = messageLenght; i >=0; --i)
{
message += (char)_connectionEventsSimulator.Next(40, 126);
message += (char)connectionEventsSimulator.Next(40, 126);
}
}

Expand Down
Expand Up @@ -7,10 +7,10 @@ public class TelemetryDiagnosticControls
{
private const string DiagnosticChannelConnectionString = "*111#";

private readonly TelemetryClient telemetryClient;
private readonly ITelemetryClient telemetryClient;
private string diagnosticInfo = string.Empty;

public TelemetryDiagnosticControls(TelemetryClient client)
public TelemetryDiagnosticControls(ITelemetryClient client)
{
telemetryClient = client;
}
Expand Down

0 comments on commit 3cc4aea

Please sign in to comment.