diff --git a/bundles/org.openhab.binding.mercedesme/src/main/java/org/openhab/binding/mercedesme/internal/handler/VehicleHandler.java b/bundles/org.openhab.binding.mercedesme/src/main/java/org/openhab/binding/mercedesme/internal/handler/VehicleHandler.java index 70e8a9780be..8e410277b2e 100644 --- a/bundles/org.openhab.binding.mercedesme/src/main/java/org/openhab/binding/mercedesme/internal/handler/VehicleHandler.java +++ b/bundles/org.openhab.binding.mercedesme/src/main/java/org/openhab/binding/mercedesme/internal/handler/VehicleHandler.java @@ -110,6 +110,7 @@ import com.google.protobuf.Int32Value; * {@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 { @@ -552,24 +553,33 @@ public class VehicleHandler extends BaseThingHandler { public void distributeCommandStatus(AppTwinCommandStatusUpdatesByPID cmdUpdates) { Map 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); }); } diff --git a/bundles/org.openhab.binding.mercedesme/src/test/java/org/openhab/binding/mercedesme/internal/handler/VehicleHandlerTest.java b/bundles/org.openhab.binding.mercedesme/src/test/java/org/openhab/binding/mercedesme/internal/handler/VehicleHandlerTest.java index 5aab32f86e9..cf55bd982d8 100644 --- a/bundles/org.openhab.binding.mercedesme/src/test/java/org/openhab/binding/mercedesme/internal/handler/VehicleHandlerTest.java +++ b/bundles/org.openhab.binding.mercedesme/src/test/java/org/openhab/binding/mercedesme/internal/handler/VehicleHandlerTest.java @@ -39,11 +39,14 @@ import org.openhab.core.thing.link.ItemChannelLinkRegistry; 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 { @@ -468,4 +471,24 @@ 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(); + } + } }