Skip to content

Commit

Permalink
Merge branch 'release/v1.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
sfeilmeier committed Oct 19, 2017
2 parents dd3b1c4 + 207aa90 commit 2e497da
Show file tree
Hide file tree
Showing 160 changed files with 6,764 additions and 4,772 deletions.
2 changes: 2 additions & 0 deletions backend/.settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
eclipse.preferences.version=1
encoding//src/io/openems/impl/scheduler/SimpleScheduler.java=UTF-8
encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8
encoding/src=UTF-8
47 changes: 35 additions & 12 deletions backend/pom.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<name>OpenEMS Backend</name>
<parent>
<groupId>io.openems</groupId>
<artifactId>pom</artifactId>
<version>1.3</version>
</parent>
<artifactId>backend</artifactId>
<description>Open Source Energy Management System</description>
<url>http://openems.io</url>
<groupId>io.openems</groupId>
<artifactId>edge</artifactId>
<version>1.5.0</version>
<packaging>jar</packaging>
<scm>
<url>https://github.com/OpenEMS/openems</url>
<connection>scm:git:git://github.com/OpenEMS/openems.git</connection>
</scm>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<gson.version>2.8.2</gson.version>
<guava.version>23.1-jre</guava.version>
<influxdb.version>2.7</influxdb.version>
<junit.version>4.8.1</junit.version>
<logback.version>1.2.3</logback.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven-assembly-plugin.version>3.1.0</maven-assembly-plugin.version>
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
<restlet.version>2.3.10</restlet.version>
<odoo-java-api-version>1.1.0.RELEASE</odoo-java-api-version>
<slf4j.version>1.7.25</slf4j.version>
<websocket.version>1.3.4</websocket.version>
</properties>
<repositories>
<repository>
<id>maven-restlet</id>
Expand All @@ -17,6 +35,11 @@
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>io.openems</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down Expand Up @@ -50,7 +73,7 @@
<dependency>
<groupId>com.abercap</groupId>
<artifactId>odoo-java-api</artifactId>
<version>1.1.0.RELEASE</version>
<version>${odoo-java-api-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand All @@ -67,7 +90,7 @@
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<version>${maven-jar-plugin.version}</version>
<executions>
<execution>
<id>default-jar</id>
Expand All @@ -77,7 +100,7 @@
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<version>${maven-assembly-plugin.version}</version>
<configuration>
<finalName>openems-backend</finalName>
<appendAssemblyId>false</appendAssemblyId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package io.openems.backend.browserwebsocket;

import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Set;

import org.java_websocket.WebSocket;
import org.java_websocket.framing.CloseFrame;
Expand All @@ -17,10 +16,12 @@
import io.openems.backend.browserwebsocket.session.BrowserSessionData;
import io.openems.backend.browserwebsocket.session.BrowserSessionManager;
import io.openems.backend.metadata.Metadata;
import io.openems.backend.metadata.api.device.MetadataDevice;
import io.openems.backend.openemswebsocket.OpenemsWebsocket;
import io.openems.backend.openemswebsocket.OpenemsWebsocketSingleton;
import io.openems.backend.timedata.Timedata;
import io.openems.common.exceptions.OpenemsException;
import io.openems.common.session.Role;
import io.openems.common.types.Device;
import io.openems.common.utils.JsonUtils;
import io.openems.common.websocket.AbstractWebsocketServer;
Expand Down Expand Up @@ -136,14 +137,13 @@ protected void _onMessage(WebSocket websocket, JsonObject jMessage, Optional<Jso
*/
if (jMessage.has("historicData")) {
// parse deviceId
Matcher matcher = Pattern.compile("\\d+").matcher(deviceName); // extracts '0' from 'openems0'
matcher.find();
Optional<Integer> deviceIdOpt = Optional.ofNullable(Integer.valueOf(matcher.group()));
Optional<Integer> deviceIdOpt = MetadataDevice.parseNumberFromName(deviceName);
JsonArray jMessageId = jMessageIdOpt.get();
try {
JsonObject jHistoricData = JsonUtils.getAsJsonObject(jMessage, "historicData");
JsonObject jReply = WebSocketUtils.historicData(jMessageId, jHistoricData, deviceIdOpt,
Timedata.instance());
Timedata.instance(), Role.ADMIN);
// TODO read role from device
WebSocketUtils.send(websocket, jReply);
} catch (OpenemsException e) {
// TODO Auto-generated catch block
Expand All @@ -158,7 +158,7 @@ protected void _onMessage(WebSocket websocket, JsonObject jMessage, Optional<Jso
try {
forwardMessageToOpenems(websocket, jMessage, deviceName);
} catch (OpenemsException e) {
WebSocketUtils.sendNotification(websocket, LogBehaviour.WRITE_TO_LOG,
WebSocketUtils.sendNotification(websocket, new JsonArray(), LogBehaviour.WRITE_TO_LOG,
Notification.EDGE_UNABLE_TO_FORWARD, deviceName, e.getMessage());
}
}
Expand All @@ -182,17 +182,30 @@ private void forwardMessageToOpenems(WebSocket websocket, JsonObject jMessage, S
jMessage.remove("device");
}

// get session
Optional<BrowserSession> sessionOpt = this.getSessionFromWebsocket(websocket);
if (!sessionOpt.isPresent()) {
throw new OpenemsException("No BrowserSession available.");
}
BrowserSession session = sessionOpt.get();

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

// add authentication role
Role role = Role.GUEST;
for (Device device : session.getData().getDevices(deviceName)) {
role = device.getRole();
}
jMessage.addProperty("role", role.name().toLowerCase());

// get OpenEMS websocket and forward message
Optional<WebSocket> openemsWebsocketOpt = OpenemsWebsocket.instance().getOpenemsWebsocket(deviceName);
if (openemsWebsocketOpt.isPresent()) {
Expand Down Expand Up @@ -251,7 +264,7 @@ public void openemsConnectionClosed(String name) {
for (Device device : session.getData().getDevices()) {
if (name.equals(device.getName())) {
Optional<WebSocket> websocketOpt = this.getWebsocketFromSession(session);
WebSocketUtils.sendNotification(websocketOpt, LogBehaviour.DO_NOT_WRITE_TO_LOG,
WebSocketUtils.sendNotification(websocketOpt, new JsonArray(), LogBehaviour.DO_NOT_WRITE_TO_LOG,
Notification.EDGE_CONNECTION_ClOSED, name);
}
}
Expand All @@ -263,13 +276,15 @@ public void openemsConnectionClosed(String name) {
*
* @param name
*/
public void openemsConnectionOpened(String name) {
public void openemsConnectionOpened(Set<String> names) {
for (BrowserSession session : this.sessionManager.getSessions()) {
for (Device device : session.getData().getDevices()) {
if (name.equals(device.getName())) {
Optional<WebSocket> websocketOpt = this.getWebsocketFromSession(session);
WebSocketUtils.sendNotification(websocketOpt, LogBehaviour.DO_NOT_WRITE_TO_LOG,
Notification.EDGE_CONNECTION_OPENED, name);
for (String name : names) {
if (name.equals(device.getName())) {
Optional<WebSocket> websocketOpt = this.getWebsocketFromSession(session);
WebSocketUtils.sendNotification(websocketOpt, new JsonArray(), LogBehaviour.DO_NOT_WRITE_TO_LOG,
Notification.EDGE_CONNECTION_OPENED, name);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.openems.backend.browserwebsocket.session;

import java.util.ArrayList;
import java.util.List;
import java.util.Collection;
import java.util.Optional;
import java.util.Set;

import com.google.common.collect.LinkedHashMultimap;
import com.google.gson.JsonObject;

import io.openems.common.session.SessionData;
Expand All @@ -13,7 +14,7 @@ public class BrowserSessionData extends SessionData {
private String userName = "";
private Optional<Integer> userId = Optional.empty();
private Optional<String> odooSessionId = Optional.empty();
private List<Device> devices = new ArrayList<>();
private LinkedHashMultimap<String, Device> devices = LinkedHashMultimap.create();

public Optional<String> getOdooSessionId() {
return odooSessionId;
Expand All @@ -23,8 +24,8 @@ public void setOdooSessionId(String odooSessionId) {
this.odooSessionId = Optional.ofNullable(odooSessionId);
}

public void setDevices(List<Device> deviceInfos) {
this.devices = deviceInfos;
public void setDevices(LinkedHashMultimap<String, Device> deviceMap) {
this.devices = deviceMap;
}

public void setUserId(Integer userId) {
Expand All @@ -43,8 +44,12 @@ public String getUserName() {
return userName;
}

public List<Device> getDevices() {
return devices;
public Set<Device> getDevices(String name) {
return this.devices.get(name);
}

public Collection<Device> getDevices() {
return this.devices.values();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
package io.openems.backend.metadata.api.device;

import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.gson.JsonObject;

import io.openems.common.exceptions.OpenemsException;

public interface MetadataDevice {

final static Pattern NAME_NUMBER_PATTERN = Pattern.compile("[^0-9]+([0-9]+)$");

Integer getId();

Optional<Integer> getNameNumber();
public static Optional<Integer> parseNumberFromName(String name) {
Matcher matcher = NAME_NUMBER_PATTERN.matcher(name);
if (matcher.find()) {
String nameNumberString = matcher.group(1);
return Optional.ofNullable(Integer.parseInt(nameNumberString));
}
return Optional.empty();
}

public default Optional<Integer> getNameNumber() {
return MetadataDevice.parseNumberFromName(this.getName());
}

String getName();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package io.openems.backend.metadata.api.device;

import java.util.Optional;

import io.openems.common.exceptions.OpenemsException;

public interface MetadataDeviceModel {
/**
* Gets the device for this apikey.
*
* Note: if there is more than one matching device it returns the first match.
* Gets the devices for this apikey.
*
* @param apikey
* @return device or null
* @throws OpenemsException
*/
public Optional<MetadataDevice> getDeviceForApikey(String apikey) throws OpenemsException;
public MetadataDevices getDevicesForApikey(String apikey) throws OpenemsException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.openems.backend.metadata.api.device;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.google.gson.JsonArray;

public class MetadataDevices implements Iterable<MetadataDevice> {
private List<MetadataDevice> devices = new ArrayList<>();

public Set<String> getNames() {
Set<String> names = new HashSet<>();
for (MetadataDevice device : this.devices) {
names.add(device.getName());
}
return names;
}

public String getNamesString() {
return String.join(",", this.getNames());
}

public boolean isEmpty() {
return this.devices.isEmpty();
}

public void add(MetadataDevice device) {
this.devices.add(device);
}

@Override
public Iterator<MetadataDevice> iterator() {
return this.devices.iterator();
}

public JsonArray toJson() {
JsonArray j = new JsonArray();
for (MetadataDevice device : this.devices) {
j.add(device.toJsonObject());
}
return j;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.openems.backend.metadata.dummy;

import java.util.ArrayList;
import java.util.List;
import com.google.common.collect.LinkedHashMultimap;

import io.openems.backend.browserwebsocket.session.BrowserSession;
import io.openems.backend.browserwebsocket.session.BrowserSessionData;
Expand Down Expand Up @@ -32,12 +31,12 @@ public void getInfoWithSession(BrowserSession session) throws OpenemsException {
SessionData sessionData = session.getData();
BrowserSessionData data = (BrowserSessionData) sessionData;
data.setUserId(0);
// Allow access to all available devices
List<Device> deviceInfos = new ArrayList<>();
// Devices can have the same name, that's why we use a Multimap.
LinkedHashMultimap<String, Device> deviceMap = LinkedHashMultimap.create();
for (Device device : this.deviceModel.getAllDevices()) {
deviceInfos.add(device);
deviceMap.put(device.getName(), device);
}
data.setDevices(deviceInfos);
data.setDevices(deviceMap);
return;
}

Expand Down
Loading

0 comments on commit 2e497da

Please sign in to comment.