signalk-server
Version:
An implementation of a [Signal K](http://signalk.org) server for boats.
175 lines (173 loc) • 5.79 kB
JavaScript
"use strict";
/*
* Copyright 2016 Teppo Kurki <teppo.kurki@iki.fi>
*
* 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.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StreamBundle = void 0;
exports.toDelta = toDelta;
const baconjs_1 = __importDefault(require("baconjs"));
class StreamBundle {
selfContext;
buses;
allPathsBus;
selfBuses;
selfAllPathsBus;
selfStreams;
selfAllPathsStream;
keys;
availableSelfPaths;
metaBus;
selfMetaBus;
constructor(selfId) {
this.selfContext = 'vessels.' + selfId;
this.buses = {};
this.allPathsBus = new baconjs_1.default.Bus();
this.selfBuses = {};
this.selfAllPathsBus = new baconjs_1.default.Bus();
this.selfStreams = {};
this.selfAllPathsStream = new baconjs_1.default.Bus();
this.keys = new baconjs_1.default.Bus();
this.availableSelfPaths = {};
this.metaBus = new baconjs_1.default.Bus();
this.selfMetaBus = new baconjs_1.default.Bus();
}
pushDelta(delta) {
try {
if (delta.updates) {
delta.updates.forEach((update) => {
const base = {
context: delta.context, // TSTODO: make optional/required match
source: update.source,
$source: update.$source, // TSTODO: make optional/required match
timestamp: update.timestamp // TSTODO: make optional/required match
};
if ('meta' in update) {
update.meta.forEach((meta) => {
this.push(meta.path, {
...base,
path: meta.path,
value: meta.value,
isMeta: true
});
});
}
if ('values' in update) {
update.values.forEach((pathValue) => {
this.push(pathValue.path, {
...base,
path: pathValue.path,
value: pathValue.value,
isMeta: false
});
});
}
});
}
}
catch (e) {
console.error(e);
}
}
push(path, normalizedDelta) {
const { isMeta } = normalizedDelta;
const isSelf = normalizedDelta.context === this.selfContext;
if (isMeta) {
this.metaBus.push(normalizedDelta);
if (isSelf) {
this.selfMetaBus.push(normalizedDelta);
}
}
if (!this.availableSelfPaths[path]) {
this.availableSelfPaths[path] = true;
}
this.getBus().push(normalizedDelta);
this.getBus(path).push(normalizedDelta);
if (isSelf) {
this.getSelfBus().push(normalizedDelta);
this.getSelfBus(path).push(normalizedDelta);
if (!isMeta) {
this.getSelfStream().push(normalizedDelta.value);
this.getSelfStream(path).push(normalizedDelta.value);
}
}
}
getMetaBus() {
return this.metaBus;
}
getSelfMetaBus() {
return this.selfMetaBus;
}
getBus(path) {
if (path !== undefined) {
let result = this.buses[path];
if (!result) {
result = this.buses[path] = new baconjs_1.default.Bus();
this.keys.push(path);
}
return result;
}
else {
return this.allPathsBus;
}
}
getSelfStream(path) {
if (path !== undefined) {
let result = this.selfStreams[path];
if (!result) {
result = this.selfStreams[path] = new baconjs_1.default.Bus();
}
return result;
}
else {
return this.selfAllPathsStream;
}
}
getSelfBus(path) {
if (path !== undefined) {
let result = this.selfBuses[path];
if (!result) {
result = this.selfBuses[path] = new baconjs_1.default.Bus();
}
return result;
}
else {
return this.selfAllPathsBus;
}
}
getAvailablePaths() {
return Object.keys(this.availableSelfPaths);
}
}
exports.StreamBundle = StreamBundle;
function toDelta(normalizedDeltaData) {
const type = normalizedDeltaData.isMeta ? 'meta' : 'values';
const update = {
source: normalizedDeltaData.source,
$source: normalizedDeltaData.$source,
timestamp: normalizedDeltaData.timestamp,
[type]: [
{
path: normalizedDeltaData.path,
value: normalizedDeltaData.value
}
]
};
return {
context: normalizedDeltaData.context,
updates: [update]
};
}