-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
223 lines (184 loc) · 5.56 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
const fs = require('fs');
const fsp = require('fs').promises;
module.exports = function (app) {
let unsubscribes = []
let fuelUsage = {}
let record = {}
let updateDeltaInterval
let saveFuelUsageInterval
let updateDelta = function(record) {
for (const [key, value] of Object.entries(record)) {
timeTrigger=Date.now()-record[key].fuelUsedTime
if (timeTrigger<=5000) {
usedPath=key.split('.')
usedPath[3]='used'
usedPath=usedPath.join('.')
app.handleMessage(plugin.id, {
updates: [
{
values: [
{
path: usedPath,
value: value.fuelUsed
}
]
}
]
})
}
}
}
let _localSubscription = function(options) {
const subscribeArray = []
options.paths.forEach(path => {
const subscribe = {}
subscribe.path = path
subscribe.policy = "instant"
subscribeArray.push(subscribe)
})
return (localSubscription = {
"context" : "vessels.self",
"subscribe" : subscribeArray
})
}
//needs to receive an array for pathArray
let _convertRateToUsed = function (pathArray) {
const usedArray = []
pathArray.forEach(path => {
splitRate = path.split('.')
splitRate[3] = 'used'
splitRate=splitRate.join('.')
usedArray.push(splitRate)
})
return usedArray
}
let _loadFuelUsage = function(options) {
const usage = {}
const usedPath = _convertRateToUsed(options.paths)
if (options.savedUsage) {
usedPath.forEach(path => {
if (options.savedUsage[path] === undefined) {
options.savedUsage[path] = 0
}
else {
record[path]={'fuelUsed':options.savedUsage[path]}
}
})
}
else {
options.savedUsage = {}
usedPath.forEach(path => {
options.savedUsage[path] = 0
})
}
_saveFuelUsage(options)
return options
}
let _saveFuelUsage = function(options) {
app.savePluginOptions(options, () => {
app.debug(`Fuel Used: ${JSON.stringify(options.savedUsage)}`)
});
}
let fuelCalc = function (options,record,path,value,timestamp) {
//app.debug(`record is now ${JSON.stringify(record)}`)
if (record[path]['firstTime'] == undefined) {
app.debug(`${record[path]} is undefined so creating the first timestamp'`)
record[path]['firstTime']={'firstTime':timestamp}
}
if (record[path].firstTime) {
record[path].secondTime=timestamp
var elapsedTime=record[path].secondTime-record[path].firstTime
//app.debug(`elapsedTime is ${elapsedTime}`)
if (elapsedTime!= 0) {
if (elapsedTime <= options.timeout) {
const ratio=1000/elapsedTime
const instantFuelUsage=value/ratio
//app.debug(`instantFuelUsage: ${instantFuelUsage}`)
if (record[path].fuelUsed == undefined) {
record[path].fuelUsed=instantFuelUsage
record[path].fuelUsedTime=Date.now()
}
if (record[path].fuelUsed != undefined) {
record[path].fuelUsed+=instantFuelUsage
record[path].fuelUsedTime=Date.now()
}
options.savedUsage[path]=record[path].fuelUsed
//app.debug(JSON.stringify(options))
//_saveFuelUsage(options)
}
record[path].firstTime=timestamp
}
}
}
let _start = function(options) {
app.debug(`${plugin.name} Started...`)
fuelUsage = _loadFuelUsage(options).savedUsage
app.debug(fuelUsage)
updateDeltaInterval = setInterval(function() {
updateDelta(record);
}, 1000);
saveFuelUsageInterval = setInterval(function() {
_saveFuelUsage(options);
}, options.saveFreq);
app.subscriptionmanager.subscribe(
_localSubscription(options),
unsubscribes,
subscriptionError => {
app.error('Error:' + subscriptionError);
},
delta => {
delta.updates.forEach(u => {
fuelCalc(options,record,_convertRateToUsed([u.values[0].path]),u.values[0].value,Date.parse(u.timestamp))
//app.debug(u);
});
}
);
}
let _stop = function(options) {
app.debug(`${plugin.name} Stopped...`)
unsubscribes.forEach(f => f());
unsubscribes = [];
if (updateDeltaInterval) {
clearInterval(updateDeltaInterval);
}
// clean up the state
updateDeltaInterval = undefined;
if (saveFuelUsageInterval) {
clearInterval(saveFuelUsageInterval);
}
// clean up the state
saveFuelUsageInterval = undefined;
}
const plugin = {
id: 'fuel-usage-calculator',
name: 'Fuel Usage Calculator',
description: 'A Signalk plugin to calculate your fuel usage based on propulsion.*.fuel.rate',
schema: {
type: 'object',
required: ['paths', 'saveFreq'],
properties: {
paths: {
type: 'array',
title: 'Paths to use for fuel calculations',
default: ['propulsion.port.fuel.rate','propulsion.starboard.fuel.rate'],
items: {
type: 'string'
}
},
saveFreq: {
type: 'number',
title: 'How often to save the fuel used to disk',
default: 15000
},
timeout: {
type: 'number',
title: 'How long until timeout',
default: 10000
}
}
},
start: _start,
stop: _stop
}
return plugin;
}