-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
171 lines (163 loc) · 4.54 KB
/
index.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const { readFile, writeFile } = require('fs/promises');
const { join } = require('path');
const StateMachine = require('./StateMachine');
module.exports = function createPlugin(app) {
const plugin = {};
plugin.id = 'signalk-autostate';
plugin.name = 'Auto-state';
plugin.description = 'Automatically change navigation state based on vessel movement';
let unsubscribes = [];
let stateMachine = null;
const setStatus = app.setPluginStatus || app.setProviderStatus;
plugin.start = function start(options) {
const stateFile = join(app.getDataDirPath(), 'persisted-state.json');
const posMinutes = options.position_minutes || 10;
const subscription = {
context: 'vessels.self',
subscribe: [
{
path: 'navigation.position',
period: (posMinutes * 60000) / 10,
},
{
path: 'navigation.anchor.position',
period: 6000,
},
{
path: 'propulsion.*.revolutions',
period: 6000,
},
{
path: 'propulsion.*.state',
period: 6000,
},
{
path: 'navigation.speedOverGround',
period: 6000,
},
],
};
const currentStatus = {};
let lastUpdate = 0;
function setState(state, update) {
const currentUpdate = new Date().getTime();
if (currentStatus.state === state && (lastUpdate + 600000) > currentUpdate) {
return;
}
currentStatus.state = state;
app.handleMessage(plugin.id, {
context: `vessels.${app.selfId}`,
updates: [
{
source: {
label: plugin.id,
},
timestamp: update.time || new Date().toISOString(),
values: [
{
path: 'navigation.state',
value: state,
},
],
},
],
});
setStatus(`Detected state: ${state}`);
lastUpdate = currentUpdate;
writeFile(stateFile, JSON.stringify({
state,
time: update.time,
}), 'utf-8')
.catch((e) => {
app.error(e.message);
});
}
stateMachine = new StateMachine(
options.position_minutes,
options.underway_threshold,
options.default_propulsion,
options.moored_threshold,
);
function handleValue(update) {
setState(stateMachine.update(update), update);
}
app.subscriptionmanager.subscribe(
subscription,
unsubscribes,
(subscriptionError) => {
app.error(`Error:${subscriptionError}`);
},
(delta) => {
if (!delta.updates) {
return;
}
delta.updates.forEach((u) => {
if (!u.values) {
return;
}
u.values.forEach((v) => {
handleValue({
path: v.path,
value: v.value,
time: new Date(u.timestamp),
});
});
});
},
);
setStatus('Waiting for updates');
readFile(stateFile, 'utf-8')
.then((content) => JSON.parse(content))
.then((data) => {
currentStatus.state = data.state;
stateMachine.lastState = data.state;
stateMachine.stateChangeTime = new Date(data.time);
setStatus(`Persisted state: ${data.state}`);
})
.catch((e) => {
app.error(e.message);
const initialState = app.getSelfPath('navigation.state');
if (initialState) {
currentStatus.state = initialState;
setStatus(`Initial state: ${initialState}`);
}
});
};
plugin.stop = function stop() {
unsubscribes.forEach((f) => f());
unsubscribes = [];
};
plugin.schema = {
type: 'object',
properties: {
default_propulsion: {
type: 'string',
default: 'sailing',
title: 'Default means of propulsion when the vessel is moving',
enum: [
'sailing',
'motoring',
],
},
position_minutes: {
type: 'integer',
default: 10,
minimum: 2,
title: 'How often to check whether vessel is under way (in minutes)',
},
underway_threshold: {
type: 'integer',
default: 100,
title: 'Distance the vessel must move within the time to be considered under way (in meters)',
},
moored_threshold: {
type: 'number',
default: 0,
minimum: 0,
maximum: 1,
title: 'Speed the vessel can have when stopping the engine to be considered moored immediately (in m/s)',
},
},
};
return plugin;
};