mirror of
https://git.rwth-aachen.de/acs/public/villas/web/
synced 2025-03-16 00:00:03 +01:00
Add running-simulation service
Simulation-model index now shows correct simulator data if the selected simulation is running. running-simulation service can be used to get the running simulation anywhere in the app.
This commit is contained in:
parent
a4507f095c
commit
b9d40a861e
6 changed files with 155 additions and 15 deletions
app
controllers/simulation-model
mixins
services
templates
tests/unit/services
|
@ -1,4 +1,47 @@
|
|||
/**
|
||||
* File: index.js
|
||||
* Author: Markus Grigull <mgrigull@eonerc.rwth-aachen.de>
|
||||
* Date: 20.07.2016
|
||||
* Copyright: 2016, Institute for Automation of Complex Power Systems, EONERC
|
||||
* This file is part of VILLASweb. All Rights Reserved. Proprietary and confidential.
|
||||
* Unauthorized copying of this file, via any medium is strictly prohibited.
|
||||
**********************************************************************************/
|
||||
|
||||
import Ember from 'ember';
|
||||
|
||||
export default Ember.Controller.extend({
|
||||
values: function() {
|
||||
return this.get('simulationData.values');
|
||||
}.property('simulationData.values.@each'),
|
||||
|
||||
_getData: function() {
|
||||
if (this.get('model.simulation.running') == true) {
|
||||
var simulator = this.get('model.simulator');
|
||||
if (simulator == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
var data = this.store.peekRecord('simulation-data', simulator);
|
||||
this.set('simulationData', data);
|
||||
|
||||
// load model again if simulation data is null
|
||||
// this prevents from simulation data not being loaded when the controller
|
||||
// is loaded before the websocket connects
|
||||
if (data === null) {
|
||||
Ember.run.later(this, function() {
|
||||
// trigger _getData
|
||||
this.notifyPropertyChange('model');
|
||||
}, 1000);
|
||||
}
|
||||
} else {
|
||||
// clear simulation data
|
||||
this.set('simulationData', null);
|
||||
|
||||
// check again if simulation is running now
|
||||
Ember.run.later(this, function() {
|
||||
// trigger _getData
|
||||
this.notifyPropertyChange('model');
|
||||
}, 1000);
|
||||
}
|
||||
}.observes('model').on('init')
|
||||
});
|
||||
|
|
|
@ -1,25 +1,56 @@
|
|||
/**
|
||||
* File: websocket-live-stream-mixin.js
|
||||
* Author: Markus Grigull <mgrigull@eonerc.rwth-aachen.de>
|
||||
* Date: 21.07.2016
|
||||
* Copyright: 2016, Institute for Automation of Complex Power Systems, EONERC
|
||||
* This file is part of VILLASweb. All Rights Reserved. Proprietary and confidential.
|
||||
* Unauthorized copying of this file, via any medium is strictly prohibited.
|
||||
**********************************************************************************/
|
||||
|
||||
import Ember from 'ember';
|
||||
import ENV from '../config/environment';
|
||||
|
||||
const { service } = Ember.inject;
|
||||
|
||||
export default Ember.Mixin.create({
|
||||
host: 'ws://' + ENV.APP.LIVE_HOST,
|
||||
namespace: '',
|
||||
runningSimulation: service('running-simulation'),
|
||||
|
||||
socket: null,
|
||||
|
||||
init() {
|
||||
this._super(...arguments);
|
||||
|
||||
// create socket
|
||||
var socket = new WebSocket(this.host + this.namespace);
|
||||
socket.binaryType = 'arraybuffer';
|
||||
|
||||
// register event callbacks
|
||||
var self = this;
|
||||
socket.onopen = function(event) { self.onopen.apply(self, [event]); };
|
||||
socket.onclose = function(event) { self.onclose.apply(self, [event]); };
|
||||
socket.onmessage = function(event) { self.onmessage.apply(self, [event]); };
|
||||
socket.onerror = function(event) { self.onerror.apply(self, [event]); };
|
||||
// start simulation service
|
||||
this.get('runningSimulation').loadRunningSimulation();
|
||||
},
|
||||
|
||||
_runningSimulationChanged: function() {
|
||||
// called each time running simulation did change
|
||||
var simulation = this.get('runningSimulation.simulation');
|
||||
if (simulation !== null) {
|
||||
if (this.socket === null) {
|
||||
// create new socket connection
|
||||
this.socket = new WebSocket(this.host + this.namespace);
|
||||
this.socket.binaryType = 'arraybuffer';
|
||||
|
||||
// register callbacks
|
||||
var self = this;
|
||||
this.socket.onopen = function(event) { self.onopen.apply(self, [event]); };
|
||||
this.socket.onclose = function(event) { self.onclose.apply(self, [event]); };
|
||||
this.socket.onmessage = function(event) { self.onmessage.apply(self, [event]); };
|
||||
this.socket.onerror = function(event) { self.onerror.apply(self, [event]); };
|
||||
}
|
||||
} else {
|
||||
// stop stream if still opened
|
||||
if (this.socket !== null) {
|
||||
this.socket.close();
|
||||
this.socket = null;
|
||||
}
|
||||
}
|
||||
}.observes('runningSimulation.simulation'),
|
||||
|
||||
onopen(event) {
|
||||
Ember.debug('websocket opened');
|
||||
},
|
||||
|
|
41
app/services/running-simulation.js
Normal file
41
app/services/running-simulation.js
Normal file
|
@ -0,0 +1,41 @@
|
|||
/**
|
||||
* File: running-simulation.js
|
||||
* Author: Markus Grigull <mgrigull@eonerc.rwth-aachen.de>
|
||||
* Date: 26.07.2016
|
||||
* Copyright: 2016, Institute for Automation of Complex Power Systems, EONERC
|
||||
* This file is part of VILLASweb. All Rights Reserved. Proprietary and confidential.
|
||||
* Unauthorized copying of this file, via any medium is strictly prohibited.
|
||||
**********************************************************************************/
|
||||
|
||||
import Ember from 'ember';
|
||||
|
||||
const {
|
||||
inject: { service }
|
||||
} = Ember;
|
||||
|
||||
export default Ember.Service.extend({
|
||||
session: service('session'),
|
||||
store: service(),
|
||||
|
||||
loadRunningSimulation: function() {
|
||||
var self = this;
|
||||
|
||||
// check every second for running simulation
|
||||
setInterval(function() {
|
||||
// check if running simulation did changed
|
||||
self.get('store').findAll('simulation').then(function(simulations) {
|
||||
var newSimulation = null;
|
||||
|
||||
simulations.forEach(function(simulation) {
|
||||
if (simulation.get('running') == true) {
|
||||
newSimulation = simulation;
|
||||
}
|
||||
});
|
||||
|
||||
if (newSimulation != self.get('simulation')) {
|
||||
self.set('simulation', newSimulation);
|
||||
}
|
||||
});
|
||||
}, 1000);
|
||||
}
|
||||
});
|
|
@ -1,9 +1,18 @@
|
|||
<h1>{{model.name}}</h1>
|
||||
|
||||
<p>
|
||||
Running: {{#if model.running}}
|
||||
true
|
||||
{{else}}
|
||||
false
|
||||
{{/if}}
|
||||
Running: {{model.simulation.running}}
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Simulator: {{model.simulator}}
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Data:
|
||||
</p>
|
||||
|
||||
{{#each values as |value|}}
|
||||
{{value}}
|
||||
<br />
|
||||
{{/each}}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
<table class="table-full-width">
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th width="80px" class="column-center">Running</th>
|
||||
<th width="100px"></th>
|
||||
</tr>
|
||||
{{#each model as |simulation|}}
|
||||
|
@ -11,6 +12,9 @@
|
|||
<td>
|
||||
{{#link-to "simulation.index" simulation.id}}{{simulation.name}}{{/link-to}}
|
||||
</td>
|
||||
<td class="column-center">
|
||||
{{simulation.running}}
|
||||
</td>
|
||||
<td>
|
||||
<div class="simulations-row-controls">
|
||||
{{#link-to "simulation.edit" simulation.id}}Edit{{/link-to}}
|
||||
|
|
12
tests/unit/services/running-simulation-test.js
Normal file
12
tests/unit/services/running-simulation-test.js
Normal file
|
@ -0,0 +1,12 @@
|
|||
import { moduleFor, test } from 'ember-qunit';
|
||||
|
||||
moduleFor('service:running-simulation', 'Unit | Service | running simulation', {
|
||||
// Specify the other units that are required for this test.
|
||||
// needs: ['service:foo']
|
||||
});
|
||||
|
||||
// Replace this with your real tests.
|
||||
test('it exists', function(assert) {
|
||||
let service = this.subject();
|
||||
assert.ok(service);
|
||||
});
|
Loading…
Add table
Reference in a new issue