Skip to content

Commit

Permalink
Prepare release
Browse files Browse the repository at this point in the history
- Set default scheduler cycleTime to 1000ms
- Set UI currentData update interval to 2000ms
- Update OpenEMS-Backend path
- Fix matching of deviceName behind OpenEMS Backend
  • Loading branch information
sfeilmeier committed Sep 7, 2017
1 parent 291f5d5 commit 38c5965
Show file tree
Hide file tree
Showing 20 changed files with 68 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Optional;

import org.java_websocket.WebSocket;
import org.java_websocket.framing.CloseFrame;
import org.java_websocket.handshake.ClientHandshake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -95,10 +96,9 @@ public void onOpen(WebSocket websocket, ClientHandshake handshake) {
// send connection failed to OpenEMS
JsonObject jReply = DefaultMessages.openemsConnectionFailedReply(e.getMessage());
WebSocketUtils.send(websocket, jReply);
log.info("OpenEMS connection failed. Device [" + deviceName + "] Apikey [" + apikey + "]");

// close websocket
websocket.close();
websocket.closeConnection(CloseFrame.REFUSE,
"OpenEMS connection failed. Device [" + deviceName + "] Apikey [" + apikey + "]");
}
}

Expand All @@ -120,14 +120,14 @@ protected void onMessage(WebSocket websocket, JsonObject jMessage, Optional<Json
Optional<String> deviceNameOpt) {
MetadataDevice device = websockets.get(websocket).getData().getDevice();

// TODO Remove after Debugging
if (!jMessage.has("timedata") && !jMessage.has("currentData") && !jMessage.has("log")) {
log.info("Received from " + device.getName() + ": " + jMessage.toString());
}
// if (!jMessage.has("timedata") && !jMessage.has("currentData") && !jMessage.has("log")
// && !jMessage.has("config")) {
// log.info("Received from " + device.getName() + ": " + jMessage.toString());
// }

// Is this a reply?
if (jMessage.has("id")) {
forwardReplyToBrowser(websocket, jMessage);
forwardReplyToBrowser(websocket, device.getName(), jMessage);
}

/*
Expand All @@ -145,7 +145,7 @@ protected void onMessage(WebSocket websocket, JsonObject jMessage, Optional<Json
}
}

private void forwardReplyToBrowser(WebSocket openemsWebsocket, JsonObject jMessage) {
private void forwardReplyToBrowser(WebSocket openemsWebsocket, String deviceName, JsonObject jMessage) {
try {
// get browser websocket
JsonArray jId = JsonUtils.getAsJsonArray(jMessage, "id");
Expand All @@ -168,6 +168,8 @@ private void forwardReplyToBrowser(WebSocket openemsWebsocket, JsonObject jMessa
// remove token from message id
jId.remove(jId.size() - 1);
jMessage.add("id", jId);
// always add device name
jMessage.addProperty("device", deviceName);

// send
WebSocketUtils.send(browserWebsocket, jMessage);
Expand All @@ -190,6 +192,7 @@ private void timedata(MetadataDevice device, JsonElement jTimedataElement) {
// Write some data to Odoo
// This is only to provide feedback for FENECON Service-Team that the device is online.
device.setLastUpdate();
device.setLastMessage();
jTimedata.entrySet().forEach(entry -> {
try {
JsonObject jChannels = JsonUtils.getAsJsonObject(entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static JsonObject historicData(JsonArray jMessageId, JsonObject jHistoric
return DefaultMessages.historicDataQueryReply(jMessageId, jData);
}
} catch (Exception e) {
log.error("Error", e);
log.error("HistoricData Error: ", e);
e.printStackTrace();
}
return new JsonObject();
Expand Down
1 change: 1 addition & 0 deletions doc/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ For 'unsubscribe' the channels object is empty.

```
{
device?: string,
currentData: {[{
channel: string,
value: any
Expand Down
6 changes: 4 additions & 2 deletions edge/src/io/openems/api/scheduler/Scheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import io.openems.core.utilities.AbstractWorker;

public abstract class Scheduler extends AbstractWorker implements Thing {
private final static int DEFAULT_CYCLETIME = 1000;
public final static String THINGID_PREFIX = "_scheduler";
private static int instanceCounter = 0;
protected final Map<String, Controller> controllers = new ConcurrentHashMap<>();
Expand All @@ -49,11 +50,12 @@ public abstract class Scheduler extends AbstractWorker implements Thing {
* Config
*/
@ConfigInfo(title = "Sets the duration of each cycle in milliseconds", type = Integer.class, isOptional = true)
public ConfigChannel<Integer> cycleTime = new ConfigChannel<Integer>("cycleTime", this);
public ConfigChannel<Integer> cycleTime = new ConfigChannel<Integer>("cycleTime", this)
.defaultValue(DEFAULT_CYCLETIME);

@Override
protected int getCycleTime() {
int time = cycleTime.valueOptional().orElse(500);
int time = cycleTime.valueOptional().orElse(DEFAULT_CYCLETIME);
if (actualCycleTime != null) {
time = actualCycleTime;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import io.openems.common.exceptions.NotImplementedException;
import io.openems.common.utils.JsonUtils;
import io.openems.api.exception.NotImplementedException;
import io.openems.common.websocket.DefaultMessages;
import io.openems.common.websocket.WebSocketUtils;
import io.openems.core.Databus;
import io.openems.core.utilities.JsonUtils;

public class CurrentDataWorker {

private final static int UPDATE_INTERVAL_IN_SECONDS = 1;
private final static int UPDATE_INTERVAL_IN_SECONDS = 2;

private Logger log = LoggerFactory.getLogger(CurrentDataWorker.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class FeneconPersistence extends Persistence implements ChannelChangeList
@ConfigInfo(title = "Apikey", description = "Sets the apikey for FENECON Cloud.", type = String.class)
public final ConfigChannel<String> apikey = new ConfigChannel<String>("apikey", this).doNotPersist();

@ConfigInfo(title = "Uri", description = "Sets the connection Uri to FENECON Cloud.", type = String.class, defaultValue = "\"wss://fenecon.de:443/femsserver\"")
@ConfigInfo(title = "Uri", description = "Sets the connection Uri to FENECON Cloud.", type = String.class, defaultValue = "\"wss://fenecon.de:443/openems-backend\"")
public final ConfigChannel<String> uri = new ConfigChannel<String>("uri", this).doNotPersist();

@ConfigInfo(title = "Sets the duration of each cycle in milliseconds", type = Integer.class)
Expand Down
2 changes: 1 addition & 1 deletion ui/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<md-list-item>
<button routerLinkActive="active" routerLink="/overview" md-button><span translate>Menu.Overview</span></button>
</md-list-item>
<md-list-item *ngIf="env.backend == 'OpenEMS_Edge'">
<md-list-item *ngIf="env.backend === 'OpenEMS Edge'">
<!--TODO: is this link still necessary?-->
<a href="/grafana/dashboard/file/fems-asymmetric.json" md-button><span translate>General.History</span></a>
</md-list-item>
Expand Down
2 changes: 1 addition & 1 deletion ui/src/app/device/config/overview/overview.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class OverviewComponent implements OnInit {

constructor(
private route: ActivatedRoute,
private websocket: Websocket,
public websocket: Websocket,
private formBuilder: FormBuilder
) { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ export class SocChartComponent implements OnInit, OnChanges {

ngOnChanges() {
this.loading = true;
if (Object.keys(this.channels).length === 0) {
return;
}
// TODO stop previous subscribe; show only results for latest query. Otherwise the chart misbehaves on fast switch of period
this.device.historicDataQuery(this.fromDate, this.toDate, this.channels).then(historicData => {
// prepare datas array and prefill with each device
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ export class EnergymonitorChartComponent implements OnInit, OnDestroy {
public translation: string;
public width: number;
public height: number;
public loading: boolean = true;

private style: string;
private ngUnsubscribe: Subject<void> = new Subject<void>();
private marginLeft: number = 0;
private loading: boolean = true;

ngOnInit() {
// make sure chart is redrawn in the beginning and on window resize
Expand Down
12 changes: 6 additions & 6 deletions ui/src/app/overview/overview.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div fxLayout fxLayoutAlign="center start" fxLayoutWrap="wrap">
<div fxFlex="50%" fxFlex.xs="100%">
<form [formGroup]="form" (ngSubmit)="doLogin(form)">
<form [formGroup]="form" (ngSubmit)="doLogin()">
<md-card class="single">
<md-card-header *ngIf="websocket.status == 'online'">
<md-icon md-card-avatar>face</md-icon>
Expand All @@ -20,7 +20,7 @@
<md-card-subtitle><small>{{ websocket.url }}</small></md-card-subtitle>
</md-card-header>
<!-- Waiting for authentication -->
<ng-container *ngIf="env.backend == 'openems_edge' && websocket.status == 'waiting for authentication'">
<ng-container *ngIf="env.backend === 'OpenEMS Edge' && websocket.status == 'waiting for authentication'">
<md-card-header>
<md-icon md-card-avatar>stop</md-icon>
<md-card-title>Bitte geben Sie Ihre Zugangsdaten ein</md-card-title>
Expand All @@ -32,7 +32,7 @@
<p fxFlex="100%">{{ (websocket.event | async)?.message }}</p>
</md-card-content>
<md-card-actions fxLayout fxLayoutAlign="end start">
<button type="button" md-fab (click)="doLogin(form)" color="primary"><md-icon>check</md-icon></button>
<button type="button" md-fab (click)="doLogin()" color="primary"><md-icon>check</md-icon></button>
<!-- TODO <button type="button" md-fab *ngIf="websocket.status === 'online'" (click)="doLogout(form)" color="accent"><md-icon>exit_to_app</md-icon></button> -->
</md-card-actions>
</ng-container>
Expand All @@ -45,12 +45,12 @@
<md-card-header>
<md-icon *ngIf="device.online" md-card-avatar color="primary">folder</md-icon>
<md-icon *ngIf="!device.online" md-card-avatar>error</md-icon>
<md-card-title *ngIf="env.backend == 'openems_edge'" translate>Overview.ToEnergymonitor</md-card-title>
<md-card-title *ngIf="env.backend == 'openems_backend'">
<md-card-title *ngIf="env.backend === 'OpenEMS Edge'" translate>Overview.ToEnergymonitor</md-card-title>
<md-card-title *ngIf="env.backend === 'OpenEMS Backend'">
<span>{{ device.comment }} <span *ngIf="!device.online"><br/><span translate>Overview.IsOffline</span></span>
</span>
</md-card-title>
<md-card-subtitle *ngIf="env.backend == 'openems_backend'">
<md-card-subtitle *ngIf="env.backend === 'OpenEMS Backend'">
<span>{{ device.name }}: {{ device.producttype }}</span>
</md-card-subtitle>
</md-card-header>
Expand Down
4 changes: 2 additions & 2 deletions ui/src/app/overview/overview.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export class OverviewComponent {
private stopOnDestroy: Subject<void> = new Subject<void>();

constructor(
private websocket: Websocket,
private utils: Utils,
public websocket: Websocket,
public utils: Utils,
private translate: TranslateService,
private formBuilder: FormBuilder,
private router: Router) {
Expand Down
2 changes: 2 additions & 0 deletions ui/src/app/shared/service/defaulttypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as moment from 'moment';

export module DefaultTypes {

export type Backend = "OpenEMS Backend" | "OpenEMS Edge";

export type ConnectionStatus = "online" | "connecting" | "waiting for authentication" | "failed";

export interface ChannelAddresses {
Expand Down
26 changes: 17 additions & 9 deletions ui/src/app/shared/service/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { environment as env } from '../../../environments';
import { Service } from './service';
import { Utils } from './utils';
import { Device } from '../device/device';
import { Backend } from '../type/backend';
import { ROLES } from '../type/role';
import { DefaultTypes } from '../service/defaulttypes';
import { DefaultMessages } from '../service/defaultmessages';
Expand Down Expand Up @@ -129,7 +128,7 @@ export class Websocket {

}).subscribe(message => {
// called on every receive of message from server
console.log(message);
// console.log(message);
/*
* Authenticate
*/
Expand All @@ -145,7 +144,7 @@ export class Websocket {
this.service.setToken(message.authenticate.token);
}

if ("role" in message.authenticate && env.backend == Backend.OpenEMS_Edge) {
if ("role" in message.authenticate && env.backend === "OpenEMS Edge") {
// for OpenEMS Edge we have only one device
let role = ROLES.getRole(message.authenticate.role);
let replyStream: { [messageId: string]: Subject<any> } = {};
Expand All @@ -168,10 +167,10 @@ export class Websocket {
this.status = "failed";
this.service.removeToken();
this.initialize();
if (env.backend == Backend.OpenEMS_Backend) {
if (env.backend === "OpenEMS Backend") {
console.log("would redirect...") // TODO fix redirect
//window.location.href = "/web/login?redirect=/m/overview";
} else if (env.backend == Backend.OpenEMS_Edge) {
} else if (env.backend === "OpenEMS Edge") {
this.router.navigate(['/overview']);
}
}
Expand All @@ -181,12 +180,21 @@ export class Websocket {
* Query reply
*/
if ("id" in message && message.id instanceof Array) {
// Receive a reply with a message id -> forward to devices' replyStream
let id = message.id[0];
for (let deviceName in this.replyStreams) {
if (id in this.replyStreams[deviceName]) {
let deviceName = Websocket.DEFAULT_DEVICENAME;
if ("device" in message) {
// Receive a reply with a message id and a device -> forward to devices' replyStream
deviceName = message.device;
if (deviceName in this.replyStreams && id in this.replyStreams[deviceName]) {
this.replyStreams[deviceName][id].next(message);
break;
}
} else {
// Receive a reply with a message id -> find device and forward to devices' replyStream
for (let deviceName in this.replyStreams) {
if (id in this.replyStreams[deviceName]) {
this.replyStreams[deviceName][id].next(message);
break;
}
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions ui/src/app/shared/type/backend.ts

This file was deleted.

4 changes: 2 additions & 2 deletions ui/src/app/shared/type/environment.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CustomFieldDefinition } from './customfielddefinition';
import { Backend } from './backend';
import { DefaultTypes } from '../service/defaulttypes';

export abstract class Environment {
public readonly abstract production: boolean;
public readonly abstract url: string;
public readonly abstract backend: Backend;
public readonly abstract backend: DefaultTypes.Backend;

public getCustomFields(): CustomFieldDefinition {
return {};
Expand Down
4 changes: 2 additions & 2 deletions ui/src/environments/environment.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Environment } from "../app/shared/type/environment";
import { Backend } from "../app/shared/type/backend";
import { DefaultTypes } from "../app/shared/service/defaulttypes";

class DefaultEnvironment extends Environment {
public readonly production = false;
public readonly url = "ws://" + location.hostname + ":8085";
public readonly backend = Backend.OpenEMS_Edge;
public readonly backend: DefaultTypes.Backend = "OpenEMS Backend";
}

export const environment = new DefaultEnvironment();
6 changes: 3 additions & 3 deletions ui/src/environments/openems-backend-dev.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Environment } from "../app/shared/type/environment";
import { Backend } from "../app/shared/type/backend";
import { DefaultTypes } from '../app/shared/service/defaulttypes';

class OpenemsBackendDevEnvironment extends Environment {
public readonly production = false;
public readonly url = "ws://" + location.hostname + ":8078";
public readonly backend = Backend.OpenEMS_Backend;
public readonly url = "ws://" + location.hostname + ":8076";
public readonly backend: DefaultTypes.Backend = "OpenEMS Backend"
}

export const environment = new OpenemsBackendDevEnvironment();
8 changes: 4 additions & 4 deletions ui/src/environments/openems-backend.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Environment } from "../app/shared/type/environment";
import { Backend } from "../app/shared/type/backend";
import { DefaultTypes } from '../app/shared/service/defaulttypes';

class OpenemsBackendEnvironment extends Environment {
public readonly production = true;
public readonly url = (location.protocol == "https" ? "wss" : "ws") +
"://" + location.hostname + ":" + location.port + "/femsmonitor";
public readonly backend = Backend.OpenEMS_Backend;
public readonly url = (location.protocol == "https:" ? "wss" : "ws") +
"://" + location.hostname + ":" + location.port + "/openems-backend-ui";
public readonly backend: DefaultTypes.Backend = "OpenEMS Backend";
}

export const environment = new OpenemsBackendEnvironment();
4 changes: 2 additions & 2 deletions ui/src/environments/openems-edge.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Environment } from "../app/shared/type/environment";
import { Backend } from "../app/shared/type/backend";
import { DefaultTypes } from '../app/shared/service/defaulttypes';

class OpenemsEnvironment extends Environment {
public readonly production = true;
public readonly url = "ws://" + location.hostname + (location.port ? ":" + location.port : "") + "/websocket";
public readonly backend = Backend.OpenEMS_Edge;
public readonly backend: DefaultTypes.Backend = "OpenEMS Edge";
}

export const environment = new OpenemsEnvironment();

0 comments on commit 38c5965

Please sign in to comment.