@yachteye/signalk-engineroom-plugin
Version:
Get EngineRoom data from the source (database or other) and add it to the SignalK graph.
69 lines (68 loc) • 3.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const faker_1 = require("@faker-js/faker");
const _1 = require(".");
class Simulator {
constructor(definitions) {
this.dataDefinitions = definitions;
this.faker = new faker_1.SimpleFaker();
}
/**
* Get the simulated data values.
* @returns Dictionary with the simulated data, keyed by the SignalK path.
*/
getData() {
const data = {};
this.dataDefinitions.forEach((v, i) => {
// Skip items that have derived values.
if (v.displayNameDb.startsWith('N.A.')) {
return;
}
switch (v.type) {
case 'number':
data[v.path] = this.faker.number.float({ min: v.min, max: v.max, fractionDigits: v.fractionDigits });
break;
case 'ratio':
data[v.path] = this.faker.number.float({ min: v.min === undefined ? 0 : v.min, max: v.max === undefined ? 1 : v.max, fractionDigits: v.fractionDigits === undefined ? 2 : v.fractionDigits });
break;
case 'running_off':
// Let's assume this thing is running 90% of the time.
const f = this.faker.number.int({ min: 0, max: 9 });
data[v.path] = f === 0 ? 'off' : 'running';
break;
case 'fixed-value':
data[v.path] = v.fixed === undefined ? 0 : v.fixed;
break;
case _1.IDataType.IntegerNumber:
data[v.path] = this.faker.number.int({ min: v.min, max: v.max });
break;
case _1.IDataType.NumberDecreasing:
if (v.startVal !== undefined && v.min !== undefined && v.max !== undefined) {
let newVal = v.startVal - (0.001 * (v.max - v.min));
if (newVal < v.min) {
newVal = v.max * 0.95;
}
data[v.path] = newVal;
v.startVal = newVal;
}
break;
case _1.IDataType.IntegerStatus:
if (v.startVal !== undefined && v.min !== undefined && v.max !== undefined) {
let newVal = v.startVal + (0.04 * (v.max - v.min));
if (Math.floor(newVal) > v.max) {
newVal = v.min;
}
data[v.path] = Math.floor(newVal);
// console.log('startVal', v.startVal, 'integerVal', Math.floor(newVal));
v.startVal = newVal;
}
break;
default:
console.error('[Simulator]', 'invalid data type', v.type);
break;
}
});
return data;
}
}
exports.default = Simulator;