-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
133 lines (114 loc) · 3.61 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
const Ultrasonic = require('./lib/calypso-ultrasonic')
module.exports = function signalkCalypsoUltrasonic (app) {
const plugin = {}
let _ultrasonic = null
plugin.STATUS = {
retrying: 'Retrying to connect, retry',
searching: 'Searching for Ultrasonic',
found_ultrasonic: 'Found Ultrasonic',
connecting: 'Connecting...',
connected: 'Connected to Ultrasonic',
received_characteristic: 'Received characteristic',
subscribed_to_dataservice: 'Subscribed to data service',
sleeping: 'Ultrasonic in sleep mode...'
}
plugin.Ultrasonic = Ultrasonic
plugin.id = 'calypso-ultrasonic'
plugin.name = 'Calypso Ultrasonic plugin for Signal K'
plugin.description = plugin.name
plugin.schema = {
type: 'object',
required: [
'setRate',
'setCompass',
'maxRetries',
'turnOnHour',
'turnOffHour'
],
properties: {
setRate: {
type: 'number',
title: 'Update rate (Hz)',
default: 1
},
setCompass: {
type: 'number',
title: 'Compass/9-DOF sensor state',
default: 0
},
maxRetries: {
type: 'number',
title: 'Max. number of connection retries (0 = infinite)',
default: 0
},
enableSleep: {
type: 'number',
title: 'Enable automatic sleeping of the plugin during off-hours (experimental)',
default: 0
},
turnOnHour: {
type: 'number',
title: 'Hour of the day when the Ultrasonic should be awake (experimental; e.g. 6 => 6am)',
default: 7
},
turnOffHour: {
type: 'number',
title: 'Hour of the day when the Ultrasonic should go to sleep (experimental; e.g. 21 => 9pm)',
default: 20
}
// @TODO add setAngleOffset & setWindSpeedCorrection options
}
}
plugin.start = function (options) {
if (_ultrasonic !== null) {
plugin.stop()
}
const opts = {
setRate: false,
setCompass: false,
maxRetries: Infinity,
turnOffHour: 20,
turnOnHour: 7
}
if (options && typeof options === 'object') {
if (options.hasOwnProperty('setRate') && (options.setRate === 1 || options.setRate === 4 || options.setRate === 8)) {
opts.setRate = options.setRate
}
if (options.hasOwnProperty('setCompass') && (options.setCompass === 1 || options.setCompass === 0)) {
opts.setCompass = options.setCompass
}
if (options.hasOwnProperty('enableSleep') && (options.enableSleep === 1 || options.enableSleep === 0)) {
opts.sleep = (options.enableSleep === 1)
}
if (options.hasOwnProperty('maxRetries') && !isNaN(options.maxRetries)) {
opts.setCompass = options.maxRetries > 0 ? options.maxRetries : Infinity
}
if (options.hasOwnProperty('turnOffHour') && !isNaN(options.turnOffHour)) {
opts.turnOffHour = options.turnOffHour
}
if (options.hasOwnProperty('turnOnHour') && !isNaN(options.turnOnHour)) {
opts.turnOnHour = options.turnOnHour
}
}
_ultrasonic = new plugin.Ultrasonic(opts)
_ultrasonic.on('delta', delta => {
// console.log('SENDING DELTA', JSON.stringify(delta))
app.handleMessage(plugin.id, delta)
})
_ultrasonic.on('status', status => {
app.setProviderStatus(`${plugin.STATUS[status.status]}${status.data === '' ? '' : `: ${status.data}`}`)
})
_ultrasonic.start()
}
plugin.stop = function () {
if (_ultrasonic === null) {
return
}
// Tear-down
_ultrasonic.disconnect()
_ultrasonic.stop()
_ultrasonic.removeAllListeners()
_ultrasonic = null
}
return plugin
}