Skip to content

Commit d40ab53

Browse files
authored
feature: add meta deltas support to FullSignalK (#597)
1 parent 3104f15 commit d40ab53

File tree

2 files changed

+72
-11
lines changed

2 files changed

+72
-11
lines changed

src/fullsignalk.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,12 @@ FullSignalK.prototype.addUpdate = function(context, contextPath, update) {
105105
} else {
106106
console.error("No source in delta update:" + JSON.stringify(update));
107107
}
108-
addValues(context, contextPath, update.source || update['$source'], update.timestamp, update.values);
108+
if ( update.values ) {
109+
addValues(context, contextPath, update.source || update['$source'], update.timestamp, update.values);
110+
}
111+
if ( update.meta ) {
112+
addMetas(context, contextPath, update.source || update['$source'], update.timestamp, update.meta);
113+
}
109114
}
110115

111116
FullSignalK.prototype.updateDollarSource = function(context, dollarSource, timestamp) {
@@ -198,17 +203,13 @@ function addValue(context, contextPath, source, timestamp, pathValue) {
198203
if (!previous[pathPart]) {
199204
previous[pathPart] = {};
200205
}
201-
if ( i === splitPath.length-1 && typeof previous[pathPart].value === 'undefined' ) {
202-
let meta = signalkSchema.getMetadata(contextPath + '.' + pathValue.path)
203-
if (meta ) {
204-
//ignore properties from keyswithmetadata.json
205-
meta = JSON.parse(JSON.stringify(meta))
206-
delete meta.properties
207-
206+
if ( i === splitPath.length-1 && typeof previous[pathPart].value === 'undefined' ) {
207+
let meta = signalkSchema.internalGetMetadata(contextPath + '.' + pathValue.path)
208+
if (meta) {
208209
_.assign(meta, previous[pathPart].meta)
209-
previous[pathPart].meta = meta
210+
previous[pathPart].meta = meta
210211
}
211-
}
212+
}
212213
return previous[pathPart];
213214
}, context);
214215
}
@@ -267,5 +268,17 @@ function setMessage(leaf, source) {
267268
}
268269
}
269270

271+
function addMetas(context, contextPath, source, timestamp, metas) {
272+
metas.forEach(metaPathValue => addMeta(context, contextPath, source, timestamp, metaPathValue))
273+
}
274+
275+
function addMeta(context, contextPath, source, timestamp, pathValue) {
276+
if (_.isUndefined(pathValue.path) || _.isUndefined(pathValue.value)) {
277+
console.error("Illegal value in delta:" + JSON.stringify(pathValue));
278+
return;
279+
}
280+
signalkSchema.addMetaData(contextPath, pathValue.path, pathValue.value)
281+
}
282+
270283

271284
module.exports = FullSignalK;

src/index.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,55 @@ module.exports.getMetadata = function (path) {
346346
const result = metadataByRegex.find(entry =>
347347
entry.regexp.test('/' + path.replace(/\./g, '/'))
348348
)
349-
return result ? result.metadata : undefined
349+
350+
return result && Object.keys(result.metadata).length > 0 ? result.metadata : undefined
351+
}
352+
353+
module.exports.internalGetMetadata = function (path) {
354+
const result = metadataByRegex.find(entry =>
355+
entry.regexp.test('/' + path.replace(/\./g, '/'))
356+
)
357+
358+
let meta = result ? result.metadata : undefined
359+
const parts = path.split('.')
360+
const key = `/${parts[0]}/*/` + parts.slice(2).join('/')
361+
if ( !module.exports.metadata[key] ) {
362+
meta = result ? JSON.parse(JSON.stringify(result.metadata)) : {}
363+
module.exports.metadata[key] = meta
364+
const regexpKey =
365+
'^' + key.replace(/\*/g, '.*').replace(/RegExp/g, '.*') + '$'
366+
metadataByRegex.unshift({
367+
regexp: new RegExp(regexpKey),
368+
metadata: meta
369+
})
370+
}
371+
372+
return meta
373+
}
374+
375+
module.exports.addMetaData = function(context, path, meta) {
376+
const root = context.split('.')[0]
377+
const key = `/${root}/*/${path.replace(/\./g, '/')}`
378+
let existing = module.exports.metadata[key]
379+
if ( existing ) {
380+
_.merge(existing, meta)
381+
} else {
382+
let regexMeta = module.exports.getMetadata(context + '.' + path)
383+
if ( regexMeta ) {
384+
let newMeta = JSON.parse(JSON.stringify(regexMeta))
385+
_.merge(newMeta, meta)
386+
meta = newMeta
387+
}
388+
389+
module.exports.metadata[key] = meta
390+
391+
const regexpKey =
392+
'^' + key.replace(/\*/g, '.*').replace(/RegExp/g, '.*') + '$'
393+
metadataByRegex.unshift({
394+
regexp: new RegExp(regexpKey),
395+
metadata: meta
396+
})
397+
}
350398
}
351399

352400
module.exports.getAISShipTypeName = function(id) {

0 commit comments

Comments
 (0)