mirror of
https://git.rwth-aachen.de/acs/public/villas/web/
synced 2025-03-09 00:00:01 +01:00
Scenario import and export working #216
This commit is contained in:
parent
704c36ab90
commit
01edd65710
9 changed files with 98 additions and 211 deletions
|
@ -99,7 +99,7 @@ class RestDataManager {
|
|||
});
|
||||
|
||||
if (this.onLoad != null) {
|
||||
this.onLoad(data);
|
||||
this.onLoad(data, token);
|
||||
}
|
||||
}).catch(error => {
|
||||
AppDispatcher.dispatch({
|
||||
|
@ -121,7 +121,7 @@ class RestDataManager {
|
|||
});
|
||||
|
||||
if (this.onLoad != null) {
|
||||
this.onLoad(data);
|
||||
this.onLoad(data, token);
|
||||
}
|
||||
}).catch(error => {
|
||||
AppDispatcher.dispatch({
|
||||
|
@ -163,7 +163,6 @@ class RestDataManager {
|
|||
newObj.configID = response[this.type].id
|
||||
}
|
||||
|
||||
console.log("Adding new object of type", type, "with content", newObj, "to object of type ", this.type, "with ID ", response[this.type].id)
|
||||
// iterate over all objects of type 'type' add issue add dispatch
|
||||
AppDispatcher.dispatch({
|
||||
type: type + '/start-add',
|
||||
|
|
|
@ -29,7 +29,6 @@ class ConfigStore extends ArrayStore {
|
|||
|
||||
case 'configs/loaded':
|
||||
|
||||
ConfigsDataManager.loadSignals(action.token, action.data);
|
||||
ConfigsDataManager.loadFiles(action.token, action.data);
|
||||
return super.reduce(state, action);
|
||||
|
||||
|
|
|
@ -26,43 +26,35 @@ class ConfigDataManager extends RestDataManager {
|
|||
this.onLoad = this.onConfigsLoad;
|
||||
}
|
||||
|
||||
onConfigsLoad(data) {
|
||||
onConfigsLoad(data, token) {
|
||||
if (!Array.isArray(data))
|
||||
data = [ data ];
|
||||
|
||||
for (let config of data)
|
||||
this.loadICData(config);
|
||||
}
|
||||
for (let config of data) {
|
||||
|
||||
loadICData(config) {
|
||||
AppDispatcher.dispatch({
|
||||
type: 'icData/prepare',
|
||||
inputLength: parseInt(config.inputLength, 10),
|
||||
outputLength: parseInt(config.outputLength, 10),
|
||||
id: config.icID
|
||||
});
|
||||
}
|
||||
// prepare IC data
|
||||
AppDispatcher.dispatch({
|
||||
type: 'icData/prepare',
|
||||
inputLength: parseInt(config.inputLength, 10),
|
||||
outputLength: parseInt(config.outputLength, 10),
|
||||
id: config.icID
|
||||
});
|
||||
|
||||
loadSignals(token, configs){
|
||||
|
||||
for (let config of configs) {
|
||||
// request in signals
|
||||
RestAPI.get(this.makeURL('/signals?direction=in&configID=' + config.id), token).then(response => {
|
||||
AppDispatcher.dispatch({
|
||||
type: 'signals/loaded',
|
||||
data: response.signals
|
||||
});
|
||||
AppDispatcher.dispatch({
|
||||
type: 'signals/start-load',
|
||||
token: token,
|
||||
param: '?direction=in&configID=' + config.id,
|
||||
});
|
||||
|
||||
// request out signals
|
||||
RestAPI.get(this.makeURL('/signals?direction=out&configID=' + config.id), token).then(response => {
|
||||
AppDispatcher.dispatch({
|
||||
type: 'signals/loaded',
|
||||
data: response.signals
|
||||
});
|
||||
AppDispatcher.dispatch({
|
||||
type: 'signals/start-load',
|
||||
token: token,
|
||||
param: '?direction=out&configID=' + config.id,
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
loadFiles(token, configs){
|
||||
|
|
|
@ -24,6 +24,7 @@ class DashboardStore extends ArrayStore {
|
|||
}
|
||||
|
||||
reduce(state, action) {
|
||||
|
||||
switch (action.type) {
|
||||
case 'dashboards/start-add':
|
||||
|
||||
|
|
|
@ -16,5 +16,30 @@
|
|||
******************************************************************************/
|
||||
|
||||
import RestDataManager from '../common/data-managers/rest-data-manager';
|
||||
import AppDispatcher from "../common/app-dispatcher";
|
||||
|
||||
export default new RestDataManager('dashboard', '/dashboards');
|
||||
class DashboardsDataManager extends RestDataManager{
|
||||
constructor() {
|
||||
super('dashboard', '/dashboards');
|
||||
this.onLoad = this.onDashboardsLoad
|
||||
}
|
||||
|
||||
onDashboardsLoad(data, token){
|
||||
|
||||
if (!Array.isArray(data)) {
|
||||
data = [data];
|
||||
}
|
||||
|
||||
console.log("onDashboardsLoad");
|
||||
for (let dashboard of data){
|
||||
AppDispatcher.dispatch({
|
||||
type: 'widgets/start-load',
|
||||
token: token,
|
||||
param: '?dashboardID=' + dashboard.id
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default new DashboardsDataManager();
|
||||
|
|
|
@ -52,15 +52,6 @@ class ImportScenarioDialog extends React.Component {
|
|||
}
|
||||
|
||||
handleChange(e, index) {
|
||||
/*if (e.target.id === 'icID') {
|
||||
const configs = this.state.configs;
|
||||
configs[index].icID = JSON.parse(e.target.value);
|
||||
|
||||
this.setState({ configs: configs });
|
||||
|
||||
return;
|
||||
}*/
|
||||
|
||||
this.setState({ [e.target.id]: e.target.value });
|
||||
|
||||
// check all controls
|
||||
|
|
|
@ -26,6 +26,7 @@ class ScenarioStore extends ArrayStore{
|
|||
|
||||
reduce(state, action) {
|
||||
switch (action.type) {
|
||||
|
||||
case 'scenarios/start-add':
|
||||
|
||||
// Check if this is a recursive scenario import or not
|
||||
|
|
|
@ -16,41 +16,36 @@
|
|||
******************************************************************************/
|
||||
|
||||
import RestDataManager from '../common/data-managers/rest-data-manager';
|
||||
import RestAPI from "../common/api/rest-api";
|
||||
import AppDispatcher from "../common/app-dispatcher";
|
||||
|
||||
class ScenariosDataManager extends RestDataManager {
|
||||
constructor() {
|
||||
super('scenario', '/scenarios');
|
||||
|
||||
this.onLoad = this.onScenariosLoad
|
||||
}
|
||||
|
||||
getComponentConfigs(token, id) {
|
||||
RestAPI.get(this.makeURL('/scenarios/' + id + '/configs'), token).then(response => {
|
||||
AppDispatcher.dispatch({
|
||||
type: 'scenarios/configs',
|
||||
configs: response.configs
|
||||
});
|
||||
}).catch(error => {
|
||||
AppDispatcher.dispatch({
|
||||
type: 'scenarios/configs-error',
|
||||
error: error
|
||||
});
|
||||
});
|
||||
}
|
||||
onScenariosLoad(data, token){
|
||||
|
||||
getDashboards(token, id) {
|
||||
RestAPI.get(this.makeURL('/scenarios/' + id + '/dashboards'), token).then(response => {
|
||||
AppDispatcher.dispatch({
|
||||
type: 'scenarios/dashboards',
|
||||
dashboards: response.dashboards
|
||||
});
|
||||
}).catch(error => {
|
||||
AppDispatcher.dispatch({
|
||||
type: 'scenarios/dashboards-error',
|
||||
error: error
|
||||
});
|
||||
});
|
||||
}
|
||||
if (!Array.isArray(data)) {
|
||||
data = [data];
|
||||
}
|
||||
|
||||
for (let scenario of data){
|
||||
AppDispatcher.dispatch({
|
||||
type: 'configs/start-load',
|
||||
token: token,
|
||||
param: '?scenarioID=' + scenario.id
|
||||
});
|
||||
|
||||
AppDispatcher.dispatch({
|
||||
type: 'dashboards/start-load',
|
||||
token: token,
|
||||
param: '?scenarioID=' + scenario.id
|
||||
});
|
||||
|
||||
// TODO add dispatch for files
|
||||
}
|
||||
}
|
||||
}
|
||||
export default new ScenariosDataManager();
|
||||
|
|
|
@ -26,6 +26,7 @@ import LoginStore from '../user/login-store';
|
|||
import DashboardStore from '../dashboard/dashboard-store';
|
||||
import WidgetStore from "../widget/widget-store";
|
||||
import ConfigStore from '../componentconfig/config-store';
|
||||
import SignalStore from '../signal/signal-store'
|
||||
|
||||
import Icon from '../common/icon';
|
||||
import Table from '../common/table';
|
||||
|
@ -40,21 +41,16 @@ import DeleteDialog from '../common/dialogs/delete-dialog';
|
|||
class Scenarios extends Component {
|
||||
|
||||
static getStores() {
|
||||
return [ScenarioStore, LoginStore, DashboardStore, WidgetStore, ConfigStore];
|
||||
return [ScenarioStore, LoginStore, DashboardStore, WidgetStore, ConfigStore, SignalStore];
|
||||
}
|
||||
|
||||
static calculateState() {
|
||||
const scenarios = ScenarioStore.getState();
|
||||
const sessionToken = LoginStore.getState().token;
|
||||
|
||||
let dashboards = DashboardStore.getState();
|
||||
let configs = ConfigStore.getState();
|
||||
|
||||
return {
|
||||
scenarios,
|
||||
dashboards,
|
||||
configs,
|
||||
sessionToken,
|
||||
scenarios: ScenarioStore.getState(),
|
||||
dashboards: DashboardStore.getState(),
|
||||
configs: ConfigStore.getState(),
|
||||
sessionToken: LoginStore.getState().token,
|
||||
|
||||
newModal: false,
|
||||
deleteModal: false,
|
||||
|
@ -73,133 +69,20 @@ class Scenarios extends Component {
|
|||
});
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps, prevState) {
|
||||
|
||||
for (let i = prevState.scenarios.length; i < this.state.scenarios.length; i++) {
|
||||
AppDispatcher.dispatch({
|
||||
type: 'dashboards/start-load',
|
||||
token: this.state.sessionToken,
|
||||
param: '?scenarioID=' + this.state.scenarios[i].id
|
||||
});
|
||||
AppDispatcher.dispatch({
|
||||
type: 'configs/start-load',
|
||||
token: this.state.sessionToken,
|
||||
param: '?scenarioID=' + this.state.scenarios[i].id
|
||||
});
|
||||
}
|
||||
|
||||
// when length of scenarios array has increased, either add data (after import)
|
||||
// or load data (after export)
|
||||
/*
|
||||
if (this.state.scenarios.length > prevState.scenarios.length) {
|
||||
if (this.addDashboards || this.addConfigs) {
|
||||
let scenarioID = this.state.scenarios[this.state.scenarios.length - 1].id;
|
||||
|
||||
if (this.addDashboards) {
|
||||
this.dashboardsToAdd.forEach((dashboard) => {
|
||||
if (dashboard.widgets) {
|
||||
this.addWidgets = true;
|
||||
}
|
||||
dashboard.scenarioID = scenarioID;
|
||||
AppDispatcher.dispatch({
|
||||
type: 'dashboards/start-add',
|
||||
token: this.state.sessionToken,
|
||||
data: dashboard
|
||||
});
|
||||
})
|
||||
this.addDashboards = false;
|
||||
}
|
||||
|
||||
if (this.addConfigs) {
|
||||
this.configsToAdd.forEach((config) => {
|
||||
config.scenarioID = scenarioID;
|
||||
AppDispatcher.dispatch({
|
||||
type: 'configs/start-add',
|
||||
token: this.state.sessionToken,
|
||||
data: config
|
||||
})
|
||||
})
|
||||
delete this.configsToAdd;
|
||||
this.addConfigs = false;
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
let scenarios = Object.assign([], this.state.scenarios); // copying neccessary?
|
||||
for (var i = prevState.scenarios.length; i < scenarios.length; i++) {
|
||||
AppDispatcher.dispatch({
|
||||
type: 'dashboards/start-load',
|
||||
token: this.state.sessionToken,
|
||||
param: '?scenarioID=' + scenarios[i].id
|
||||
});
|
||||
AppDispatcher.dispatch({
|
||||
type: 'configs/start-load',
|
||||
token: this.state.sessionToken,
|
||||
param: '?scenarioID=' + scenarios[i].id
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
// when length of dashboards array has increased, either add widgets (after import)
|
||||
// or load widgets (after export)
|
||||
if (this.state.dashboards.length > prevState.dashboards.length) {
|
||||
if (this.addWidgets && !this.addDashboards) { // add widget data
|
||||
let dashboards = Object.assign([], this.state.dashboards);
|
||||
for (var j = prevState.dashboards.length; j < dashboards.length; j++) {
|
||||
let dboard = dashboards[j];
|
||||
let dboardID = dboard.id;
|
||||
let dashboard = this.dashboardsToAdd.shift();
|
||||
if (dashboard.name !== dboard.name) {
|
||||
console.log("Cannot add widgets, dashboard was not added as expected!");
|
||||
this.addWidgets = false;
|
||||
return;
|
||||
}
|
||||
dashboard.widgets.forEach((widget) => {
|
||||
widget.dashboardID = dboardID;
|
||||
AppDispatcher.dispatch({
|
||||
type: 'widgets/start-add',
|
||||
token: this.state.sessionToken,
|
||||
data: widget
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if (this.dashboardsToAdd.length === 0) {
|
||||
delete this.dashboardsToAdd;
|
||||
this.addWidgets = false;
|
||||
}
|
||||
}
|
||||
else { // load widget data
|
||||
let dashboards = Object.assign([], this.state.dashboards);
|
||||
for (var j = prevState.dashboards.length; j < dashboards.length; j++) {
|
||||
AppDispatcher.dispatch({
|
||||
type: 'widgets/start-load',
|
||||
token: this.state.sessionToken,
|
||||
param: '?dashboardID=' + dashboards[j].id
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
closeNewModal(data) {
|
||||
if(data) {
|
||||
AppDispatcher.dispatch({
|
||||
type: 'scenarios/start-add',
|
||||
data: data,
|
||||
token: this.state.sessionToken,
|
||||
});
|
||||
}
|
||||
this.setState({ newModal: false });
|
||||
|
||||
// TODO create dispatch here to add scenario to Backend database!
|
||||
}
|
||||
|
||||
showDeleteModal(id) {
|
||||
// get scenario by id
|
||||
var deleteScenario;
|
||||
let deleteScenario;
|
||||
|
||||
this.state.scenarios.forEach((scenario) => {
|
||||
if (scenario.id === id) {
|
||||
|
@ -263,19 +146,6 @@ class Scenarios extends Component {
|
|||
this.setState({ importModal: false });
|
||||
|
||||
if (data) {
|
||||
//let newScenario = JSON.parse(JSON.stringify(data));
|
||||
// temporarily store dashboard data until scenario is created
|
||||
/*if (data.dashboards) {
|
||||
this.addDashboards = true;
|
||||
this.dashboardsToAdd = data.dashboards;
|
||||
}
|
||||
if (data.configs) {
|
||||
this.addConfigs = true;
|
||||
this.configsToAdd = data.configs;
|
||||
}
|
||||
delete newScenario.dashboards;
|
||||
*/
|
||||
|
||||
AppDispatcher.dispatch({
|
||||
type: 'scenarios/start-add',
|
||||
data: data,
|
||||
|
@ -304,6 +174,20 @@ class Scenarios extends Component {
|
|||
let jsonObj = scenario;
|
||||
|
||||
configs.forEach((config) => {
|
||||
let signals = JSON.parse(JSON.stringify(SignalStore.getState().filter(s => s.configID === parseInt(config.id, 10))));
|
||||
signals.forEach((signal) => {
|
||||
delete signal.configID;
|
||||
delete signal.id;
|
||||
})
|
||||
|
||||
// two separate lists for inputMapping and outputMapping
|
||||
let inputSignals = signals.filter(s => s.direction === 'in');
|
||||
let outputSignals = signals.filter(s => s.direction === 'out');
|
||||
|
||||
// add signal mappings to config
|
||||
config["inputMapping"] = inputSignals;
|
||||
config["outputMapping"] = outputSignals;
|
||||
|
||||
delete config.id;
|
||||
delete config.scenarioID;
|
||||
})
|
||||
|
|
Loading…
Add table
Reference in a new issue