diff --git a/calcs/cpa_tcpa.js b/calcs/cpa_tcpa.js index f96a8fb..a1f4815 100755 --- a/calcs/cpa_tcpa.js +++ b/calcs/cpa_tcpa.js @@ -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', @@ -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' ) @@ -155,27 +199,6 @@ 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' ) @@ -183,6 +206,21 @@ module.exports = function (app, plugin) { '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 = {