Skip to content

Commit

Permalink
Review + apply formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
sfeilmeier committed Feb 20, 2024
1 parent 416e28d commit 6006eae
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,9 @@ private void handleEdgeOnSetSumState(EventReader event) {
@Override
public Consumer<EventReader> getEventHandler(String eventTopic) {
return switch (eventTopic) {
case Edge.Events.ON_SET_ONLINE:
yield this::handleEdgeOnSetOnline;

case Edge.Events.ON_SET_SUM_STATE:
yield this::handleEdgeOnSetSumState;

default:
yield null;
case Edge.Events.ON_SET_ONLINE -> this::handleEdgeOnSetOnline;
case Edge.Events.ON_SET_SUM_STATE -> this::handleEdgeOnSetSumState;
default -> null;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,10 @@ public record SumStateAlertingSetting(String edgeId, String userLogin, int fault
*/
public int getDelay(Level state) {
return switch (state) {
case OK:
case INFO:
yield 0;
case FAULT:
yield this.faultDelay;
case WARNING:
yield this.warningDelay;
default:
yield -1;
case OK, INFO -> 0;
case FAULT -> this.faultDelay;
case WARNING -> this.warningDelay;
default -> -1;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
public record UserAlertingSettings(String deviceId, String userLogin, //
int edgeOfflineDelay, int edgeFaultDelay, int edgeWarningDelay, //
ZonedDateTime lastEdgeOfflineNotification, ZonedDateTime lastSumStateNotification) {

public UserAlertingSettings(String userLogin, int offlineEdgeDelay, int faultStateDelay, int warningStateDelay) {
this(null, userLogin, offlineEdgeDelay, faultStateDelay, warningStateDelay, null, null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package io.openems.backend.common.jsonrpc.request;

import static io.openems.common.utils.JsonUtils.buildJsonObject;
import static io.openems.common.utils.JsonUtils.generateJsonArray;
import static io.openems.common.utils.JsonUtils.getAsInt;
import static io.openems.common.utils.JsonUtils.getAsJsonArray;
import static io.openems.common.utils.JsonUtils.getAsString;

import java.util.ArrayList;
import java.util.List;

Expand All @@ -9,7 +15,6 @@
import io.openems.backend.common.alerting.UserAlertingSettings;
import io.openems.common.exceptions.OpenemsError.OpenemsNamedException;
import io.openems.common.jsonrpc.base.JsonrpcRequest;
import io.openems.common.utils.JsonUtils;

/**
* Represents a JSON-RPC Request for 'getEdgeConfig'.
Expand Down Expand Up @@ -55,14 +60,14 @@ private SetUserAlertingConfigsRequest(JsonrpcRequest request) throws OpenemsName
super(request, SetUserAlertingConfigsRequest.METHOD);
var params = request.getParams();

this.edgeId = JsonUtils.getAsString(params, "edgeId");
JsonUtils.getAsJsonArray(params, "userSettings").forEach(user -> {
this.edgeId = getAsString(params, "edgeId");
getAsJsonArray(params, "userSettings").forEach(user -> {
var userJsonObject = user.getAsJsonObject();
try {
var userLogin = JsonUtils.getAsString(userJsonObject, "userLogin");
var offlineEdgeDelay = JsonUtils.getAsInt(userJsonObject, "offlineEdgeDelay");
var faultEdgeDelay = JsonUtils.getAsInt(userJsonObject, "faultEdgeDelay");
var warningEdgeDelay = JsonUtils.getAsInt(userJsonObject, "warningEdgeDelay");
var userLogin = getAsString(userJsonObject, "userLogin");
var offlineEdgeDelay = getAsInt(userJsonObject, "offlineEdgeDelay");
var faultEdgeDelay = getAsInt(userJsonObject, "faultEdgeDelay");
var warningEdgeDelay = getAsInt(userJsonObject, "warningEdgeDelay");

this.userSettings
.add(new UserAlertingSettings(userLogin, offlineEdgeDelay, faultEdgeDelay, warningEdgeDelay));
Expand Down Expand Up @@ -92,14 +97,14 @@ public List<UserAlertingSettings> getUserSettings() {

@Override
public JsonObject getParams() {
return JsonUtils.buildJsonObject() //
return buildJsonObject() //
.addProperty("edgeId", this.edgeId) //
.add("userSettings", JsonUtils.generateJsonArray(this.userSettings, this::toJson)) //
.add("userSettings", generateJsonArray(this.userSettings, this::toJson)) //
.build();
}

private JsonElement toJson(UserAlertingSettings setting) {
return JsonUtils.buildJsonObject() //
return buildJsonObject() //
.addProperty("userLogin", setting.userLogin()) //
.addProperty("offlineEdgeDelay", setting.edgeOfflineDelay()) //
.addProperty("faultEdgeDelay", setting.edgeFaultDelay()) //
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.openems.backend.common.jsonrpc.response;

import static io.openems.common.utils.JsonUtils.buildJsonObject;
import static io.openems.common.utils.JsonUtils.generateJsonArray;

import java.util.List;
import java.util.UUID;

Expand All @@ -8,7 +11,6 @@

import io.openems.backend.common.alerting.UserAlertingSettings;
import io.openems.common.jsonrpc.base.JsonrpcResponseSuccess;
import io.openems.common.utils.JsonUtils;

/**
* Represents a JSON-RPC Response for 'getAlertingConfig'.
Expand Down Expand Up @@ -50,14 +52,14 @@ public GetUserAlertingConfigsResponse(UUID id, UserAlertingSettings currentUserS

@Override
public JsonObject getResult() {
return JsonUtils.buildJsonObject() //
return buildJsonObject() //
.add("currentUserSettings", this.toJson(this.currentUserSettings)) //
.add("otherUsersSettings", JsonUtils.generateJsonArray(this.otherUsersSettings, this::toJson)) //
.add("otherUsersSettings", generateJsonArray(this.otherUsersSettings, this::toJson)) //
.build();
}

private JsonElement toJson(UserAlertingSettings setting) {
return JsonUtils.buildJsonObject() //
return buildJsonObject() //
.addProperty("userLogin", setting.userLogin()) //
.addProperty("offlineEdgeDelay", setting.edgeOfflineDelay()) //
.addProperty("faultEdgeDelay", setting.edgeFaultDelay()) //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public class User extends AbstractUser {
*/
private final boolean hasMultipleEdges;

public User(String id, String name, String token, Language language, Role globalRole, boolean hasMultipleEdges, JsonObject settings) {
public User(String id, String name, String token, Language language, Role globalRole, boolean hasMultipleEdges,
JsonObject settings) {
this(id, name, token, language, globalRole, new TreeMap<>(), hasMultipleEdges, settings);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Collection<Edge> getAllOfflineEdges() {
public UserAlertingSettings getUserAlertingSettings(String edgeId, String userId) throws OpenemsException {
throw new UnsupportedOperationException("Unsupported by Dummy Class");
}

@Override
public List<UserAlertingSettings> getUserAlertingSettings(String edgeId) {
throw new UnsupportedOperationException("Unsupported by Dummy Class");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.openems.backend.common.jsonrpc;

import static io.openems.common.utils.JsonUtils.buildJsonArray;
import static io.openems.common.utils.JsonUtils.buildJsonObject;
import static java.util.UUID.randomUUID;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.util.List;
import java.util.UUID;

import org.junit.Test;

Expand All @@ -16,17 +18,17 @@
import io.openems.backend.common.jsonrpc.response.GetUserAlertingConfigsResponse;
import io.openems.common.exceptions.OpenemsError.OpenemsNamedException;
import io.openems.common.jsonrpc.base.JsonrpcRequest;
import io.openems.common.utils.JsonUtils;

public class UserAlertingSettingsJsonRpc {

@Test
public void testGetUserAlertingConfigsRequest() {
assertEquals("getUserAlertingConfigs", GetUserAlertingConfigsRequest.METHOD);

var id = UUID.randomUUID();
var id = randomUUID();
var edgeId = "edge4";
var params = JsonUtils.buildJsonObject().addProperty("edgeId", edgeId).build();
var params = buildJsonObject() //
.addProperty("edgeId", edgeId).build();

var json = new JsonrpcRequest(id, GetUserAlertingConfigsRequest.METHOD, 0) {
@Override
Expand All @@ -40,6 +42,7 @@ public JsonObject getParams() {
assertEquals(id, request.id);
assertEquals(edgeId, request.getEdgeId());
assertEquals(params, request.getParams());

} catch (OpenemsNamedException e) {
e.printStackTrace();
fail(e.getMessage());
Expand All @@ -50,21 +53,22 @@ public JsonObject getParams() {
public void testSetUserAlertingConfigsRequest() {
assertEquals("setUserAlertingConfigs", SetUserAlertingConfigsRequest.METHOD);

var userSettingJson = JsonUtils.buildJsonObject() //
var userSettingJson = buildJsonObject() //
.addProperty("userLogin", "user1") //
.addProperty("offlineEdgeDelay", 15) //
.addProperty("faultEdgeDelay", 30) //
.addProperty("warningEdgeDelay", 60) //
.build();
var illegalSettingJson = JsonUtils.buildJsonObject() //
var illegalSettingJson = buildJsonObject() //
.addProperty("err", "wrong") //
.build();

var id = UUID.randomUUID();
var id = randomUUID();
var edgeId = "edge4";
var params = JsonUtils.buildJsonObject() //
var params = buildJsonObject() //
.addProperty("edgeId", edgeId) //
.add("userSettings", JsonUtils.buildJsonArray().add(userSettingJson) //
.add("userSettings", buildJsonArray() //
.add(userSettingJson) //
.add(illegalSettingJson) //
.build()) //
.build();
Expand All @@ -83,15 +87,17 @@ public JsonObject getParams() {
assertEquals(id, request.id);
assertEquals(edgeId, request.getEdgeId());

var expected = JsonUtils.buildJsonObject() //
var expected = buildJsonObject() //
.addProperty("edgeId", edgeId) //
.add("userSettings", JsonUtils.buildJsonArray().add(userSettingJson).build()) //
.add("userSettings", buildJsonArray() //
.add(userSettingJson).build()) //
.build();
assertEquals(expected, request.getParams());

var settings = request.getUserSettings();
assertEquals(1, settings.size());
assertEquals(userSetting, settings.get(0));

} catch (OpenemsNamedException e) {
e.printStackTrace();
fail(e.getMessage());
Expand All @@ -103,25 +109,25 @@ public void testGetUserAlertingConfigsResponse() {
var sett1 = new UserAlertingSettings("edge1", "user1", 0, 15, 30, null, null);
var sett2 = new UserAlertingSettings("edge2", "user2", 10, 10, 10, null, null);

var id = UUID.randomUUID();
var id = randomUUID();

var response = new GetUserAlertingConfigsResponse(id, sett1, List.of(sett2));

var sett1Json = JsonUtils.buildJsonObject() //
var sett1Json = buildJsonObject() //
.addProperty("userLogin", sett1.userLogin()) //
.addProperty("offlineEdgeDelay", sett1.edgeOfflineDelay()) //
.addProperty("faultEdgeDelay", sett1.edgeFaultDelay()) //
.addProperty("warningEdgeDelay", sett1.edgeWarningDelay()) //
.build();
var sett2Json = JsonUtils.buildJsonObject() //
var sett2Json = buildJsonObject() //
.addProperty("userLogin", sett2.userLogin()) //
.addProperty("offlineEdgeDelay", sett2.edgeOfflineDelay()) //
.addProperty("faultEdgeDelay", sett2.edgeFaultDelay()) //
.addProperty("warningEdgeDelay", sett2.edgeWarningDelay()) //
.build();
var settArrJson = JsonUtils.buildJsonObject() //
var settArrJson = buildJsonObject() //
.add("currentUserSettings", sett1Json)
.add("otherUsersSettings", JsonUtils.buildJsonArray().add(sett2Json).build()) //
.add("otherUsersSettings", buildJsonArray().add(sett2Json).build()) //
.build();

var jsonObj = response.getResult();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.openems.backend.metadata.odoo;

import java.util.stream.Collectors;
import static java.util.stream.Collectors.joining;

import java.util.stream.Stream;

public interface Field {
Expand Down Expand Up @@ -34,13 +35,9 @@ public interface Field {
public boolean isQuery();

private static <T extends Enum<? extends Field>> Stream<Field> getSqlQueryFieldsOf(Class<T> field) {
return Stream.of(field.getEnumConstants()).map(v -> {
if (v instanceof Field f) {
return f;
} else {
return null;
}
}).filter(Field::isQuery);
return Stream.of(field.getEnumConstants()) //
.map(v -> v instanceof Field f ? f : null) //
.filter(Field::isQuery);
}

/**
Expand All @@ -53,7 +50,7 @@ public static String getSqlQueryFields(Field[] fields) {
return Stream.of(fields) //
.filter(Field::isQuery) //
.map(Field::id) //
.collect(Collectors.joining(","));
.collect(joining(","));
}

/**
Expand All @@ -65,7 +62,8 @@ public static String getSqlQueryFields(Field[] fields) {
* @return list of fields
*/
public static <T extends Enum<? extends Field>> Field[] getSqlQueryFields(Class<T> field) {
return Field.getSqlQueryFieldsOf(field).toArray(Field[]::new);
return Field.getSqlQueryFieldsOf(field) //
.toArray(Field[]::new);
}

/**
Expand All @@ -85,7 +83,9 @@ public static <T extends Enum<? extends Field>> Field[] getSqlQueryFields(Class<

public record GenericField(String id) implements Field {
public GenericField(Field... fields) {
this(Stream.of(fields).map(Field::name).collect(Collectors.joining(".")));
this(Stream.of(fields) //
.map(Field::name) //
.collect(joining(".")));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,6 @@ public Optional<Level> getSumState(String edgeId) {
}
}


@Override
public void updateUserSettings(User user, JsonObject settings) throws OpenemsNamedException {
this.odooHandler.updateUserSettings(user, settings);
Expand Down
Loading

0 comments on commit 6006eae

Please sign in to comment.