Skip to content

Commit 878f7e7

Browse files
sfeilmeierAnasShetlalukasrgrfabian94533michaelgrill
authored
FEMS Backports 2025-01-20 (#2976)
- UI - use user language instead of browser lang for locale - replace locale being overwritten with browserlang to be using the user lang - fix czech locale, `cz` throwing errors for - piping (using angular pipes) - formatting of numbers - replacing it with `cs` -> [Ref](https://github.com/angular/common-builds/blob/main/locales/global/cs.js) - Improve Modbus Widget - fixes name of channel in ModbusTCP widget (e.g. Ess1SetActivePowerEquals was shown as SetActivePowerEquals/706 in OM) - Several ess lead to repeating register names in ModbusTcp widget. - Dark mode pick-date improvements - Enhancing styles of popover in accordance with dark-mode feature. - Add handling for `Controller.IO.Heating.Room` - Edge - Code Cleanup for Java 21 - Migrate Switch-Case-Expressions to Switch-Case-Statements - General code cleanup (using Eclipse "Clean up" tool) - EVCS Mennekes: cleanup - Cleanup Eclipse warning (#2971) --------- Co-authored-by: Anas Shetla <[email protected]> Co-authored-by: Lukas Rieger <[email protected]> Co-authored-by: Fabian Brandtner <[email protected]> Co-authored-by: Michael Grill <[email protected]> Co-authored-by: Sebastian Asen <[email protected]> Co-authored-by: Christian Lehne <[email protected]> Co-authored-by: Stefan Feilmeier <[email protected]> Co-authored-by: Thomas Sicking <[email protected]>
1 parent 79650cb commit 878f7e7

File tree

211 files changed

+2014
-3151
lines changed

Some content is hidden

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

211 files changed

+2014
-3151
lines changed

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,9 @@ private void handleJsonRpc(User user, Request baseRequest, HttpServletRequest ht
197197
}
198198
// parse JSON-RPC Request
199199
var message = JsonrpcMessage.from(json);
200-
if (!(message instanceof JsonrpcRequest)) {
200+
if (!(message instanceof JsonrpcRequest request)) {
201201
throw new OpenemsException("Only JSON-RPC Request is supported here.");
202202
}
203-
var request = (JsonrpcRequest) message;
204-
205203
// handle the request
206204
CompletableFuture<? extends JsonrpcResponseSuccess> responseFuture = this.parent.jsonRpcRequestHandler
207205
.handleRequest(this.parent.getName(), user, request);

io.openems.backend.common/src/io/openems/backend/common/metadata/Metadata.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ public static String activeStateChannelsToString(
194194
var states = new HashMap<Level, HashMultimap<String, Channel>>();
195195
for (Entry<ChannelAddress, Channel> entry : activeStateChannels.entrySet()) {
196196
var detail = entry.getValue().getDetail();
197-
if (detail instanceof ChannelDetailState) {
198-
var level = ((ChannelDetailState) detail).getLevel();
197+
if (detail instanceof ChannelDetailState cds) {
198+
var level = cds.getLevel();
199199
var channelsByComponent = states.get(level);
200200
if (channelsByComponent == null) {
201201
channelsByComponent = HashMultimap.create();

io.openems.backend.core/src/io/openems/backend/core/jsonrpcrequesthandler/CoreJsonRpcRequestHandlerImpl.java

+11-16
Original file line numberDiff line numberDiff line change
@@ -98,26 +98,21 @@ private void updateConfig(Config config) {
9898
@Override
9999
public CompletableFuture<? extends JsonrpcResponseSuccess> handleRequest(String context, User user,
100100
JsonrpcRequest request) throws OpenemsNamedException {
101-
switch (request.getMethod()) {
102-
103-
case EdgeRpcRequest.METHOD:
104-
return this.edgeRpcRequestHandler.handleRequest(user, request.getId(), EdgeRpcRequest.from(request));
105-
106-
case GetEdgesStatusRequest.METHOD:
107-
return this.handleGetEdgesStatusRequest(user, request.getId(), GetEdgesStatusRequest.from(request));
108-
109-
case GetEdgesChannelsValuesRequest.METHOD:
110-
return this.handleGetEdgesChannelsValuesRequest(user, request.getId(),
101+
return switch (request.getMethod()) {
102+
case EdgeRpcRequest.METHOD //
103+
-> this.edgeRpcRequestHandler.handleRequest(user, request.getId(), EdgeRpcRequest.from(request));
104+
case GetEdgesStatusRequest.METHOD //
105+
-> this.handleGetEdgesStatusRequest(user, request.getId(), GetEdgesStatusRequest.from(request));
106+
case GetEdgesChannelsValuesRequest.METHOD //
107+
-> this.handleGetEdgesChannelsValuesRequest(user, request.getId(),
111108
GetEdgesChannelsValuesRequest.from(request));
112-
113-
case SetGridConnScheduleRequest.METHOD:
114-
return this.handleSetGridConnScheduleRequest(user, request.getId(),
115-
SetGridConnScheduleRequest.from(request));
116-
117-
default:
109+
case SetGridConnScheduleRequest.METHOD //
110+
-> this.handleSetGridConnScheduleRequest(user, request.getId(), SetGridConnScheduleRequest.from(request));
111+
default -> {
118112
this.logWarn(context, "Unhandled Request: " + request);
119113
throw OpenemsError.JSONRPC_UNHANDLED_METHOD.exception(request.getMethod());
120114
}
115+
};
121116
}
122117

123118
/**

io.openems.backend.edgewebsocket/src/io/openems/backend/edgewebsocket/OnNotification.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.openems.backend.edgewebsocket;
22

3+
import static io.openems.common.utils.FunctionUtils.doNothing;
4+
35
import java.util.Map.Entry;
46
import java.util.concurrent.TimeUnit;
57

@@ -118,14 +120,17 @@ private void handleDataNotification(AbstractDataNotification message, WsData wsD
118120
var edgeId = wsData.assertEdgeId(message);
119121

120122
try {
121-
// TODO java 21 switch case with type
122-
if (message instanceof TimestampedDataNotification timestampNotification) {
123+
switch (message) {
124+
case TimestampedDataNotification timestampNotification -> {
123125
wsData.edgeCache.updateCurrentData(timestampNotification);
124126
this.parent.timedataManager.write(edgeId, timestampNotification);
125-
} else if (message instanceof AggregatedDataNotification aggregatedNotification) {
127+
}
128+
case AggregatedDataNotification aggregatedNotification -> {
126129
wsData.edgeCache.updateAggregatedData(aggregatedNotification);
127130
this.parent.timedataManager.write(edgeId, aggregatedNotification);
128131
}
132+
case ResendDataNotification resendNotification -> doNothing(); // handled in handleResendDataNotification()
133+
}
129134
} catch (IllegalArgumentException e) {
130135
e.printStackTrace();
131136
}

io.openems.backend.metadata.odoo/src/io/openems/backend/metadata/odoo/MetadataOdoo.java

+14-20
Original file line numberDiff line numberDiff line change
@@ -320,50 +320,45 @@ public void handleEvent(Event event) {
320320
var reader = new EventReader(event);
321321

322322
switch (event.getTopic()) {
323-
case Edge.Events.ON_SET_ONLINE: {
323+
case Edge.Events.ON_SET_ONLINE -> {
324324
var edgeId = reader.getString(Edge.Events.OnSetOnline.EDGE_ID);
325325
var isOnline = reader.getBoolean(Edge.Events.OnSetOnline.IS_ONLINE);
326326

327327
this.getEdge(edgeId).ifPresent(edge -> {
328-
if (edge instanceof MyEdge) {
328+
if (edge instanceof MyEdge myEdge) {
329329
// Set OpenEMS Is Connected in Odoo/Postgres
330-
this.postgresHandler.getPeriodicWriteWorker().onSetOnline((MyEdge) edge, isOnline);
330+
this.postgresHandler.getPeriodicWriteWorker().onSetOnline(myEdge, isOnline);
331331
}
332332
});
333333
}
334-
break;
335334

336-
case Edge.Events.ON_SET_CONFIG:
337-
this.onSetConfigEvent(reader);
338-
break;
335+
case Edge.Events.ON_SET_CONFIG //
336+
-> this.onSetConfigEvent(reader);
339337

340-
case Edge.Events.ON_SET_VERSION: {
338+
case Edge.Events.ON_SET_VERSION -> {
341339
var edge = (MyEdge) reader.getProperty(Edge.Events.OnSetVersion.EDGE);
342340
var version = (SemanticVersion) reader.getProperty(Edge.Events.OnSetVersion.VERSION);
343341

344342
// Set Version in Odoo
345343
this.odooHandler.writeEdge(edge, new FieldValue<>(Field.EdgeDevice.OPENEMS_VERSION, version.toString()));
346344
}
347-
break;
348345

349-
case Edge.Events.ON_SET_LASTMESSAGE: {
346+
case Edge.Events.ON_SET_LASTMESSAGE -> {
350347
var edge = (MyEdge) reader.getProperty(Edge.Events.OnSetLastmessage.EDGE);
351348
// Set LastMessage timestamp in Odoo/Postgres
352349
this.postgresHandler.getPeriodicWriteWorker().onLastMessage(edge);
353350
}
354-
break;
355351

356-
case Edge.Events.ON_SET_SUM_STATE: {
352+
case Edge.Events.ON_SET_SUM_STATE -> {
357353
var edgeId = reader.getString(Edge.Events.OnSetSumState.EDGE_ID);
358354
var sumState = (Level) reader.getProperty(Edge.Events.OnSetSumState.SUM_STATE);
359355

360356
var edge = this.edgeCache.getEdgeFromEdgeId(edgeId);
361357
// Set Sum-State in Odoo/Postgres
362358
this.postgresHandler.getPeriodicWriteWorker().onSetSumState(edge, sumState);
363359
}
364-
break;
365360

366-
case Edge.Events.ON_SET_PRODUCTTYPE: {
361+
case Edge.Events.ON_SET_PRODUCTTYPE -> {
367362
var edge = (MyEdge) reader.getProperty(Edge.Events.OnSetProducttype.EDGE);
368363
var producttype = reader.getString(Edge.Events.OnSetProducttype.PRODUCTTYPE);
369364
// Set Producttype in Odoo/Postgres
@@ -376,8 +371,6 @@ public void handleEvent(Event event) {
376371
}
377372
});
378373
}
379-
break;
380-
381374
}
382375
}
383376

@@ -568,10 +561,11 @@ public List<SumStateAlertingSetting> getSumStateAlertingSettings(String edgeId)
568561
@Override
569562
public void setUserAlertingSettings(User user, String edgeId, List<UserAlertingSettings> settings)
570563
throws OpenemsException {
571-
if (user instanceof MyUser odooUser) {
572-
this.odooHandler.setUserAlertingSettings(odooUser, edgeId, settings);
573-
} else {
574-
throw new OpenemsException("User information is from foreign source!!");
564+
switch (user) {
565+
case MyUser odooUser //
566+
-> this.odooHandler.setUserAlertingSettings(odooUser, edgeId, settings);
567+
default //
568+
-> throw new OpenemsException("User information is from foreign source!!");
575569
}
576570
}
577571

io.openems.backend.metadata.odoo/src/io/openems/backend/metadata/odoo/odoo/FieldValue.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package io.openems.backend.metadata.odoo.odoo;
22

3+
import static io.openems.common.utils.StringUtils.toShortString;
4+
35
import com.google.gson.JsonElement;
46

57
import io.openems.backend.metadata.odoo.Field;
6-
import io.openems.common.utils.StringUtils;
78

89
public class FieldValue<T> {
910
private final Field field;
@@ -24,14 +25,14 @@ public T getValue() {
2425

2526
@Override
2627
public String toString() {
27-
String string;
28-
if (this.value instanceof JsonElement) {
29-
string = StringUtils.toShortString((JsonElement) this.value, 100);
30-
} else if (this.value instanceof String) {
31-
string = StringUtils.toShortString((String) this.value, 100);
32-
} else {
33-
string = this.value.toString();
34-
}
28+
var string = switch (this.value) {
29+
case JsonElement je //
30+
-> toShortString(je, 100);
31+
case String s //
32+
-> toShortString(s, 100);
33+
default //
34+
-> this.value.toString();
35+
};
3536
string = string.replace("\n", "");
3637
return this.field.id() + ":" + string;
3738
}

io.openems.backend.metadata.odoo/src/io/openems/backend/metadata/odoo/odoo/OdooHandler.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -925,8 +925,8 @@ public Optional<String> getSerialNumberForEdge(Edge edge) {
925925
Field.EdgeDevice.STOCK_PRODUCTION_LOT_ID);
926926

927927
var serialNumber = serialNumberField.get(Field.EdgeDevice.STOCK_PRODUCTION_LOT_ID.id());
928-
if (serialNumber instanceof Object[] && ((Object[]) serialNumber).length > 1) {
929-
return getAsOptional(((Object[]) serialNumber)[1], String.class);
928+
if (serialNumber instanceof Object[] sns && sns.length > 1) {
929+
return getAsOptional(sns[1], String.class);
930930
}
931931
return Optional.empty();
932932
} catch (OpenemsException ex) {

io.openems.backend.metadata.odoo/src/io/openems/backend/metadata/odoo/odoo/OdooUtils.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -536,10 +536,10 @@ protected static <T extends Object> Optional<T> getAsOptional(Object object, Cla
536536
* @return the odoo reference id or empty {@link Optional}
537537
*/
538538
protected static Optional<Integer> getOdooReferenceId(Object object) {
539-
if (object instanceof Object[] odooReference) {
540-
if (odooReference.length > 0 && odooReference[0] instanceof Integer) {
541-
return Optional.of((Integer) odooReference[0]);
542-
}
539+
if (object instanceof Object[] odooReference //
540+
&& odooReference.length > 0 //
541+
&& odooReference[0] instanceof Integer i) {
542+
return Optional.of(i);
543543
}
544544

545545
return Optional.empty();

io.openems.common/src/io/openems/common/jsonrpc/base/AbstractJsonrpcRequest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @see <a href="https://www.jsonrpc.org/specification">JSON-RPC
1212
* specification</a>
1313
*/
14-
public abstract class AbstractJsonrpcRequest extends JsonrpcMessage {
14+
public abstract sealed class AbstractJsonrpcRequest extends JsonrpcMessage permits JsonrpcRequest, JsonrpcNotification {
1515

1616
private final String method;
1717

io.openems.common/src/io/openems/common/jsonrpc/base/JsonrpcMessage.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* @see <a href="https://www.jsonrpc.org/specification">JSON-RPC
2121
* specification</a>
2222
*/
23-
public abstract class JsonrpcMessage {
23+
public abstract sealed class JsonrpcMessage permits AbstractJsonrpcRequest, JsonrpcResponse {
2424

2525
public static final String JSONRPC_VERSION = "2.0";
2626

io.openems.common/src/io/openems/common/jsonrpc/base/JsonrpcNotification.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @see <a href="https://www.jsonrpc.org/specification#notification">JSON-RPC
1515
* specification</a>
1616
*/
17-
public abstract class JsonrpcNotification extends AbstractJsonrpcRequest {
17+
public abstract non-sealed class JsonrpcNotification extends AbstractJsonrpcRequest {
1818

1919
public JsonrpcNotification(String method) {
2020
super(method);

io.openems.common/src/io/openems/common/jsonrpc/base/JsonrpcRequest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* @see <a href="https://www.jsonrpc.org/specification#request_object">JSON-RPC
2424
* specification</a>
2525
*/
26-
public abstract class JsonrpcRequest extends AbstractJsonrpcRequest {
26+
public abstract non-sealed class JsonrpcRequest extends AbstractJsonrpcRequest {
2727

2828
public static final int DEFAULT_TIMEOUT_SECONDS = 60;
2929
public static final int NO_TIMEOUT = -1;

io.openems.common/src/io/openems/common/jsonrpc/base/JsonrpcResponse.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* @see <a href="https://www.jsonrpc.org/specification#response_object">JSON-RPC
2424
* specification</a>
2525
*/
26-
public abstract class JsonrpcResponse extends JsonrpcMessage {
26+
public abstract non-sealed class JsonrpcResponse extends JsonrpcMessage {
2727

2828
/**
2929
* Parses a JSON String to a {@link JsonrpcResponse}.

io.openems.common/src/io/openems/common/jsonrpc/base/JsonrpcResponseSuccess.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ public abstract class JsonrpcResponseSuccess extends JsonrpcResponse {
3232
* @throws OpenemsNamedException if it was not a Success Response
3333
*/
3434
public static JsonrpcResponseSuccess from(JsonObject j) throws OpenemsNamedException {
35-
var response = JsonrpcResponse.from(j);
36-
if (!(response instanceof JsonrpcResponseSuccess)) {
37-
throw OpenemsError.GENERIC.exception("Expected a JSON-RPC Success Response");
38-
}
39-
return (JsonrpcResponseSuccess) response;
35+
return switch (JsonrpcResponse.from(j)) {
36+
case JsonrpcResponseSuccess r -> r;
37+
default -> throw OpenemsError.GENERIC.exception("Expected a JSON-RPC Success Response");
38+
};
4039
}
4140

4241
public JsonrpcResponseSuccess(UUID id) {

io.openems.common/src/io/openems/common/jsonrpc/notification/AbstractDataNotification.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
* }
2727
* </pre>
2828
*/
29-
// TODO change to sealed class
30-
public abstract class AbstractDataNotification extends JsonrpcNotification {
29+
public abstract sealed class AbstractDataNotification extends JsonrpcNotification
30+
permits TimestampedDataNotification, AggregatedDataNotification, ResendDataNotification {
3131

3232
private final TreeBasedTable<Long, String, JsonElement> data;
3333

io.openems.common/src/io/openems/common/jsonrpc/notification/AggregatedDataNotification.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* }
2323
* </pre>
2424
*/
25-
public class AggregatedDataNotification extends AbstractDataNotification {
25+
public final class AggregatedDataNotification extends AbstractDataNotification {
2626

2727
public static final String METHOD = "aggregatedData";
2828

io.openems.common/src/io/openems/common/jsonrpc/notification/ResendDataNotification.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* }
2222
* </pre>
2323
*/
24-
public class ResendDataNotification extends AbstractDataNotification {
24+
public final class ResendDataNotification extends AbstractDataNotification {
2525

2626
public static final String METHOD = "resendData";
2727

io.openems.common/src/io/openems/common/jsonrpc/notification/TimestampedDataNotification.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* }
2323
* </pre>
2424
*/
25-
public class TimestampedDataNotification extends AbstractDataNotification {
25+
public final class TimestampedDataNotification extends AbstractDataNotification {
2626

2727
public static final String METHOD = "timestampedData";
2828

io.openems.common/src/io/openems/common/session/Role.java

+7-11
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,13 @@ public int getLevel() {
4545
* @return the Role
4646
*/
4747
public static Role getRole(String name) {
48-
switch (name.toLowerCase()) {
49-
case "admin":
50-
return ADMIN;
51-
case "installer":
52-
return INSTALLER;
53-
case "owner":
54-
return OWNER;
55-
case "guest":
56-
default:
57-
return GUEST;
58-
}
48+
return switch (name.toLowerCase()) {
49+
case "admin" -> ADMIN;
50+
case "installer" -> INSTALLER;
51+
case "owner" -> OWNER;
52+
case "guest" -> GUEST;
53+
default -> GUEST;
54+
};
5955
}
6056

6157
/**

io.openems.common/src/io/openems/common/timedata/Resolution.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,12 @@ public ChronoUnit getUnit() {
3838
* @return Date without offset
3939
*/
4040
public ZonedDateTime revertInfluxDbOffset(ZonedDateTime date) {
41-
switch (this.unit) {
42-
case DAYS:
43-
case MONTHS:
44-
return date.minus(this.value, this.unit);
45-
case MINUTES:
46-
case HOURS:
47-
default:
48-
return date;
49-
}
41+
return switch (this.unit) {
42+
case DAYS, MONTHS //
43+
-> date.minus(this.value, this.unit);
44+
default // MINUTES, HOURS, etc.
45+
-> date;
46+
};
5047
}
5148

5249
/**

0 commit comments

Comments
 (0)