Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimeOfUseTariff: improvements for EVCS #2947

Merged
merged 3 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion io.openems.common/src/io/openems/common/utils/JsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static io.openems.common.utils.EnumUtils.toEnum;

import java.net.Inet4Address;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -37,7 +38,11 @@
import io.openems.common.exceptions.OpenemsError.OpenemsNamedException;
import io.openems.common.types.OpenemsType;

public class JsonUtils {
public final class JsonUtils {

private JsonUtils() {
}

/**
* Provide a easy way to generate a JsonArray from a list using the given
* convert function to add each element.
Expand Down Expand Up @@ -291,6 +296,24 @@ public JsonObjectBuilder addProperty(String property, ZonedDateTime value) {
return this;
}

/**
* Add a {@link LocalDateTime} value to the {@link JsonObject}.
*
* <p>
* The value gets added in the format of
* {@link DateTimeFormatter#ISO_LOCAL_DATE_TIME}.
*
* @param property the key
* @param value the value
* @return the {@link JsonObjectBuilder}
*/
public JsonObjectBuilder addProperty(String property, LocalDateTime value) {
if (value != null) {
this.j.addProperty(property, value.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
}
return this;
}

/**
* Add a {@link Boolean} value to the {@link JsonObject}.
*
Expand Down Expand Up @@ -1723,6 +1746,32 @@ public static ZonedDateTime getAsZonedDateWithZeroTime(JsonElement element, Stri
}
}

/**
* Takes a JSON in the form '2020-01-01T00:00:00' and converts it to a
* {@link LocalDateTime}.
*
* @param jElement the {@link JsonElement}
* @param memberName the name of the member of the JsonObject
* @return the {@link ZonedDateTime}
*/
public static LocalDateTime getAsLocalDateTime(JsonElement jElement, String memberName)
throws OpenemsNamedException {
return DateUtils.parseLocalDateTimeOrError(toString(toPrimitive(toSubElement(jElement, memberName))));
}

/**
* Takes a JSON in the form '2020-01-01T00:00:00Z' and converts it to a
* {@link ZonedDateTime}.
*
* @param jElement the {@link JsonElement}
* @param memberName the name of the member of the JsonObject
* @return the {@link ZonedDateTime}
*/
public static ZonedDateTime getAsZonedDateTime(JsonElement jElement, String memberName)
throws OpenemsNamedException {
return DateUtils.parseZonedDateTimeOrError(toString(toPrimitive(toSubElement(jElement, memberName))));
}

/**
* Takes a JSON in the form 'YYYY-MM-DD' and converts it to a
* {@link ZonedDateTime}.
Expand Down
16 changes: 16 additions & 0 deletions io.openems.common/test/io/openems/common/utils/JsonUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import static io.openems.common.utils.JsonUtils.getAsJsonArray;
import static io.openems.common.utils.JsonUtils.getAsJsonElement;
import static io.openems.common.utils.JsonUtils.getAsJsonObject;
import static io.openems.common.utils.JsonUtils.getAsLocalDateTime;
import static io.openems.common.utils.JsonUtils.getAsLong;
import static io.openems.common.utils.JsonUtils.getAsOptionalBoolean;
import static io.openems.common.utils.JsonUtils.getAsOptionalDouble;
Expand All @@ -64,6 +65,7 @@
import static io.openems.common.utils.JsonUtils.getAsStringOrElse;
import static io.openems.common.utils.JsonUtils.getAsType;
import static io.openems.common.utils.JsonUtils.getAsUUID;
import static io.openems.common.utils.JsonUtils.getAsZonedDateTime;
import static io.openems.common.utils.JsonUtils.getAsZonedDateWithZeroTime;
import static io.openems.common.utils.JsonUtils.getOptionalSubElement;
import static io.openems.common.utils.JsonUtils.getSubElement;
Expand All @@ -80,11 +82,13 @@
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;
Expand Down Expand Up @@ -148,6 +152,8 @@ private static final <T extends Throwable> void assertAllThrow(Class<T> expected
.addProperty("Enum3", (Unit) null) //
.addProperty("Inet4Address", "192.168.1.2") //
.addProperty("UUID", "c48e2e28-09be-41d5-8e58-260d162991cc") //
.addProperty("ZonedDateTime", ZonedDateTime.of(1900, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC"))) //
.addProperty("LocalDateTime", LocalDateTime.of(1900, 1, 1, 0, 0, 0, 0)) //
.addPropertyIfNotNull("Boolean1", (Boolean) null) //
.addPropertyIfNotNull("Boolean2", Boolean.FALSE) //
.addPropertyIfNotNull("Double1", (Double) null) //
Expand Down Expand Up @@ -680,6 +686,13 @@ public void testGetAsZonedDateTime() throws OpenemsNamedException {
assertOpenemsError(JSON_NO_DATE_MEMBER, //
() -> getAsZonedDateWithZeroTime(j, "foo", ZoneId.of("UTC")) //
);

assertEquals("1900-01-01T00:00Z", getAsZonedDateTime(JSON_OBJECT, "ZonedDateTime").toString());
}

@Test
public void testGetAsLocalDateTime() throws OpenemsNamedException {
assertEquals("1900-01-01T00:00", getAsLocalDateTime(JSON_OBJECT, "LocalDateTime").toString());
}

@Test
Expand Down Expand Up @@ -744,6 +757,9 @@ public void testIsEmptyJsonArray() throws OpenemsNamedException {

@Test
public void testGenerateJsonArray() {
assertNull(generateJsonArray(null));
assertEquals(JsonNull.INSTANCE, generateJsonArray(List.of(JsonNull.INSTANCE)).get(0));

var list = List.of("foo", "bar");
var r = generateJsonArray(list, v -> new JsonPrimitive(v));
assertEquals("foo", r.get(0).getAsString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
protected void modified(ComponentContext context, String id, String alias, boolean enabled) {
super.modified(context, id, alias, enabled);
this.updateConfig(this.config);
this.energyScheduleHandler.triggerReschedule();
this.energyScheduleHandler.triggerReschedule("ControllerEssEmergencyCapacityReserveImpl::modified()");

Check warning on line 95 in io.openems.edge.controller.ess.emergencycapacityreserve/src/io/openems/edge/controller/ess/emergencycapacityreserve/ControllerEssEmergencyCapacityReserveImpl.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.controller.ess.emergencycapacityreserve/src/io/openems/edge/controller/ess/emergencycapacityreserve/ControllerEssEmergencyCapacityReserveImpl.java#L95

Added line #L95 was not covered by tests
}

@Override
Expand Down Expand Up @@ -219,15 +219,16 @@
* @return a {@link EnergyScheduleHandler}
*/
public static EnergyScheduleHandler buildEnergyScheduleHandler(Supplier<Integer> minSoc) {
return EnergyScheduleHandler.of(//
simContext -> minSoc.get() == null //
return EnergyScheduleHandler.WithOnlyOneState.<Integer>create() //
.setContextFunction(simContext -> minSoc.get() == null //
? null //
: socToEnergy(simContext.ess().totalEnergy(), minSoc.get()), //
(simContext, period, energyFlow, minEnergy) -> {
: socToEnergy(simContext.ess().totalEnergy(), minSoc.get())) //

Check warning on line 225 in io.openems.edge.controller.ess.emergencycapacityreserve/src/io/openems/edge/controller/ess/emergencycapacityreserve/ControllerEssEmergencyCapacityReserveImpl.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.controller.ess.emergencycapacityreserve/src/io/openems/edge/controller/ess/emergencycapacityreserve/ControllerEssEmergencyCapacityReserveImpl.java#L225

Added line #L225 was not covered by tests
.setSimulator((simContext, period, energyFlow, minEnergy) -> {
if (minEnergy != null) {
energyFlow.setEssMaxDischarge(max(0, simContext.ess.getInitialEnergy() - minEnergy));
}
});
}) //

Check warning on line 230 in io.openems.edge.controller.ess.emergencycapacityreserve/src/io/openems/edge/controller/ess/emergencycapacityreserve/ControllerEssEmergencyCapacityReserveImpl.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.controller.ess.emergencycapacityreserve/src/io/openems/edge/controller/ess/emergencycapacityreserve/ControllerEssEmergencyCapacityReserveImpl.java#L230

Added line #L230 was not covered by tests
.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
if (this.applyConfig(context, config)) {
return;
}
this.energyScheduleHandler.triggerReschedule();
this.energyScheduleHandler.triggerReschedule("ControllerEssFixActivePowerImpl::modified()");

Check warning on line 88 in io.openems.edge.controller.ess.fixactivepower/src/io/openems/edge/controller/ess/fixactivepower/ControllerEssFixActivePowerImpl.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.controller.ess.fixactivepower/src/io/openems/edge/controller/ess/fixactivepower/ControllerEssFixActivePowerImpl.java#L88

Added line #L88 was not covered by tests
}

private boolean applyConfig(ComponentContext context, Config config) {
Expand Down Expand Up @@ -165,9 +165,9 @@
* @return a {@link EnergyScheduleHandler}
*/
public static EnergyScheduleHandler buildEnergyScheduleHandler(Supplier<EshContext> context) {
return EnergyScheduleHandler.of(//
simContext -> context.get(), //
(simContext, period, energyFlow, ctrlContext) -> {
return EnergyScheduleHandler.WithOnlyOneState.<EshContext>create() //
.setContextFunction(simContext -> context.get()) //
.setSimulator((simContext, period, energyFlow, ctrlContext) -> {
switch (ctrlContext.mode) {
case MANUAL_ON:
switch (ctrlContext.relationship) {
Expand All @@ -179,7 +179,8 @@
case MANUAL_OFF:
break;
}
});
}) //

Check warning on line 182 in io.openems.edge.controller.ess.fixactivepower/src/io/openems/edge/controller/ess/fixactivepower/ControllerEssFixActivePowerImpl.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.controller.ess.fixactivepower/src/io/openems/edge/controller/ess/fixactivepower/ControllerEssFixActivePowerImpl.java#L182

Added line #L182 was not covered by tests
.build();
}

public static record EshContext(Mode mode, int energy, Relationship relationship) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,8 @@ public Timedata getTimedata() {
*/
public static EnergyScheduleHandler buildEnergyScheduleHandler(Supplier<Mode> mode,
Supplier<LocalTime> manualTargetTime) {
return EnergyScheduleHandler.of(//
simContext -> {
return EnergyScheduleHandler.WithOnlyOneState.<EshContext>create() //
.setContextFunction(simContext -> {
// TODO try to reuse existing logic for parsing, calculating limits, etc.; for
// now this only works for current day and MANUAL mode
final var limits = ImmutableSortedMap.<ZonedDateTime, OptionalInt>naturalOrder();
Expand Down Expand Up @@ -526,8 +526,8 @@ public static EnergyScheduleHandler buildEnergyScheduleHandler(Supplier<Mode> mo
}

return new EshContext(limits.build());
}, //
(simContext, period, energyFlow, ctrlContext) -> {
}) //
.setSimulator((simContext, period, energyFlow, ctrlContext) -> {
var limitEntry = ctrlContext.limits.floorEntry(period.time());
if (limitEntry == null) {
return;
Expand All @@ -536,7 +536,8 @@ public static EnergyScheduleHandler buildEnergyScheduleHandler(Supplier<Mode> mo
if (limit.isPresent()) {
energyFlow.setEssMaxCharge(limit.getAsInt());
}
});
}) //
.build();
}

private static record EshContext(ImmutableSortedMap<ZonedDateTime, OptionalInt> limits) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,12 @@
* @return a {@link EnergyScheduleHandler}
*/
public static EnergyScheduleHandler buildEnergyScheduleHandler(IntSupplier minSoc) {
return EnergyScheduleHandler.of(//
simContext -> socToEnergy(simContext.ess().totalEnergy(), minSoc.getAsInt()), //
(simContext, period, energyFlow, minEnergy) -> {
return EnergyScheduleHandler.WithOnlyOneState.<Integer>create() //
.setContextFunction(simContext -> socToEnergy(simContext.ess().totalEnergy(), minSoc.getAsInt())) //
.setSimulator((simContext, period, energyFlow, minEnergy) -> {
energyFlow.setEssMaxDischarge(max(0, simContext.ess.getInitialEnergy() - minEnergy));
});
}) //

Check warning on line 239 in io.openems.edge.controller.ess.limittotaldischarge/src/io/openems/edge/controller/ess/limittotaldischarge/ControllerEssLimitTotalDischargeImpl.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.controller.ess.limittotaldischarge/src/io/openems/edge/controller/ess/limittotaldischarge/ControllerEssLimitTotalDischargeImpl.java#L239

Added line #L239 was not covered by tests
.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
private void modified(ComponentContext context, Config config) {
super.modified(context, config.id(), config.alias(), config.enabled());
this.applyConfig(config);
this.energyScheduleHandler.triggerReschedule();
this.energyScheduleHandler.triggerReschedule("TimeOfUseTariffControllerImpl::modified()");

Check warning on line 153 in io.openems.edge.controller.ess.timeofusetariff/src/io/openems/edge/controller/ess/timeofusetariff/TimeOfUseTariffControllerImpl.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.controller.ess.timeofusetariff/src/io/openems/edge/controller/ess/timeofusetariff/TimeOfUseTariffControllerImpl.java#L153

Added line #L153 was not covered by tests
}

private synchronized void applyConfig(Config config) {
Expand Down Expand Up @@ -270,17 +270,17 @@
*/
public static EnergyScheduleHandler.WithDifferentStates<StateMachine, EshContext> buildEnergyScheduleHandler(
Supplier<ManagedSymmetricEss> ess, Supplier<ControlMode> controlMode, IntSupplier maxChargePowerFromGrid) {
return EnergyScheduleHandler.of(//
StateMachine.BALANCING, //
() -> controlMode.get().states, //
simContext -> {
return EnergyScheduleHandler.WithDifferentStates.<StateMachine, EshContext>create() //
.setDefaultState(StateMachine.BALANCING) //
.setAvailableStates(() -> controlMode.get().states) //
.setContextFunction(simContext -> {
// Maximium-SoC in CHARGE_GRID is 90 %
var maxSocEnergyInChargeGrid = round(simContext.ess().totalEnergy() * (ESS_MAX_SOC / 100));
var essChargeInChargeGrid = calculateChargeEnergyInChargeGrid(simContext);
return new EshContext(ess.get(), controlMode.get(), maxChargePowerFromGrid.getAsInt(),
maxSocEnergyInChargeGrid, essChargeInChargeGrid);
}, //
(simContext, period, energyFlow, ctrlContext, state) -> {
}) //
.setSimulator((simContext, period, energyFlow, ctrlContext, state) -> {
switch (state) {
case BALANCING -> applyBalancing(energyFlow); // TODO Move to CtrlBalancing
case DELAY_DISCHARGE -> applyDelayDischarge(energyFlow);
Expand All @@ -290,8 +290,10 @@
applyChargeGrid(energyFlow, ctrlContext.essChargeInChargeGrid);
}
}
}, //
Utils::postprocessSimulatorState);
return 0.;

Check warning on line 293 in io.openems.edge.controller.ess.timeofusetariff/src/io/openems/edge/controller/ess/timeofusetariff/TimeOfUseTariffControllerImpl.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.controller.ess.timeofusetariff/src/io/openems/edge/controller/ess/timeofusetariff/TimeOfUseTariffControllerImpl.java#L293

Added line #L293 was not covered by tests
}) //
.setPostProcessor(Utils::postprocessSimulatorState) //
.build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
@AttributeDefinition(name = "Is enabled?", description = "Is this Component enabled?")
boolean enabled() default true;

// TODO this will change in future
@AttributeDefinition(name = "Enable EnergyScheduler SMART-Mode", description = "")
boolean smartMode() default false;

@AttributeDefinition(name = "JSON Configuration for SMART mode", description = "")
String smartConfig() default "";

@AttributeDefinition(name = "Debug Mode", description = "Activates the debug mode")
boolean debugMode() default false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public interface ControllerEvcs extends OpenemsComponent {
public enum ChannelId implements io.openems.edge.common.channel.ChannelId {

AWAITING_HYSTERESIS(Doc.of(BOOLEAN) //
.persistencePriority(HIGH)),
.persistencePriority(HIGH)), //
SMART_MODE(Doc.of(SmartMode.values()) //
.persistencePriority(HIGH)), //
EVCS_IS_READ_ONLY(Doc.of(Level.INFO) //
.translationKey(ControllerEvcs.class, "evcsIsReadOnly")); //

Expand Down
Loading
Loading