Skip to content

Commit

Permalink
TelemetrySystem/Java: synchronised code with the latest changes to th…
Browse files Browse the repository at this point in the history
…e C# version.
  • Loading branch information
Luca Minudel committed Sep 12, 2013
1 parent 7ebc888 commit fb487a9
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 75 deletions.
Expand Up @@ -4,12 +4,18 @@

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 static final String DIAGNOSTIC_MESSAGE = "AT#UD";

private boolean onlineStatus;
private String diagnosticMessageResult = "";
private boolean diagnosticMessageJustSent = false;

private final Random connectionEventsSimulator = new Random(42);
private final Random connectionEventsSimulator = new Random();
private final Random randomMessageSimulator = new Random();

public boolean getOnlineStatus()
{
Expand All @@ -23,8 +29,8 @@ public void connect(String telemetryServerConnectionString)
throw new IllegalArgumentException();
}

// simulate the operation on a real modem
boolean success = connectionEventsSimulator.nextInt(10) <= 8;
// Fake the connection with 20% chances of success
boolean success = connectionEventsSimulator.nextInt(10) <= 2;

onlineStatus = success;
}
Expand All @@ -41,51 +47,52 @@ public void send(String message)
throw new IllegalArgumentException();
}

if (message == DIAGNOSTIC_MESSAGE)
{
// simulate a status report
diagnosticMessageResult =
"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";

return;
}

// here should go the real Send operation
}
// 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 == DIAGNOSTIC_MESSAGE)
{
diagnosticMessageJustSent = true;
}
else
{
diagnosticMessageJustSent = false;
}
}

public String receive()
{
String message;

if (diagnosticMessageResult == null || "".equals(diagnosticMessageResult))
{
// simulate a received message
message = "";
int messageLength = connectionEventsSimulator.nextInt(50) + 60;
for(int i = messageLength; i >=0; --i)
{
message += (char)connectionEventsSimulator.nextInt(40) + 86;
}

}
else
{
message = diagnosticMessageResult;
diagnosticMessageResult = "";
}
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 = "";
int messageLength = randomMessageSimulator.nextInt(50) + 60;
for(int i = messageLength; i > 0; --i)
{
message += (char)randomMessageSimulator.nextInt(40) + 86;
}
}

return message;
}
Expand Down
Expand Up @@ -2,43 +2,45 @@

public class TelemetryDiagnosticControls
{
private final String DiagnosticChannelConnectionString = "*111#";
private final static String DIAGNOSTIC_CHANNEL_CONNECTION_STRING = "*111#";

private final TelemetryClient telemetryClient;
private String diagnosticInfo = "";

public TelemetryDiagnosticControls()
public TelemetryDiagnosticControls()
{
telemetryClient = new TelemetryClient();
}

public String getDiagnosticInfo()
{
return diagnosticInfo;
}

public void setDiagnosticInfo(String diagnosticInfo)
{
this.diagnosticInfo = diagnosticInfo;
}

public void checkTransmission() throws Exception
{
diagnosticInfo = "";

telemetryClient.disconnect();

int retryLeft = 3;
while (telemetryClient.getOnlineStatus() == false && retryLeft > 0)
{
telemetryClient = new TelemetryClient();
}

public String getDiagnosticInfo(){
return diagnosticInfo;
}

public void setDiagnosticInfo(String diagnosticInfo){
this.diagnosticInfo = diagnosticInfo;
telemetryClient.connect(DIAGNOSTIC_CHANNEL_CONNECTION_STRING);
retryLeft -= 1;
}
public void checkTransmission() throws Exception

if(telemetryClient.getOnlineStatus() == false)
{
diagnosticInfo = "";

telemetryClient.disconnect();

int retryLeft = 3;
while (telemetryClient.getOnlineStatus() == false && retryLeft > 0)
{
telemetryClient.connect(DiagnosticChannelConnectionString);
retryLeft -= 1;
}

if(telemetryClient.getOnlineStatus() == false)
{
throw new Exception("Unable to connect.");
}

telemetryClient.send(TelemetryClient.DIAGNOSTIC_MESSAGE);
diagnosticInfo = telemetryClient.receive();
throw new Exception("Unable to connect.");
}

telemetryClient.send(TelemetryClient.DIAGNOSTIC_MESSAGE);
diagnosticInfo = telemetryClient.receive();
}
}

0 comments on commit fb487a9

Please sign in to comment.