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

Fix: other vessels data updating beyond timeout for their data #151

Merged
merged 3 commits into from
Feb 21, 2025
Merged
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
80 changes: 59 additions & 21 deletions calcs/cpa_tcpa.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@ var alarmSent = []
var notificationLevels = ['normal', 'alert', 'warn', 'alarm', 'emergency']

module.exports = function (app, plugin) {

const secondsSinceVesselUpdate = (vessel, path) => {
const _vesselTimestamp = app.getPath(
'vessels.' + vessel + '.' + path
)
if (!_vesselTimestamp) {
return Date.now() / 1000
}
const vesselTimestamp = new Date(_vesselTimestamp).getTime()

let currentTime
const currentTimeString = app.getSelfPath('navigation.datetime.value')
if (currentTimeString) {
currentTime = new Date(currentTimeString).getTime()
} else {
currentTime = Date.now()
}

return Math.floor(
(currentTime - vesselTimestamp) / 1e3
)
}

return {
group: 'traffic',
optionKey: 'CPA',
Expand Down Expand Up @@ -116,6 +139,27 @@ module.exports = function (app, plugin) {
if (typeof vessel === 'undefined' || vessel == app.selfId) {
continue
}

if (secondsSinceVesselUpdate(vessel, 'navigation.position.timestamp') > plugin.properties.traffic.timelimit) {
app.debug('old position of vessel, not calculating')
if (app.getPath(
'vessels.' + vessel + '.navigation.distanceToSelf.value'
) !== null) {
deltas.push({
context: 'vessels.' + vessel,
updates: [
{
values: [CPA_TCPA(null, null), {
path: 'navigation.distanceToSelf',
value: null
}]
}
]
})
}
continue
} // old data from vessel, not calculating

var vesselPos = app.getPath(
'vessels.' + vessel + '.navigation.position.value'
)
Expand Down Expand Up @@ -155,34 +199,28 @@ module.exports = function (app, plugin) {
continue
} // if distance outside range, don't calculate

var vesselTimestamp = app.getPath(
'vessels.' + vessel + '.navigation.position.timestamp'
)
vesselTimestamp = new Date(vesselTimestamp).getTime()

var currentTime
var currentTimeString = app.getSelfPath('navigation.datetime.value')
if (currentTimeString) {
currentTime = new Date(currentTimeString).getTime()
} else {
currentTime = Date.now()
}

var secondsSinceVesselUpdate = Math.floor(
(currentTime - vesselTimestamp) / 1e3
)
if (secondsSinceVesselUpdate > plugin.properties.traffic.timelimit) {
app.debug('old data from vessel, not calculating')
continue
} // old data from vessel, not calculating

var vesselCourse = app.getPath(
'vessels.' + vessel + '.navigation.courseOverGroundTrue.value'
)
var vesselSpeed = app.getPath(
'vessels.' + vessel + '.navigation.speedOverGround.value'
)

if (secondsSinceVesselUpdate(vessel, 'navigation.courseOverGroundTrue') > plugin.properties.traffic.timelimit ||
secondsSinceVesselUpdate(vessel, 'navigation.speedOverGround') > plugin.properties.traffic.timelimit) {
app.debug('old course data from vessel, not calculating CPA')
if (vesselCourse !== null || vesselSpeed !== null) {
deltas.push({
context: 'vessels.' + vessel,
updates: [
{
values: [CPA_TCPA(null, null)]
}
]
})
}
continue
}
if (!_.isUndefined(vesselCourse) && !_.isUndefined(vesselSpeed)) {
var vesselCourseDeg = geoutils.radToDeg(vesselCourse)
var otherVessel = {
Expand Down