From 1f19f83863da1e2954c106045b3aa15b14a739cc Mon Sep 17 00:00:00 2001 From: Scott Bender Date: Thu, 2 Nov 2017 19:43:41 -0400 Subject: [PATCH] feature: add preferredSource to common fields --- schemas/definitions.json | 3 +++ scripts/processSchemaFiles.js | 4 ++-- src/fullsignalk.js | 14 +++++++++----- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/schemas/definitions.json b/schemas/definitions.json index c1f291fb7..6bdd86776 100644 --- a/schemas/definitions.json +++ b/schemas/definitions.json @@ -306,6 +306,9 @@ }, "sentence": { "type": "string" + }, + "preferredSource": { + "type": "string" } } }, diff --git a/scripts/processSchemaFiles.js b/scripts/processSchemaFiles.js index 76056f377..cde5fd685 100755 --- a/scripts/processSchemaFiles.js +++ b/scripts/processSchemaFiles.js @@ -146,7 +146,7 @@ class Parser { } } - const skipFields = ['timestamp', '$source', 'source', '_attr', 'meta', 'pgn', 'sentence', 'value', 'values'] + const skipFields = ['timestamp', '$source', 'source', '_attr', 'meta', 'pgn', 'sentence', 'value', 'values', 'preferredSource'] const embeddedFields = !this.tree[`${path}/timestamp`] ? {} : _.pick(subtree.properties ? _.omit(subtree.properties || {}, skipFields) : {}, (value, key) => { @@ -212,7 +212,7 @@ class Parser { */ .then(results => { const filenames = {} - const filter = ['/timestamp', '/$source', '/source', '/_attr', '/meta', '/pgn', '/sentence', '/value', '/values'] + const filter = ['/timestamp', '/$source', '/source', '/_attr', '/meta', '/pgn', '/sentence', '/value', '/values', '/preferredSource'] results.forEach(result => { filenames[result.name] = result.path diff --git a/src/fullsignalk.js b/src/fullsignalk.js index 34373a9dd..8a8d20d7d 100644 --- a/src/fullsignalk.js +++ b/src/fullsignalk.js @@ -220,11 +220,15 @@ function addValue(context, contextPath, source, timestamp, pathValue) { valueLeaf.values[sourceId].timestamp = timestamp; setMessage(valueLeaf.values[sourceId], source); } - assignValueToLeaf(pathValue.value, valueLeaf); - if (pathValue.path.length != 0) { - valueLeaf['$source'] = getId(source); - valueLeaf.timestamp = timestamp; - setMessage(valueLeaf, source); + var sourceId = getId(source); + if ( !valueLeaf.preferredSource || valueLeaf.preferredSource == sourceId ) + { + assignValueToLeaf(pathValue.value, valueLeaf); + if (pathValue.path.length != 0) { + valueLeaf['$source'] = getId(source); + valueLeaf.timestamp = timestamp; + setMessage(valueLeaf, source); + } } }