Skip to content

Commit

Permalink
[mercedesme] take end of charge weekday into account (openhab#17001)
Browse files Browse the repository at this point in the history
* take end of charge day into account

Signed-off-by: Bernd Weymann <[email protected]>
Signed-off-by: Patrik Gfeller <[email protected]>
  • Loading branch information
weymann authored and pgfeller committed Sep 29, 2024
1 parent 3cc8e8f commit a43ea06
Show file tree
Hide file tree
Showing 7 changed files with 1,922 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public class Constants {
public static final String MB_KEY_SOC = "soc";
public static final String MB_KEY_TIRE_PRESS_MEAS_TIMESTAMP = "tirePressMeasTimestamp";
public static final String MB_KEY_ENDOFCHARGETIME = "endofchargetime";
public static final String MB_KEY_ENDOFCHARGEDAY = "endofChargeTimeWeekday";
public static final String MB_KEY_LIQUIDCONSUMPTIONRESET = "liquidconsumptionreset";
public static final String MB_KEY_LIQUIDCONSUMPTIONSTART = "liquidconsumptionstart";
public static final String MB_KEY_ELECTRICCONSUMPTIONRESET = "electricconsumptionreset";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import static org.openhab.binding.mercedesme.internal.Constants.*;

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -894,6 +895,36 @@ public void distributeContent(VEPUpdate data) {
}
}

/**
* handle day of charge end
*/
ChannelStateMap chargeTimeEndCSM = eventStorage.get(GROUP_CHARGE + "#" + OH_CHANNEL_END_TIME);
if (chargeTimeEndCSM != null) {
State entTimeState = chargeTimeEndCSM.getState();
if (entTimeState instanceof DateTimeType endDateTimeType) {
// we've a valid charged end time
VehicleAttributeStatus vas = atts.get(MB_KEY_ENDOFCHARGEDAY);
if (vas != null && !Utils.isNil(vas)) {
// proto weekday starts with MONDAY=0, java ZonedDateTime starts with MONDAY=1
long estimatedWeekday = Utils.getInt(vas) + 1;
ZonedDateTime storedZdt = endDateTimeType.getZonedDateTime();
long storedWeekday = storedZdt.getDayOfWeek().getValue();
// check if estimated weekday is smaller than stored
// estimation Monday=1 vs. stored Saturday=6 => (7+1)-6=2 days ahead
if (estimatedWeekday < storedWeekday) {
estimatedWeekday += 7;
}
if (estimatedWeekday != storedWeekday) {
DateTimeType adjustedDtt = new DateTimeType(
storedZdt.plusDays(estimatedWeekday - storedWeekday));
ChannelStateMap adjustedCsm = new ChannelStateMap(OH_CHANNEL_END_TIME, GROUP_CHARGE,
adjustedDtt);
updateChannel(adjustedCsm);
}
}
}
}

/**
* Check if Websocket shall be kept alive
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.daimler.mbcarkit.proto.VehicleEvents.PushMessage;
import com.daimler.mbcarkit.proto.Vehicleapi.AcknowledgeAppTwinCommandStatusUpdatesByVIN;
import com.daimler.mbcarkit.proto.Vehicleapi.AppTwinCommandStatusUpdatesByVIN;
import com.daimler.mbcarkit.proto.Vehicleapi.AppTwinPendingCommandsRequest;

/**
* {@link MBWebsocket} as socket endpoint to communicate with Mercedes
Expand Down Expand Up @@ -227,7 +228,10 @@ public void onBytes(InputStream is) {
.build();
sendAcknowledgeMessage(cm);
} else if (pm.hasApptwinPendingCommandRequest()) {
logger.trace("Pending Command {}", pm.getApptwinPendingCommandRequest().getAllFields());
AppTwinPendingCommandsRequest pending = pm.getApptwinPendingCommandRequest();
if (!pending.getAllFields().isEmpty()) {
logger.trace("Pending Command {}", pending.getAllFields());
}
} else if (pm.hasDebugMessage()) {
logger.trace("MB Debug Message: {}", pm.getDebugMessage().getMessage());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
<channel-type id="end-time">
<item-type>DateTime</item-type>
<label>Charge End Estimation</label>
<state pattern="%1$tH:%1$tM" readOnly="true"/>
<state pattern="%1$td.%1$tm. %1$tH:%1$tM" readOnly="true"/>
</channel-type>
<channel-type id="max-soc">
<item-type>Number:Dimensionless</item-type>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,32 @@ public void testBEVCharging() {
.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM"), "End of Charge Time");
}

@Test
public void testBEVChargeEndtime() {
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));
vh.accountHandler = Optional.of(mock(AccountHandler.class));
VehicleConfiguration vehicleConfig = new VehicleConfiguration();
vh.config = Optional.of(vehicleConfig);
ThingCallbackListener updateListener = new ThingCallbackListener();
vh.setCallback(updateListener);

String json = FileReader.readFileInString("src/test/resources/proto-json/MB-BEV-EQA-Charging-Weekday.json");
VEPUpdate update = ProtoConverter.json2Proto(json, true);
vh.distributeContent(update);
assertEquals("2023-09-09 13:54", ((DateTimeType) updateListener.getResponse("test::bev:charge#end-time"))
.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM"), "End of Charge Time");

json = FileReader.readFileInString("src/test/resources/proto-json/MB-BEV-EQA-Charging-Weekday-Underrun.json");
update = ProtoConverter.json2Proto(json, true);
vh.distributeContent(update);
assertEquals("2023-09-11 13:55", ((DateTimeType) updateListener.getResponse("test::bev:charge#end-time"))
.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM"), "End of Charge Time");
}

@Test
public void testBEVPartialChargingUpdate() {
Thing thingMock = mock(Thing.class);
Expand Down
Loading

0 comments on commit a43ea06

Please sign in to comment.