1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/web/ synced 2025-03-09 00:00:01 +01:00

Add websocket data receiving

Very buggy early tests
This commit is contained in:
Markus Grigull 2016-07-21 08:57:04 +02:00
parent b2eac53675
commit 4a9563f603
13 changed files with 250 additions and 5 deletions

View file

@ -0,0 +1,108 @@
/**
* File: simulation-data.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';
import DS from 'ember-data';
import ENV from '../config/environment';
export default DS.Adapter.extend({
host: 'ws://' + ENV.APP.LIVE_HOST,
namespace: '',
socket: null,
findRecord(store, type, id, snapshot) {
this._init();
return new Ember.RSVP.Promise(function(resolve, reject) {
//reject(new Error('no record'));
reject();
});
},
_init() {
if (this.socket === null) {
// create new websocket
this.socket = new WebSocket(this.host + this.namespace);
this.socket.binaryType = 'arraybuffer';
// register event callbacks
var self = this;
this.socket.onopen = function(event) {
self.open.apply(self, [event]);
};
this.socket.onmessage = function(event) {
self.message.apply(self, [event]);
};
this.socket.onerror = function(event) {
self.error.apply(self, [event]);
};
this.socket.onclose = function(event) {
self.close.apply(self, [event]);
};
}
},
open(event) {
Ember.debug('websocket opened');
},
close(event) {
Ember.debug('websocket closed: ' + event.code);
},
message(event) {
// read the message into JSON
var message = this._messageToJSON(event.data);
var id = 0;
var simulationData = this.store.peekRecord('simulation-data', id);
if (simulationData != null) {
simulationData.set('sequence', message.sequence);
simulationData.set('values', message.values);
} else {
this.store.createRecord('simulation-data', {
sequence: message.sequence,
values: message.values,
id: id
});
}
},
error(err) {
Ember.debug('websocket error');
},
_messageToJSON(blob) {
var data = new DataView(blob);
let OFFSET_ENDIAN = 1;
let OFFSET_TYPE = 2;
let OFFSET_VERSION = 4;
var bits = data.getUint8(0);
var endian = (bits >> OFFSET_ENDIAN) & 0x1 ? 0 : 1;
var length = data.getUint16(0x02, endian);
var values = new Float32Array(data.buffer, data.byteOffset + 0x10, length);
return {
endian: endian,
version: (bits >> OFFSET_VERSION) & 0xF,
type: (bits >> OFFSET_TYPE) & 0x3,
length: length,
sequence: data.getUint32(0x04, endian),
timestamp: data.getUint32(0x08, endian) * 1e3 + data.getUint32(0x0C, endian) * 1e-6,
values: values
};
}
});

View file

@ -0,0 +1,18 @@
/**
* File: simulation-data.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 Model from 'ember-data/model';
import attr from 'ember-data/attr';
// import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
simulator: attr('number'),
sequence: attr('number'),
values: attr('array')
});

View file

@ -1,4 +1,7 @@
import Ember from 'ember';
export default Ember.Route.extend({
/*model() {
return this.store.findRecord('simulation-data', 0);
}*/
});

View file

@ -1,4 +1,17 @@
import Ember from 'ember';
/**
* 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.
**********************************************************************************/
export default Ember.Route.extend({
import Ember from 'ember';
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';
export default Ember.Route.extend(AuthenticatedRouteMixin, {
model(params) {
return this.store.findRecord('simulation-model', params.modelid);
}
});

View file

@ -0,0 +1,23 @@
/**
* File: simulation-data.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';
import DS from 'ember-data';
export default DS.Serializer.extend({
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
console.log('normalizeResponse');
return {};
},
serialize(record, options) {
console.log('serialize');
return null;
}
});

View file

@ -1 +1 @@
{{outlet}}

View file

@ -1 +1,9 @@
{{outlet}}
<h1>{{model.name}}</h1>
<p>
Running: {{#if model.running}}
true
{{else}}
false
{{/if}}
</p>

20
app/transforms/array.js Normal file
View file

@ -0,0 +1,20 @@
import Ember from 'ember';
import Transform from 'ember-data/transform';
export default Transform.extend({
deserialize(serialized) {
if (Ember.isArray(serialized)) {
return serialized;
} else {
return [];
}
},
serialize(deserialized) {
if (Ember.isArray(deserialized)) {
return deserialized;
} else {
return [];
}
}
});

View file

@ -14,7 +14,8 @@ module.exports = function(environment) {
},
APP: {
API_HOST: 'localhost:3000'
API_HOST: 'localhost:3000',
LIVE_HOST: 'localhost:4000'
}
};

View file

@ -0,0 +1,12 @@
import { moduleFor, test } from 'ember-qunit';
moduleFor('adapter:simulation-data', 'Unit | Adapter | simulation data', {
// Specify the other units that are required for this test.
// needs: ['serializer:foo']
});
// Replace this with your real tests.
test('it exists', function(assert) {
let adapter = this.subject();
assert.ok(adapter);
});

View file

@ -0,0 +1,12 @@
import { moduleForModel, test } from 'ember-qunit';
moduleForModel('simulation-data', 'Unit | Model | simulation data', {
// Specify the other units that are required for this test.
needs: []
});
test('it exists', function(assert) {
let model = this.subject();
// let store = this.store();
assert.ok(!!model);
});

View file

@ -0,0 +1,15 @@
import { moduleForModel, test } from 'ember-qunit';
moduleForModel('simulation-data', 'Unit | Serializer | simulation data', {
// Specify the other units that are required for this test.
needs: ['serializer:simulation-data']
});
// Replace this with your real tests.
test('it serializes records', function(assert) {
let record = this.subject();
let serializedRecord = record.serialize();
assert.ok(serializedRecord);
});

View file

@ -0,0 +1,12 @@
import { moduleFor, test } from 'ember-qunit';
moduleFor('transform:array', 'Unit | Transform | array', {
// Specify the other units that are required for this test.
// needs: ['serializer:foo']
});
// Replace this with your real tests.
test('it exists', function(assert) {
let transform = this.subject();
assert.ok(transform);
});