From 9318f3d93580115388ac45a69c560a63cff41ca8 Mon Sep 17 00:00:00 2001 From: Scott Bender Date: Mon, 27 Aug 2018 16:28:04 -0400 Subject: [PATCH] chore: use prettier-standard to format code --- calcs/airDensity.js | 26 ++++---- calcs/batteryPower.js | 19 ++++-- calcs/depthBelowKeel.js | 26 ++++---- calcs/depthBelowKeel2.js | 27 ++++++--- calcs/depthBelowSurface.js | 24 +++++--- calcs/dewPoint.js | 24 ++++---- calcs/fuelConsumtion.js | 21 +++++-- calcs/groundWind.js | 31 ++++++---- calcs/headingTrue.js | 23 ++++---- calcs/propslip.js | 39 +++++++++--- calcs/suncalc.js | 44 +++++++------- calcs/tankVolume.js | 82 +++++++++++++------------ calcs/trueWind.js | 46 +++++++++------ calcs/vmg.js | 29 +++++---- calcs/vmg_wind.js | 31 ++++++---- calcs/windChill.js | 38 +++++++----- calcs/windDirection.js | 51 ++++++++-------- calcs/windDirectionMagnetic.js | 28 +++++---- calcs/windShift.js | 105 ++++++++++++++++----------------- package.json | 20 +++++++ 20 files changed, 431 insertions(+), 303 deletions(-) diff --git a/calcs/airDensity.js b/calcs/airDensity.js index 6f93179..6b860ce 100644 --- a/calcs/airDensity.js +++ b/calcs/airDensity.js @@ -1,19 +1,21 @@ - -module.exports = function(app, plugin) { +module.exports = function (app, plugin) { return { group: 'air', optionKey: 'Air density', - title: "Outside air density (based on humidity, temperature and pressure)", - derivedFrom: [ "environment.outside.temperature", "environment.outside.humidity", "environment.outside.pressure" ], - calculator: function(temp, hum, press) { + title: 'Outside air density (based on humidity, temperature and pressure)', + derivedFrom: [ + 'environment.outside.temperature', + 'environment.outside.humidity', + 'environment.outside.pressure' + ], + calculator: function (temp, hum, press) { var tempC = temp + 273.16 - var psat = 6.1078 * 10^(7.5 * tempC / (tempC + 237.3)) //hPa - var pv = hum * psat/100 //vapour pressure of water - var pd = press - pv //dry air pressure - var airDensity = (pd/(287.058*temp)) + (pv/(461.495*temp))//https://en.wikipedia.org/wiki/Density_of_air#cite_note-wahiduddin_01-15 - + var psat = (6.1078 * 10) ^ (7.5 * tempC / (tempC + 237.3)) // hPa + var pv = hum * psat / 100 // vapour pressure of water + var pd = press - pv // dry air pressure + var airDensity = pd / (287.058 * temp) + pv / (461.495 * temp) // https://en.wikipedia.org/wiki/Density_of_air#cite_note-wahiduddin_01-15 - return [{ path: "environment.outside.airDensity", value: airDensity}] + return [{ path: 'environment.outside.airDensity', value: airDensity }] } - }; + } } diff --git a/calcs/batteryPower.js b/calcs/batteryPower.js index 5eaebd9..d466d35 100644 --- a/calcs/batteryPower.js +++ b/calcs/batteryPower.js @@ -1,15 +1,22 @@ const _ = require('lodash') -module.exports = function(app, plugin) { +module.exports = function (app, plugin) { return plugin.batteries.map(instance => { return { group: 'electrical', optionKey: 'batterPower' + instance, - title: "Battery " + instance + " Power ", - derivedFrom: function(){ return [ "electrical.batteries." + instance + ".voltage", "electrical.batteries." + instance + ".current" ] }, - calculator: function(v, a) { - return [{ path: "electrical.batteries." + instance + ".power", value: v*a }] + title: 'Battery ' + instance + ' Power ', + derivedFrom: function () { + return [ + 'electrical.batteries.' + instance + '.voltage', + 'electrical.batteries.' + instance + '.current' + ] + }, + calculator: function (v, a) { + return [ + { path: 'electrical.batteries.' + instance + '.power', value: v * a } + ] } } - }); + }) } diff --git a/calcs/depthBelowKeel.js b/calcs/depthBelowKeel.js index d5ab869..74033b9 100644 --- a/calcs/depthBelowKeel.js +++ b/calcs/depthBelowKeel.js @@ -1,22 +1,28 @@ const _ = require('lodash') -module.exports = function(app) { +module.exports = function (app) { var draft = app.getSelfPath('design.draft.maximum.value') - if ( ! draft ) { + if (!draft) { draft = app.getSelfPath('design.draft.value.maximum') } - var derivedFrom = typeof draft === 'undefined' ? [] : [ "environment.depth.belowSurface" ]; - + var derivedFrom = + typeof draft === 'undefined' ? [] : ['environment.depth.belowSurface'] + return { - group: "depth", + group: 'depth', optionKey: 'belowKeel', - title: "Depth Below Keel (based on depth.belowSurface and design.draft.maximum)", + title: + 'Depth Below Keel (based on depth.belowSurface and design.draft.maximum)', derivedFrom: derivedFrom, - calculator: function(depthBelowSurface) - { - return [{ path: 'environment.depth.belowKeel', value: depthBelowSurface - draft}] + calculator: function (depthBelowSurface) { + return [ + { + path: 'environment.depth.belowKeel', + value: depthBelowSurface - draft + } + ] } - }; + } } diff --git a/calcs/depthBelowKeel2.js b/calcs/depthBelowKeel2.js index 04d4fef..528efa8 100644 --- a/calcs/depthBelowKeel2.js +++ b/calcs/depthBelowKeel2.js @@ -1,17 +1,28 @@ const _ = require('lodash') -module.exports = function(app) { - var depthTransducerToKeel = app.getSelfPath('environment.depth.transducerToKeel.value') +module.exports = function (app) { + var depthTransducerToKeel = app.getSelfPath( + 'environment.depth.transducerToKeel.value' + ) - var derivedFrom = typeof depthTransducerToKeel === 'undefined' ? [] : [ "environment.depth.belowTransducer"]; + var derivedFrom = + typeof depthTransducerToKeel === 'undefined' + ? [] + : ['environment.depth.belowTransducer'] return { - group: "depth", + group: 'depth', optionKey: 'belowKeel_2', - title: "Depth Below Keel (based on depth.belowTransducer and depth.transducerToKeel)", + title: + 'Depth Below Keel (based on depth.belowTransducer and depth.transducerToKeel)', derivedFrom: derivedFrom, - calculator: function(depthBelowTransducer){ - return [{ path: 'environment.depth.belowKeel', value: depthBelowTransducer + depthTransducerToKeel}] + calculator: function (depthBelowTransducer) { + return [ + { + path: 'environment.depth.belowKeel', + value: depthBelowTransducer + depthTransducerToKeel + } + ] } - }; + } } diff --git a/calcs/depthBelowSurface.js b/calcs/depthBelowSurface.js index 1a169d7..f7d79bc 100644 --- a/calcs/depthBelowSurface.js +++ b/calcs/depthBelowSurface.js @@ -1,18 +1,24 @@ const _ = require('lodash') -module.exports = function(app) { +module.exports = function (app) { var draft = app.getSelfPath('design.draft.maximum.value') - var derivedFrom = typeof draft === 'undefined' ? [] : [ "environment.depth.belowKeel" ]; - + var derivedFrom = + typeof draft === 'undefined' ? [] : ['environment.depth.belowKeel'] + return { - group: "depth", + group: 'depth', optionKey: 'belowSurface', - title: "Depth Below Surface (based on depth.belowKeel and design.draft.maximum)", + title: + 'Depth Below Surface (based on depth.belowKeel and design.draft.maximum)', derivedFrom: derivedFrom, - calculator: function(depthBelowKeel) - { - return [{ path: 'environment.depth.belowSurface', value: depthBelowKeel + draft}] + calculator: function (depthBelowKeel) { + return [ + { + path: 'environment.depth.belowSurface', + value: depthBelowKeel + draft + } + ] } - }; + } } diff --git a/calcs/dewPoint.js b/calcs/dewPoint.js index 0468c04..688fe25 100644 --- a/calcs/dewPoint.js +++ b/calcs/dewPoint.js @@ -1,18 +1,22 @@ - -module.exports = function(app, plugin) { +module.exports = function (app, plugin) { return { group: 'air', optionKey: 'dewPoint', - title: "Outside air dew point (based on humidity and temperature)", - derivedFrom: [ "environment.outside.temperature", "environment.outside.humidity" ], - calculator: function(temp, hum) { - //Magnus formula: + title: 'Outside air dew point (based on humidity and temperature)', + derivedFrom: [ + 'environment.outside.temperature', + 'environment.outside.humidity' + ], + calculator: function (temp, hum) { + // Magnus formula: var tempC = temp + 273.16 const b = 18.678 const c = 257.14 - var magnus = Math.log(hum) + (b * tempC)/(c + tempC) - var dewPoint = (c * magnus) / (b - magnus) - 273.16 - return [{ path: "environment.outside.dewPointTemperature", value: dewPoint}] + var magnus = Math.log(hum) + b * tempC / (c + tempC) + var dewPoint = c * magnus / (b - magnus) - 273.16 + return [ + { path: 'environment.outside.dewPointTemperature', value: dewPoint } + ] } - }; + } } diff --git a/calcs/fuelConsumtion.js b/calcs/fuelConsumtion.js index 74a82af..07b9d14 100644 --- a/calcs/fuelConsumtion.js +++ b/calcs/fuelConsumtion.js @@ -1,6 +1,6 @@ const _ = require('lodash') -module.exports = function(app, plugin) { +module.exports = function (app, plugin) { var engines = plugin.engines app.debug('engines: %j', engines) @@ -10,11 +10,20 @@ module.exports = function(app, plugin) { group: 'propulsion', optionKey: 'economy' + instance, title: `${instance} fuel economy (based on speed over ground, fuel rate)`, - derivedFrom: function(){ return [ "propulsion." + instance + ".fuel.rate", "navigation.speedOverGround" ] }, - calculator: function(rate, speed) { - return [{ path: "propulsion." + instance + ".fuel.economy", - value: speed/rate }] + derivedFrom: function () { + return [ + 'propulsion.' + instance + '.fuel.rate', + 'navigation.speedOverGround' + ] + }, + calculator: function (rate, speed) { + return [ + { + path: 'propulsion.' + instance + '.fuel.economy', + value: speed / rate + } + ] } } - }); + }) } diff --git a/calcs/groundWind.js b/calcs/groundWind.js index 3cf5d49..01ad220 100644 --- a/calcs/groundWind.js +++ b/calcs/groundWind.js @@ -1,17 +1,24 @@ - -module.exports = function(app, plugin) { +module.exports = function (app, plugin) { return { group: 'wind', optionKey: 'groundWind', - title: "Ground Wind Angle and Speed (based on SOG, AWA and AWS)", - derivedFrom: [ "navigation.speedOverGround", "environment.wind.speedApparent", "environment.wind.angleApparent" ], - calculator: function(sog, aws, awa) { - var apparentX = Math.cos(awa) * aws; - var apparentY = Math.sin(awa) * aws; - var angle = Math.atan2(apparentY, -sog + apparentX); - var speed = Math.sqrt(Math.pow(apparentY, 2) + Math.pow(-sog + apparentX, 2)); - return [{ path: "environment.wind.angleTrueGround", value: angle}, - { path: "environment.wind.speedOverGround", value: speed}] + title: 'Ground Wind Angle and Speed (based on SOG, AWA and AWS)', + derivedFrom: [ + 'navigation.speedOverGround', + 'environment.wind.speedApparent', + 'environment.wind.angleApparent' + ], + calculator: function (sog, aws, awa) { + var apparentX = Math.cos(awa) * aws + var apparentY = Math.sin(awa) * aws + var angle = Math.atan2(apparentY, -sog + apparentX) + var speed = Math.sqrt( + Math.pow(apparentY, 2) + Math.pow(-sog + apparentX, 2) + ) + return [ + { path: 'environment.wind.angleTrueGround', value: angle }, + { path: 'environment.wind.speedOverGround', value: speed } + ] } - }; + } } diff --git a/calcs/headingTrue.js b/calcs/headingTrue.js index 45cd1cc..6a38629 100644 --- a/calcs/headingTrue.js +++ b/calcs/headingTrue.js @@ -1,20 +1,17 @@ - - - -module.exports = function(app, plugin) { +module.exports = function (app, plugin) { return { group: 'heading', optionKey: 'heading', - title: "True Heading (based on magnetic heading and magneticVarition)", - derivedFrom: [ "navigation.headingMagnetic", "navigation.magneticVariation" ], - calculator: function(heading, magneticVariation) { + title: 'True Heading (based on magnetic heading and magneticVarition)', + derivedFrom: ['navigation.headingMagnetic', 'navigation.magneticVariation'], + calculator: function (heading, magneticVariation) { var headingTrue = heading + magneticVariation - if ( headingTrue < 0 ) { - headingTrue = (Math.PI*2) + headingTrue - } else if ( headingTrue > (Math.PI*2) ) { - headingTrue = headingTrue - (Math.PI*2) + if (headingTrue < 0) { + headingTrue = Math.PI * 2 + headingTrue + } else if (headingTrue > Math.PI * 2) { + headingTrue = headingTrue - Math.PI * 2 } - return [{ path: "navigation.headingTrue", value: headingTrue}] + return [{ path: 'navigation.headingTrue', value: headingTrue }] } - }; + } } diff --git a/calcs/propslip.js b/calcs/propslip.js index c2914be..3d88572 100644 --- a/calcs/propslip.js +++ b/calcs/propslip.js @@ -1,17 +1,38 @@ -const _ = require("lodash") +const _ = require('lodash') -module.exports = function(app, plugin) { +module.exports = function (app, plugin) { return plugin.engines.map(instance => { return { group: 'propulsion', optionKey: 'propslip' + instance, - title: "propulsion." + instance + ".slip (based on RPM, propulsion." + instance + ".transmission.gearRatio and propulsion." + instance + ".drive.propeller.pitch)", - derivedFrom: function() { return [ "propulsion." + instance + ".revolutions", "navigation.speedThroughWater"] }, - calculator: function(revolutions, stw) { - var gearRatio = app.getSelfPath('propulsion.' + instance + '.transmission.gearRatio.value') - var pitch = app.getSelfPath('propulsion.' + instance + '.drive.propeller.pitch.value') - return [{ path: 'propulsion.' + instance + '.drive.propeller.slip', value: 1 - ((stw * gearRatio)/(revolutions*pitch))}] + title: + 'propulsion.' + + instance + + '.slip (based on RPM, propulsion.' + + instance + + '.transmission.gearRatio and propulsion.' + + instance + + '.drive.propeller.pitch)', + derivedFrom: function () { + return [ + 'propulsion.' + instance + '.revolutions', + 'navigation.speedThroughWater' + ] + }, + calculator: function (revolutions, stw) { + var gearRatio = app.getSelfPath( + 'propulsion.' + instance + '.transmission.gearRatio.value' + ) + var pitch = app.getSelfPath( + 'propulsion.' + instance + '.drive.propeller.pitch.value' + ) + return [ + { + path: 'propulsion.' + instance + '.drive.propeller.slip', + value: 1 - stw * gearRatio / (revolutions * pitch) + } + ] } } - }); + }) } diff --git a/calcs/suncalc.js b/calcs/suncalc.js index d12e664..e1bfe49 100644 --- a/calcs/suncalc.js +++ b/calcs/suncalc.js @@ -1,26 +1,26 @@ -const suncalc = require('suncalc'); +const suncalc = require('suncalc') const _ = require('lodash') - -module.exports = function(app, plugin) { +module.exports = function (app, plugin) { return { group: 'sun', optionKey: 'Sun', - title: "Sets environment.sun to dawn, sunrise, day, sunset, dusk or night. Sets environment.mode to day or night. (based on navigation.datetime or system time and navigation.position)", - derivedFrom: [ "navigation.datetime", "navigation.position" ], - defaults: [ '', undefined ], - debounceDelay: 60*1000, - calculator: function(datetime, position) { + title: + 'Sets environment.sun to dawn, sunrise, day, sunset, dusk or night. Sets environment.mode to day or night. (based on navigation.datetime or system time and navigation.position)', + derivedFrom: ['navigation.datetime', 'navigation.position'], + defaults: ['', undefined], + debounceDelay: 60 * 1000, + calculator: function (datetime, position) { var value var mode var date - if ( datetime && datetime.length > 0 ) { + if (datetime && datetime.length > 0) { date = new Date(datetime) } else { date = new Date() } - + app.debug(`Using datetime: ${date} position: ${JSON.stringify(position)}`) var times = suncalc.getTimes(date, position.latitude, position.longitude) @@ -28,19 +28,19 @@ module.exports = function(app, plugin) { _.keys(times).forEach(key => { times[key] = new Date(times[key]).getTime() - }); + }) - if ( now >= times.sunrise ) { - if ( now < times.sunriseEnd ) { + if (now >= times.sunrise) { + if (now < times.sunriseEnd) { value = 'sunrise' mode = 'day' - } else if ( now <= times.sunsetStart ) { + } else if (now <= times.sunsetStart) { value = 'day' mode = 'day' - } else if ( now >= times.sunsetStart && now < times.dusk ) { + } else if (now >= times.sunsetStart && now < times.dusk) { value = 'sunset' mode = 'night' - } else if ( now < times.night ) { + } else if (now < times.night) { value = 'dusk' mode = 'night' } else { @@ -49,7 +49,7 @@ module.exports = function(app, plugin) { } } else { mode = 'night' - if ( now >= times.dawn ) { + if (now >= times.dawn) { value = 'dawn' } else { value = 'night' @@ -57,9 +57,11 @@ module.exports = function(app, plugin) { } app.debug(`Setting sun to ${value} and mode to ${mode}`) - - return [{ path: "environment.sun", value: value}, - {path: 'environment.mode', value: mode}] + + return [ + { path: 'environment.sun', value: value }, + { path: 'environment.mode', value: mode } + ] } - }; + } } diff --git a/calcs/tankVolume.js b/calcs/tankVolume.js index 47cb6ab..614e90f 100644 --- a/calcs/tankVolume.js +++ b/calcs/tankVolume.js @@ -1,68 +1,74 @@ -var spline = require("cubic-spline") -const _ = require("lodash") -const util = require('util') //dev +var spline = require('cubic-spline') +const _ = require('lodash') +const util = require('util') // dev var instance -//tankInstances = ["tanks.fuel.*", "tanks.fuel.1", "tanks.water.0" ] -module.exports = function(app, plugin) { +// tankInstances = ["tanks.fuel.*", "tanks.fuel.1", "tanks.water.0" ] +module.exports = function (app, plugin) { return plugin.tanks.map(instance => { return { group: 'tanks', optionKey: 'tankVolume_' + instance, - title: "'" + instance + "' Tank Volume (based on currentLevel (requires calibration pairs (>2 for parallell sides, >3 for straight wedge and >4 for more complex shapes))", - derivedFrom: function(){ - return [ instance + ".currentLevel" ] + title: + "'" + + instance + + "' Tank Volume (based on currentLevel (requires calibration pairs (>2 for parallell sides, >3 for straight wedge and >4 for more complex shapes))", + derivedFrom: function () { + return [instance + '.currentLevel'] }, properties: { volume_unit: { - type: "string", - title: "Input unit", - enum: ["litres", "gal", "m3"], - default: "litres" + type: 'string', + title: 'Input unit', + enum: ['litres', 'gal', 'm3'], + default: 'litres' }, - ["calibrations."+instance]: { - "type": "array", - "title": "Calibration entries (pairs of level => volume)", - "items": { - "type": "object", - "required": [ - "level", - "volume" - ], - "properties": { - "level": { - "type": "number", - "title": "level (ratio max 1)", - "description": " " + ['calibrations.' + instance]: { + type: 'array', + title: 'Calibration entries (pairs of level => volume)', + items: { + type: 'object', + required: ['level', 'volume'], + properties: { + level: { + type: 'number', + title: 'level (ratio max 1)', + description: ' ' }, - "volume": { - "type": "number", - "title": "corresponding volume (selected unit)", - "description": " " + volume: { + type: 'number', + title: 'corresponding volume (selected unit)', + description: ' ' } } } } }, - calculator: function(level) { - + calculator: function (level) { var calLevels = [] var calVolumes = [] app.debug(plugin.properties.tanks.volume_unit) - plugin.properties.tanks["calibrations."+instance].forEach(function(i) { + plugin.properties.tanks['calibrations.' + instance].forEach(function ( + i + ) { calLevels.push(i.level) - if (plugin.properties.tanks.volume_unit === "litres"){ - calVolumes.push(i.volume*0.001) - } else if (plugin.properties.tanks.volume_unit === "gal") { - calVolumes.push(i.volume*0.00378541) + if (plugin.properties.tanks.volume_unit === 'litres') { + calVolumes.push(i.volume * 0.001) + } else if (plugin.properties.tanks.volume_unit === 'gal') { + calVolumes.push(i.volume * 0.00378541) } else { calVolumes.push(i.volume) } }) - return [{ path: instance + '.currentVolume', value: spline(level, calLevels, calVolumes)}] + return [ + { + path: instance + '.currentVolume', + value: spline(level, calLevels, calVolumes) + } + ] } } }) diff --git a/calcs/trueWind.js b/calcs/trueWind.js index 425c506..814f657 100644 --- a/calcs/trueWind.js +++ b/calcs/trueWind.js @@ -1,28 +1,36 @@ - - -module.exports = function(app) { +module.exports = function (app) { return { group: 'wind', optionKey: 'trueWind', - title: "True Wind Angle, Direction and Speed (based on speed through water, AWA and AWS)", - derivedFrom: [ "navigation.headingTrue", "navigation.speedThroughWater", "environment.wind.speedApparent", "environment.wind.angleApparent" ], - calculator: function(headTrue, speed, aws, awa) { - var apparentX = Math.cos(awa) * aws; - var apparentY = Math.sin(awa) * aws; - var angle = Math.atan2(apparentY, -speed + apparentX); - var speed = Math.sqrt(Math.pow(apparentY, 2) + Math.pow(-speed + apparentX, 2)); + title: + 'True Wind Angle, Direction and Speed (based on speed through water, AWA and AWS)', + derivedFrom: [ + 'navigation.headingTrue', + 'navigation.speedThroughWater', + 'environment.wind.speedApparent', + 'environment.wind.angleApparent' + ], + calculator: function (headTrue, speed, aws, awa) { + var apparentX = Math.cos(awa) * aws + var apparentY = Math.sin(awa) * aws + var angle = Math.atan2(apparentY, -speed + apparentX) + var speed = Math.sqrt( + Math.pow(apparentY, 2) + Math.pow(-speed + apparentX, 2) + ) var dir = headTrue + angle - if ( dir > Math.PI*2 ) { - dir = dir - Math.PI*2; - } else if ( dir < 0 ) { - dir = dir + Math.PI*2; + if (dir > Math.PI * 2) { + dir = dir - Math.PI * 2 + } else if (dir < 0) { + dir = dir + Math.PI * 2 } - - return [{ path: "environment.wind.directionTrue", value: dir}, - { path: "environment.wind.angleTrueWater", value: angle}, - { path: "environment.wind.speedTrue", value: speed}] + + return [ + { path: 'environment.wind.directionTrue', value: dir }, + { path: 'environment.wind.angleTrueWater', value: angle }, + { path: 'environment.wind.speedTrue', value: speed } + ] } - }; + } } diff --git a/calcs/vmg.js b/calcs/vmg.js index d2797a9..c7f09d7 100644 --- a/calcs/vmg.js +++ b/calcs/vmg.js @@ -1,17 +1,22 @@ - -module.exports = function(app) { +module.exports = function (app) { return { group: 'vmg', optionKey: 'vmg_Course', - title: "Velocity Made Good to Course (based on courseGreatCircle.nextPoint.bearingTrue heading true and speedOverGround)", - derivedFrom: [ "navigation.courseGreatCircle.nextPoint.bearingTrue", - "navigation.headingTrue", - "navigation.speedOverGround" ], - calculator: function (bearingTrue, headingTrue, speedOverGround) - { - var angle = Math.abs(bearingTrue-headingTrue) - return [{ path: "navigation.courseGreatCircle.nextPoint.velocityMadeGood", - value: Math.cos(bearingTrue-headingTrue) * speedOverGround}] + title: + 'Velocity Made Good to Course (based on courseGreatCircle.nextPoint.bearingTrue heading true and speedOverGround)', + derivedFrom: [ + 'navigation.courseGreatCircle.nextPoint.bearingTrue', + 'navigation.headingTrue', + 'navigation.speedOverGround' + ], + calculator: function (bearingTrue, headingTrue, speedOverGround) { + var angle = Math.abs(bearingTrue - headingTrue) + return [ + { + path: 'navigation.courseGreatCircle.nextPoint.velocityMadeGood', + value: Math.cos(bearingTrue - headingTrue) * speedOverGround + } + ] } - }; + } } diff --git a/calcs/vmg_wind.js b/calcs/vmg_wind.js index f2757a3..3d61467 100644 --- a/calcs/vmg_wind.js +++ b/calcs/vmg_wind.js @@ -1,21 +1,26 @@ - -module.exports = function(app) { +module.exports = function (app) { return { group: 'vmg', optionKey: 'vmg_Wind', - title: "Velocity Made Good to wind (based on wind.directionTrue and speedOverGround)", - derivedFrom: [ "environment.wind.directionTrue", - "navigation.speedOverGround" ], - calculator: function (trueWindAngle, speedOverGround) - { + title: + 'Velocity Made Good to wind (based on wind.directionTrue and speedOverGround)', + derivedFrom: [ + 'environment.wind.directionTrue', + 'navigation.speedOverGround' + ], + calculator: function (trueWindAngle, speedOverGround) { var vmg_wind = Math.cos(trueWindAngle) * speedOverGround - if (vmg_wind < 0){ - return [{ path: "performance.velocityMadeGood", value: vmg_wind}, - {path: "performance.gybeAngleVelocityMadeGood", value: vmg_wind}] + if (vmg_wind < 0) { + return [ + { path: 'performance.velocityMadeGood', value: vmg_wind }, + { path: 'performance.gybeAngleVelocityMadeGood', value: vmg_wind } + ] } else { - return [{ path: "performance.velocityMadeGood", value: vmg_wind}, - {path: "performance.beatAngleVelocityMadeGood", value: vmg_wind}] + return [ + { path: 'performance.velocityMadeGood', value: vmg_wind }, + { path: 'performance.beatAngleVelocityMadeGood', value: vmg_wind } + ] } } - }; + } } diff --git a/calcs/windChill.js b/calcs/windChill.js index 5145ef1..9373c23 100644 --- a/calcs/windChill.js +++ b/calcs/windChill.js @@ -1,23 +1,33 @@ - -module.exports = function(app, plugin) { +module.exports = function (app, plugin) { return { group: 'air', optionKey: 'Wind Chill', - title: "Outside air wind chill (based on wind speed and temperature)", - derivedFrom: [ "environment.outside.temperature", "environment.wind.speedApparent" ], - calculator: function(temp, windSpeed) { - //standard Wind Chill formula for Environment Canada + title: 'Outside air wind chill (based on wind speed and temperature)', + derivedFrom: [ + 'environment.outside.temperature', + 'environment.wind.speedApparent' + ], + calculator: function (temp, windSpeed) { + // standard Wind Chill formula for Environment Canada const tempC = temp - 273.16 const windSpeedKt = windSpeed * 1.852 - var windChill=13.12 + 0.6215*tempC - 11.37*Math.pow(windSpeedKt,0.16) + 0.3965*tempC*Math.pow(windSpeedKt,0.16)+273.16 - - //Please Note: The calculator should not be used for outside air temperatures greater that 10 °C (50 °F ) and wind speeds less than 4.8 Km/hr - windChill = (windSpeedKt <= 4.8) ? tempC+273.16 : windChill; - windChill = (tempC > 10) ? tempC+273.16 : windChill; - + var windChill = + 13.12 + + 0.6215 * tempC - + 11.37 * Math.pow(windSpeedKt, 0.16) + + 0.3965 * tempC * Math.pow(windSpeedKt, 0.16) + + 273.16 - return [{ path: "environment.outside.apparentWindChillTemperature", value: windChill}] + // Please Note: The calculator should not be used for outside air temperatures greater that 10 °C (50 °F ) and wind speeds less than 4.8 Km/hr + windChill = windSpeedKt <= 4.8 ? tempC + 273.16 : windChill + windChill = tempC > 10 ? tempC + 273.16 : windChill + return [ + { + path: 'environment.outside.apparentWindChillTemperature', + value: windChill + } + ] } - }; + } } diff --git a/calcs/windDirection.js b/calcs/windDirection.js index 13a50da..784cd5f 100644 --- a/calcs/windDirection.js +++ b/calcs/windDirection.js @@ -1,37 +1,36 @@ - -module.exports = function(app, plugin) { +module.exports = function (app, plugin) { return [ { group: 'wind', optionKey: 'directionTrue', - title: "True Wind Direction (based on AWA and headingTrue)", - derivedFrom: [ "navigation.headingTrue", "environment.wind.angleApparent" ], - calculator: function(headingTrue, awa) { - - let windHeading = headingTrue + awa; + title: 'True Wind Direction (based on AWA and headingTrue)', + derivedFrom: ['navigation.headingTrue', 'environment.wind.angleApparent'], + calculator: function (headingTrue, awa) { + let windHeading = headingTrue + awa + + if (windHeading > Math.PI * 2) windHeading -= Math.PI * 2 + else if (windHeading < 0) windHeading += Math.PI * 2 - if ( windHeading > (Math.PI*2) ) - windHeading -= (Math.PI*2); - else if ( windHeading < 0 ) - windHeading += (Math.PI*2); - - return [{ path: "environment.wind.directionTrue", value: windHeading}] + return [{ path: 'environment.wind.directionTrue', value: windHeading }] } - }, { + }, + { group: 'wind', optionKey: 'directionMagnetic2', - title: "Magnetic Wind Direction (based on AWA and headingMagnetic)", - derivedFrom: [ "navigation.headingMagnetic", "environment.wind.angleApparent" ], - calculator: function(headingMagnetic, awa) { - - let windHeading = headingMagnetic + awa; - - if ( windHeading > (Math.PI*2) ) - windHeading -= (Math.PI*2); - else if ( windHeading < 0 ) - windHeading += (Math.PI*2); - - return [{ path: "environment.wind.directionMagnetic", value: windHeading}] + title: 'Magnetic Wind Direction (based on AWA and headingMagnetic)', + derivedFrom: [ + 'navigation.headingMagnetic', + 'environment.wind.angleApparent' + ], + calculator: function (headingMagnetic, awa) { + let windHeading = headingMagnetic + awa + + if (windHeading > Math.PI * 2) windHeading -= Math.PI * 2 + else if (windHeading < 0) windHeading += Math.PI * 2 + + return [ + { path: 'environment.wind.directionMagnetic', value: windHeading } + ] } } ] diff --git a/calcs/windDirectionMagnetic.js b/calcs/windDirectionMagnetic.js index aadb52e..9166334 100644 --- a/calcs/windDirectionMagnetic.js +++ b/calcs/windDirectionMagnetic.js @@ -1,19 +1,23 @@ - - -module.exports = function(app, plugin) { +module.exports = function (app, plugin) { return { group: 'wind', optionKey: 'directionMagnetic', - title: "Magnetic Wind Direction (based on wind.directionTrue and magneticVarition)", - derivedFrom: [ "environment.wind.directionTrue", "navigation.magneticVariation" ], - calculator: function(directionTrue, magneticVariation) { + title: + 'Magnetic Wind Direction (based on wind.directionTrue and magneticVarition)', + derivedFrom: [ + 'environment.wind.directionTrue', + 'navigation.magneticVariation' + ], + calculator: function (directionTrue, magneticVariation) { var directionMagnetic = directionTrue - magneticVariation - if ( directionMagnetic < 0 ) { - directionMagnetic = (Math.PI*2) + directionMagnetic - } else if ( directionMagnetic > (Math.PI*2) ) { - directionMagnetic = directionMagnetic - (Math.PI*2) + if (directionMagnetic < 0) { + directionMagnetic = Math.PI * 2 + directionMagnetic + } else if (directionMagnetic > Math.PI * 2) { + directionMagnetic = directionMagnetic - Math.PI * 2 } - return [{ path: "environment.wind.directionMagnetic", value: directionMagnetic}] + return [ + { path: 'environment.wind.directionMagnetic', value: directionMagnetic } + ] } - }; + } } diff --git a/calcs/windShift.js b/calcs/windShift.js index 897d514..539fc61 100644 --- a/calcs/windShift.js +++ b/calcs/windShift.js @@ -3,87 +3,86 @@ const _ = require('lodash') var windAvg var alarmSent = false -module.exports = function(app, plugin) { +module.exports = function (app, plugin) { return { - group:'wind', + group: 'wind', optionKey: 'windShift', - title: "Wind Shift (experimental)", - derivedFrom: [ "environment.wind.angleApparent" ], - stop: function() { - windAvg = undefined; - if ( alarmSent ) { - alarmSent = false; + title: 'Wind Shift (experimental)', + derivedFrom: ['environment.wind.angleApparent'], + stop: function () { + windAvg = undefined + if (alarmSent) { + alarmSent = false app.handleMessage(plugin.id, { - "context": "vessels." + app.selfId, - "updates": [ + context: 'vessels.' + app.selfId, + updates: [ { - "source": { - //"src": key + source: { + // "src": key }, - "timestamp": (new Date()).toISOString(), - "values": [ normalAlarmDelta() ] + timestamp: new Date().toISOString(), + values: [normalAlarmDelta()] } ] - }); + }) } }, - calculator: function(angleApparent) { - var alarm = app.getSelfPath( - 'environment.wind.directionChangeAlarm.value' - ) - if ( typeof alarm === 'undefined' ) - { - console.log("signall-Derived-data: no directionChangeAlarm value") + calculator: function (angleApparent) { + var alarm = app.getSelfPath('environment.wind.directionChangeAlarm.value') + if (typeof alarm === 'undefined') { + console.log('signall-Derived-data: no directionChangeAlarm value') return undefined } - var values = undefined - app.debug("angleApparent: " + angleApparent) - if ( angleApparent < 0 ) - angleApparent = angleApparent + Math.PI/2; - app.debug("angleApparent2: " + angleApparent) - app.debug("alarm: " + alarm) - if ( typeof windAvg === 'undefined' ) { - windAvg = angleApparent; + var values + app.debug('angleApparent: ' + angleApparent) + if (angleApparent < 0) angleApparent = angleApparent + Math.PI / 2 + app.debug('angleApparent2: ' + angleApparent) + app.debug('alarm: ' + alarm) + if (typeof windAvg === 'undefined') { + windAvg = angleApparent } else { - var diff = Math.abs(windAvg-angleApparent) - app.debug("" + windAvg + ", " + angleApparent + ", " + diff) - if ( diff > alarm ) { + var diff = Math.abs(windAvg - angleApparent) + app.debug('' + windAvg + ', ' + angleApparent + ', ' + diff) + if (diff > alarm) { values = [ { - "path": 'notifications.windShift', - "value": { - "state": "alert", - "method": [ "visual", "sound" ], - "message": "Wind has shifted by " + Math.round(radsToDeg(diff)) + " degrees", - "timestamp": (new Date()).toISOString() + path: 'notifications.windShift', + value: { + state: 'alert', + method: ['visual', 'sound'], + message: + 'Wind has shifted by ' + + Math.round(radsToDeg(diff)) + + ' degrees', + timestamp: new Date().toISOString() } - }] + } + ] alarmSent = true } else { - if ( alarmSent ) { - values = [ normalAlarmDelta() ] + if (alarmSent) { + values = [normalAlarmDelta()] alarmSent = false } - windAvg = (windAvg + angleApparent) / 2; + windAvg = (windAvg + angleApparent) / 2 } } - return values; + return values } - }; + } } -function normalAlarmDelta() -{ +function normalAlarmDelta () { return { - "path": 'notifications.windShift', - "value": { - "state": "normal", - "timestamp": (new Date()).toISOString() + path: 'notifications.windShift', + value: { + state: 'normal', + timestamp: new Date().toISOString() } - }; + } } -function radsToDeg(radians) { +function radsToDeg (radians) { return radians * 180 / Math.PI } diff --git a/package.json b/package.json index fb3d364..30749a2 100644 --- a/package.json +++ b/package.json @@ -19,12 +19,32 @@ } ], "license": "ISC", + "scripts": { + "precommit": "lint-staged", + "format": "prettier-standard calcs/*.js index.js" + }, + "lint-staged": { + "linters": { + "calcs/*.js": [ + "prettier-standard", + "git add" + ], + "index.js": [ + "prettier-standard", + "git add" + ] + } + }, "dependencies": { "baconjs": "^0.7.88", "lodash": "^4.17.4", "cubic-spline": "^1.0.4", "suncalc": "^1.8.0" }, + "devDependencies": { + "lint-staged": "^7.0.0", + "prettier-standard": "^8.0.0" + }, "repository": { "type": "git", "url": "https://github.com/sbender9/signalk-derived-data"