mirror of
https://git.rwth-aachen.de/acs/public/villas/web/
synced 2025-03-16 00:00:03 +01:00
Improve simulator detection
Simulator status is now also requested if simulator is online but no websocket connection was made
This commit is contained in:
parent
febb2cbb0a
commit
466bdf24cb
2 changed files with 80 additions and 74 deletions
src
|
@ -11,6 +11,40 @@ import RestDataManager from './rest-data-manager';
|
|||
import RestAPI from '../api/rest-api';
|
||||
import AppDispatcher from '../app-dispatcher';
|
||||
|
||||
function isRunning(simulator) {
|
||||
// get path to nodes.json and simulator name
|
||||
var path = simulator.endpoint.substring(0, simulator.endpoint.lastIndexOf('/'));
|
||||
path += '/nodes.json';
|
||||
|
||||
var name = simulator.endpoint.substring(simulator.endpoint.lastIndexOf('/') + 1);
|
||||
|
||||
// send request
|
||||
RestAPI.get('http://' + path).then(response => {
|
||||
// check if simulator is running
|
||||
simulator.running = false;
|
||||
|
||||
response.forEach(sim => {
|
||||
if (sim.name === name) {
|
||||
simulator.running = true;
|
||||
}
|
||||
});
|
||||
|
||||
AppDispatcher.dispatch({
|
||||
type: 'simulators/running',
|
||||
simulator: simulator,
|
||||
running: simulator.running
|
||||
});
|
||||
}).catch(error => {
|
||||
simulator.running = false;
|
||||
|
||||
AppDispatcher.dispatch({
|
||||
type: 'simulators/running',
|
||||
simulator: simulator,
|
||||
running: simulator.running
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
class SimulatorsDataManager extends RestDataManager {
|
||||
constructor() {
|
||||
super('simulator', '/simulators', [ '_id', 'name', 'endpoint' ]);
|
||||
|
@ -18,76 +52,35 @@ class SimulatorsDataManager extends RestDataManager {
|
|||
this._timers = [];
|
||||
}
|
||||
|
||||
isRunning(simulator) {
|
||||
// get path and name
|
||||
var path = simulator.endpoint.substring(0, simulator.endpoint.lastIndexOf('/'));
|
||||
path += '/nodes.json';
|
||||
|
||||
var name = simulator.endpoint.substring(simulator.endpoint.lastIndexOf('/') + 1);
|
||||
|
||||
// send request
|
||||
RestAPI.get('http://' + path).then(response => {
|
||||
// check if simulator is running
|
||||
simulator.running = false;
|
||||
|
||||
response.forEach(sim => {
|
||||
if (sim.name === name) {
|
||||
// save properties
|
||||
simulator.running = true;
|
||||
//simulator.defaultTypes = sim.units;
|
||||
//simulator.defaultLabels = sim.series;
|
||||
}
|
||||
});
|
||||
|
||||
AppDispatcher.dispatch({
|
||||
type: 'simulators/running',
|
||||
simulator: simulator,
|
||||
running: simulator.running
|
||||
});
|
||||
|
||||
// remove timer if needed
|
||||
if (simulator.running) {
|
||||
const index = this._timers.findIndex(timer => {
|
||||
return timer.simulator === simulator._id;
|
||||
});
|
||||
|
||||
if (index !== -1) {
|
||||
clearInterval(this._timers[index].id);
|
||||
|
||||
console.log('stop interval ' + this._timers[index].id);
|
||||
|
||||
this._timers.splice(index, 1);
|
||||
}
|
||||
}
|
||||
}).catch(error => {
|
||||
//console.log(error);
|
||||
|
||||
simulator.running = false;
|
||||
|
||||
AppDispatcher.dispatch({
|
||||
type: 'simulators/running',
|
||||
simulator: simulator,
|
||||
running: simulator.running
|
||||
});
|
||||
|
||||
// check for existing timer
|
||||
const timer = this._timers.find(element => {
|
||||
return element.simulator === simulator._id;
|
||||
});
|
||||
|
||||
if (timer == null) {
|
||||
// add timer
|
||||
var self = this;
|
||||
|
||||
const timerID = setInterval(function() {
|
||||
self.isRunning(simulator);
|
||||
}, 5000);
|
||||
|
||||
console.log('start interval ' + timerID);
|
||||
|
||||
this._timers.push({ id: timerID, simulator: simulator._id });
|
||||
}
|
||||
startRunningDetection(simulator) {
|
||||
// check if timer is already running
|
||||
const index = this._timers.findIndex(timer => {
|
||||
return timer.simulator === simulator._id;
|
||||
});
|
||||
|
||||
if (index !== -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
// do first request for fast response time
|
||||
isRunning(simulator);
|
||||
|
||||
// start new timer
|
||||
const timerID = setInterval(isRunning, 5000, simulator);
|
||||
this._timers.push({ id: timerID, simulator: simulator._id });
|
||||
}
|
||||
|
||||
stopRunningDetection(simulator) {
|
||||
// remove timer
|
||||
const index = this._timers.findIndex(timer => {
|
||||
return timer.simulator === simulator._id;
|
||||
});
|
||||
|
||||
if (index !== -1) {
|
||||
// stop timer and delete from list
|
||||
clearInterval(this._timers[index].id);
|
||||
this._timers.splice(index, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -16,16 +16,18 @@ class SimulatorStore extends ArrayStore {
|
|||
}
|
||||
|
||||
reduce(state, action) {
|
||||
var simulator;
|
||||
|
||||
switch (action.type) {
|
||||
case 'simulators/loaded':
|
||||
case 'simulators/is-running':
|
||||
//case 'simulators/is-running':
|
||||
// get simulator running state
|
||||
if (Array.isArray(action.data)) {
|
||||
action.data.forEach((simulator) => {
|
||||
SimulatorsDataManager.isRunning(simulator);
|
||||
SimulatorsDataManager.startRunningDetection(simulator);
|
||||
});
|
||||
} else {
|
||||
SimulatorsDataManager.isRunning(action.data);
|
||||
SimulatorsDataManager.startRunningDetection(action.data);
|
||||
}
|
||||
|
||||
return super.reduce(state, action);
|
||||
|
@ -33,9 +35,20 @@ class SimulatorStore extends ArrayStore {
|
|||
case 'simulators/running':
|
||||
return this.updateElements(state, [ action.simulator ]);
|
||||
|
||||
case 'simulatorData/opened':
|
||||
// get simulator
|
||||
simulator = state.find(element => {
|
||||
return element._id === action.identifier;
|
||||
});
|
||||
|
||||
// restart requesting again
|
||||
SimulatorsDataManager.stopRunningDetection(simulator);
|
||||
|
||||
return state;
|
||||
|
||||
case 'simulatorData/closed':
|
||||
// get simulator
|
||||
var simulator = state.find(element => {
|
||||
simulator = state.find(element => {
|
||||
return element._id === action.identifier;
|
||||
});
|
||||
|
||||
|
@ -43,7 +56,7 @@ class SimulatorStore extends ArrayStore {
|
|||
simulator.running = false;
|
||||
|
||||
// restart requesting again
|
||||
SimulatorsDataManager.isRunning(simulator);
|
||||
SimulatorsDataManager.startRunningDetection(simulator);
|
||||
|
||||
return this.updateElements(state, [ simulator ]);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue