From 4c68c725b1dbabd47cb5a1c4177cf3a18780b666 Mon Sep 17 00:00:00 2001 From: Joshi Date: Wed, 15 Jan 2020 10:51:45 -0800 Subject: [PATCH] Adding telemetry options to ConditionEdit --- src/plugins/condition/Condition.js | 59 +++++++++---------- .../components/ConditionCollection.vue | 48 +++++++++------ .../condition/components/ConditionEdit.vue | 27 ++++++++- .../condition/criterion/TelemetryCriterion.js | 16 +++-- 4 files changed, 95 insertions(+), 55 deletions(-) diff --git a/src/plugins/condition/Condition.js b/src/plugins/condition/Condition.js index 21eec8fbe8..ba51c21ebb 100644 --- a/src/plugins/condition/Condition.js +++ b/src/plugins/condition/Condition.js @@ -43,12 +43,10 @@ export default class ConditionClass extends EventEmitter { constructor(conditionDefinition, openmct) { super(); - this.id = uuid(); - this.name = conditionDefinition.name; - this.description = conditionDefinition.description; - this.isDefault = conditionDefinition.isDefault; + this.openmct = openmct; + this.id = this.openmct.objects.makeKeyString(conditionDefinition.identifier); if (conditionDefinition.criteria) { - this.createCriteria(conditionDefinition.criteria, openmct); + this.createCriteria(conditionDefinition.criteria); } else { this.criteria = []; } @@ -73,9 +71,9 @@ export default class ConditionClass extends EventEmitter { }; } - createCriteria(criterionDefinitions, openmct) { + createCriteria(criterionDefinitions) { criterionDefinitions.forEach((criterionDefinition) => { - this.addCriterion(criterionDefinition, openmct); + this.addCriterion(criterionDefinition); }); } @@ -87,22 +85,18 @@ export default class ConditionClass extends EventEmitter { /** * adds criterion to the condition. */ - addCriterion(criterionDefinition, openmct) { + addCriterion(criterionDefinition) { let criterionDefinitionWithId = this.generateCriterion(criterionDefinition || null); - openmct.objects.get(openmct.objects.makeKeyString(criterionDefinitionWithId.key)).then((obj) => { - if (openmct.telemetry.isTelemetryObject(obj)) { - let criterion = new TelemetryCriterion(obj, openmct); - criterion.on('criterionUpdated', this.handleCriterionUpdated); - if (!this.criteria) { - this.criteria = []; - } - this.criteria.push(criterion); - this.handleConditionUpdated(); - return criterionDefinitionWithId.id; - } else { - return null; - } + let criterion = new TelemetryCriterion(criterionDefinitionWithId, this.openmct); + criterion.on('criterionUpdated', (data) => { + this.handleCriterionUpdated(data); }); + if (!this.criteria) { + this.criteria = []; + } + this.criteria.push(criterion); + this.handleConditionUpdated(); + return criterionDefinitionWithId.id; } findCriterion(id) { @@ -120,11 +114,11 @@ export default class ConditionClass extends EventEmitter { return criterion; } - updateCriterion(id, criterionDefinition, openmct) { + updateCriterion(id, criterionDefinition) { let found = this.findCriterion(id); if (found) { const newCriterionDefinition = this.generateCriterion(criterionDefinition); - let newCriterion = new TelemetryCriterion(newCriterionDefinition, openmct); + let newCriterion = new TelemetryCriterion(newCriterionDefinition, this.openmct); let criterion = found.item; criterion.unsubscribe(); criterion.off('criterionUpdated', (result) => { @@ -155,14 +149,19 @@ export default class ConditionClass extends EventEmitter { return false; } - handleCriterionUpdated(id, result) { + handleCriterionUpdated(criterion) { // reevaluate the condition's output // TODO: should we save the result of a criterion here or in the criterion object itself? - this.evaluate(); - this.handleConditionUpdated(); + let found = this.findCriterion(criterion.id); + if (found) { + this.criteria[found.index] = criterion; + this.handleConditionUpdated(); + } + // this.handleConditionUpdated(); } handleConditionUpdated() { + console.log(this); // trigger an updated event so that consumers can react accordingly this.emitResult(); } @@ -191,11 +190,9 @@ export default class ConditionClass extends EventEmitter { emitResult() { this.emit('conditionUpdated', { - identifier: this.id, - data: { - trigger: this.trigger, - criteria: this.criteria - } + id: this.id, + trigger: this.trigger, + criteria: this.criteria }); } } diff --git a/src/plugins/condition/components/ConditionCollection.vue b/src/plugins/condition/components/ConditionCollection.vue index 94725c2ae5..eede7235e0 100644 --- a/src/plugins/condition/components/ConditionCollection.vue +++ b/src/plugins/condition/components/ConditionCollection.vue @@ -30,7 +30,7 @@
@@ -64,7 +64,8 @@ export default { return { expanded: true, parentKeyString: this.openmct.objects.makeKeyString(this.domainObject.identifier), - conditionCollection: [] + conditionCollection: [], + conditions: [] }; }, destroyed() { @@ -72,39 +73,52 @@ export default { }, mounted() { this.telemetryObjs = []; + this.instantiate = this.openmct.$injector.get('instantiate'); this.composition = this.openmct.composition.get(this.domainObject); this.composition.on('add', this.addTelemetry); + this.composition.load(); this.conditionCollection = this.domainObject.configuration ? this.domainObject.configuration.conditionCollection : []; - if (!this.conditionCollection.length) {this.addCondition(true)} + if (!this.conditionCollection.length) {this.addCondition(null, true)} }, methods: { - addTelemetry(domainObjectAdded) { - this.telemetryObjs.push(domainObjectAdded); - console.log(this.telemetryObjs); + addTelemetry(telemetryDomainObject) { + this.telemetryObjs.push(telemetryDomainObject); }, - addCondition(isDefault) { - if (isDefault !== true) {isDefault = false} + addCondition(event, isDefault) { + let conditionDO = this.getConditionDomainObject(!!isDefault); + this.conditionCollection.unshift(conditionDO); + + let condition = new ConditionClass(conditionDO, this.openmct); + this.conditions.push(condition); + }, + getConditionDomainObject(isDefault) { let conditionObj = { isDefault: isDefault, + identifier: { + namespace: "", + key: uuid() + }, name: isDefault ? 'Default' : 'Unnamed Condition', trigger: 'any', criteria: isDefault ? [] : [{ operation: '', input: '', - metaDataKey: 'sin', - key: isDefault ? '' : this.telemetryObjs.length ? this.openmct.objects.makeKeyString(this.telemetryObjs[0].identifier) : '' + metaDataKey: this.openmct.telemetry.getMetadata(this.telemetryObjs[0]).values()[0].key, + key: this.telemetryObjs.length ? this.openmct.objects.makeKeyString(this.telemetryObjs[0].identifier) : null }], - output: 'Default test' + output: 'Default test', + type: 'condition' }; - - let conditionDO = new ConditionClass(conditionObj, this.openmct); - console.log(JSON.stringify(conditionDO)); - this.conditionCollection.unshift(conditionDO); - console.log(JSON.stringify(this.conditionCollection)); + let conditionDOKeyString = this.openmct.objects.makeKeyString(conditionObj.identifier); + let newDO = this.instantiate(conditionObj, conditionDOKeyString); + return newDO.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) => this.openmct.objects.makeKeyString(identifier) === condition.identifier.key); - this.conditionCollection.splice(index, 1); }, reorder(reorderPlan) { diff --git a/src/plugins/condition/components/ConditionEdit.vue b/src/plugins/condition/components/ConditionEdit.vue index 5cad06217d..5840b4b355 100644 --- a/src/plugins/condition/components/ConditionEdit.vue +++ b/src/plugins/condition/components/ConditionEdit.vue @@ -36,7 +36,7 @@
  • - +
  • +
  • + + + + +
  • @@ -74,14 +82,17 @@ export default { return { expanded: true, name: this.condition.name, - description: this.condition.description + description: this.condition.description, + telemetryObject: this.telemetryObject }; }, mounted() { + this.updateTelemetry(); }, updated() { console.log('updated'); - this.persist() + this.persist(); + // this.updateTelemetry(); }, methods: { removeCondition(ev) { @@ -91,6 +102,16 @@ export default { this.domainObject.configuration.conditionCollection.splice(index, 1); }, + updateTelemetry() { + this.telemetryObject = this.hasTelemetry() ? this.openmct.objects.get(this.condition.criteria[0].key) : null; + if (this.telemetryObject) { + this.telemetryMetadata = this.openmct.telemetry.getMetadata().values(); + } + console.log('ConditionEdit', this.telemetryObject); + }, + hasTelemetry() { + return this.condition.criteria.length && this.condition.criteria[0].key; + }, persist(index) { if (index) { this.openmct.objects.mutate(this.domainObject, `configuration.conditionCollection[${index}]`, this.domainObject.configuration.conditionCollection[index]); diff --git a/src/plugins/condition/criterion/TelemetryCriterion.js b/src/plugins/condition/criterion/TelemetryCriterion.js index 36497cb70f..0bdbb462e6 100644 --- a/src/plugins/condition/criterion/TelemetryCriterion.js +++ b/src/plugins/condition/criterion/TelemetryCriterion.js @@ -24,13 +24,21 @@ import * as EventEmitter from 'eventemitter3'; export default class TelemetryCriterion extends EventEmitter { - constructor(telemetryDomainObject, openmct) { + constructor(telemetryDomainObjectDefinition, openmct) { super(); + this.id = telemetryDomainObjectDefinition.id; this.subscription = null; this.telemetryMetadata = null; - this.telemetryObject = telemetryDomainObject; - this.telemetryObjectIdAsString = openmct.objects.makeKeyString(this.telemetryObject.identifier); + this.telemetryObjectIdAsString = null; + openmct.objects.get(openmct.objects.makeKeyString(telemetryDomainObjectDefinition.key)).then((obj) => { + if (openmct.telemetry.isTelemetryObject(obj)) { + this.telemetryObject = obj; + this.telemetryObjectIdAsString = openmct.objects.makeKeyString(this.telemetryObject.identifier); + this.telemetryMetadata = openmct.telemetry.getMetadata(this.telemetryObject.identifier); + this.emit('criterionUpdated', this); + } + }); } handleSubscription(datum) { @@ -48,7 +56,7 @@ export default class TelemetryCriterion extends EventEmitter { emitResult(data, error) { this.emit('criterionUpdated', { - identifier: this.telemetryObjectIdAsString, + identifier: this.id, data: data, error: error });