signalk-polar-performance-plugin 0.0.35 → 0.0.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "signalk-polar-performance-plugin",
3
- "version": "0.0.35",
3
+ "version": "0.0.36",
4
4
  "description": "A plugin that calculates performance information based on a (CSV) polar diagram.",
5
5
  "main": "plugin/index.js",
6
6
  "scripts": {
package/plugin/index.js CHANGED
@@ -195,6 +195,10 @@ module.exports = function (app) {
195
195
  function sendUpdates (perfObj) {
196
196
  let values = []
197
197
  let metas = []
198
+ values.push({path: 'performance.boatSpeedDamped', value: roundDec(BSP)})
199
+ metas.push({path: 'performance.boatSpeedDamped', value: {"units": "m/s"}})
200
+ values.push({path: 'environment.wind.angleTrueWaterDamped', value: roundDec(TWA)})
201
+ metas.push({path: 'environment.wind.angleTrueWaterDamped', value: {"units": "rad"}})
198
202
  if (typeof perfObj.beatAngle != 'undefined') {
199
203
  if (options.beatAngle == true) {
200
204
  values.push({path: 'performance.beatAngle', value: roundDec(perfObj.beatAngle)})
package/public/index.html CHANGED
@@ -8,13 +8,51 @@
8
8
  <canvas id="myChart" style="width:100%;max-width:1200px"></canvas>
9
9
 
10
10
  <script>
11
+
12
+ var polarSpeed, boatSpeed, TWA
13
+ var myChart, chartData
14
+
11
15
  $.getJSON("/plugins/signalk-polar-performance-plugin/chartData", function(json) {
12
- showChart(json)
16
+
17
+ // Add actual values dots
18
+ json.datasets.unshift({
19
+ label: 'Polar Speed',
20
+ backgroundColor: 'rgba(0,255,0,0.7)',
21
+ borderColor: 'rgba(0,255,0,0.2)',
22
+ borderWidth: 1,
23
+ radius: 30,
24
+ type: 'bubble',
25
+ data: [
26
+ {
27
+ y: 0,
28
+ x: 0,
29
+ r: 10
30
+ }
31
+ ]
32
+ },
33
+ {
34
+ label: 'Boat Speed',
35
+ backgroundColor: 'rgba(0,150,255,0.7)',
36
+ borderColor: 'rgba(0,150,255,0.2)',
37
+ borderWidth: 1,
38
+ radius: 30,
39
+ type: 'bubble',
40
+ data: [
41
+ {
42
+ y: 0,
43
+ x: 0,
44
+ r: 10
45
+ }
46
+ ]
47
+ })
48
+
49
+ chartData = json
50
+ showChart(chartData)
13
51
  })
14
52
 
15
53
  function showChart (data) {
16
54
  console.log(data)
17
- new Chart("myChart", {
55
+ myChart = new Chart("myChart", {
18
56
  type: "scatter",
19
57
  data: data,
20
58
  options: {
@@ -104,5 +142,92 @@ function showChart (data) {
104
142
  ctx.style.backgroundColor = 'rgba(0,0,0,0.8)'
105
143
  ctx.style.fontColor = 'white'
106
144
  }
145
+
146
+
147
+ function connect () {
148
+ console.log("connect()")
149
+ ws = new WebSocket((window.location.protocol === 'https:' ? 'wss' : 'ws') + "://" + window.location.host + "/signalk/v1/stream?subscribe=none");
150
+ ws.onopen = function() {
151
+
152
+ // Start listening for value updates
153
+ startListeners();
154
+
155
+ ws.onmessage = function(event) {
156
+ if (event.data.includes('signalk-server')) {
157
+ welcomeMessage = event.data;
158
+ console.log("Skipping welcome message: " + welcomeMessage)
159
+ } else {
160
+ handleData(JSON.parse(event.data));
161
+ }
162
+ }
163
+
164
+ ws.onclose = function() {
165
+ console.log("WebSocket closed")
166
+ }
167
+
168
+ ws.onerror = function(err) {
169
+ console.log("WebSocket connection error: " + err.message + " - closing connection");
170
+ }
171
+
172
+ }
173
+ }
174
+
175
+ function startListeners () {
176
+ var paths = [{'path': 'environment.wind.angleTrueWaterDamped'}, {'path': 'performance.polarSpeed'}, {'path': 'performance.boatSpeedDamped'}]
177
+
178
+ var subscriptionObject = {
179
+ "context": "vessels.self",
180
+ "policy" : "ideal",
181
+ "minPeriod": 2000,
182
+ "subscribe": paths
183
+ }
184
+
185
+ var subscriptionMessage = JSON.stringify(subscriptionObject);
186
+ console.log("subscriptionMessage: " + subscriptionMessage);
187
+ ws.send(subscriptionMessage);
188
+ }
189
+
190
+ function handleData (data) {
191
+ if (typeof data.updates[0].meta != 'undefined') {
192
+ return
193
+ }
194
+ var path = data.updates[0].values[0].path
195
+ var value = data.updates[0].values[0].value
196
+
197
+ if (path == 'performance.polarSpeed') {
198
+ polarSpeed = msToKts(value)
199
+ // console.log('polarSpeed set to %s', polarSpeed)
200
+ chartData.datasets[0].data[0].y = polarSpeed
201
+ } else if (path == 'environment.wind.angleTrueWaterDamped') {
202
+ TWA = radToDeg(value)
203
+ // console.log('TWA set to %s', TWA)
204
+ chartData.datasets[0].data[0].x = TWA
205
+ chartData.datasets[1].data[0].x = TWA
206
+ } else if (path == 'performance.boatSpeedDamped') {
207
+ boatSpeed = msToKts(value)
208
+ // console.log('boatSpeed set to %s', boatSpeed)
209
+ chartData.datasets[1].data[0].y = boatSpeed
210
+ }
211
+ }
212
+
213
+
214
+ connect()
215
+
216
+ setInterval(updateChart, 300)
217
+
218
+ function updateChart () {
219
+ myChart.update()
220
+ }
221
+
222
+ function radToDeg(radians) {
223
+ return radians * 180 / Math.PI
224
+ }
225
+
226
+ function msToKts(ms) {
227
+ return ms * 1.94384
228
+ }
229
+
230
+
231
+
107
232
  </script>
108
233