-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
245 lines (232 loc) · 6.64 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
const awsIot = require('aws-iot-device-sdk');
module.exports = (app) => {
const plugin = {};
let device;
let unsubscribes = [];
let state = 'default';
let connected = false;
const setStatus = app.setPluginStatus || app.setProviderStatus;
const setError = app.setPluginError || app.setProviderError;
plugin.id = 'signalk-aws-iot';
plugin.name = 'SignalK AWS IoT';
plugin.description = 'Plugin that sends data to AWS IoT Core';
function isPublishState(options) {
const sendStates = options.send_states || [];
if (sendStates.indexOf(state) === -1) {
return false;
}
return true;
}
function setState(newState, options) {
if (newState === state) {
return;
}
state = newState;
if (!connected) {
// Since we're offline we get better provider state messages
// via connection handling
return;
}
if (isPublishState(options)) {
setStatus('Connected to AWS IoT Core and sending data');
} else {
setStatus(`Connected to AWS IoT Core. Not sending data due to "${state}" state`);
}
}
function sendValue(v, options) {
if (v.path === 'navigation.state') {
setState(v.value, options);
} else if (!isPublishState(options)) {
// Not in a state where we want to send stuff
return;
}
const topic = options.single_topic ? 'signalk' : v.path.replace(/\./g, '/');
app.debug(`PUB ${topic} ${JSON.stringify(v.value)}`);
device.publish(topic, JSON.stringify(v.value));
}
plugin.start = (options) => {
// Here we put our plugin logic
app.debug('Plugin started');
setStatus('Initializing');
device = awsIot.device({
host: options.aws_host,
clientId: options.aws_client_id,
privateKey: Buffer.from(options.aws_key || '', 'utf8'),
clientCert: Buffer.from(options.aws_cert || '', 'utf8'),
caCert: Buffer.from(options.aws_ca || '', 'utf8'),
});
const localSubscription = {
context: 'vessels.self',
subscribe: [{
path: '*',
period: (options.send_interval || 10) * 1000,
}],
};
app.subscriptionmanager.subscribe(
localSubscription,
unsubscribes,
(subscriptionError) => {
app.error(subscriptionError);
},
(delta) => {
if (!delta.updates) {
return;
}
delta.updates.forEach((u) => {
if (!u.values) {
return;
}
u.values.forEach((v) => sendValue(v, options));
});
},
);
device
.on('connect', () => {
setStatus('Connected to AWS IoT Core');
connected = true;
});
device
.on('reconnect', () => {
setStatus('Reconnecting to AWS IoT Core');
});
device
.on('offline', () => {
setStatus('Offline');
connected = false;
});
device
.on('close', () => {
setStatus('Connection closed');
connected = false;
});
device
.on('error', (error) => {
app.error(error);
setError(error.message);
});
};
plugin.stop = () => {
unsubscribes.forEach((f) => f());
unsubscribes = [];
if (device) {
device.end(true);
}
app.debug('Plugin stopped');
};
plugin.schema = {
type: 'object',
required: [
'aws_host',
'aws_client_id',
'aws_key',
'aws_cert',
],
properties: {
aws_host: {
type: 'string',
format: 'hostname',
title: 'AWS IoT endpoint hostname',
},
aws_client_id: {
type: 'string',
title: 'AWS IoT client ID (must match Thing name)',
},
aws_key: {
type: 'string',
title: 'AWS IoT device private key',
},
aws_cert: {
type: 'string',
title: 'AWS IoT device certificate',
},
aws_ca: {
type: 'string',
title: 'AWS IoT certificate authority',
default: `-----BEGIN CERTIFICATE-----
MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF
ADA5MQswCQYDVQQGEwJVUzEPMA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Send data to AWS IoT when vessel is in the following states',
items: {
type: 'string',
enum: [
'default',
'not under command',
'anchored',
'moored',
'sailing',
'motoring',
'towing < 200m',
'towing > 200m',
'pushing',
'fishing',
'fishing-hampered',
'trawling',
'trawling-shooting',
'trawling-hauling',
'pilotage',
'not-under-way',
'aground',
'restricted manouverability',
'restricted manouverability towing < 200m',
'restricted manouverability towing > 200m',
'restricted manouverability underwater operations',
'constrained by draft',
'mine clearance',
],
},
default: [
'default',
'sailing',
'motoring',
'anchored',
],
uniqueItems: true,
},
send_interval: {
type: 'number',
title: 'How often to send data, in seconds',
default: 10,
},
single_topic: {
type: 'boolean',
title: 'Publish all paths to a single topic',
default: false,
},
},
};
plugin.uiSchema = {
aws_key: {
'ui:widget': 'textarea',
},
aws_cert: {
'ui:widget': 'textarea',
},
aws_ca: {
'ui:widget': 'textarea',
},
send_states: {
'ui:widget': 'checkboxes',
},
};
return plugin;
};