* {@link VehicleHandler} transform data into state updates and handling of vehicle commands
*
* @author Bernd Weymann - Initial contribution
+ * @author Bernd Weymann - Bugfix https://github.com/openhab/openhab-addons/issues/16932
*/
@NonNullByDefault
public class VehicleHandler extends BaseThingHandler {
public void distributeCommandStatus(AppTwinCommandStatusUpdatesByPID cmdUpdates) {
Map<Long, AppTwinCommandStatus> updates = cmdUpdates.getUpdatesByPidMap();
updates.forEach((key, value) -> {
- // Command name
- ChannelStateMap csmCommand = new ChannelStateMap(OH_CHANNEL_CMD_NAME, GROUP_COMMAND,
- new DecimalType(value.getType().getNumber()));
- updateChannel(csmCommand);
- // Command State
- ChannelStateMap csmState = new ChannelStateMap(OH_CHANNEL_CMD_STATE, GROUP_COMMAND,
- new DecimalType(value.getState().getNumber()));
- updateChannel(csmState);
- // Command Time
- DateTimeType dtt = Utils.getDateTimeType(value.getTimestampInMs());
- UOMObserver observer = null;
- if (Locale.US.getCountry().equals(Utils.getCountry())) {
- observer = new UOMObserver(UOMObserver.TIME_US);
- } else {
- observer = new UOMObserver(UOMObserver.TIME_ROW);
+ try {
+ // getting type and state may throw Exception
+ int commandType = value.getType().getNumber();
+ int commandState = value.getState().getNumber();
+ // Command name
+ ChannelStateMap csmCommand = new ChannelStateMap(OH_CHANNEL_CMD_NAME, GROUP_COMMAND,
+ new DecimalType(commandType));
+ updateChannel(csmCommand);
+ // Command State
+ ChannelStateMap csmState = new ChannelStateMap(OH_CHANNEL_CMD_STATE, GROUP_COMMAND,
+ new DecimalType(commandState));
+ updateChannel(csmState);
+ // Command Time
+ DateTimeType dtt = Utils.getDateTimeType(value.getTimestampInMs());
+ UOMObserver observer = null;
+ if (Locale.US.getCountry().equals(Utils.getCountry())) {
+ observer = new UOMObserver(UOMObserver.TIME_US);
+ } else {
+ observer = new UOMObserver(UOMObserver.TIME_ROW);
+ }
+ ChannelStateMap csmUpdated = new ChannelStateMap(OH_CHANNEL_CMD_LAST_UPDATE, GROUP_COMMAND, dtt,
+ observer);
+ updateChannel(csmUpdated);
+ } catch (IllegalArgumentException iae) {
+ logger.trace("Cannot decode command {} {}", value.getAllFields().toString(), iae.getMessage());
+ // silent ignore update
}
- ChannelStateMap csmUpdated = new ChannelStateMap(OH_CHANNEL_CMD_LAST_UPDATE, GROUP_COMMAND, dtt, observer);
- updateChannel(csmUpdated);
});
}
import org.openhab.core.types.RefreshType;
import com.daimler.mbcarkit.proto.VehicleEvents.VEPUpdate;
+import com.daimler.mbcarkit.proto.Vehicleapi.AppTwinCommandStatus;
+import com.daimler.mbcarkit.proto.Vehicleapi.AppTwinCommandStatusUpdatesByPID;
/**
* {@link VehicleHandlerTest} check state updates and command sending of vehicles
*
* @author Bernd Weymann - Initial contribution
+ * @author Bernd Weymann - Additional test for https://github.com/openhab/openhab-addons/issues/16932
*/
@NonNullByDefault
class VehicleHandlerTest {
"Charge Program Command");
assertEquals(100, ahm.getCommand().getInt("max_soc"), "Charge Program SOC Setting");
}
+
+ @Test
+ /**
+ * Testing UNRECOGNIZED (-1) values in CommandStatus which throws Exception
+ */
+ public void testCommandDistribution() {
+ Thing thingMock = mock(Thing.class);
+ when(thingMock.getThingTypeUID()).thenReturn(Constants.THING_TYPE_BEV);
+ when(thingMock.getUID()).thenReturn(new ThingUID("test", Constants.BEV));
+ VehicleHandler vh = new VehicleHandler(thingMock, new LocationProviderMock(),
+ mock(MercedesMeCommandOptionProvider.class), mock(MercedesMeStateOptionProvider.class));
+ AppTwinCommandStatus command = AppTwinCommandStatus.newBuilder().setStateValue(-1).setTypeValue(-1).build();
+ AppTwinCommandStatusUpdatesByPID commandPid = AppTwinCommandStatusUpdatesByPID.newBuilder()
+ .putUpdatesByPid(Long.MIN_VALUE, command).build();
+ try {
+ vh.distributeCommandStatus(commandPid);
+ } catch (IllegalArgumentException iae) {
+ fail();
+ }
+ }
}