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

FEMS Backports 2024-09 #2788

Merged
merged 18 commits into from
Sep 10, 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
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
.put("App.FENECON.Home", "https://fenecon.de/fenecon-home-10/") //
.put("App.FENECON.Home.20", "https://fenecon.de/fenecon-home-20-30/") //
.put("App.FENECON.Home.30", "https://fenecon.de/fenecon-home-20-30/") //
.put("App.FENECON.Commercial.92", "https://fenecon.de/fenecon-commercial/") //
.put("App.FENECON.Industrial.L.ILK710", "https://fenecon.de/fenecon-industrial-l/") //

Check warning on line 64 in io.openems.common/src/io/openems/common/oem/DummyOpenemsEdgeOem.java

View check run for this annotation

Codecov / codecov/patch

io.openems.common/src/io/openems/common/oem/DummyOpenemsEdgeOem.java#L63-L64

Added lines #L63 - L64 were not covered by tests
.put("App.FENECON.Industrial.S.ISK010", "https://fenecon.de/fenecon-industrial-s/") //
.put("App.FENECON.Industrial.S.ISK110", "https://fenecon.de/fenecon-industrial-s/") //
.put("App.FENECON.Industrial.S.ISK011", "https://fenecon.de/fenecon-industrial-s/") //
Expand Down Expand Up @@ -92,8 +94,17 @@
.put("App.LoadControl.ThresholdControl", "") //
.put("App.Meter.Socomec", "") //
.put("App.Meter.CarloGavazzi", "") //
.put("App.Meter.PqPlus", "") //

Check warning on line 97 in io.openems.common/src/io/openems/common/oem/DummyOpenemsEdgeOem.java

View check run for this annotation

Codecov / codecov/patch

io.openems.common/src/io/openems/common/oem/DummyOpenemsEdgeOem.java#L97

Added line #L97 was not covered by tests
.put("App.Meter.Janitza", "") //
.put("App.Meter.Discovergy", "")//
.put("App.Meter.PhoenixContact", "")//
.put("App.OpenemsHardware.BeagleBoneBlack", "") //
.put("App.OpenemsHardware.Compulab", "") //
.put("App.OpenemsHardware.CM3", "") //
.put("App.OpenemsHardware.CM4", "") //
.put("App.OpenemsHardware.CM4Max", "") //
.put("App.OpenemsHardware.CM4S", "") //
.put("App.OpenemsHardware.CM4S.Gen2", "") //

Check warning on line 107 in io.openems.common/src/io/openems/common/oem/DummyOpenemsEdgeOem.java

View check run for this annotation

Codecov / codecov/patch

io.openems.common/src/io/openems/common/oem/DummyOpenemsEdgeOem.java#L100-L107

Added lines #L100 - L107 were not covered by tests
.put("App.PvInverter.Fronius", "") //
.put("App.PvInverter.Kaco", "") //
.put("App.PvInverter.Kostal", "") //
Expand All @@ -105,6 +116,7 @@
.put("App.Ess.FixStateOfCharge", "") //
.put("App.Ess.PowerPlantController", "") //
.put("App.Ess.PrepareBatteryExtension", "") //
.put("App.Ess.Limiter14a", "") //

Check warning on line 119 in io.openems.common/src/io/openems/common/oem/DummyOpenemsEdgeOem.java

View check run for this annotation

Codecov / codecov/patch

io.openems.common/src/io/openems/common/oem/DummyOpenemsEdgeOem.java#L119

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

