mirror of
https://git.rwth-aachen.de/acs/public/villas/web/
synced 2025-03-09 00:00:01 +01:00
Add key filter to rest-data-managers
Fix deleting simulator status timers
This commit is contained in:
parent
69f8648303
commit
febb2cbb0a
2 changed files with 35 additions and 6 deletions
|
@ -13,22 +13,41 @@ import AppDispatcher from '../app-dispatcher';
|
|||
const API_URL = 'http://localhost:4000/api/v1';
|
||||
|
||||
class RestDataManager {
|
||||
constructor(type, url) {
|
||||
constructor(type, url, keyFilter) {
|
||||
this.url = url;
|
||||
this.type = type;
|
||||
this.keyFilter = keyFilter;
|
||||
}
|
||||
|
||||
makeURL(part) {
|
||||
return API_URL + part;
|
||||
}
|
||||
|
||||
filterKeys(object) {
|
||||
// don't change anything if no filter is set
|
||||
if (this.keyFilter == null || Array.isArray(this.keyFilter) === false) {
|
||||
return object;
|
||||
}
|
||||
|
||||
// remove all keys not in the filter
|
||||
Object.keys(object).filter(key => {
|
||||
return this.keyFilter.indexOf(key) === -1;
|
||||
}).forEach(key => {
|
||||
delete object[key];
|
||||
});
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
load(id) {
|
||||
if (id != null) {
|
||||
// load single object
|
||||
RestAPI.get(this.makeURL(this.url + '/' + id)).then(response => {
|
||||
const data = this.filterKeys(response[this.type]);
|
||||
|
||||
AppDispatcher.dispatch({
|
||||
type: this.type + 's/loaded',
|
||||
data: response[this.type]
|
||||
data: data
|
||||
});
|
||||
}).catch(error => {
|
||||
AppDispatcher.dispatch({
|
||||
|
@ -39,9 +58,13 @@ class RestDataManager {
|
|||
} else {
|
||||
// load all objects
|
||||
RestAPI.get(this.makeURL(this.url)).then(response => {
|
||||
const data = response[this.type + 's'].map(element => {
|
||||
return this.filterKeys(element);
|
||||
});
|
||||
|
||||
AppDispatcher.dispatch({
|
||||
type: this.type + 's/loaded',
|
||||
data: response[this.type + 's']
|
||||
data: data
|
||||
});
|
||||
}).catch(error => {
|
||||
AppDispatcher.dispatch({
|
||||
|
@ -54,7 +77,7 @@ class RestDataManager {
|
|||
|
||||
add(object) {
|
||||
var obj = {};
|
||||
obj[this.type] = object;
|
||||
obj[this.type] = this.filterKeys(object);
|
||||
|
||||
RestAPI.post(this.makeURL(this.url), obj).then(response => {
|
||||
AppDispatcher.dispatch({
|
||||
|
@ -86,7 +109,7 @@ class RestDataManager {
|
|||
|
||||
update(object) {
|
||||
var obj = {};
|
||||
obj[this.type] = object;
|
||||
obj[this.type] = this.filterKeys(object);
|
||||
|
||||
RestAPI.put(this.makeURL(this.url + '/' + object._id), obj).then(response => {
|
||||
AppDispatcher.dispatch({
|
||||
|
|
|
@ -13,7 +13,7 @@ import AppDispatcher from '../app-dispatcher';
|
|||
|
||||
class SimulatorsDataManager extends RestDataManager {
|
||||
constructor() {
|
||||
super('simulator', '/simulators');
|
||||
super('simulator', '/simulators', [ '_id', 'name', 'endpoint' ]);
|
||||
|
||||
this._timers = [];
|
||||
}
|
||||
|
@ -52,6 +52,10 @@ class SimulatorsDataManager extends RestDataManager {
|
|||
});
|
||||
|
||||
if (index !== -1) {
|
||||
clearInterval(this._timers[index].id);
|
||||
|
||||
console.log('stop interval ' + this._timers[index].id);
|
||||
|
||||
this._timers.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
@ -79,6 +83,8 @@ class SimulatorsDataManager extends RestDataManager {
|
|||
self.isRunning(simulator);
|
||||
}, 5000);
|
||||
|
||||
console.log('start interval ' + timerID);
|
||||
|
||||
this._timers.push({ id: timerID, simulator: simulator._id });
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue