-
Notifications
You must be signed in to change notification settings - Fork 2
/
signalk-geofence-switch.js
79 lines (69 loc) · 2.39 KB
/
signalk-geofence-switch.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
72
73
74
75
76
77
78
79
module.exports = function(RED) {
function signalk(config) {
RED.nodes.createNode(this,config);
var node = this;
var unsubscribes = []
const geodist = node.context().global.get('geodist')
const app = node.context().global.get('app')
const context = node.context()
node.on('input', (msg) => {
if ( msg.topic === 'signalk-config' ) {
context.latitude = msg.payload.latitude
context.longitude = msg.payload.longitude
context.distance = msg.payload.distance
return
}
var pos;
if ( config.context !== 'vessels.self' ) {
pos = app.getPath(config.context + '.navigation.position.value')
} else {
pos = app.getSelfPath('navigation.position.value')
}
if ( !pos || !pos.latitude || !pos.longitude ) {
node.status({fill:"red",shape:"dot",text:"no position"});
return
}
var fencePos = null;
if ( config.myposition ) {
var mypos = app.getSelfPath('navigation.position.value')
if ( mypos && mypos.latitude && mypos.longitude ) {
fencePos = { lat: mypos.latitude, lon: mypos.longitude }
}
} else {
if ( msg.latitude ) {
fencePos = { lat: msg.latitude, lon: msg.longitude }
} else if ( context.latitude ) {
fencePos = { lat: context.latitude, lon: context.longitude }
} else {
fencePos = { lat: config.lat, lon: config.lon }
}
if ( fencePos.lat === 0 && fencePos.lon === 0 ) {
node.status({fill:"red",shape:"dot",text:"no lat/lon"});
return
}
}
if ( fencePos ) {
let curPos = {lat: pos.latitude, lon: pos.longitude}
let dist = geodist(fencePos,
curPos,
{ unit: 'meters'})
let distance
if ( msg.distance ) {
distance = msg.distance
} else if ( context.distance ) {
distance = context.distance
} else {
distance = config.distance
}
if ( dist > distance ) {
node.status({fill:"green",shape:"dot",text:"outside fence"});
node.send([null, msg ])
} else {
node.status({fill:"green",shape:"dot",text:"inside fence"});
node.send([msg, null])
}
}
})
}
RED.nodes.registerType("signalk-geofence-switch", signalk);
}