// NOTE: this will certainly get refactored in future, but it's a good start to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.openems.common.channel.Level;
import io.openems.common.channel.PersistencePriority;
import io.openems.common.channel.Unit;
import io.openems.common.session.Language;
import io.openems.common.types.OpenemsType;
import io.openems.common.types.OptionsEnum;
import io.openems.edge.common.channel.internal.AbstractDoc;
Expand Down Expand Up @@ -135,6 +136,23 @@ public static StateChannelDoc of(Level level) {
*/
public String getText();

/**
* Gets the translated text. Defaults to empty String.
*
* @param lang language to get translated text
* @return the text
*/
public String getText(Language lang);

/**
* Sets the translation key.
*
* @param channelKey the translationKey of the channel
* @param clazz the class of the channel parent
* @return myself
*/
public Doc translationKey(Class<?> clazz, String channelKey);

/**
* Is the more verbose debug mode activated?.
*
Expand All @@ -153,5 +171,4 @@ public static StateChannelDoc of(Level level) {
*/
public <C extends Channel<?>> C createChannelInstance(OpenemsComponent component,
io.openems.edge.common.channel.ChannelId channelId);

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package io.openems.edge.common.channel.internal;

import java.util.List;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

import io.openems.common.channel.AccessMode;
import io.openems.common.channel.PersistencePriority;
import io.openems.common.channel.Unit;
import io.openems.common.exceptions.OpenemsError.OpenemsNamedException;
import io.openems.common.function.ThrowingBiConsumer;
import io.openems.common.function.ThrowingConsumer;
import io.openems.common.session.Language;
import io.openems.common.types.OpenemsType;
import io.openems.edge.common.channel.Channel;
import io.openems.edge.common.channel.ChannelId;
Expand All @@ -26,6 +30,8 @@

private final OpenemsType type;

private Function<Language, String> getTextFunction;

protected AbstractDoc(OpenemsType type) {
this.type = type;
}
Expand Down Expand Up @@ -118,20 +124,56 @@
return this.initialValue;
}

/*
* Description
*/
private String text = "";

@Override
public AbstractDoc<T> text(String text) {
this.text = text;
this.getTextFunction = lang -> {
return text;
};
return this.self();
}

@Override
public String getText() {
return this.text;
return this.getText(Language.DEFAULT);

Check warning on line 137 in io.openems.edge.common/src/io/openems/edge/common/channel/internal/AbstractDoc.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.common/src/io/openems/edge/common/channel/internal/AbstractDoc.java#L137

Added line #L137 was not covered by tests
}

@Override
public String getText(Language lang) {
if (this.getTextFunction == null) {
return "";

Check warning on line 143 in io.openems.edge.common/src/io/openems/edge/common/channel/internal/AbstractDoc.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.common/src/io/openems/edge/common/channel/internal/AbstractDoc.java#L143

Added line #L143 was not covered by tests
}
return this.getTextFunction.apply(lang);
}

@Override
public AbstractDoc<T> translationKey(Class<?> clazz, String channelKey) {
this.getTextFunction = lang -> {
var bundle = AbstractDoc.getResourceBundle(lang, clazz);
if (bundle != null && bundle.containsKey(channelKey)) {
var textTranslated = bundle.getString(channelKey);
return textTranslated;
}
if (lang != Language.EN) {
// TODO: Use Language.DEFAULT for default language
bundle = AbstractDoc.getResourceBundle(Language.EN, clazz);
if (bundle != null && bundle.containsKey(channelKey)) {
var textTranslated = bundle.getString(channelKey);
return textTranslated;
}
}

return channelKey;

Check warning on line 165 in io.openems.edge.common/src/io/openems/edge/common/channel/internal/AbstractDoc.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.common/src/io/openems/edge/common/channel/internal/AbstractDoc.java#L165

Added line #L165 was not covered by tests
};
return this;
}

