Skip to content

Commit

Permalink
UI: Improve alerting user interface; add admin features; (#317)
Browse files Browse the repository at this point in the history
- Refactor JSONRPC-Requests/-Responses
- JsonUtils: refactor tests with exception; new method: generateJsonArray()
- Improve OdooUtils

NOTE: requires Backend + UI update

Co-authored-by: Maximilian Lang <maximilian.lang@fenecon.de>
Co-authored-by: Lukas Rieger <lukas.rieger@fenecon.de>
Co-authored-by: Stefan Feilmeier <stefan.feilmeier@fenecon.de>
Reviewed-on: https://git.intranet.fenecon.de/FENECON/fems/pulls/317
Reviewed-by: Lukas Rieger <lukas.rieger@fenecon.de>
Reviewed-by: Stefan Feilmeier <stefan.feilmeier@fenecon.de>
Co-authored-by: Kai Jeschek <kai.jeschek@fenecon.de>
Co-committed-by: Kai Jeschek <kai.jeschek@fenecon.de>
  • Loading branch information
4 people committed Oct 4, 2022
1 parent d5c0c7b commit 831b307
Show file tree
Hide file tree
Showing 38 changed files with 1,469 additions and 963 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.gson.JsonObject;

import io.openems.common.exceptions.OpenemsError.OpenemsNamedException;
import io.openems.common.exceptions.OpenemsException;
import io.openems.common.jsonrpc.base.JsonrpcRequest;
import io.openems.common.utils.JsonUtils;

Expand All @@ -14,36 +13,41 @@
* {
* "jsonrpc": "2.0",
* "id": "UUID",
* "method": "getEdgeConfig",
* "method": "getUserAlertingConfigs",
* "params": {
* edgeId: String
* edgeId: string,
* }
* }
* </pre>
*/
public class GetAlertingConfigRequest extends JsonrpcRequest {
public class GetUserAlertingConfigsRequest extends JsonrpcRequest {

public static final String METHOD = "getAlertingConfig";
public static final String METHOD = "getUserAlertingConfigs";

/**
* Create {@link GetAlertingConfigRequest} from a template
* Create {@link GetUserAlertingConfigsRequest} from a template
* {@link JsonrpcRequest}.
*
* @param r the template {@link JsonrpcRequest}
* @return the {@link GetAlertingConfigRequest}
* @return the {@link GetUserAlertingConfigsRequest}
* @throws OpenemsNamedException on parse error
*/
public static GetAlertingConfigRequest from(JsonrpcRequest r) throws OpenemsException {
return new GetAlertingConfigRequest(r);
public static GetUserAlertingConfigsRequest from(JsonrpcRequest r) throws OpenemsNamedException {
return new GetUserAlertingConfigsRequest(r);
}

private final String edgeId;

private GetAlertingConfigRequest(JsonrpcRequest request) {
super(request, GetAlertingConfigRequest.METHOD);
this.edgeId = request.getParams().get("edgeId").getAsString();
private GetUserAlertingConfigsRequest(JsonrpcRequest request) throws OpenemsNamedException {
super(request, GetUserAlertingConfigsRequest.METHOD);
this.edgeId = JsonUtils.getAsString(request.getParams(), "edgeId");
}

/**
* Get the Edge-ID.
*
* @return the Edge-ID
*/
public String getEdgeId() {
return this.edgeId;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package io.openems.backend.common.jsonrpc.request;

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

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import io.openems.backend.common.metadata.AlertingSetting;
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'.
*
* <pre>
* {
* "jsonrpc": "2.0",
* "id": "UUID",
* "method": "setUserAlertingConfigs",
* "params": {
* "edgeId": string,
* "userSettings": [
* {
* userId: string,
* delayTime": number
* }
* ]
* }
* </pre>
*/
public class SetUserAlertingConfigsRequest extends JsonrpcRequest {

public static final String METHOD = "setUserAlertingConfigs";

/**
* Create {@link SetUserAlertingConfigsRequest} from a template
* {@link JsonrpcRequest}.
*
* @param request the template {@link JsonrpcRequest}
* @return the {@link SetUserAlertingConfigsRequest}
* @throws OpenemsNamedException on parse error
*/
public static SetUserAlertingConfigsRequest from(JsonrpcRequest request) throws OpenemsNamedException {
return new SetUserAlertingConfigsRequest(request);
}

private final String edgeId;
private final List<AlertingSetting> userSettings = new ArrayList<>();

private SetUserAlertingConfigsRequest(JsonrpcRequest request) throws OpenemsNamedException {
super(request, SetUserAlertingConfigsRequest.METHOD);
var params = request.getParams();

this.edgeId = JsonUtils.getAsString(params, "edgeId");
JsonUtils.getAsJsonArray(params, "userSettings").forEach(user -> {
var userJsonObject = user.getAsJsonObject();
try {
var userId = JsonUtils.getAsString(userJsonObject, "userId");
var timeToWait = JsonUtils.getAsInt(userJsonObject, "delayTime");

this.userSettings.add(new AlertingSetting(userId, null, null, timeToWait));
} catch (OpenemsNamedException e) {
e.printStackTrace();
}
});
}

/**
* Get the Edge-ID.
*
* @return the Edge-ID
*/
public String getEdgeId() {
return this.edgeId;
}

/**
* Get list of {@link UserAlertingSetting}.
*
* @return list of {@link UserAlertingSetting}
*/
public List<AlertingSetting> getUserSettings() {
return this.userSettings;
}

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

private JsonElement toJson(AlertingSetting setting) {
return JsonUtils.buildJsonObject() //
.addProperty("userId", setting.getUserId()) //
.addProperty("delayTime", setting.getDelayTime()) //
.build();
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.openems.backend.common.jsonrpc.response;

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

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import io.openems.backend.common.metadata.AlertingSetting;
import io.openems.common.jsonrpc.base.JsonrpcResponseSuccess;
import io.openems.common.session.Role;
import io.openems.common.utils.JsonUtils;

/**
* Represents a JSON-RPC Response for 'getAlertingConfig'.
*
* <pre>
* {
* "jsonrpc": "2.0",
* "id": "UUID",
* "result": {
* userSettings: [
* {
* userId: string,
* role: {@link Role},
* delayTime": number
* }
* ]
* }
* }
* </pre>
*/
public class GetUserAlertingConfigsResponse extends JsonrpcResponseSuccess {

private final List<AlertingSetting> settings;

public GetUserAlertingConfigsResponse(UUID id, List<AlertingSetting> settings) {
super(id);
this.settings = settings;
}

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

private JsonElement toJson(AlertingSetting setting) {
return JsonUtils.buildJsonObject() //
.addProperty("userId", setting.getUserId()) //
.add("role", setting.getUserRole().asJson()) //
.addProperty("delayTime", setting.getDelayTime()) //
.build(); //
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.openems.backend.common.metadata;

import java.time.ZonedDateTime;

import io.openems.common.session.Role;

public class AlertingSetting {
private final String userId;
private final Role userRole;
private final ZonedDateTime lastNotification;
private final int delayTime;

public AlertingSetting(String userId, Role userRole, ZonedDateTime lastNotification, int delayTime) {
this.userId = userId;
this.userRole = userRole;
this.lastNotification = lastNotification;
this.delayTime = delayTime;
}

public String getUserId() {
return this.userId;
}

public Role getUserRole() {
return this.userRole;
}

public ZonedDateTime getLastNotification() {
return this.lastNotification;
}

public int getDelayTime() {
return this.delayTime;
}
}
Loading

0 comments on commit 831b307

Please sign in to comment.