Skip to content

Commit

Permalink
Merge branch 'release/v1.3.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
sfeilmeier committed Sep 22, 2017
2 parents 740f2d4 + 0ca4144 commit 33118fa
Show file tree
Hide file tree
Showing 25 changed files with 331 additions and 43 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
[![Dependency Status](https://www.versioneye.com/user/projects/59a3dd596725bd004562717b/badge.svg?style=flat-square)](https://www.versioneye.com/user/projects/59a3dd596725bd004562717b)

# OpenEMS
## Open Source Energy Management System

Expand Down Expand Up @@ -71,7 +69,7 @@ A number of devices, protocols and services are already implemented in OpenEMS:

* [JSON/REST](doc/rest-api.md)
* JSON/Websocket
* [Modbus/TCP (in development)](/OpenEMS/openems/issues/2)
* [Modbus/TCP](/doc/modbustcp-api.md)

## Get started

Expand Down
1 change: 1 addition & 0 deletions backend/OpenEMS-Backend (Dummy).launch
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<mapEntry key="BROWSER_WEBSOCKET_PORT" value="8078"/>
<mapEntry key="METADATA" value="DUMMY"/>
<mapEntry key="OPENEMS_WEBSOCKET_PORT" value="8077"/>
<mapEntry key="REST_API_PORT" value="8080"/>
<mapEntry key="TIMEDATA" value="DUMMY"/>
</mapAttribute>
<stringAttribute key="org.eclipse.debug.core.source_locator_id" value="org.eclipse.jdt.launching.sourceLocator.JavaSourceLookupDirector"/>
Expand Down
17 changes: 17 additions & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
</parent>
<artifactId>backend</artifactId>
<packaging>jar</packaging>
<repositories>
<repository>
<id>maven-restlet</id>
<name>Public online Restlet repository</name>
<url>http://maven.restlet.com</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
Expand All @@ -30,6 +37,16 @@
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet</artifactId>
<version>${restlet.version}</version>
</dependency>
<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet.ext.slf4j</artifactId>
<version>${restlet.version}</version>
</dependency>
<dependency>
<groupId>com.abercap</groupId>
<artifactId>odoo-java-api</artifactId>
Expand Down
8 changes: 8 additions & 0 deletions backend/src/main/java/io/openems/backend/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.openems.backend.browserwebsocket.BrowserWebsocket;
import io.openems.backend.metadata.Metadata;
import io.openems.backend.openemswebsocket.OpenemsWebsocket;
import io.openems.backend.restapi.RestApi;
import io.openems.backend.timedata.Timedata;
import io.openems.common.utils.EnvUtils;

Expand All @@ -22,6 +23,7 @@ public static void main(String[] args) throws Exception {
initTimedataProvider();
initOpenemsWebsocket();
initBrowserWebsocket();
initRestApi();

log.info("OpenEMS Backend started.");
log.info("================================================================================");
Expand Down Expand Up @@ -75,4 +77,10 @@ private static void initBrowserWebsocket() throws Exception {
log.info("Start Browser Websocket server on port [" + port + "]");
BrowserWebsocket.initialize(port);
}

private static void initRestApi() throws Exception {
int port = EnvUtils.getAsInt("REST_API_PORT");
log.info("Start Rest-Api server on port [" + port + "]");
RestApi.initialize(port);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ protected void _onMessage(WebSocket websocket, JsonObject jMessage, Optional<Jso
}
}

@Override
protected void _onClose(WebSocket websocket, Optional<BrowserSession> sessionOpt) {
// nothing to do. Session is kept open.
}

/**
* Forward message to OpenEMS websocket.
*
Expand Down Expand Up @@ -263,7 +268,8 @@ public void openemsConnectionOpened(String name) {
for (Device device : session.getData().getDevices()) {
if (name.equals(device.getName())) {
WebSocket websocket = this.websockets.inverse().get(session);
WebSocketUtils.sendNotification(Optional.of(websocket), Notification.EDGE_CONNECTION_OPENED, name);
WebSocketUtils.sendNotification(Optional.ofNullable(websocket), Notification.EDGE_CONNECTION_OPENED,
name);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.List;
import java.util.Optional;

import com.google.gson.JsonObject;

import io.openems.common.session.SessionData;
import io.openems.common.types.Device;

Expand Down Expand Up @@ -44,4 +46,11 @@ public String getUserName() {
public List<Device> getDevices() {
return devices;
}

@Override
public JsonObject toJsonObject() {
JsonObject j = new JsonObject();
// TODO Auto-generated method stub
return j;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void removeWebsocketInterconnection(WebSocket browserWebsocket, WebSocket
* Helper methods for Browser websockets
*/
public void addBrowserWebsocket(WebSocket websocket, BrowserSession session) {
this.browserWebsockets.put(websocket, session);
this.browserWebsockets.forcePut(websocket, session);
}

public void removeBrowserWebsocket(WebSocket websocket) {
Expand All @@ -57,7 +57,7 @@ public void removeBrowserWebsocket(WebSocket websocket) {
* Helper methods for OpenEMS websockets
*/
public void addOpenemsWebsocket(WebSocket websocket, String deviceName) {
this.openemsWebsockets.put(websocket, deviceName);
this.openemsWebsockets.forcePut(websocket, deviceName);
}

public void removeOpenemsWebsocket(WebSocket websocket) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,14 @@ public interface MetadataDevice {

void writeObject() throws OpenemsException;

public default JsonObject toJsonObject() {
JsonObject j = new JsonObject();
j.addProperty("name", this.getName());
j.addProperty("comment", this.getComment());
j.addProperty("id", this.getId());
// j.add("openemsConfig", this.getOpenemsConfig());
j.addProperty("productType", this.getProductType());
j.addProperty("state", this.getState());
return j;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,4 @@ public void setIpV4(String value) {
public void writeObject() throws OpenemsException {
log.debug("Metadata Dummy. Would write object");
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.openems.backend.openemswebsocket;

import java.util.Collection;
import java.util.Collections;
import java.util.Optional;

import org.java_websocket.WebSocket;
Expand Down Expand Up @@ -103,13 +105,14 @@ protected void _onOpen(WebSocket websocket, ClientHandshake handshake) {
}

/**
* Close event of websocket. Removes the session and the websocket.
* Close event of websocket. Removes the session.
*/
@Override
public void onClose(WebSocket websocket, int code, String reason, boolean remote) {
OpenemsSession session = this.websockets.get(websocket);
sessionManager.removeSession(session);
super.onClose(websocket, code, reason, remote);
public void _onClose(WebSocket websocket, Optional<OpenemsSession> sessionOpt) {
if (sessionOpt.isPresent()) {
log.info("Would remove the session... " + sessionOpt.get());
// TODO sessionManager.removeSession(sessionOpt.get());
}
}

/**
Expand Down Expand Up @@ -256,4 +259,8 @@ public Optional<WebSocket> getOpenemsWebsocket(String deviceName) {
OpenemsSession session = sessionOpt.get();
return Optional.ofNullable(this.websockets.inverse().get(session));
}

public Collection<OpenemsSession> getSessions() {
return Collections.synchronizedCollection(this.sessionManager.getSessions());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.openems.backend.openemswebsocket.session;

import com.google.gson.JsonObject;

import io.openems.backend.metadata.api.device.MetadataDevice;
import io.openems.common.session.SessionData;

Expand All @@ -13,4 +15,9 @@ public OpenemsSessionData(MetadataDevice device) {
public MetadataDevice getDevice() {
return device;
}

@Override
public JsonObject toJsonObject() {
return this.device.toJsonObject();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public OpenemsSession _createNewSession(String token, OpenemsSessionData data) {
@Override
protected void _putSession(String token, OpenemsSession session) {
super._putSession(token, session);
this.token2name.put(token, session.getData().getDevice().getName());
this.token2name.forcePut(token, session.getData().getDevice().getName());
}

@Override
Expand Down
31 changes: 31 additions & 0 deletions backend/src/main/java/io/openems/backend/restapi/RestApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.openems.backend.restapi;

/**
* Provider for RestApiSingleton singleton
*
* @author stefan.feilmeier
*
*/
public class RestApi {

private static RestApiSingleton instance;

/**
* Initialize and start the Websocketserver
*
* @param port
* @throws Exception
*/
public static synchronized void initialize(int port) throws Exception {
RestApi.instance = new RestApiSingleton(port);
}

/**
* Returns the singleton instance
*
* @return
*/
public static synchronized RestApiSingleton instance() {
return RestApi.instance;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*******************************************************************************
* OpenEMS - Open Source Energy Management System
* Copyright (c) 2016 FENECON GmbH and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Contributors:
* FENECON GmbH - initial API and implementation and initial documentation
*******************************************************************************/
package io.openems.backend.restapi;

import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;

import io.openems.backend.restapi.route.DevicesAllRestlet;

public class RestApiApplication extends Application {

/**
* Creates a root Restlet that will receive all incoming calls.
*/
@Override
public synchronized Restlet createInboundRoot() {
// ChallengeAuthenticator guard = createAuthenticator();
Router router = createRouter();
// guard.setNext(router);
// return guard;
return router;
}

// TODO authentication
// private ChallengeAuthenticator createAuthenticator() {
// ChallengeAuthenticator guard = new ChallengeAuthenticator(getContext(), ChallengeScheme.HTTP_BASIC,
// "OpenEMS REST-Api");
// guard.setVerifier(new OpenemsVerifier());
// guard.setEnroler(new OpenemsEnroler());
// return guard;
// }

private Router createRouter() {
Router router = new Router(getContext());
router.attach("/devices/all", new DevicesAllRestlet());
// router.attach("/device/{deviceId}", new ChannelRestlet());
return router;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.openems.backend.restapi;

import org.restlet.Component;
import org.restlet.data.Protocol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RestApiSingleton {

private final Logger log = LoggerFactory.getLogger(RestApiSingleton.class);
private final Component component;

public RestApiSingleton(int port) throws Exception {
this.component = new Component();
this.component.getServers().add(Protocol.HTTP, port);
this.component.getDefaultHost().attach("/rest", new RestApiApplication());
this.component.start();
log.info("REST-Api started on port [" + port + "].");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*******************************************************************************
* OpenEMS - Open Source Energy Management System
* Copyright (c) 2016 FENECON GmbH and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Contributors:
* FENECON GmbH - initial API and implementation and initial documentation
*******************************************************************************/
package io.openems.backend.restapi.route;

import org.restlet.Request;
import org.restlet.Response;
import org.restlet.Restlet;
import org.restlet.data.MediaType;
import org.restlet.data.Method;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;

import com.google.gson.JsonArray;

import io.openems.backend.openemswebsocket.OpenemsWebsocket;
import io.openems.backend.openemswebsocket.session.OpenemsSession;

public class DevicesAllRestlet extends Restlet {

@Override
public void handle(Request request, Response response) {
super.handle(request, response);

// call handler methods
if (request.getMethod().equals(Method.GET)) {
JsonArray j = new JsonArray();
for (OpenemsSession session : OpenemsWebsocket.instance().getSessions()) {
j.add(session.getData().getDevice().toJsonObject());
}
Representation entity = new StringRepresentation(j.toString(), MediaType.APPLICATION_JSON);
response.setEntity(entity);
}
}
}
Loading

0 comments on commit 33118fa

Please sign in to comment.