forked from SignalK/sailsconfiguration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
199 lines (190 loc) · 5.63 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* Copyright 2017 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.
*/
const pluginId = "sailsconfiguration";
module.exports = function(app) {
let plugin = {};
let timer;
const debug = app.debug
plugin.start = function(props) {
debug("starting");
function setDeltas() {
let totalArea = 0;
let activeArea = 0;
let activeSails = [];
const values = (props.sails || []).map(sail => {
// No id or description in the sail as used in Signal K
const sailClone = JSON.parse(JSON.stringify(sail));
delete sailClone.id;
delete sailClone.description;
// Calculate into sail area available
totalArea += sail.area;
if (sail.active) {
activeSails.push(sail.name);
// Calculate into active sail area
if (sail.reducedState && sail.reducedState.furledRatio) {
activeArea += sail.area - (sail.area * sail.reducedState.furledRatio);
} else {
activeArea += sail.area;
}
}
return {
path: "sails.inventory." + sail.id,
value: sailClone,
};
});
values.push({
path: 'sails.area.total',
value: totalArea,
});
values.push({
path: 'sails.area.active',
value: activeArea,
});
app.handleMessage(pluginId, {
updates: [
{
values: values
}
]
});
if (activeArea > 0) {
app.setPluginStatus(`${activeArea}m2 sail area active with ${activeSails.join(', ')}`);
} else {
app.setPluginStatus('No sails set as active.');
}
}
timer = setInterval(setDeltas, props.deltaInterval * 1000);
setDeltas();
debug("started");
};
plugin.stop = function() {
debug("stopping");
timer && clearTimeout(timer);
debug("stopped");
};
plugin.id = pluginId;
plugin.name = "Sails Configuration";
plugin.description =
"Plugin that allows you to define your vessel's sails inventory and configuration";
plugin.schema = {
type: "object",
required: ["deltaInterval"],
properties: {
deltaInterval: {
title: 'How often should this plugin update the state, in seconds',
type: "number",
default: 60
},
sails: {
type: "array",
title: "Sail inventory",
items: {
type: "object",
required: ["id", "name", "type", "area"],
properties: {
id: {
type: "string",
title: "Id",
pattern: "(^[a-zA-Z0-9]+$)",
},
name: {
type: "string",
title: "Name"
},
description: {
type: "string",
title: "Description",
'ui:widget': 'textarea',
},
type: {
type: "string",
title: "Sail type",
enum: [
"staysail",
"headsail",
"jib",
"genoa",
"spinnaker",
"gennaker",
"mainsail",
"lug",
"mizzen",
"steadying sail",
],
},
material: {
type: 'string',
title: 'Material',
},
brand: {
type: 'string',
title: 'Brand',
},
active: {
type: 'boolean',
title: 'Whether the sail is currently in use',
default: false,
},
area: {
type: "number",
title: "Sail area in square meters"
},
minimumWind: {
type: "number",
title: "The minimum wind speed this sail can be used with, in m/s",
},
maximumWind: {
type: "number",
title: "The maximum wind speed this sail can be used with, in m/s",
},
reefs: {
type: "array",
title: "Reefed sail areas",
description: "In descending order, leave empty if no fixed reefs",
items: {
type: "number",
}
},
continuousReefing: {
type: "boolean",
title: "The sail can be reefed continuously, with no discreet steps"
},
reducedState: {
title: "Reefing state",
type: 'object',
properties: {
reduced: {
type: 'boolean',
title: 'Whether the sail is reduced or not',
},
reefs: {
type: 'number',
title: 'Number of reefs set, 0 means full',
default: 0,
},
furledRatio: {
type: 'number',
title: 'Ratio of sail reduction, 0 means full and 1 is completely furled in',
default: 0,
},
},
},
}
}
}
}
};
return plugin;
};