@Override public String toString() { return MoreObjects.toStringHelper(this) .add("timestamp", getTimestamp()) .add("command", getCommand()) .add("status", getStatus()) .add("message", getMessage()) .add("extras", getExtras()) .toString(); }
@Override public void writeToParcel(Parcel out, int flags) { super.writeToParcel(out, flags); out.writeSerializable(getCommand()); out.writeInt(getStatus() ? 1 : 0); out.writeString(getMessage()); }
@Override public MessageKey getKey() { if(super.getKey() == null) { HashMap<String, Object> key = new HashMap<>(); key.put(Command.COMMAND_KEY, getCommand()); setKey(new MessageKey(key)); } return super.getKey(); }
if(message.getCommand().equals(CommandType.VERSION)) { messageBuilder.setType(BinaryMessages.ControlCommand.Type.VERSION); } else if(message.getCommand().equals(CommandType.DEVICE_ID)) { messageBuilder.setType(BinaryMessages.ControlCommand.Type.DEVICE_ID); } else if(message.getCommand().equals(CommandType.PLATFORM)) { messageBuilder.setType(BinaryMessages.ControlCommand.Type.PLATFORM); } else if(message.getCommand().equals(CommandType.PASSTHROUGH)) { messageBuilder.setType(BinaryMessages.ControlCommand.Type.PASSTHROUGH); } else if(message.getCommand().equals(CommandType.AF_BYPASS)) { messageBuilder.setType(BinaryMessages.ControlCommand.Type.ACCEPTANCE_FILTER_BYPASS); } else if(message.getCommand().equals(CommandType.PAYLOAD_FORMAT)) { messageBuilder.setType(BinaryMessages.ControlCommand.Type.PAYLOAD_FORMAT); } else if(message.getCommand().equals(CommandType.SD_MOUNT_STATUS)) { messageBuilder.setType(BinaryMessages.ControlCommand.Type.SD_MOUNT_STATUS); } else if(message.getCommand().equals(CommandType.RTC_CONFIGURATION)) { messageBuilder.setType(BinaryMessages.ControlCommand.Type.RTC_CONFIGURATION); } else if(message.getCommand().equals(CommandType.DIAGNOSTIC_REQUEST)) { messageBuilder.setType(BinaryMessages.ControlCommand.Type.DIAGNOSTIC); } else { throw new SerializationException( "Unrecognized command type in response: " + message.getCommand()); messageBuilder.setStatus(message.getStatus()); if(message.hasMessage()) { messageBuilder.setMessage(message.getMessage());
@Before public void setup() { response = new CommandResponse(command, status, message); }
@Test public void differentCommandDoesntEqual() { CommandResponse anotherResponse = new CommandResponse( CommandType.DEVICE_ID, status); assertFalse(response.equals(anotherResponse)); }
/** * Send a command request to the vehicle that does not require any metadata. * * @param type The command request type to send to the VI. * @return The message returned by the VI in response to this command or * null if none was received. */ public String requestCommandMessage(CommandType type) { VehicleMessage message = request(new Command(type)); String value = null; if(message != null) { // Because we use the same key and value for commands and command // responses, if for some reason a Command is echoed back to the // device instead of a CommandResponse, you could get a casting // exception when trying to cast this message here. If we got a // Command, just ignore it and assume no response - I wasn't able to // reproduce it but we did have a few Bugsnag reports about it. try { CommandResponse response = message.asCommandResponse(); if(response.getStatus()) { value = response.getMessage(); } } catch(ClassCastException e) { Log.w(TAG, "Expected a command response but got " + message + " -- ignoring, assuming no response"); } } return value; }
} else if (Command.containsRequiredFields(fields)) { message = sGson.fromJson(root, Command.class); } else if (CommandResponse.containsRequiredFields(fields)) { message = sGson.fromJson(root, CommandResponse.class); if (((CommandResponse) message).getCommand() == null) {
@Test public void getCommandReturnsCommand() { assertEquals(command, response.getCommand()); }
@Test public void getStatusReturnsStatus() { assertEquals(status, response.getStatus()); }
@Test public void keyNotNull() { assertThat(response.getKey(), notNullValue()); }
@Test public void serializeCommandResponseContainsStatus() { boolean status = true; String serialized = JsonFormatter.serialize(new CommandResponse( Command.CommandType.VERSION, true)); assertThat(serialized, containsString("status")); assertThat(serialized, containsString("true")); }
@Test public void differentMessageDoesntEqual() { CommandResponse anotherResponse = new CommandResponse( command, status, message + " different"); assertFalse(response.equals(anotherResponse)); }
@Test public void getMessageReturnsMessage() { assertEquals(command, response.getCommand()); }
@Test public void serializeCommandResponseUsesStringCommand() { boolean status = true; String serialized = JsonFormatter.serialize(new CommandResponse( Command.CommandType.VERSION, true)); assertThat(serialized, containsString("version")); }
@Test public void differentStatusDoesntEqual() { CommandResponse anotherResponse = new CommandResponse( command, !status, message); assertFalse(response.equals(anotherResponse)); }
@Test public void serializeCommandResponseWithMessage() { serializeDeserializeAndCheckEqual(new CommandResponse( Command.CommandType.DEVICE_ID, true, "bar")); serializeDeserializeAndCheckEqual(new CommandResponse( Command.CommandType.VERSION, true, "bar")); }
@Test public void serializeCommandResponse() { serializeDeserializeAndCheckEqual(new CommandResponse( Command.CommandType.DEVICE_ID, true)); }
@Test public void sameCommandAndMessageEquals() { CommandResponse anotherResponse = new CommandResponse( command, status, message); assertEquals(response, anotherResponse); }
message = response.getMessage(); return new CommandResponse(commandType, response.getStatus(), message);