1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/web/ synced 2025-03-16 00:00:03 +01:00
VILLASweb/app/serializers/application.js

190 lines
5.3 KiB
JavaScript
Raw Normal View History

2015-10-08 16:04:06 +02:00
import DS from 'ember-data';
export default DS.RESTSerializer.extend({
normalizeFindAllResponse: function(store, primaryModelClass, payload, id, requestType) {
var json = { data: [] };
2015-10-12 12:25:14 +02:00
var _this = this;
2015-10-08 16:04:06 +02:00
this._normalizePayload(payload, function(item) {
2015-10-09 09:22:35 +02:00
if (item.type === 'entity') {
json.data.push(item);
} else if (item.type === 'property') {
2015-10-12 12:25:14 +02:00
_this._updateProperty(item);
2015-10-09 09:22:35 +02:00
}
2015-10-08 16:04:06 +02:00
return true;
});
return json;
},
normalizeFindRecordResponse: function(store, primaryModelClass, payload, id, requestType) {
var json = { data: {} };
2015-10-12 12:25:14 +02:00
var _this = this;
2015-10-09 09:22:35 +02:00
2015-10-12 12:25:14 +02:00
this._normalizePayload(payload, function(item) {
2015-10-09 09:22:35 +02:00
if (item.type === 'entity') {
json.data = item;
} else if (item.type === 'property') {
2015-10-12 12:25:14 +02:00
_this._updateProperty(item);
2015-10-09 09:22:35 +02:00
}
return true;
2015-10-08 16:04:06 +02:00
});
return json;
},
normalizeQueryResponse: function(store, primaryModelClass, payload, id, requestType) {
var json = { data: [] };
2015-10-12 12:25:14 +02:00
var _this = this;
2015-10-08 16:04:06 +02:00
this._normalizePayload(payload, function(item) {
if (item.type === 'entity') {
json.data.push(item);
2015-10-09 09:22:35 +02:00
} else if (item.type === 'property') {
2015-10-12 12:25:14 +02:00
_this._updateProperty(item);
2015-10-08 16:04:06 +02:00
}
return true;
});
return json;
},
_normalizePayload: function(payload, handleItem) {
var propertyIndex = 0;
// check if payload has context responses
if (payload.contextResponses) {
payload.contextResponses.forEach(function(item) {
// check if item has context element
if (item.contextElement) {
// create new entity object
var entity = {
type: 'entity',
id: item.contextElement.id,
attributes: {
2015-10-12 12:25:14 +02:00
type: item.contextElement.type
2015-10-08 16:04:06 +02:00
},
relationships: {
properties: {
data: []
}
}
}
if (item.contextElement.attributes) {
item.contextElement.attributes.forEach(function(attribute) {
if (attribute.type !== 'category') {
2015-10-14 14:09:43 +02:00
// find metadata
2015-10-12 12:25:14 +02:00
var timestamp = 0;
2015-10-14 14:09:43 +02:00
var source = "";
var minValue;
var maxValue;
2015-10-12 12:25:14 +02:00
attribute.metadatas.forEach(function(metadata) {
if (metadata.name === 'timestamp') {
timestamp = Date.parse(metadata.value);
2015-10-14 14:09:43 +02:00
} else if (metadata.name === 'source') {
source = metadata.value;
} else if (metadata.name === 'min') {
minValue = metadata.value;
} else if (metadata.name === 'max') {
maxValue = metadata.value;
2015-10-12 12:25:14 +02:00
}
});
2015-10-13 14:12:34 +02:00
if (timestamp === 0) {
timestamp = (new Date()).getTime();
}
2015-10-14 14:09:43 +02:00
2015-10-13 14:12:34 +02:00
2015-10-08 16:04:06 +02:00
// create property
var property = {
type: 'property',
id: 'property_' + propertyIndex++,
attributes: {
name: attribute.name,
type: attribute.type,
2015-10-13 14:12:34 +02:00
timestamp: timestamp,
2015-10-12 12:25:14 +02:00
visible: false,
source: source,
minValue: minValue,
maxValue: maxValue,
values: []
2015-10-08 16:04:06 +02:00
},
relationships: {
entity: {
data: { type: 'entity', id: entity.id }
}
}
}
// add values
if (attribute.value) {
2015-10-13 14:12:34 +02:00
if ($.isArray(attribute.value)) {
attribute.value.forEach(function (value) {
2015-10-14 13:05:14 +02:00
// fix for second to millisecond
value[0] = +value[0] * 1000;
property.attributes.values.push(value);
2015-10-13 14:12:34 +02:00
});
} else {
property.attributes.values.push([timestamp, attribute.value]);
}
}
2015-10-08 16:04:06 +02:00
entity.relationships.properties.data.push({ type: 'property', id: property.id });
handleItem(property);
} else {
var category = {
type: 'category',
id: 'category_' + propertyIndex++,
attributes: {
name: attribute.name,
},
relationships: {
entity: {
data: { type: 'entity', id: entity.id }
}
}
}
handleItem(category);
}
});
}
// pass entity to caller function
if (handleItem(entity) == false) {
// if false returned the caller needs no more entites
return;
}
}
});
}
2015-10-12 12:25:14 +02:00
},
_updateProperty: function(item) {
// create record if needed, otherwise add to current one
var record = this.store.peekRecord('property', item.id);
if (record) {
if (record.timestamp !== item.attributes.timestamp) {
item.attributes.values.forEach(function (value) {
record.get('values').push(value);
});
2015-10-12 12:25:14 +02:00
record.set('timestamp', item.attributes.timestamp);
}
} else {
// add new item
this.store.push(item);
}
2015-10-08 16:04:06 +02:00
}
});