From 23aaada79d2bfb919ac992b3b1774add7a1fa4e1 Mon Sep 17 00:00:00 2001 From: Joshi Date: Thu, 23 Jan 2020 12:08:28 -0800 Subject: [PATCH] Evaluate telemetry criterion results and display the output of conditions accordingly --- src/plugins/condition/Condition.js | 45 ++++++++++++++----- .../components/ConditionCollection.vue | 1 - .../condition/components/ConditionEdit.vue | 1 - .../condition/criterion/TelemetryCriterion.js | 32 ++++++++++--- src/plugins/condition/utils/evaluator.js | 32 +++++++++++-- 5 files changed, 89 insertions(+), 22 deletions(-) diff --git a/src/plugins/condition/Condition.js b/src/plugins/condition/Condition.js index 15f101f027..6ff4a41b56 100644 --- a/src/plugins/condition/Condition.js +++ b/src/plugins/condition/Condition.js @@ -24,6 +24,7 @@ import * as EventEmitter from 'eventemitter3'; import uuid from 'uuid'; import TelemetryCriterion from "@/plugins/condition/criterion/TelemetryCriterion"; import { TRIGGER } from "@/plugins/condition/utils/constants"; +import {computeConditionForAll, computeConditionForAny} from "@/plugins/condition/utils/evaluator"; /* * conditionDefinition = { @@ -56,6 +57,7 @@ export default class ConditionClass extends EventEmitter { this.openmct = openmct; this.id = this.openmct.objects.makeKeyString(conditionDefinition.identifier); this.criteria = []; + this.criteriaResults = {}; if (conditionDefinition.definition.criteria) { this.createCriteria(conditionDefinition.definition.criteria); } @@ -69,15 +71,13 @@ export default class ConditionClass extends EventEmitter { } update(newDomainObject) { - console.log('ConditionClass: ', newDomainObject.definition); this.updateTrigger(newDomainObject.definition.trigger); this.updateCriteria(newDomainObject.definition.criteria); - this.handleConditionUpdated(); } - updateTrigger(conditionDefinition) { - if (this.trigger !== conditionDefinition.trigger) { - this.trigger = conditionDefinition.trigger; + updateTrigger(trigger) { + if (this.trigger !== trigger) { + this.trigger = trigger; this.handleConditionUpdated(); } } @@ -110,6 +110,7 @@ export default class ConditionClass extends EventEmitter { let criterionDefinitionWithId = this.generateCriterion(criterionDefinition || null); let criterion = new TelemetryCriterion(criterionDefinitionWithId, this.openmct); criterion.on('criterionUpdated', (obj) => this.handleCriterionUpdated(obj)); + criterion.on('criterionResultUpdated', (obj) => this.handleCriterionResult(obj)); if (!this.criteria) { this.criteria = []; } @@ -139,12 +140,17 @@ export default class ConditionClass extends EventEmitter { if (found) { const newCriterionDefinition = this.generateCriterion(criterionDefinition); let newCriterion = new TelemetryCriterion(newCriterionDefinition, this.openmct); + newCriterion.on('criterionUpdated', (obj) => this.handleCriterionUpdated(obj)); + newCriterion.on('criterionResultUpdated', (obj) => this.handleCriterionResult(obj)); + let criterion = found.item; criterion.unsubscribe(); - criterion.off('criterionUpdated', (result) => { - this.handleCriterionUpdated(id, result); - }); + criterion.off('criterionUpdated', (obj) => this.handleCriterionUpdated(obj)); + criterion.off('criterionResultUpdated', (obj) => this.handleCriterionResult(obj)); this.criteria.splice(found.index, 1, newCriterion); + if (this.criteriaResults[criterion.id] !== undefined) { + delete this.criteriaResults[criterion.id]; + } this.handleConditionUpdated(); } } @@ -164,6 +170,9 @@ export default class ConditionClass extends EventEmitter { this.handleCriterionUpdated(id, result); }); this.criteria.splice(found.index, 1); + if (this.criteriaResults[criterion.id] !== undefined) { + delete this.criteriaResults[criterion.id]; + } return true; } return false; @@ -174,14 +183,30 @@ export default class ConditionClass extends EventEmitter { if (found) { this.criteria[found.index] = criterion.data; //Most likely don't need this. + this.subscribe(); this.emitEvent('conditionUpdated', { trigger: this.trigger, criteria: this.criteria }); } + } + + handleCriterionResult(eventData) { + let id = eventData.id; + let result = eventData.data.result; + let found = this.findCriterion(id); + if (found) { + this.criteriaResults[id] = result; + } this.handleConditionUpdated(); } + subscribe() { + this.criteria.forEach((criterion) => { + criterion.subscribe(); + }) + } + handleConditionUpdated() { // trigger an updated event so that consumers can react accordingly this.evaluate(); @@ -204,9 +229,9 @@ export default class ConditionClass extends EventEmitter { //TODO: implement as part of the evaluator class task. evaluate() { if (this.trigger === TRIGGER.ANY) { - this.result = true; + this.result = computeConditionForAny(this.criteriaResults); } else if (this.trigger === TRIGGER.ALL) { - this.result = false; + this.result = computeConditionForAll(this.criteriaResults); } } diff --git a/src/plugins/condition/components/ConditionCollection.vue b/src/plugins/condition/components/ConditionCollection.vue index a118a2fb75..e618a8ef1d 100644 --- a/src/plugins/condition/components/ConditionCollection.vue +++ b/src/plugins/condition/components/ConditionCollection.vue @@ -92,7 +92,6 @@ export default { }, methods: { handleConditionResult(args) { - console.log('ConditionCollection: ', args.result); let idAsString = this.openmct.objects.makeKeyString(args.id); this.conditionResults[idAsString] = args.result; this.updateCurrentConditionId(); diff --git a/src/plugins/condition/components/ConditionEdit.vue b/src/plugins/condition/components/ConditionEdit.vue index dda4ca96b9..fde3f4f28d 100644 --- a/src/plugins/condition/components/ConditionEdit.vue +++ b/src/plugins/condition/components/ConditionEdit.vue @@ -206,7 +206,6 @@ export default { }, methods: { handleConditionResult(args) { - console.log('ConditionEdit::Result', args); this.$emit('condition-result-updated', { id: this.conditionIdentifier, result: args.data.result diff --git a/src/plugins/condition/criterion/TelemetryCriterion.js b/src/plugins/condition/criterion/TelemetryCriterion.js index 30117c76a5..8940079abe 100644 --- a/src/plugins/condition/criterion/TelemetryCriterion.js +++ b/src/plugins/condition/criterion/TelemetryCriterion.js @@ -21,6 +21,7 @@ *****************************************************************************/ import * as EventEmitter from 'eventemitter3'; +import {OPERATIONS} from '../utils/operations'; export default class TelemetryCriterion extends EventEmitter { @@ -38,6 +39,9 @@ export default class TelemetryCriterion extends EventEmitter { this.objectAPI = this.openmct.objects; this.telemetryAPI = this.openmct.telemetry; this.id = telemetryDomainObjectDefinition.id; + this.operation = telemetryDomainObjectDefinition.operation; + this.input = telemetryDomainObjectDefinition.input; + this.metaDataKey = telemetryDomainObjectDefinition.metaDataKey; this.subscription = null; this.telemetryMetadata = null; this.telemetryObjectIdAsString = null; @@ -51,18 +55,33 @@ export default class TelemetryCriterion extends EventEmitter { this.emitEvent('criterionUpdated', this); } - handleSubscription(datum) { - let data = this.normalizeData(datum); + handleSubscription(data) { + let result = this.computeResult(data); this.emitEvent('criterionResultUpdated', { - result: data, + result: result, error: null }) } - normalizeData(datum) { - return { - [datum.key]: datum[datum.source] + findOperation(operation) { + for (let i=0, ii=OPERATIONS.length; i < ii; i++) { + if (operation === OPERATIONS[i].name) { + return OPERATIONS[i].operation; + } } + return null; + } + + computeResult(data) { + let comparator = this.findOperation(this.operation); + let params = []; + let result = false; + params.push(data[this.metaDataKey]); + params.push(this.input); + if (typeof comparator === 'function') { + result = comparator(params); + } + return result; } emitEvent(eventName, data) { @@ -76,6 +95,7 @@ export default class TelemetryCriterion extends EventEmitter { * Subscribes to the telemetry object and returns an unsubscribe function */ subscribe() { + this.unsubscribe(); this.subscription = this.telemetryAPI.subscribe(this.telemetryObject, (datum) => { this.handleSubscription(datum); }); diff --git a/src/plugins/condition/utils/evaluator.js b/src/plugins/condition/utils/evaluator.js index 51bd7173f6..f74bb1e5d4 100644 --- a/src/plugins/condition/utils/evaluator.js +++ b/src/plugins/condition/utils/evaluator.js @@ -1,7 +1,31 @@ -export const computeConditionForAny = (args) => { - return false; +/** + * Returns true only if at least one of the results is true + **/ +export const computeConditionForAny = (resultMap) => { + let result = false; + for (let key in resultMap) { + if (resultMap.hasOwnProperty(key)) { + result = resultMap[key]; + if (result) { + break; + } + } + } + return result; }; -export const computeConditionForAll = (args) => { - return false; +/** + * Returns true only if all the results are true + **/ +export const computeConditionForAll = (resultMap) => { + let result = false; + for (let key in resultMap) { + if (resultMap.hasOwnProperty(key)) { + result = resultMap[key]; + if (!result) { + break; + } + } + } + return result; };