Skip to content

Commit 104a71b

Browse files
authored
bugfix websocket exception (openhab#16962)
Signed-off-by: Bernd Weymann <[email protected]>
1 parent bd2fd55 commit 104a71b

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

bundles/org.openhab.binding.mercedesme/src/main/java/org/openhab/binding/mercedesme/internal/handler/VehicleHandler.java

+27-17
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
* {@link VehicleHandler} transform data into state updates and handling of vehicle commands
111111
*
112112
* @author Bernd Weymann - Initial contribution
113+
* @author Bernd Weymann - Bugfix https://github.com/openhab/openhab-addons/issues/16932
113114
*/
114115
@NonNullByDefault
115116
public class VehicleHandler extends BaseThingHandler {
@@ -552,24 +553,33 @@ private void configureSeats(ChannelUID channelUID, State command) {
552553
public void distributeCommandStatus(AppTwinCommandStatusUpdatesByPID cmdUpdates) {
553554
Map<Long, AppTwinCommandStatus> updates = cmdUpdates.getUpdatesByPidMap();
554555
updates.forEach((key, value) -> {
555-
// Command name
556-
ChannelStateMap csmCommand = new ChannelStateMap(OH_CHANNEL_CMD_NAME, GROUP_COMMAND,
557-
new DecimalType(value.getType().getNumber()));
558-
updateChannel(csmCommand);
559-
// Command State
560-
ChannelStateMap csmState = new ChannelStateMap(OH_CHANNEL_CMD_STATE, GROUP_COMMAND,
561-
new DecimalType(value.getState().getNumber()));
562-
updateChannel(csmState);
563-
// Command Time
564-
DateTimeType dtt = Utils.getDateTimeType(value.getTimestampInMs());
565-
UOMObserver observer = null;
566-
if (Locale.US.getCountry().equals(Utils.getCountry())) {
567-
observer = new UOMObserver(UOMObserver.TIME_US);
568-
} else {
569-
observer = new UOMObserver(UOMObserver.TIME_ROW);
556+
try {
557+
// getting type and state may throw Exception
558+
int commandType = value.getType().getNumber();
559+
int commandState = value.getState().getNumber();
560+
// Command name
561+
ChannelStateMap csmCommand = new ChannelStateMap(OH_CHANNEL_CMD_NAME, GROUP_COMMAND,
562+
new DecimalType(commandType));
563+
updateChannel(csmCommand);
564+
// Command State
565+
ChannelStateMap csmState = new ChannelStateMap(OH_CHANNEL_CMD_STATE, GROUP_COMMAND,
566+
new DecimalType(commandState));
567+
updateChannel(csmState);
568+
// Command Time
569+
DateTimeType dtt = Utils.getDateTimeType(value.getTimestampInMs());
570+
UOMObserver observer = null;
571+
if (Locale.US.getCountry().equals(Utils.getCountry())) {
572+
observer = new UOMObserver(UOMObserver.TIME_US);
573+
} else {
574+
observer = new UOMObserver(UOMObserver.TIME_ROW);
575+
}
576+
ChannelStateMap csmUpdated = new ChannelStateMap(OH_CHANNEL_CMD_LAST_UPDATE, GROUP_COMMAND, dtt,
577+
observer);
578+
updateChannel(csmUpdated);
579+
} catch (IllegalArgumentException iae) {
580+
logger.trace("Cannot decode command {} {}", value.getAllFields().toString(), iae.getMessage());
581+
// silent ignore update
570582
}
571-
ChannelStateMap csmUpdated = new ChannelStateMap(OH_CHANNEL_CMD_LAST_UPDATE, GROUP_COMMAND, dtt, observer);
572-
updateChannel(csmUpdated);
573583
});
574584
}
575585

bundles/org.openhab.binding.mercedesme/src/test/java/org/openhab/binding/mercedesme/internal/handler/VehicleHandlerTest.java

+23
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,14 @@
3939
import org.openhab.core.types.RefreshType;
4040

4141
import com.daimler.mbcarkit.proto.VehicleEvents.VEPUpdate;
42+
import com.daimler.mbcarkit.proto.Vehicleapi.AppTwinCommandStatus;
43+
import com.daimler.mbcarkit.proto.Vehicleapi.AppTwinCommandStatusUpdatesByPID;
4244

4345
/**
4446
* {@link VehicleHandlerTest} check state updates and command sending of vehicles
4547
*
4648
* @author Bernd Weymann - Initial contribution
49+
* @author Bernd Weymann - Additional test for https://github.com/openhab/openhab-addons/issues/16932
4750
*/
4851
@NonNullByDefault
4952
class VehicleHandlerTest {
@@ -468,4 +471,24 @@ public void testChargeProgramSelection() {
468471
"Charge Program Command");
469472
assertEquals(100, ahm.getCommand().getInt("max_soc"), "Charge Program SOC Setting");
470473
}
474+
475+
@Test
476+
/**
477+
* Testing UNRECOGNIZED (-1) values in CommandStatus which throws Exception
478+
*/
479+
public void testCommandDistribution() {
480+
Thing thingMock = mock(Thing.class);
481+
when(thingMock.getThingTypeUID()).thenReturn(Constants.THING_TYPE_BEV);
482+
when(thingMock.getUID()).thenReturn(new ThingUID("test", Constants.BEV));
483+
VehicleHandler vh = new VehicleHandler(thingMock, new LocationProviderMock(),
484+
mock(MercedesMeCommandOptionProvider.class), mock(MercedesMeStateOptionProvider.class));
485+
AppTwinCommandStatus command = AppTwinCommandStatus.newBuilder().setStateValue(-1).setTypeValue(-1).build();
486+
AppTwinCommandStatusUpdatesByPID commandPid = AppTwinCommandStatusUpdatesByPID.newBuilder()
487+
.putUpdatesByPid(Long.MIN_VALUE, command).build();
488+
try {
489+
vh.distributeCommandStatus(commandPid);
490+
} catch (IllegalArgumentException iae) {
491+
fail();
492+
}
493+
}
471494
}

0 commit comments

Comments
 (0)