Skip to content

Commit 2c36e41

Browse files
authored
Refactor: apply best practices and coding guidelines globally (OpenEMS#2209)
The PR contains strictly **NO LOGIC CHANGES**, but applies best practices and coding guidelines (https://openems.github.io/openems.io/openems/latest/contribute/coding-guidelines.html) globally in OpenEMS Edge/Backend, e.g. - Use separate Interface and Implementation files for OpenEMS Edge Components - Move ChannelIds to Interfaces ("Natures" - https://openems.github.io/openems.io/openems/latest/coreconcepts.html#_nature) - Rename Component files equal to Factory-ID (makes Ctrl+R in Eclipse IDE much more convenient to use) - Reduce variable scope; use `private` wherever possible (e.g. for the `activate()`-method). This is enforced by adding and applying Checkstyle `VisibilityModifierCheck` (see https://checkstyle.sourceforge.io/apidocs/com/puppycrawl/tools/checkstyle/checks/design/VisibilityModifierCheck.html) - Sort variables in code (from `public static final` to `private`; @reference to constructor to @activate, @Modifed, @deactivate) - Use short JavaDoc for single-line comments - Use `@EventTopics` - Always implement @deactivate - Remove `package-info.java` if not necessary (i.e. reduce package scope) - Add (at least simple) JUnit tests - Update BND Bundle templates
1 parent 86f7ae8 commit 2c36e41

File tree

1,187 files changed

+72612
-65693
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,187 files changed

+72612
-65693
lines changed

cnf/checkstyle.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@
188188
<message key="name.invalidPattern" value="Method name ''{0}'' must match pattern ''{1}''."/>
189189
</module>
190190
<module name="SingleLineJavadoc">
191+
<property name="severity" value="ignore"/>
191192
<property name="ignoreInlineTags" value="false"/>
193+
<metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
192194
</module>
193195
<module name="EmptyCatchBlock">
194196
<property name="exceptionVariableName" value="expected"/>
@@ -206,6 +208,11 @@
206208
<property name="ignoreMethodNamesRegex" value="doc|set.+|build"/>
207209
<property name="tokens" value="METHOD_DEF"/>
208210
</module>
211+
<module name="VisibilityModifier">
212+
<property name="protectedAllowed" value="true"/>
213+
<property name="allowPublicFinalFields" value="true"/>
214+
<property name="allowPublicImmutableFields" value="true"/>
215+
</module>
209216
</module>
210217
<module name="LineLength">
211218
<property name="severity" value="ignore"/>

io.openems.backend.alerting/src/io/openems/backend/alerting/Alerting.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ public class Alerting extends AbstractOpenemsBackendComponent implements EventHa
4141
private static final byte THREAD_POOL_SIZE = 2;
4242
private static final byte THREAD_QUEUE_WARNING_THRESHOLD = 50;
4343

44-
private final Logger log = LoggerFactory.getLogger(Alerting.class);
44+
protected final Scheduler scheduler;
4545

46+
private final Logger log = LoggerFactory.getLogger(Alerting.class);
4647
private final ThreadPoolExecutor executor;
4748

4849
@Reference
@@ -51,7 +52,6 @@ public class Alerting extends AbstractOpenemsBackendComponent implements EventHa
5152
@Reference
5253
protected Mailer mailer;
5354

54-
protected final Scheduler scheduler;
5555
protected Handler<?>[] handlers = {};
5656

5757
protected Alerting(Scheduler scheduler) {

io.openems.backend.alerting/test/io/openems/backend/alerting/AlertingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void testHandleEvent() {
7979

8080
/* ********** */
8181
static class DummyAlerting extends Alerting {
82-
Event lastEvent;
82+
private Event lastEvent;
8383

8484
public DummyAlerting() {
8585
super.metadata = new DummyMetadata();

io.openems.backend.alerting/test/io/openems/backend/alerting/Dummy.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
import io.openems.backend.common.test.DummyMetadata;
2222

2323
public class Dummy {
24+
2425
public static class MailerImpl implements Mailer {
25-
public Map<ZonedDateTime, String> sentMails = new HashMap<>();
26+
public final Map<ZonedDateTime, String> sentMails = new HashMap<>();
2627

2728
@Override
2829
public void sendMail(ZonedDateTime sendAt, String template, JsonElement params) {
@@ -67,8 +68,8 @@ public EventAdmin getEventAdmin() {
6768
}
6869

6970
public static class MetadataImpl extends SimpleMetadataImpl {
70-
public List<Edge> edges;
71-
public Map<String, List<AlertingSetting>> settings;
71+
private List<Edge> edges;
72+
private Map<String, List<AlertingSetting>> settings;
7273

7374
/**
7475
* Initialize Metadata with test data.
@@ -86,6 +87,10 @@ public boolean isInitialized() {
8687
return true;
8788
}
8889

90+
public Map<String, List<AlertingSetting>> getSettings() {
91+
return this.settings;
92+
}
93+
8994
@Override
9095
public Optional<Edge> getEdge(String edgeId) {
9196
return this.edges.stream().filter(e -> e.getId() == edgeId).findFirst();
@@ -103,7 +108,7 @@ public List<AlertingSetting> getUserAlertingSettings(String edgeId) {
103108
}
104109

105110
public static class EventAdminImpl implements EventAdmin {
106-
public List<Event> lastEvents = new ArrayList<>();
111+
private List<Event> lastEvents = new ArrayList<>();
107112

108113
public EventAdminImpl() {
109114

io.openems.backend.alerting/test/io/openems/backend/alerting/handler/TestOfflineEdgeHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void checkmetadata() {
9898
final var msgsch = new Dummy.MessageSchedulerServiceImpl();
9999
final var handler = new OfflineEdgeHandler(msgsch, null, this.metadata, 0);
100100

101-
final var expected = (int) this.metadata.settings.values().stream().filter(e -> e.stream()
101+
final var expected = (int) this.metadata.getSettings().values().stream().filter(e -> e.stream()
102102
.filter(s -> s.getDelayTime() > 0).filter(s -> s.getLastNotification().isBefore(this.now)).count() != 0)
103103
.count();
104104
assertEquals(expected, msgsch.find(handler).size());

io.openems.backend.alerting/test/io/openems/backend/alerting/scheduler/SchedulerTest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ private void testHandle() {
117117
}
118118

119119
/* *********************************************** */
120-
static class DummyMessage extends Message {
121-
ZonedDateTime timeStamp;
120+
private static class DummyMessage extends Message {
121+
private ZonedDateTime timeStamp;
122122

123123
public DummyMessage(String messageId, int timeShift) {
124124
super(messageId);
@@ -140,9 +140,7 @@ public JsonObject getParams() {
140140
}
141141
}
142142

143-
static class DummyHandler implements Handler<DummyMessage> {
144-
ZonedDateTime wasSentAt = null;
145-
143+
private static class DummyHandler implements Handler<DummyMessage> {
146144
@Override
147145
public Runnable getEventHandler(EventReader event) {
148146
throw new UnsupportedOperationException();
@@ -155,7 +153,6 @@ public void stop() {
155153

156154
@Override
157155
public void send(ZonedDateTime sentAt, List<DummyMessage> messages) {
158-
this.wasSentAt = sentAt;
159156
}
160157

161158
@Override

io.openems.backend.b2brest/src/io/openems/backend/b2brest/B2bRest.java

Lines changed: 0 additions & 94 deletions
This file was deleted.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package io.openems.backend.b2brest;
2+
3+
import org.eclipse.jetty.server.Server;
4+
import org.osgi.service.component.annotations.Activate;
5+
import org.osgi.service.component.annotations.Component;
6+
import org.osgi.service.component.annotations.ConfigurationPolicy;
7+
import org.osgi.service.component.annotations.Deactivate;
8+
import org.osgi.service.component.annotations.Reference;
9+
import org.osgi.service.component.annotations.ReferenceCardinality;
10+
import org.osgi.service.component.annotations.ReferencePolicy;
11+
import org.osgi.service.metatype.annotations.Designate;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
15+
import io.openems.backend.common.component.AbstractOpenemsBackendComponent;
16+
import io.openems.backend.common.jsonrpc.JsonRpcRequestHandler;
17+
import io.openems.backend.common.metadata.Metadata;
18+
import io.openems.common.exceptions.OpenemsException;
19+
20+
@Designate(ocd = Config.class, factory = true)
21+
@Component(//
22+
name = "Backend2Backend.Rest", //
23+
immediate = true, //
24+
configurationPolicy = ConfigurationPolicy.REQUIRE //
25+
)
26+
public class Backend2BackendRest extends AbstractOpenemsBackendComponent {
27+
28+
public static final int DEFAULT_PORT = 8075;
29+
30+
private final Logger log = LoggerFactory.getLogger(Backend2BackendRest.class);
31+
32+
@Reference(cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.DYNAMIC)
33+
protected volatile JsonRpcRequestHandler jsonRpcRequestHandler;
34+
35+
@Reference(cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.DYNAMIC)
36+
protected volatile Metadata metadata;
37+
38+
private Server server = null;
39+
40+
public Backend2BackendRest() {
41+
super("Backend2Backend.Rest");
42+
}
43+
44+
@Activate
45+
private void activate(Config config) throws OpenemsException {
46+
this.startServer(config.port());
47+
}
48+
49+
@Deactivate
50+
private void deactivate() {
51+
this.stopServer();
52+
}
53+
54+
/**
55+
* Create and start new server.
56+
*
57+
* @param port the port
58+
* @throws OpenemsException on error
59+
*/
60+
private synchronized void startServer(int port) throws OpenemsException {
61+
try {
62+
this.server = new Server(port);
63+
this.server.setHandler(new RestHandler(this));
64+
this.server.start();
65+
this.logInfo(this.log, "Backend2Backend.Rest started on port [" + port + "].");
66+
} catch (Exception e) {
67+
throw new OpenemsException("Backend2Backend.Rest failed on port [" + port + "].", e);
68+
}
69+
}
70+
71+
/**
72+
* Stop existing server.
73+
*/
74+
private synchronized void stopServer() {
75+
if (this.server != null) {
76+
try {
77+
this.server.stop();
78+
} catch (Exception e) {
79+
this.logWarn(this.log, "Backend2Backend.Rest failed to stop: " + e.getMessage());
80+
}
81+
}
82+
}
83+
84+
@Override
85+
protected void logInfo(Logger log, String message) {
86+
super.logInfo(log, message);
87+
}
88+
89+
@Override
90+
protected void logWarn(Logger log, String message) {
91+
super.logWarn(log, message);
92+
}
93+
94+
}

io.openems.backend.b2brest/src/io/openems/backend/b2brest/Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
@interface Config {
1010

1111
@AttributeDefinition(name = "Port", description = "The port of the REST server.")
12-
int port() default B2bRest.DEFAULT_PORT;
12+
int port() default Backend2BackendRest.DEFAULT_PORT;
1313

1414
String webconsole_configurationFactory_nameHint() default "Backend2Backend Rest";
1515

io.openems.backend.b2brest/src/io/openems/backend/b2brest/RestHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public class RestHandler extends AbstractHandler {
3737

3838
private final Logger log = LoggerFactory.getLogger(RestHandler.class);
3939

40-
private final B2bRest parent;
40+
private final Backend2BackendRest parent;
4141

42-
public RestHandler(B2bRest parent) {
42+
public RestHandler(Backend2BackendRest parent) {
4343
this.parent = parent;
4444
}
4545

0 commit comments

Comments
 (0)