-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
331 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
backend/src/main/java/io/openems/backend/restapi/RestApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
backend/src/main/java/io/openems/backend/restapi/RestApiApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
backend/src/main/java/io/openems/backend/restapi/RestApiSingleton.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "]."); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
backend/src/main/java/io/openems/backend/restapi/route/DevicesAllRestlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.