mirror of
https://github.com/nasa/openmct.git
synced 2025-01-20 03:36:44 +00:00
Merge pull request #2685 from nasa/conditionals-refactor
Conditionals refactor
This commit is contained in:
commit
cfafecdd64
@ -22,9 +22,9 @@
|
||||
|
||||
import * as EventEmitter from 'eventemitter3';
|
||||
import uuid from 'uuid';
|
||||
import TelemetryCriterion from "@/plugins/condition/criterion/TelemetryCriterion";
|
||||
import { TRIGGER } from "@/plugins/condition/utils/constants";
|
||||
import {computeCondition} from "@/plugins/condition/utils/evaluator";
|
||||
import TelemetryCriterion from "./criterion/TelemetryCriterion";
|
||||
import { TRIGGER } from "./utils/constants";
|
||||
import {computeCondition} from "./utils/evaluator";
|
||||
|
||||
/*
|
||||
* conditionConfiguration = {
|
||||
@ -58,11 +58,11 @@ export default class ConditionClass extends EventEmitter {
|
||||
this.id = this.openmct.objects.makeKeyString(conditionConfiguration.identifier);
|
||||
this.criteria = [];
|
||||
this.criteriaResults = {};
|
||||
this.result = null;
|
||||
if (conditionConfiguration.configuration.criteria) {
|
||||
this.createCriteria(conditionConfiguration.configuration.criteria);
|
||||
}
|
||||
this.trigger = conditionConfiguration.configuration.trigger;
|
||||
this.result = null;
|
||||
this.openmct.objects.get(this.id).then(obj => this.observeForChanges(obj));
|
||||
}
|
||||
|
||||
@ -199,9 +199,7 @@ export default class ConditionClass extends EventEmitter {
|
||||
|
||||
subscribe() {
|
||||
this.criteria.forEach((criterion) => {
|
||||
if (criterion.isValid()) {
|
||||
criterion.subscribe();
|
||||
}
|
||||
criterion.subscribe();
|
||||
})
|
||||
}
|
||||
|
||||
|
250
src/plugins/condition/ConditionManager.js
Normal file
250
src/plugins/condition/ConditionManager.js
Normal file
@ -0,0 +1,250 @@
|
||||
/*****************************************************************************
|
||||
* Open MCT, Copyright (c) 2014-2020, United States Government
|
||||
* as represented by the Administrator of the National Aeronautics and Space
|
||||
* Administration. All rights reserved.
|
||||
*
|
||||
* Open MCT is licensed under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
* Open MCT includes source code licensed under additional open source
|
||||
* licenses. See the Open Source Licenses file (LICENSES.md) included with
|
||||
* this source code distribution or the Licensing information page available
|
||||
* at runtime from the About dialog for additional information.
|
||||
*****************************************************************************/
|
||||
|
||||
import Condition from "./Condition";
|
||||
import uuid from "uuid";
|
||||
import * as EventEmitter from 'eventemitter3';
|
||||
|
||||
export default class ConditionManager extends EventEmitter {
|
||||
constructor(domainObject, openmct) {
|
||||
super();
|
||||
this.domainObject = domainObject;
|
||||
this.openmct = openmct;
|
||||
this.instantiate = this.openmct.$injector.get('instantiate');
|
||||
this.initialize();
|
||||
}
|
||||
|
||||
initialize() {
|
||||
this.conditionResults = {};
|
||||
this.openmct.objects.get(this.domainObject.identifier).then((obj) => {
|
||||
this.observeForChanges(obj);
|
||||
this.conditionCollection = [];
|
||||
if (this.domainObject.configuration.conditionCollection.length) {
|
||||
this.domainObject.configuration.conditionCollection.forEach((conditionConfigurationId, index) => {
|
||||
this.openmct.objects.get(conditionConfigurationId).then((conditionConfiguration) => {
|
||||
this.initCondition(conditionConfiguration, index)
|
||||
});
|
||||
});
|
||||
} else {
|
||||
this.addCondition(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
observeForChanges(domainObject) {
|
||||
//TODO: Observe only the conditionCollection property instead of the whole domainObject
|
||||
this.stopObservingForChanges = this.openmct.objects.observe(domainObject, '*', this.handleConditionCollectionUpdated.bind(this));
|
||||
}
|
||||
|
||||
handleConditionCollectionUpdated(newDomainObject) {
|
||||
let oldConditionIdentifiers = this.domainObject.configuration.conditionCollection.map((conditionConfigurationId) => {
|
||||
return this.openmct.objects.makeKeyString(conditionConfigurationId);
|
||||
});
|
||||
let newConditionIdentifiers = newDomainObject.configuration.conditionCollection.map((conditionConfigurationId) => {
|
||||
return this.openmct.objects.makeKeyString(conditionConfigurationId);
|
||||
});
|
||||
|
||||
this.domainObject = newDomainObject;
|
||||
|
||||
//check for removed conditions
|
||||
oldConditionIdentifiers.forEach((identifier, index) => {
|
||||
if (newConditionIdentifiers.indexOf(identifier) < 0) {
|
||||
this.removeCondition(identifier);
|
||||
}
|
||||
});
|
||||
|
||||
let newConditionCount = this.domainObject.configuration.conditionCollection.length - this.conditionCollection.length;
|
||||
|
||||
for (let i = 0; i < newConditionCount; i++) {
|
||||
let conditionConfigurationId = this.domainObject.configuration.conditionCollection[i];
|
||||
this.openmct.objects.get(conditionConfigurationId).then((conditionConfiguration) => {
|
||||
this.initCondition(conditionConfiguration, i);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
initCondition(conditionConfiguration, index) {
|
||||
let condition = new Condition(conditionConfiguration, this.openmct);
|
||||
condition.on('conditionResultUpdated', this.handleConditionResult.bind(this));
|
||||
if (index !== undefined) {
|
||||
this.conditionCollection.splice(index + 1, 0, condition);
|
||||
} else {
|
||||
this.conditionCollection.unshift(condition);
|
||||
}
|
||||
//There are no criteria for a default condition and hence no subscriptions.
|
||||
//Hence the conditionResult must be manually triggered for it.
|
||||
if (conditionConfiguration.isDefault) {
|
||||
this.handleConditionResult();
|
||||
}
|
||||
}
|
||||
|
||||
createConditionDomainObject(isDefault, conditionConfiguration) {
|
||||
let conditionObj;
|
||||
if (conditionConfiguration) {
|
||||
conditionObj = {
|
||||
...conditionConfiguration,
|
||||
name: `Copy of ${conditionConfiguration.name}`,
|
||||
identifier: {
|
||||
...this.domainObject.identifier,
|
||||
key: uuid()
|
||||
}
|
||||
};
|
||||
} else {
|
||||
conditionObj = {
|
||||
isDefault: isDefault,
|
||||
type: 'condition',
|
||||
identifier: {
|
||||
...this.domainObject.identifier,
|
||||
key: uuid()
|
||||
},
|
||||
configuration: {
|
||||
name: isDefault ? 'Default' : 'Unnamed Condition',
|
||||
output: 'false',
|
||||
trigger: 'all',
|
||||
criteria: isDefault ? [] : [{
|
||||
telemetry: '',
|
||||
operation: '',
|
||||
input: [],
|
||||
metadata: ''
|
||||
}]
|
||||
},
|
||||
summary: ''
|
||||
};
|
||||
}
|
||||
let conditionDomainObjectKeyString = this.openmct.objects.makeKeyString(conditionObj.identifier);
|
||||
let newDomainObject = this.instantiate(conditionObj, conditionDomainObjectKeyString);
|
||||
|
||||
return newDomainObject.useCapability('adapter');
|
||||
}
|
||||
|
||||
addCondition(isDefault, index) {
|
||||
this.createAndSaveConditionDomainObject(!!isDefault, index);
|
||||
}
|
||||
|
||||
cloneCondition(conditionConfigurationId, index) {
|
||||
this.openmct.objects.get(conditionConfigurationId).then((conditionConfiguration) => {
|
||||
this.createAndSaveConditionDomainObject(false, index, conditionConfiguration);
|
||||
});
|
||||
}
|
||||
|
||||
createAndSaveConditionDomainObject(isDefault, index, conditionConfiguration) {
|
||||
let newConditionDomainObject = this.createConditionDomainObject(isDefault, conditionConfiguration);
|
||||
//persist the condition domain object so that we can do an openmct.objects.get on it and only persist the identifier in the conditionCollection of conditionSet
|
||||
this.openmct.objects.mutate(newConditionDomainObject, 'created', new Date());
|
||||
if (index !== undefined) {
|
||||
this.domainObject.configuration.conditionCollection.splice(index + 1, 0, newConditionDomainObject.identifier);
|
||||
} else {
|
||||
this.domainObject.configuration.conditionCollection.unshift(newConditionDomainObject.identifier);
|
||||
}
|
||||
this.persist();
|
||||
}
|
||||
|
||||
removeCondition(identifier) {
|
||||
let found = this.findConditionById(identifier);
|
||||
if (found) {
|
||||
let index = found.index;
|
||||
let condition = this.conditionCollection[index];
|
||||
let conditionIdAsString = condition.id;
|
||||
condition.destroyCriteria();
|
||||
condition.off('conditionResultUpdated', this.handleConditionResult.bind(this));
|
||||
this.conditionCollection.splice(index, 1);
|
||||
this.domainObject.configuration.conditionCollection.splice(index, 1);
|
||||
if (this.conditionResults[conditionIdAsString] !== undefined) {
|
||||
delete this.conditionResults[conditionIdAsString];
|
||||
}
|
||||
this.persist();
|
||||
this.handleConditionResult();
|
||||
}
|
||||
}
|
||||
|
||||
findConditionById(identifier) {
|
||||
let found;
|
||||
for (let i=0, ii=this.conditionCollection.length; i < ii; i++) {
|
||||
if (this.conditionCollection[i].id === this.openmct.objects.makeKeyString(identifier)) {
|
||||
found = {
|
||||
item: this.conditionCollection[i],
|
||||
index: i
|
||||
};
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
//this.$set(this.conditionCollection, reorderEvent.newIndex, oldConditions[reorderEvent.oldIndex]);
|
||||
reorderConditions(reorderPlan) {
|
||||
let oldConditions = Array.from(this.domainObject.configuration.conditionCollection);
|
||||
let newCollection = [];
|
||||
reorderPlan.forEach((reorderEvent) => {
|
||||
let item = oldConditions[reorderEvent.oldIndex];
|
||||
newCollection.push(item);
|
||||
this.domainObject.configuration.conditionCollection = newCollection;
|
||||
});
|
||||
this.persist();
|
||||
}
|
||||
|
||||
handleConditionResult(resultObj) {
|
||||
let conditionCollection = this.domainObject.configuration.conditionCollection;
|
||||
let currentConditionIdentifier = conditionCollection[conditionCollection.length-1];
|
||||
|
||||
if (resultObj) {
|
||||
let idAsString = this.openmct.objects.makeKeyString(resultObj.id);
|
||||
let found = this.findConditionById(idAsString);
|
||||
if (found) {
|
||||
this.conditionResults[idAsString] = resultObj.data.result;
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0, ii = conditionCollection.length - 1; i < ii; i++) {
|
||||
let conditionIdAsString = this.openmct.objects.makeKeyString(conditionCollection[i]);
|
||||
if (this.conditionResults[conditionIdAsString]) {
|
||||
//first condition to be true wins
|
||||
currentConditionIdentifier = conditionCollection[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
this.openmct.objects.get(currentConditionIdentifier).then((obj) => {
|
||||
this.emit('conditionSetResultUpdated', {
|
||||
id: this.domainObject.identifier,
|
||||
output: obj.configuration.output,
|
||||
conditionId: currentConditionIdentifier
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
persist() {
|
||||
this.openmct.objects.mutate(this.domainObject, 'configuration.conditionCollection', this.domainObject.configuration.conditionCollection);
|
||||
}
|
||||
|
||||
destroy() {
|
||||
if (typeof this.stopObservingForChanges === 'function') {
|
||||
this.stopObservingForChanges();
|
||||
}
|
||||
this.conditionCollection.forEach((condition) => {
|
||||
condition.off('conditionResultUpdated', this.handleConditionResult);
|
||||
condition.destroy();
|
||||
})
|
||||
}
|
||||
}
|
98
src/plugins/condition/ConditionManagerSpec.js
Normal file
98
src/plugins/condition/ConditionManagerSpec.js
Normal file
@ -0,0 +1,98 @@
|
||||
/*****************************************************************************
|
||||
* Open MCT, Copyright (c) 2014-2020, United States Government
|
||||
* as represented by the Administrator of the National Aeronautics and Space
|
||||
* Administration. All rights reserved.
|
||||
*
|
||||
* Open MCT is licensed under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
* Open MCT includes source code licensed under additional open source
|
||||
* licenses. See the Open Source Licenses file (LICENSES.md) included with
|
||||
* this source code distribution or the Licensing information page available
|
||||
* at runtime from the About dialog for additional information.
|
||||
*****************************************************************************/
|
||||
|
||||
import ConditionManager from './ConditionManager';
|
||||
|
||||
describe('ConditionManager', () => {
|
||||
|
||||
let conditionMgr;
|
||||
let mockListener;
|
||||
let openmct = {};
|
||||
let conditionSetDomainObject = {
|
||||
identifier: {
|
||||
namespace: "",
|
||||
key: "600a7372-8d48-4dc4-98b6-548611b1ff7e"
|
||||
},
|
||||
type: "conditionSet",
|
||||
location: "mine",
|
||||
configuration: {
|
||||
conditionCollection: []
|
||||
}
|
||||
};
|
||||
let mockConditionDomainObject = {
|
||||
isDefault: true,
|
||||
type: 'condition',
|
||||
identifier: {
|
||||
namespace: '',
|
||||
key: '1234-5678'
|
||||
}
|
||||
};
|
||||
|
||||
function mockAngularComponents() {
|
||||
let mockInjector = jasmine.createSpyObj('$injector', ['get']);
|
||||
|
||||
let mockInstantiate = jasmine.createSpy('mockInstantiate');
|
||||
mockInstantiate.and.returnValue(mockInstantiate);
|
||||
|
||||
let mockDomainObject = {
|
||||
useCapability: function () {
|
||||
return mockConditionDomainObject;
|
||||
}
|
||||
};
|
||||
mockInstantiate.and.callFake(function () {
|
||||
return mockDomainObject;
|
||||
});
|
||||
mockInjector.get.and.callFake(function (service) {
|
||||
return {
|
||||
'instantiate': mockInstantiate
|
||||
}[service];
|
||||
});
|
||||
|
||||
openmct.$injector = mockInjector;
|
||||
}
|
||||
|
||||
beforeAll(function () {
|
||||
|
||||
mockAngularComponents();
|
||||
|
||||
openmct.objects = jasmine.createSpyObj('objects', ['get', 'makeKeyString', 'observe', 'mutate']);
|
||||
openmct.objects.get.and.returnValues(new Promise(function (resolve, reject) {
|
||||
resolve(conditionSetDomainObject);
|
||||
}), new Promise(function (resolve, reject) {
|
||||
resolve(mockConditionDomainObject);
|
||||
}));
|
||||
openmct.objects.makeKeyString.and.returnValue(conditionSetDomainObject.identifier.key);
|
||||
openmct.objects.observe.and.returnValue(function () {});
|
||||
openmct.objects.mutate.and.returnValue(function () {});
|
||||
conditionMgr = new ConditionManager(conditionSetDomainObject, openmct);
|
||||
mockListener = jasmine.createSpy('mockListener');
|
||||
|
||||
conditionMgr.on('conditionSetResultUpdated', mockListener);
|
||||
});
|
||||
|
||||
it('creates a conditionCollection with a default condition', function () {
|
||||
expect(conditionMgr.domainObject.configuration.conditionCollection.length).toEqual(1);
|
||||
let defaultConditionIdentifier = conditionMgr.domainObject.configuration.conditionCollection[0];
|
||||
expect(defaultConditionIdentifier).toEqual(mockConditionDomainObject.identifier);
|
||||
});
|
||||
|
||||
});
|
@ -177,8 +177,7 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import ConditionClass from "@/plugins/condition/Condition";
|
||||
import Criterion from '../../condition/components/Criterion.vue';
|
||||
import Criterion from './Criterion.vue';
|
||||
|
||||
export default {
|
||||
inject: ['openmct'],
|
||||
@ -236,10 +235,6 @@ export default {
|
||||
methods: {
|
||||
initialize() {
|
||||
this.setOutput();
|
||||
if (!this.domainObject.isDefault) {
|
||||
this.conditionClass = new ConditionClass(this.domainObject, this.openmct);
|
||||
this.conditionClass.on('conditionResultUpdated', this.handleConditionResult.bind(this));
|
||||
}
|
||||
},
|
||||
addCriteria() {
|
||||
const criteriaObject = {
|
||||
@ -256,19 +251,6 @@ export default {
|
||||
this.$emit('setMoveIndex', this.conditionIndex);
|
||||
},
|
||||
destroy() {
|
||||
if (this.conditionClass) {
|
||||
this.conditionClass.off('conditionResultUpdated', this.handleConditionResult.bind(this));
|
||||
if (typeof this.conditionClass.destroy === 'function') {
|
||||
this.conditionClass.destroy();
|
||||
}
|
||||
delete this.conditionClass;
|
||||
}
|
||||
},
|
||||
handleConditionResult(args) {
|
||||
this.$emit('conditionResultUpdated', {
|
||||
id: this.conditionIdentifier,
|
||||
result: args.data.result
|
||||
})
|
||||
},
|
||||
removeCondition(ev) {
|
||||
this.$emit('removeCondition', this.conditionIdentifier);
|
||||
@ -309,9 +291,6 @@ export default {
|
||||
this.domainObject.configuration.output = this.selectedOutputKey;
|
||||
}
|
||||
},
|
||||
updateCurrentCondition() {
|
||||
this.$emit('updateCurrentCondition', this.currentConditionIdentifier);
|
||||
},
|
||||
hasTelemetry(identifier) {
|
||||
// TODO: check parent domainObject.composition.hasTelemetry
|
||||
return this.currentCriteria && identifier;
|
||||
|
@ -70,10 +70,8 @@
|
||||
:condition-index="index"
|
||||
:telemetry="telemetryObjs"
|
||||
:is-editing="isEditing"
|
||||
@updateCurrentCondition="updateCurrentCondition"
|
||||
@removeCondition="removeCondition"
|
||||
@cloneCondition="cloneCondition"
|
||||
@conditionResultUpdated="handleConditionResult"
|
||||
@setMoveIndex="setMoveIndex"
|
||||
/>
|
||||
</li>
|
||||
@ -84,8 +82,8 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import Condition from '../../condition/components/Condition.vue';
|
||||
import uuid from 'uuid';
|
||||
import Condition from './Condition.vue';
|
||||
import ConditionManager from '../ConditionManager';
|
||||
|
||||
export default {
|
||||
inject: ['openmct', 'domainObject'],
|
||||
@ -111,21 +109,30 @@ export default {
|
||||
destroyed() {
|
||||
this.composition.off('add', this.addTelemetryObject);
|
||||
this.composition.off('remove', this.removeTelemetryObject);
|
||||
if(this.conditionManager) {
|
||||
this.conditionManager.off('conditionSetResultUpdated', this.handleOutputUpdated);
|
||||
this.conditionManager.destroy();
|
||||
}
|
||||
if (typeof this.stopObservingForChanges === 'function') {
|
||||
this.stopObservingForChanges();
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
this.instantiate = this.openmct.$injector.get('instantiate');
|
||||
this.composition = this.openmct.composition.get(this.domainObject);
|
||||
this.composition.on('add', this.addTelemetryObject);
|
||||
this.composition.on('remove', this.removeTelemetryObject);
|
||||
this.composition.load();
|
||||
this.conditionCollection = this.domainObject.configuration ? this.domainObject.configuration.conditionCollection : [];
|
||||
if (!this.conditionCollection.length) {
|
||||
this.addCondition(null, true);
|
||||
} else {
|
||||
this.updateCurrentCondition(this.conditionCollection[0]);
|
||||
}
|
||||
this.conditionCollection = this.domainObject.configuration.conditionCollection;
|
||||
this.conditionManager = new ConditionManager(this.domainObject, this.openmct);
|
||||
this.conditionManager.on('conditionSetResultUpdated', this.handleOutputUpdated.bind(this));
|
||||
this.observeForChanges();
|
||||
},
|
||||
methods: {
|
||||
observeForChanges() {
|
||||
this.stopObservingForChanges = this.openmct.objects.observe(this.domainObject, '*', (newDomainObject) => {
|
||||
this.conditionCollection = newDomainObject.configuration.conditionCollection;
|
||||
});
|
||||
},
|
||||
setMoveIndex(index) {
|
||||
this.moveIndex = index;
|
||||
this.isDragging = true;
|
||||
@ -172,22 +179,8 @@ export default {
|
||||
dragLeave(e) {
|
||||
e.target.classList.remove("dragging");
|
||||
},
|
||||
handleConditionResult(args) {
|
||||
let idAsString = this.openmct.objects.makeKeyString(args.id);
|
||||
this.conditionResults[idAsString] = args.result;
|
||||
this.updateCurrentConditionId();
|
||||
},
|
||||
updateCurrentConditionId() {
|
||||
let currentConditionIdentifier = this.conditionCollection[this.conditionCollection.length-1];
|
||||
for (let i = 0; i < this.conditionCollection.length - 1; i++) {
|
||||
let conditionIdAsString = this.openmct.objects.makeKeyString(this.conditionCollection[i]);
|
||||
if (this.conditionResults[conditionIdAsString]) {
|
||||
//first condition to be true wins
|
||||
currentConditionIdentifier = this.conditionCollection[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
this.$emit('currentConditionUpdated', currentConditionIdentifier);
|
||||
handleOutputUpdated(args) {
|
||||
this.$emit('currentConditionSetOutputUpdated', args);
|
||||
},
|
||||
addTelemetryObject(domainObject) {
|
||||
this.telemetryObjs.push(domainObject);
|
||||
@ -202,98 +195,20 @@ export default {
|
||||
this.telemetryObjs.splice(index, 1);
|
||||
}
|
||||
},
|
||||
removeTelemetry(telemetryDomainObjectIdentifier) {
|
||||
let index = _.findIndex(this.telemetryObjs, (obj) => {
|
||||
let objId = this.openmct.objects.makeKeyString(obj.identifier);
|
||||
let id = this.openmct.objects.makeKeyString(telemetryDomainObjectIdentifier);
|
||||
return objId === id;
|
||||
});
|
||||
if (index > -1) {
|
||||
this.telemetryObjs.splice(index, 1);
|
||||
}
|
||||
},
|
||||
/*
|
||||
Adds a condition to list via programatic creation of default for initial list, manual
|
||||
creation via Add Condition button, or duplication via button in title bar of condition.
|
||||
Params:
|
||||
event: always null,
|
||||
isDefault (boolean): true if conditionList is empty
|
||||
isClone (boolean): true if duplicating a condition
|
||||
definition (string): definition property of condition being duplicated with new name
|
||||
index (number): index of condition being duplicated
|
||||
*/
|
||||
addCondition(event, isDefault, isClone, configuration, index) {
|
||||
let conditionDomainObject = this.createConditionDomainObject(!!isDefault, isClone, configuration);
|
||||
//persist the condition domain object so that we can do an openmct.objects.get on it and only persist the identifier in the conditionCollection of conditionSet
|
||||
this.openmct.objects.mutate(conditionDomainObject, 'created', new Date());
|
||||
if (!isClone) {
|
||||
this.conditionCollection.unshift(conditionDomainObject.identifier);
|
||||
} else {
|
||||
this.conditionCollection.splice(index + 1, 0, conditionDomainObject.identifier);
|
||||
}
|
||||
this.persist();
|
||||
addCondition(event, isDefault, index) {
|
||||
this.conditionManager.addCondition(!!isDefault, index);
|
||||
},
|
||||
updateCurrentCondition(identifier) {
|
||||
this.currentConditionIdentifier = identifier;
|
||||
},
|
||||
createConditionDomainObject(isDefault, isClone, configuration) {
|
||||
const configurationTemplate = {
|
||||
name: isDefault ? 'Default' : (isClone ? 'Copy of ' : '') + 'Unnamed Condition',
|
||||
output: 'false',
|
||||
trigger: 'all',
|
||||
criteria: isDefault ? [] : [{
|
||||
telemetry: '',
|
||||
operation: '',
|
||||
input: '',
|
||||
metadata: '',
|
||||
key: ''
|
||||
}]
|
||||
};
|
||||
let conditionObj = {
|
||||
isDefault: isDefault,
|
||||
type: 'condition',
|
||||
identifier: {
|
||||
namespace: this.domainObject.identifier.namespace,
|
||||
key: uuid()
|
||||
},
|
||||
configuration: isClone ? configuration: configurationTemplate,
|
||||
summary: 'summary description',
|
||||
created: new Date()
|
||||
};
|
||||
let conditionDomainObjectKeyString = this.openmct.objects.makeKeyString(conditionObj.identifier);
|
||||
let newDomainObject = this.instantiate(conditionObj, conditionDomainObjectKeyString);
|
||||
|
||||
return newDomainObject.useCapability('adapter');
|
||||
},
|
||||
updateCondition(updatedCondition) {
|
||||
let index = _.findIndex(this.conditions, (condition) => condition.id === updatedCondition.id);
|
||||
this.conditions[index] = updatedCondition;
|
||||
},
|
||||
removeCondition(identifier) {
|
||||
let index = _.findIndex(this.conditionCollection, (condition) => {
|
||||
let conditionId = this.openmct.objects.makeKeyString(condition);
|
||||
let id = this.openmct.objects.makeKeyString(identifier);
|
||||
return conditionId === id;
|
||||
});
|
||||
this.conditionCollection.splice(index, 1);
|
||||
this.persist();
|
||||
this.updateCurrentConditionId();
|
||||
this.conditionManager.removeCondition(identifier);
|
||||
},
|
||||
reorder(reorderPlan) {
|
||||
let oldConditions = Array.from(this.conditionCollection);
|
||||
reorderPlan.forEach((reorderEvent) => {
|
||||
this.$set(this.conditionCollection, reorderEvent.newIndex, oldConditions[reorderEvent.oldIndex]);
|
||||
});
|
||||
this.persist();
|
||||
this.conditionManager.reorderConditions(reorderPlan);
|
||||
},
|
||||
cloneCondition(condition) {
|
||||
this.openmct.objects.get(condition.identifier).then((obj) => {
|
||||
obj.configuration.name = 'Copy of ' + obj.configuration.name;
|
||||
this.addCondition(null, false, true, obj.configuration, condition.index);
|
||||
});
|
||||
},
|
||||
persist() {
|
||||
this.openmct.objects.mutate(this.domainObject, 'configuration.conditionCollection', this.conditionCollection);
|
||||
this.conditionManager.cloneCondition(condition.identifier, condition.index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -23,10 +23,10 @@
|
||||
<template>
|
||||
<div class="c-cs-edit w-condition-set">
|
||||
<div class="c-sw-edit__ui holder">
|
||||
<CurrentOutput :condition="currentCondition" />
|
||||
<CurrentOutput :output="currentConditionOutput" />
|
||||
<TestData :is-editing="isEditing" />
|
||||
<ConditionCollection :is-editing="isEditing"
|
||||
@currentConditionUpdated="updateCurrentCondition"
|
||||
@currentConditionSetOutputUpdated="updateCurrentOutput"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
@ -49,24 +49,17 @@ export default {
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
currentCondition: this.currentCondition
|
||||
currentConditionOutput: ''
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
let conditionCollection = this.domainObject.configuration.conditionCollection;
|
||||
this.currentConditionIdentifier = conditionCollection.length ? this.updateCurrentCondition(conditionCollection[0]) : null;
|
||||
this.conditionSetIdentifier = this.openmct.objects.makeKeyString(this.domainObject.identifier);
|
||||
},
|
||||
methods: {
|
||||
setCurrentCondition() {
|
||||
if (this.currentConditionIdentifier) {
|
||||
this.openmct.objects.get(this.currentConditionIdentifier).then((obj) => {
|
||||
this.currentCondition = obj;
|
||||
});
|
||||
updateCurrentOutput(currentConditionResult) {
|
||||
if (this.openmct.objects.makeKeyString(currentConditionResult.id) === this.conditionSetIdentifier) {
|
||||
this.currentConditionOutput = currentConditionResult.output;
|
||||
}
|
||||
},
|
||||
updateCurrentCondition(conditionIdentifier) {
|
||||
this.currentConditionIdentifier = conditionIdentifier;
|
||||
this.setCurrentCondition();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -22,7 +22,7 @@
|
||||
|
||||
<template>
|
||||
<section id="current-output">
|
||||
<div v-if="condition"
|
||||
<div v-if="output"
|
||||
class="c-cs__ui__header"
|
||||
>
|
||||
<span class="c-cs__ui__header-label">Current Output</span>
|
||||
@ -32,11 +32,11 @@
|
||||
@click="expanded = !expanded"
|
||||
></span>
|
||||
</div>
|
||||
<div v-if="expanded && condition"
|
||||
<div v-if="expanded && output"
|
||||
class="c-cs__ui_content"
|
||||
>
|
||||
<div>
|
||||
<span class="current-output">{{ condition.configuration.output }}</span>
|
||||
<span class="current-output">{{ output }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
@ -47,9 +47,9 @@ export default {
|
||||
inject: ['openmct', 'domainObject'],
|
||||
props: {
|
||||
isEditing: Boolean,
|
||||
condition: {
|
||||
output: {
|
||||
default: () => {return null;},
|
||||
type: Object
|
||||
type: String
|
||||
}
|
||||
},
|
||||
data() {
|
||||
|
@ -72,17 +72,19 @@ export default class TelemetryCriterion extends EventEmitter {
|
||||
}
|
||||
|
||||
computeResult(data) {
|
||||
let comparator = this.findOperation(this.operation);
|
||||
let params = [];
|
||||
let result = false;
|
||||
params.push(data[this.metadata]);
|
||||
if (this.input instanceof Array && this.input.length) {
|
||||
params.push(this.input[0]);
|
||||
} else if (this.input) {
|
||||
params.push(this.input);
|
||||
}
|
||||
if (typeof comparator === 'function') {
|
||||
result = comparator(params);
|
||||
if (data) {
|
||||
let comparator = this.findOperation(this.operation);
|
||||
let params = [];
|
||||
params.push(data[this.metadata]);
|
||||
if (this.input instanceof Array && this.input.length) {
|
||||
params.push(this.input[0]);
|
||||
} else {
|
||||
params.push(this.input);
|
||||
}
|
||||
if (typeof comparator === 'function') {
|
||||
result = comparator(params);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@ -100,12 +102,17 @@ export default class TelemetryCriterion extends EventEmitter {
|
||||
|
||||
/**
|
||||
* Subscribes to the telemetry object and returns an unsubscribe function
|
||||
* If the telemetry is not valid, returns nothing
|
||||
*/
|
||||
subscribe() {
|
||||
this.unsubscribe();
|
||||
this.subscription = this.telemetryAPI.subscribe(this.telemetryObject, (datum) => {
|
||||
this.handleSubscription(datum);
|
||||
});
|
||||
if (this.isValid()) {
|
||||
this.unsubscribe();
|
||||
this.subscription = this.telemetryAPI.subscribe(this.telemetryObject, (datum) => {
|
||||
this.handleSubscription(datum);
|
||||
});
|
||||
} else {
|
||||
this.handleSubscription();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user