private static ResourceBundle getResourceBundle(Language lang, Class<?> clazz) {
try {
return ResourceBundle.getBundle(clazz.getPackageName() + ".translation", lang.getLocal(),
clazz.getModule());
} catch (MissingResourceException e) {
return null;

Check warning on line 175 in io.openems.edge.common/src/io/openems/edge/common/channel/internal/AbstractDoc.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.common/src/io/openems/edge/common/channel/internal/AbstractDoc.java#L174-L175

Added lines #L174 - L175 were not covered by tests
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,16 @@
}

private final Dictionary<String, Object> properties;
private final ComponentInstance<?> instance;

public DummyComponentContext(Dictionary<String, Object> properties) {
public DummyComponentContext(Dictionary<String, Object> properties, ComponentInstance<?> instance) {
super();

Check warning on line 39 in io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentContext.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentContext.java#L39

Added line #L39 was not covered by tests
this.properties = properties;
this.instance = instance;
}

Check warning on line 42 in io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentContext.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentContext.java#L41-L42

Added lines #L41 - L42 were not covered by tests

public DummyComponentContext(Dictionary<String, Object> properties) {
this(properties, null);

Check warning on line 45 in io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentContext.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentContext.java#L45

Added line #L45 was not covered by tests
}

public DummyComponentContext() {
Expand Down Expand Up @@ -85,8 +92,9 @@
}

@Override
@SuppressWarnings("unchecked")
public <S> ComponentInstance<S> getComponentInstance() {
return null;
return (ComponentInstance<S>) this.instance;

Check warning on line 97 in io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentContext.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentContext.java#L97

Added line #L97 was not covered by tests
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.openems.edge.common.test;

import org.osgi.service.component.ComponentInstance;

import io.openems.common.utils.FunctionUtils;

public class DummyComponentInstance<S> implements ComponentInstance<S> {

public static class DummyComponentInstanceBuilder<S> {

Check warning on line 9 in io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentInstance.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentInstance.java#L9

Added line #L9 was not covered by tests

private Runnable dispose;
private S instance;

public DummyComponentInstanceBuilder<S> setDispose(Runnable dispose) {
this.dispose = dispose;
return this;

Check warning on line 16 in io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentInstance.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentInstance.java#L15-L16

Added lines #L15 - L16 were not covered by tests
}

public DummyComponentInstanceBuilder<S> setInstance(S instance) {
this.instance = instance;
return this;

Check warning on line 21 in io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentInstance.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentInstance.java#L20-L21

Added lines #L20 - L21 were not covered by tests
}

public DummyComponentInstance<S> build() {
return new DummyComponentInstance<>(this.dispose, this.instance);

Check warning on line 25 in io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentInstance.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentInstance.java#L25

Added line #L25 was not covered by tests
}

}

/**
* Creates a builder for a {@link DummyComponentInstance}.
*
* @param <S> the type of the service
* @return the builder
*/
public static <S> DummyComponentInstanceBuilder<S> create() {
return new DummyComponentInstanceBuilder<>();

Check warning on line 37 in io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentInstance.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentInstance.java#L37

Added line #L37 was not covered by tests
}

private final Runnable dispose;
private final S instance;

public DummyComponentInstance(Runnable dispose, S instance) {
super();

Check warning on line 44 in io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentInstance.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentInstance.java#L44

Added line #L44 was not covered by tests
this.dispose = dispose != null ? dispose : FunctionUtils::doNothing;
this.instance = instance;
}

Check warning on line 47 in io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentInstance.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentInstance.java#L46-L47

Added lines #L46 - L47 were not covered by tests

@Override
public void dispose() {
this.dispose.run();
}

Check warning on line 52 in io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentInstance.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentInstance.java#L51-L52

Added lines #L51 - L52 were not covered by tests

@Override
public S getInstance() {
return this.instance;

Check warning on line 56 in io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentInstance.java

View check run for this annotation

Codecov / codecov/patch

io.openems.edge.common/src/io/openems/edge/common/test/DummyComponentInstance.java#L56

Added line #L56 was not covered by tests
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.openems.edge.common.channel;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import io.openems.common.channel.Level;
import io.openems.common.session.Language;

public class ChannelTranslationTest {

public enum ChannelId implements io.openems.edge.common.channel.ChannelId {
TEST_CHANNEL(Doc.of(Level.WARNING) //
.translationKey(ChannelTranslationTest.class, "Test.TestChannel")), //
ONLY_ENGLISH(Doc.of(Level.INFO) //
.translationKey(ChannelTranslationTest.class, "Test.OnlyEnglish")), //
NO_TRANSLATION(Doc.of(Level.OK) //
.text("No Translation")),;

private final Doc doc;

private ChannelId(Doc doc) {
this.doc = doc;
}

@Override
public Doc doc() {
return this.doc;
}
}

@Test
public void testTranslatedGermanChannelText() {
assertEquals("German Test", ChannelTranslationTest.ChannelId.TEST_CHANNEL.doc().getText(Language.DE));
}

@Test
public void testTranslatedEnglishChannelText() {
assertEquals("English Test", ChannelTranslationTest.ChannelId.TEST_CHANNEL.doc().getText(Language.EN));
}

@Test
public void testOnlyEnglishTranslationTest() {
assertEquals("Only English", ChannelTranslationTest.ChannelId.ONLY_ENGLISH.doc().getText(Language.DE));
}

@Test
public void testNoTranslation() {
assertEquals("No Translation", ChannelTranslationTest.ChannelId.NO_TRANSLATION.doc().getText(Language.DE));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test.TestChannel = German Test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Test.TestChannel = English Test
Test.OnlyEnglish = Only English
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -20,10 +21,12 @@
import io.openems.common.jsonrpc.base.JsonrpcResponseSuccess;
import io.openems.common.jsonrpc.request.SetChannelValueRequest;
import io.openems.common.types.OpenemsType;
import io.openems.common.utils.FunctionUtils;
import io.openems.common.utils.JsonUtils;
import io.openems.edge.common.channel.Channel;
import io.openems.edge.common.channel.StringReadChannel;
import io.openems.edge.common.channel.WriteChannel;
import io.openems.edge.common.component.AbstractOpenemsComponent;
import io.openems.edge.common.component.ComponentManager;
import io.openems.edge.common.component.OpenemsComponent;
import io.openems.edge.common.user.User;
Expand All @@ -40,7 +43,7 @@ public class ApiWorker {

private final Logger log = LoggerFactory.getLogger(ApiWorker.class);

private final OpenemsComponent parent;
private final AbstractOpenemsComponent parent;

/**
* Debug information about writes to channels is sent to this channel.
Expand All @@ -58,8 +61,24 @@ public class ApiWorker {

private int timeoutSeconds = DEFAULT_TIMEOUT_SECONDS;

public ApiWorker(OpenemsComponent parent) {
/**
* Handles write-only channel overriding.
*/
public record WriteHandler(Consumer<Entry<WriteChannel<?>, WriteObject>> handleWrites, //
Consumer<Status> setOverrideStatus, //
Runnable handleTimeout) {
}

private WriteHandler writeHandler;

public ApiWorker(AbstractOpenemsComponent parent) {
this(parent, new WriteHandler(FunctionUtils::doNothing, //
FunctionUtils::doNothing, FunctionUtils::doNothing));
}

public ApiWorker(AbstractOpenemsComponent parent, WriteHandler writeHandler) { //
this.parent = parent;
this.writeHandler = writeHandler;
this.executor = Executors.newSingleThreadScheduledExecutor();
}

Expand Down Expand Up @@ -150,6 +169,8 @@ private synchronized void resetTimeout() {
+ entry.getKey().address() + "] after [" + this.timeoutSeconds + "s]");
entry.getValue().notifyTimeout();
}
this.writeHandler.setOverrideStatus.accept(Status.INACTIVE);
this.writeHandler.handleTimeout.run();
this.values.clear();
}
}, this.timeoutSeconds, TimeUnit.SECONDS);
Expand Down Expand Up @@ -186,7 +207,11 @@ public void run() throws OpenemsNamedException {
"Set Channel [" + channel.address() + "] to Value [" + writeObject.valueToString() + "]");
writeObject.setNextWriteValue(channel);
writeObject.notifySuccess();

logs.add(channel.address() + ":" + writeObject.valueToString());

this.writeHandler.handleWrites.accept(entry);
this.writeHandler.setOverrideStatus.accept(Status.ACTIVE);
} catch (OpenemsException e) {
OpenemsComponent.logError(this.parent, this.log, "Unable to set Channel [" + channel.address()
+ "] to Value [" + writeObject.valueToString() + "]: " + e.getMessage());
Expand Down
Loading