signalk-usage 0.1.0 → 0.1.2
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 +1 -1
- package/plugin/index.js +3 -3
- package/plugin/lib/publisher.js +14 -12
- package/plugin/lib/schema.js +1 -1
package/package.json
CHANGED
package/plugin/index.js
CHANGED
|
@@ -6,7 +6,7 @@ const Publisher = require('./lib/publisher');
|
|
|
6
6
|
module.exports = function (app) {
|
|
7
7
|
let plugin = {
|
|
8
8
|
id: 'signalk-usage',
|
|
9
|
-
name: 'SignalK
|
|
9
|
+
name: 'SignalK-Usage',
|
|
10
10
|
description: 'Report Electrical and Tank Usage',
|
|
11
11
|
schema: schema,
|
|
12
12
|
|
|
@@ -18,7 +18,7 @@ module.exports = function (app) {
|
|
|
18
18
|
|
|
19
19
|
plugin.start = function (options, restartPlugin) {
|
|
20
20
|
try {
|
|
21
|
-
app.debug('Starting SignalK
|
|
21
|
+
app.debug('Starting SignalK-Usage plugin');
|
|
22
22
|
|
|
23
23
|
// Validate configuration
|
|
24
24
|
if (!options.influx) {
|
|
@@ -95,7 +95,7 @@ module.exports = function (app) {
|
|
|
95
95
|
|
|
96
96
|
plugin.stop = function () {
|
|
97
97
|
try {
|
|
98
|
-
app.debug('Stopping SignalK
|
|
98
|
+
app.debug('Stopping SignalK-Usage plugin');
|
|
99
99
|
|
|
100
100
|
if (plugin.updateTimer) {
|
|
101
101
|
clearInterval(plugin.updateTimer);
|
package/plugin/lib/publisher.js
CHANGED
|
@@ -19,9 +19,10 @@ Publisher.prototype.start = function() {
|
|
|
19
19
|
}, interval);
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
-
Publisher.prototype.round = function(value) {
|
|
23
|
-
// Round to
|
|
24
|
-
|
|
22
|
+
Publisher.prototype.round = function(value, decimalPlaces = 1) {
|
|
23
|
+
// Round to specified decimal places (default 1)
|
|
24
|
+
const multiplier = Math.pow(10, decimalPlaces);
|
|
25
|
+
return Math.round(value * multiplier) / multiplier;
|
|
25
26
|
};
|
|
26
27
|
|
|
27
28
|
Publisher.prototype.publish = function() {
|
|
@@ -100,10 +101,10 @@ Publisher.prototype.publishTankageItems = function(items, deltas, meta) {
|
|
|
100
101
|
return;
|
|
101
102
|
}
|
|
102
103
|
|
|
103
|
-
// Publish
|
|
104
|
+
// Publish totals with 1 decimal place
|
|
104
105
|
deltas.push({
|
|
105
106
|
path: `${basePath}.consumed.${period}`,
|
|
106
|
-
value: this.round(periodData.consumed || 0)
|
|
107
|
+
value: this.round(periodData.consumed || 0, 1)
|
|
107
108
|
});
|
|
108
109
|
meta.push({
|
|
109
110
|
path: `${basePath}.consumed.${period}`,
|
|
@@ -112,16 +113,17 @@ Publisher.prototype.publishTankageItems = function(items, deltas, meta) {
|
|
|
112
113
|
|
|
113
114
|
deltas.push({
|
|
114
115
|
path: `${basePath}.added.${period}`,
|
|
115
|
-
value: this.round(periodData.added || 0)
|
|
116
|
+
value: this.round(periodData.added || 0, 1)
|
|
116
117
|
});
|
|
117
118
|
meta.push({
|
|
118
119
|
path: `${basePath}.added.${period}`,
|
|
119
120
|
value: { units: unit }
|
|
120
121
|
});
|
|
121
122
|
|
|
123
|
+
// Publish rates with 4 decimal places (more precision needed)
|
|
122
124
|
deltas.push({
|
|
123
125
|
path: `${basePath}.consumptionRate.${period}`,
|
|
124
|
-
value: this.round(periodData.consumptionRate || 0)
|
|
126
|
+
value: this.round(periodData.consumptionRate || 0, 4)
|
|
125
127
|
});
|
|
126
128
|
meta.push({
|
|
127
129
|
path: `${basePath}.consumptionRate.${period}`,
|
|
@@ -176,12 +178,12 @@ Publisher.prototype.publishPowerItems = function(items, deltas, meta) {
|
|
|
176
178
|
// Skip if no energy data
|
|
177
179
|
if (!periodData.energy) return;
|
|
178
180
|
|
|
179
|
-
// Publish
|
|
181
|
+
// Publish energy values with 1 decimal place
|
|
180
182
|
if (directionality === 'producer') {
|
|
181
183
|
// Only publish generated
|
|
182
184
|
deltas.push({
|
|
183
185
|
path: `${basePath}.${generatedLabel}.${period}`,
|
|
184
|
-
value: this.round(periodData.energy.generatedWh || 0)
|
|
186
|
+
value: this.round(periodData.energy.generatedWh || 0, 1)
|
|
185
187
|
});
|
|
186
188
|
meta.push({
|
|
187
189
|
path: `${basePath}.${generatedLabel}.${period}`,
|
|
@@ -191,7 +193,7 @@ Publisher.prototype.publishPowerItems = function(items, deltas, meta) {
|
|
|
191
193
|
// Only publish consumed
|
|
192
194
|
deltas.push({
|
|
193
195
|
path: `${basePath}.${consumedLabel}.${period}`,
|
|
194
|
-
value: this.round(periodData.energy.consumedWh || 0)
|
|
196
|
+
value: this.round(periodData.energy.consumedWh || 0, 1)
|
|
195
197
|
});
|
|
196
198
|
meta.push({
|
|
197
199
|
path: `${basePath}.${consumedLabel}.${period}`,
|
|
@@ -201,7 +203,7 @@ Publisher.prototype.publishPowerItems = function(items, deltas, meta) {
|
|
|
201
203
|
// Bidirectional or auto-detected - publish both
|
|
202
204
|
deltas.push({
|
|
203
205
|
path: `${basePath}.${consumedLabel}.${period}`,
|
|
204
|
-
value: this.round(periodData.energy.consumedWh || 0)
|
|
206
|
+
value: this.round(periodData.energy.consumedWh || 0, 1)
|
|
205
207
|
});
|
|
206
208
|
meta.push({
|
|
207
209
|
path: `${basePath}.${consumedLabel}.${period}`,
|
|
@@ -210,7 +212,7 @@ Publisher.prototype.publishPowerItems = function(items, deltas, meta) {
|
|
|
210
212
|
|
|
211
213
|
deltas.push({
|
|
212
214
|
path: `${basePath}.${generatedLabel}.${period}`,
|
|
213
|
-
value: this.round(periodData.energy.generatedWh || 0)
|
|
215
|
+
value: this.round(periodData.energy.generatedWh || 0, 1)
|
|
214
216
|
});
|
|
215
217
|
meta.push({
|
|
216
218
|
path: `${basePath}.${generatedLabel}.${period}`,
|
package/plugin/lib/schema.js
CHANGED