Skip to content

Commit

Permalink
Merge branch 'release/v1.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
sfeilmeier committed Oct 5, 2017
2 parents 33118fa + 035b097 commit 2a5b0ff
Show file tree
Hide file tree
Showing 79 changed files with 2,971 additions and 1,419 deletions.
31 changes: 18 additions & 13 deletions backend/src/main/java/io/openems/backend/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,36 @@
import io.openems.backend.openemswebsocket.OpenemsWebsocket;
import io.openems.backend.restapi.RestApi;
import io.openems.backend.timedata.Timedata;
import io.openems.common.exceptions.OpenemsException;
import io.openems.common.utils.EnvUtils;

public class App {
private static Logger log = LoggerFactory.getLogger(App.class);

public static void main(String[] args) throws Exception {
public static void main(String[] args) {
log.info("OpenEMS-Backend starting...");

// Configure everything
initMetadataProvider();
initTimedataProvider();
initOpenemsWebsocket();
initBrowserWebsocket();
initRestApi();
try {
initMetadataProvider();
initTimedataProvider();
initOpenemsWebsocket();
initBrowserWebsocket();
initRestApi();

log.info("OpenEMS Backend started.");
log.info("================================================================================");
log.info("OpenEMS Backend started.");
log.info("================================================================================");
} catch (OpenemsException e) {
log.error("OpenEMS Backend failed to start: " + e.getMessage());
}
}

/**
* Configures a metadata provider. It uses either Odoo as backend or a simple Dummy provider.
*
* @throws Exception
*/
private static void initMetadataProvider() throws Exception {
private static void initMetadataProvider() throws OpenemsException {
Optional<String> metadataOpt = EnvUtils.getAsOptionalString("METADATA");
if (metadataOpt.isPresent() && metadataOpt.get().equals("DUMMY")) {
log.info("Start Dummy Metadata provider");
Expand All @@ -50,7 +55,7 @@ private static void initMetadataProvider() throws Exception {
}
}

private static void initTimedataProvider() throws Exception {
private static void initTimedataProvider() throws OpenemsException {
Optional<String> timedataOpt = EnvUtils.getAsOptionalString("TIMEDATA");
if (timedataOpt.isPresent() && timedataOpt.get().equals("DUMMY")) {
log.info("Start Dummy Timedata provider");
Expand All @@ -66,19 +71,19 @@ private static void initTimedataProvider() throws Exception {
}
}

private static void initOpenemsWebsocket() throws Exception {
private static void initOpenemsWebsocket() throws OpenemsException {
int port = EnvUtils.getAsInt("OPENEMS_WEBSOCKET_PORT");
log.info("Start OpenEMS Websocket server on port [" + port + "]");
OpenemsWebsocket.initialize(port);
}

private static void initBrowserWebsocket() throws Exception {
private static void initBrowserWebsocket() throws OpenemsException {
int port = EnvUtils.getAsInt("BROWSER_WEBSOCKET_PORT");
log.info("Start Browser Websocket server on port [" + port + "]");
BrowserWebsocket.initialize(port);
}

private static void initRestApi() throws Exception {
private static void initRestApi() throws OpenemsException {
int port = EnvUtils.getAsInt("REST_API_PORT");
log.info("Start Rest-Api server on port [" + port + "]");
RestApi.initialize(port);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.openems.backend.browserwebsocket;

import io.openems.common.exceptions.OpenemsException;

/**
* Provider for OpenemsWebsocketServer singleton
*
Expand All @@ -16,7 +18,7 @@ public class BrowserWebsocket {
* @param port
* @throws Exception
*/
public static synchronized void initialize(int port) throws Exception {
public static synchronized void initialize(int port) throws OpenemsException {
BrowserWebsocket.instance = new BrowserWebsocketSingleton(port);
BrowserWebsocket.instance.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.openems.common.utils.JsonUtils;
import io.openems.common.websocket.AbstractWebsocketServer;
import io.openems.common.websocket.DefaultMessages;
import io.openems.common.websocket.LogBehaviour;
import io.openems.common.websocket.Notification;
import io.openems.common.websocket.WebSocketUtils;

Expand All @@ -38,7 +39,7 @@ public class BrowserWebsocketSingleton
extends AbstractWebsocketServer<BrowserSession, BrowserSessionData, BrowserSessionManager> {
private final Logger log = LoggerFactory.getLogger(BrowserWebsocketSingleton.class);

protected BrowserWebsocketSingleton(int port) throws Exception {
protected BrowserWebsocketSingleton(int port) throws OpenemsException {
super(port, new BrowserSessionManager());
}

Expand Down Expand Up @@ -89,7 +90,7 @@ protected void _onOpen(WebSocket websocket, ClientHandshake handshake) {

// check if the session is now valid and send reply to browser
BrowserSessionData data = session.getData();
if (error.isEmpty() && session.isValid()) {
if (error.isEmpty()) {
// add isOnline information
OpenemsWebsocketSingleton openemsWebsocket = OpenemsWebsocket.instance();
for (Device device : data.getDevices()) {
Expand All @@ -103,18 +104,17 @@ protected void _onOpen(WebSocket websocket, ClientHandshake handshake) {
WebSocketUtils.send(websocket, jReply);

// add websocket to local cache
this.websockets.forcePut(websocket, session);
this.addWebsocket(websocket, session);

log.info("User [" + data.getUserName() + "] connected with Session [" + data.getOdooSessionId().orElse("")
+ "]. Total websockets [" + this.websockets.size() + "]");
+ "].");

} else {
// send connection failed to browser
JsonObject jReply = DefaultMessages.browserConnectionFailedReply();
WebSocketUtils.send(websocket, jReply);
log.warn("User [" + data.getUserName() + "] connection failed. Session ["
+ data.getOdooSessionId().orElse("") + "] Error [" + error + "]. Total websockets ["
+ this.websockets.size() + "]");
+ data.getOdooSessionId().orElse("") + "] Error [" + error + "].");

websocket.closeConnection(CloseFrame.REFUSE, error);
}
Expand Down Expand Up @@ -158,9 +158,8 @@ protected void _onMessage(WebSocket websocket, JsonObject jMessage, Optional<Jso
try {
forwardMessageToOpenems(websocket, jMessage, deviceName);
} catch (OpenemsException e) {
WebSocketUtils.sendNotification(websocket, Notification.EDGE_UNABLE_TO_FORWARD, deviceName,
e.getMessage());
log.error("Unable to forward to Device [" + deviceName + "] : " + e.getMessage());
WebSocketUtils.sendNotification(websocket, LogBehaviour.WRITE_TO_LOG,
Notification.EDGE_UNABLE_TO_FORWARD, deviceName, e.getMessage());
}
}
}
Expand All @@ -184,14 +183,14 @@ private void forwardMessageToOpenems(WebSocket websocket, JsonObject jMessage, S
}

// add session token to message id for identification
BrowserSession session = this.websockets.get(websocket);
Optional<BrowserSession> session = this.getSessionFromWebsocket(websocket);
JsonArray jId;
if (jMessage.has("id")) {
jId = JsonUtils.getAsJsonArray(jMessage, "id");
} else {
jId = new JsonArray();
}
jId.add(session.getToken());
jId.add(session.get().getToken());
jMessage.add("id", jId);

// get OpenEMS websocket and forward message
Expand Down Expand Up @@ -251,8 +250,9 @@ public void openemsConnectionClosed(String name) {
for (BrowserSession session : this.sessionManager.getSessions()) {
for (Device device : session.getData().getDevices()) {
if (name.equals(device.getName())) {
WebSocket websocket = this.websockets.inverse().get(session);
WebSocketUtils.sendNotification(websocket, Notification.EDGE_CONNECTION_ClOSED, name);
Optional<WebSocket> websocketOpt = this.getWebsocketFromSession(session);
WebSocketUtils.sendNotification(websocketOpt, LogBehaviour.DO_NOT_WRITE_TO_LOG,
Notification.EDGE_CONNECTION_ClOSED, name);
}
}
}
Expand All @@ -267,9 +267,9 @@ public void openemsConnectionOpened(String name) {
for (BrowserSession session : this.sessionManager.getSessions()) {
for (Device device : session.getData().getDevices()) {
if (name.equals(device.getName())) {
WebSocket websocket = this.websockets.inverse().get(session);
WebSocketUtils.sendNotification(Optional.ofNullable(websocket), Notification.EDGE_CONNECTION_OPENED,
name);
Optional<WebSocket> websocketOpt = this.getWebsocketFromSession(session);
WebSocketUtils.sendNotification(websocketOpt, LogBehaviour.DO_NOT_WRITE_TO_LOG,
Notification.EDGE_CONNECTION_OPENED, name);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.openems.backend.metadata.api.MetadataSingleton;
import io.openems.backend.metadata.dummy.MetadataDummySingleton;
import io.openems.backend.metadata.odoo.OdooSingleton;
import io.openems.common.exceptions.OpenemsException;

/**
* Provider for Metadata singleton
Expand All @@ -21,9 +22,9 @@ public class Metadata {
* @throws Exception
*/
public static synchronized void initializeOdoo(String url, int port, String database, String username,
String password) throws Exception {
String password) throws OpenemsException {
if (url == null || database == null || username == null || password == null) {
throw new Exception("Config missing: database [" + database + "], url [" + url + "], port [" + port
throw new OpenemsException("Config missing: database [" + database + "], url [" + url + "], port [" + port
+ "] username [" + username + "], password [" + password + "]");
}
Metadata.instance = new OdooSingleton(url, port, database, username, password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public void getInfoWithSession(BrowserSession session) throws OpenemsException {
deviceInfos.add(device);
}
data.setDevices(deviceInfos);
session.setValid();
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.xmlrpc.XmlRpcException;

import com.abercap.odoo.OdooApiException;
import com.abercap.odoo.Session;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
Expand All @@ -32,15 +35,24 @@ public class OdooSingleton implements MetadataSingleton {
private MetadataDeviceModel deviceModel;
private final String url;

public OdooSingleton(String url, int port, String database, String username, String password) throws Exception {
public OdooSingleton(String url, int port, String database, String username, String password)
throws OpenemsException {
this.session = new Session(url, port, database, username, password);
this.connect();
this.deviceModel = new OdooDeviceModel(this.session);
try {
this.deviceModel = new OdooDeviceModel(this.session);
} catch (XmlRpcException | OdooApiException e) {
throw new OpenemsException("Initializing OdooDeviceModel failed: " + e.getMessage());
}
this.url = "http://" + url + ":" + port;
}

private void connect() throws Exception {
session.startSession();
private void connect() throws OpenemsException {
try {
session.startSession();
} catch (Exception e) {
throw new OpenemsException("Odoo connection failed: " + e.getMessage());
}
}

@Override
Expand Down Expand Up @@ -113,7 +125,6 @@ public void getInfoWithSession(BrowserSession session) throws OpenemsException {
JsonUtils.getAsString(jDevice, "role")));
}
data.setDevices(deviceInfos);
session.setValid();
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class OpenemsWebsocket {
* @param port
* @throws Exception
*/
public static synchronized void initialize(int port) throws Exception {
public static synchronized void initialize(int port) {
OpenemsWebsocket.instance = new OpenemsWebsocketSingleton(port);
OpenemsWebsocket.instance.start();
}
Expand Down
Loading

0 comments on commit 2a5b0ff

Please sign in to comment.