Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add support to enable/disable track recording via a PUT #72

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* limitations under the License.
*/

const { deltaToPointsConverter, influxClientP, pruneTimestamps } = require('./skToInflux')
const { deltaToPointsConverter, influxClientP, pruneTimestamps, enableRecordTrack } = require('./skToInflux')

import {registerHistoryApiRoute} from './HistoryAPI'

Expand Down Expand Up @@ -381,6 +381,20 @@ module.exports = function (app) {
clearInterval(pruneInterval)
})

sendEnableRecordTrack(app, options.recordTrack)
app.registerPutHandler(
'vessels.self',
'navigation.trackRecordingEnabled.state',
(context, path, value, cb) => {
enableRecordTrack(value ? true : false)
sendEnableRecordTrack(app, value)
return {
state: 'SUCCESS',
statusCode: 200
}
}
)

registerHistoryApiRoute(app, clientP, app.selfId, app.debug)
},
stop: function () {
Expand Down Expand Up @@ -524,4 +538,21 @@ function endTimeExpression(timespanValue, offsetValue) {
let tKey= timespanValue.slice(-1) // timespan Y value
return `now() - ${Math.abs( Number(offsetValue) ) + influxDurationKeys[ tKey ]}`
}
}
}

function sendEnableRecordTrack(app, value)
{
let delta = {
updates: [
{
values: [
{
path: 'navigation.trackRecordingEnabled.state',
value: value ? 1 : 0
}
]
}
]
}
app.handleMessage('signalk-to-influxdb', delta);
}
7 changes: 6 additions & 1 deletion src/skToInflux.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const { getSourceId } = require('@signalk/signalk-schema')

var lastUpdates = {}
var lastPositionStored = {}
var recordTrackEnabled = false

function addSource(update, tags) {
if (update['$source']) {
Expand All @@ -39,6 +40,7 @@ module.exports = {
storeOthers,
honorDeltaTimestamp = true
) => {
recordTrackEnabled = recordTrack
return delta => {

if (delta.context === 'vessels.self') {
Expand All @@ -55,7 +57,7 @@ module.exports = {
update.values.reduce((acc, pathValue) => {

if (pathValue.path === 'navigation.position') {
if (recordTrack && shouldStorePositionNow(delta, tags.source, time)) {
if (recordTrackEnabled && shouldStorePositionNow(delta, tags.source, time)) {
const point = {
measurement: pathValue.path,
tags: tags,
Expand Down Expand Up @@ -179,6 +181,9 @@ module.exports = {
pruneTimestamps(maxAge) {
clearContextTimestamps(lastUpdates, maxAge)
clearContextTimestamps(lastPositionStored, maxAge)
},
enableRecordTrack(enabled) {
recordTrackEnabled = enabled
}
}

Expand Down