@Override public String toString() { return MoreObjects.toStringHelper(this) .add("timestamp", getTimestamp()) .add("bus", getBusId()) .add("id", getId()) .add("data", Arrays.toString(getData())) .toString(); }
protected CanMessage(Parcel in) { readFromParcel(in); } }
/** * Sort by bus, then by message ID. */ @Override public int compareTo(VehicleMessage other) { CanMessage otherMessage = (CanMessage) other; if(getBusId() < otherMessage.getBusId()) { return -1; } else if(getBusId() > otherMessage.getBusId()) { return 1; } else { if(getId() < otherMessage.getId()) { return -1; } else if(getId() > otherMessage.getId()) { return 1; } } return 0; }
@Test public void differentBusNotEqual() { CanMessage anotherMessage = new CanMessage(id, bus + 1, data); assertThat(message, not(equalTo(anotherMessage))); }
@Test public void setNoData() { message = new CanMessage(bus, id, null); assertThat(message.getData(), notNullValue()); assertArrayEquals(new byte[]{0,0,0,0,0,0,0,0}, message.getData()); }
private void onSendCanMessage(Spinner busSpinner, EditText idView, EditText payloadView) { boolean validInput = true; if(idView.getText().toString().isEmpty()) { idView.setError("ID is required"); validInput = false; } if(payloadView.getText().toString().isEmpty()) { payloadView.setError("Payload is required"); validInput = false; } if(payloadView.getText().toString().length() % 2 == 1) { payloadView.setError("Payload must be specified as full bytes"); validInput = false; } if(validInput) { System.out.println(payloadView.getText().toString()); CanMessage message = new CanMessage( Integer.valueOf(busSpinner.getSelectedItem().toString()), Integer.valueOf(idView.getText().toString(), 16), ByteAdapter.hexStringToByteArray(payloadView.getText().toString())); mVehicleManager.send(message); // Make sure to update after sending so the timestamp is set by the // VehicleManager mAdapter.add(message.asCanMessage()); } else { Log.i(TAG, "Form is invalid, not sending message"); } }
@Test public void getBusReturnsBus() { assertEquals(bus, message.getBusId()); }
@Test public void getDataReturnsData() { assertArrayEquals(data, message.getData()); }
@Test public void getIdReturnsId() { assertEquals(id, message.getId()); }
@Test public void keyNotNull() { assertThat(message.getKey(), notNullValue()); } }
if (CanMessage.containsRequiredFields(fields)) { message = sGson.fromJson(root, CanMessage.class); } else if (DiagnosticResponse.containsRequiredFields(fields)) {
private static CanMessage deserializeCanMessage( BinaryMessages.VehicleMessage binaryMessage) { BinaryMessages.CanMessage canMessage = binaryMessage.getCanMessage(); return new CanMessage(canMessage.getBus(), canMessage.getId(), canMessage.getData().toByteArray()); }
@Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView == null) { convertView = LayoutInflater.from(mContext) .inflate(R.layout.can_message_list_item, parent, false); } CanMessage message = getItem(position); TextView timestampView = (TextView) convertView.findViewById(R.id.timestamp); timestampView.setText(formatTimestamp(message)); TextView busView = (TextView) convertView.findViewById(R.id.bus); busView.setText("" + message.getBusId()); TextView idView = (TextView) convertView.findViewById(R.id.id); idView.setText("0x" + Integer.toHexString(message.getId())); TextView dataView = (TextView) convertView.findViewById(R.id.data); dataView.setText("0x" + ByteAdapter.byteArrayToHexString( message.getData())); return convertView; } }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.can_message_details); Bundle extras = getIntent().getExtras(); if (extras != null) { CanMessage message = extras.getParcelable(EXTRA_CAN_MESSAGE); TextView timestampView = (TextView) findViewById(R.id.timestamp); timestampView.setText("" + message.getTimestamp()); TextView busView = (TextView) findViewById(R.id.bus); busView.setText("" + message.getBusId()); TextView idView = (TextView) findViewById(R.id.id); idView.setText("0x" + Integer.toHexString(message.getId())); TextView dataView = (TextView) findViewById(R.id.data); dataView.setText("0x" + ByteAdapter.byteArrayToHexString( message.getData())); } else { finish(); } } }
@Test public void differentIdNotEqual() { CanMessage anotherMessage = new CanMessage(id + 1, bus, data); assertThat(message, not(equalTo(anotherMessage))); }
private static void serializeCanMessage(BinaryMessages.VehicleMessage.Builder builder, CanMessage message) { builder.setType(BinaryMessages.VehicleMessage.Type.CAN); BinaryMessages.CanMessage.Builder messageBuilder = BinaryMessages.CanMessage.newBuilder(); messageBuilder.setBus(message.getBusId()); messageBuilder.setId(message.getId()); messageBuilder.setData(ByteString.copyFrom(message.getData())); builder.setCanMessage(messageBuilder); }
@Test public void differentDataNotEqual() { CanMessage anotherMessage = new CanMessage(id, bus, new byte[] {8,7,6,5,4,3,2,1}); assertThat(message, not(equalTo(anotherMessage))); }
@Before public void setup() { message = new CanMessage(bus, id, data); }