-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.js
71 lines (59 loc) · 3.59 KB
/
map.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const NO_FRONT_DETECTED = "No front detected";
const propertyMap = [
{ signalK: "environment.outside.pressure.trend.tendency", src: (json) => validateProperty(json.trend.tendency) },
{ signalK: "environment.outside.pressure.trend.trend", src: (json) => validateProperty(json.trend.trend) },
{ signalK: "environment.outside.pressure.trend.severity", src: (json) => validateProperty(json.trend.severity) },
{ signalK: "environment.outside.pressure.trend.period", src: (json) => validateProperty(json.trend.period * 60) },
{ signalK: "environment.outside.pressure.trend.period.from", src: (json) => validateProperty(json.trend.from.meta.value) },
{ signalK: "environment.outside.pressure.trend.period.to", src: (json) => validateProperty(json.trend.to.meta.value) },
{ signalK: "environment.outside.pressure.prediction.pressureOnly", src: (json) => validateProperty(json.predictions.pressureOnly) },
{ signalK: "environment.outside.pressure.prediction.quadrant", src: (json) => validateProperty(json.predictions.quadrant) },
{ signalK: "environment.outside.pressure.prediction.season", src: (json) => validateProperty(json.predictions.season) },
{ signalK: "environment.outside.pressure.prediction.beaufort", src: (json) => validateProperty(json.predictions.beaufort.force) },
{ signalK: "environment.outside.pressure.prediction.beaufort.description", src: (json) => validateProperty(json.predictions.beaufort.description) },
{ signalK: "environment.outside.pressure.prediction.front.tendency", src: (json) => validateProperty(json.predictions.front.tendency, NO_FRONT_DETECTED) },
{ signalK: "environment.outside.pressure.prediction.front.prognose", src: (json) => validateProperty(json.predictions.front.prognose, NO_FRONT_DETECTED) },
{ signalK: "environment.outside.pressure.prediction.front.wind", src: (json) => validateProperty(json.predictions.front.wind, NO_FRONT_DETECTED) },
{ signalK: "environment.outside.pressure.system", src: (json) => validateProperty(json.system.name) },
{ signalK: "environment.outside.pressure.1hr", src: (json) => history(json, 1) },
{ signalK: "environment.outside.pressure.3hr", src: (json) => history(json, 3) },
{ signalK: "environment.outside.pressure.6hr", src: (json) => history(json, 6) },
{ signalK: "environment.outside.pressure.12hr", src: (json) => history(json, 12) },
{ signalK: "environment.outside.pressure.24hr", src: (json) => history(json, 24) },
{ signalK: "environment.outside.pressure.48hr", src: (json) => history(json, 48) }
]
/**
*
* @param {Array<Object>} json barometer-trend (npm-package) JSON structure
* @returns [{path: path, value: value}]
*/
function mapProperties(json) {
const deltaUpdates = [];
propertyMap.forEach((p) => {
try {
let value = (json !== null) ? p.src(json) : defaultPropertyValue;
let deltaUpdate = buildDeltaPath(p.signalK, value);
deltaUpdates.push(deltaUpdate);
} catch {
console.debug("Failed to map property: " + p.signalK);
}
});
return deltaUpdates.length > 0 ? deltaUpdates : null;
}
const history = (json, hour) => {
let pressure = json.history.find((h) => h.hour === hour).pressure;
return pressure !== null ? validateProperty(pressure.meta.value) : null;
}
const defaultPropertyValue = null;
function validateProperty(value, defaultValue = defaultPropertyValue) {
return (value !== null || value !== undefined) ? value : defaultValue;
}
function buildDeltaPath(path, value) {
return {
path: path,
value: value
}
}
module.exports = {
mapProperties
}