Skip to content

Commit

Permalink
Fix Dashboard Crashes (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
PPRAKA30 authored May 4, 2020
1 parent 8b7cf91 commit caf970f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
55 changes: 33 additions & 22 deletions library/src/main/java/com/openxc/measurements/BaseMeasurement.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package com.openxc.measurements;

import com.google.common.base.Objects;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.util.Log;

import com.google.common.base.Objects;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.openxc.NoValueException;
Expand All @@ -15,11 +21,6 @@
import com.openxc.util.AgingData;
import com.openxc.util.Range;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

/**
* The BaseMeasurement is the base implementation of the Measurement, and
* wraps an instance of a {@link Unit}, and the value it returns is always
Expand All @@ -38,7 +39,7 @@
* Measurement. If you know of a better way, please say so.
*/
public abstract class BaseMeasurement<TheUnit extends Unit>
implements Measurement {
implements Measurement {
private static final String TAG = BaseMeasurement.class.toString();

protected AgingData<TheUnit> mValue;
Expand Down Expand Up @@ -71,9 +72,7 @@ public abstract class BaseMeasurement<TheUnit extends Unit>
cacheMeasurementId(VehicleDoorStatus.class);
cacheMeasurementId(VehicleSpeed.class);
cacheMeasurementId(WindshieldWiperStatus.class);
} catch(UnrecognizedMeasurementTypeException e) {
Log.e("BaseMeasurement", "exception: " + e.toString());
}
} catch(UnrecognizedMeasurementTypeException e) { }
}

public abstract String getGenericName();
Expand Down Expand Up @@ -180,7 +179,7 @@ private static void cacheMeasurementId(
} catch(IllegalAccessException e) {
throw new UnrecognizedMeasurementTypeException(
measurementType + " has an inaccessible " +
"ID field", e);
"ID field", e);
}
}

Expand All @@ -195,31 +194,31 @@ public static MessageKey getKeyForMeasurement(
}

public static Class<? extends Measurement>
getClassForId(String measurementId)
getClassForId(String measurementId)
throws UnrecognizedMeasurementTypeException {
Class<? extends Measurement> result = sMeasurementIdToClass.get(
measurementId);
if(result == null) {
throw new UnrecognizedMeasurementTypeException(
"Didn't have a measurement with ID " + measurementId +
" cached");
" cached");
}
return result;
}

public static Measurement getMeasurementFromMessage(
SimpleVehicleMessage message)
throws UnrecognizedMeasurementTypeException, NoValueException {
throws UnrecognizedMeasurementTypeException, NoValueException {
Class<? extends Measurement> measurementClass =
BaseMeasurement.getClassForId(message.getName());
BaseMeasurement.getClassForId(message.getName());
return BaseMeasurement.getMeasurementFromMessage(measurementClass,
message);
}

public static Measurement getMeasurementFromMessage(
Class<? extends Measurement> measurementType,
SimpleVehicleMessage message)
throws UnrecognizedMeasurementTypeException, NoValueException {
throws UnrecognizedMeasurementTypeException, NoValueException {
Constructor<? extends Measurement> constructor;
if(message == null) {
throw new NoValueException();
Expand All @@ -242,15 +241,30 @@ public static Measurement getMeasurementFromMessage(
eventClass = Number.class;
}

try {
constructor = measurementType.getConstructor(
valueClass, eventClass);

} catch(NoSuchMethodException e) {
throw new UnrecognizedMeasurementTypeException(
measurementType +
" doesn't have the expected constructor, " +
measurementType + "(" +
valueClass + ", " + eventClass + ")");
}

measurement = constructor.newInstance(
eventedMessage.getValue(),
eventedMessage.getEvent());
} else {
try {
constructor = measurementType.getConstructor(valueClass);
} catch(NoSuchMethodException e) {
throw new UnrecognizedMeasurementTypeException(
measurementType +
" doesn't have the expected constructor, " +
measurementType + "(" +
valueClass + ")");
}

measurement = constructor.newInstance(
simpleMessage.getValue());
Expand All @@ -274,10 +288,7 @@ public static Measurement getMeasurementFromMessage(
throw new UnrecognizedMeasurementTypeException(
measurementType + "'s constructor threw an exception",
e);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return null;
}

@Override
Expand All @@ -297,11 +308,11 @@ public boolean equals(Object obj) {
@SuppressWarnings("unchecked")
final BaseMeasurement<TheUnit> other = (BaseMeasurement<TheUnit>) obj;
return Objects.equal(getValue(), other.getValue()) &&
Objects.equal(other.getRange(), getRange());
Objects.equal(other.getRange(), getRange());
}

@Override
public String toString() {
return getValue().toString();
}
}
}
6 changes: 6 additions & 0 deletions library/src/test/java/com/openxc/BaseMeasurementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ public void buildFromNull() throws NoValueException,
BaseMeasurement.getMeasurementFromMessage(VehicleSpeed.class, null);
}

@Test(expected=UnrecognizedMeasurementTypeException.class)
public void testForUnrecognizedMeasurementTypeException() throws UnrecognizedMeasurementTypeException, NoValueException {
SimpleVehicleMessage message = new SimpleVehicleMessage(0L, "door_status", "rear_left: false");
Measurement measurement = BaseMeasurement.getMeasurementFromMessage(VehicleDoorStatus.class, message);
}

@Test
public void buildEventedFromMessage()
throws UnrecognizedMeasurementTypeException, NoValueException {
Expand Down

0 comments on commit caf970f

Please sign in to comment.