bugfix websocket exception (#16962)

Signed-off-by: Bernd Weymann <bernd.weymann@gmail.com>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Bernd Weymann 2024-06-29 19:02:38 +02:00 committed by Ciprian Pascu
parent f61d4ed3bf
commit 287afde891
2 changed files with 50 additions and 17 deletions

View File

@ -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<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);
});
}

View File

@ -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();
}
}
}