Skip to content

Commit

Permalink
feature: use name and mmsi or uuid from defaults.json if available (#380
Browse files Browse the repository at this point in the history
)

* fix: existing defaults getting overwritten
* feature: use name and mmsi or uuid from defaults.json if available
  • Loading branch information
sbender9 authored and tkurki committed Jan 13, 2018
1 parent 41968cb commit 1401c24
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 37 deletions.
48 changes: 39 additions & 9 deletions lib/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ function load (app) {

config.appPath = config.appPath || path.normalize(__dirname + '/../../')
debug('appPath:' + config.appPath)

setConfigDirectory(app)
_.isObject(app.config.settings) || readSettingsFile(app)
setSelfSettings(app)
setFullDefaults(app)
setSelfSettings(app)

if (env.SSLPORT) {
config.settings.ssl = true
Expand Down Expand Up @@ -89,6 +88,7 @@ function setFullDefaults (app) {
} else {
console.log(e)
}
app.config.defaults = { vessels: { self: {} } }
}
}

Expand All @@ -97,14 +97,44 @@ function writeDefaultsFile (app, defaults, cb) {
}

function setSelfSettings (app) {
if (_.isObject(app.config.settings.vessel)) {
if (typeof app.config.settings.vessel.mmsi === 'string') {
app.selfType = 'mmsi'
app.selfId = 'urn:mrn:imo:mmsi:' + app.config.settings.vessel.mmsi
} else if (typeof app.config.settings.vessel.uuid === 'string') {
app.selfType = 'uuid'
app.selfId = app.config.settings.vessel.uuid
var name = _.get(app.config.defaults, 'vessels.self.name')
var mmsi = _.get(app.config.defaults, 'vessels.self.mmsi')
var uuid = _.get(app.config.defaults, 'vessels.self.uuid')

if (app.config.settings.vessel) {
// backwards compatibility for settings files with 'vessel'
if (!mmsi && !uuid) {
mmsi = app.config.settings.vessel.mmsi
uuid = app.config.settings.vessel.uuid
if (mmsi) {
app.config.defaults.vessels.self.mmsi = mmsi
}
if (uuid) {
app.config.defaults.vessels.self.uuid = uuid
}
}
if (!name) {
name = app.config.settings.vessel.name
app.config.defaults.vessels.self.name = name
}
}

if (mmsi && !_.isString(mmsi)) {
throw new Error(`invalid mmsi: ${mmsi}`)
}

if (uuid && !_.isString(uuid)) {
throw new Error(`invalid uuid: ${uuid}`)
}

if (mmsi) {
app.selfType = 'mmsi'
app.selfId = 'urn:mrn:imo:mmsi:' + mmsi
} else if (uuid) {
app.selfType = 'uuid'
app.selfId = uuid
}
if (app.selfType) {
debug(app.selfType.toUpperCase() + ': ' + app.selfId)
}
app.selfContext = 'vessels.' + app.selfId
Expand Down
10 changes: 0 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,6 @@ function Server(opts) {

require('./serverroutes')(app, saveSecurityConfig, getSecurityConfig)

if (app.config.settings.vessel) {
if (!app.config.defaults) {
app.config.defaults = { vessels: { self: {} } }
}
var vessel = app.config.settings.vessel
if (vessel.name) {
app.config.defaults.vessels.self.name = vessel.name
}
}

app.signalk = new FullSignalK(app.selfId, app.selfType, app.config.defaults)

app.handleMessage = function(providerId, data) {
Expand Down
16 changes: 3 additions & 13 deletions lib/mdns.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ let _ = require('lodash'),
mdns = require('mdns'),
ports = require('./ports')

function getConfigString (settingsObject, key) {
if (_.isObject(settingsObject) && typeof settingsObject[key] === 'string') {
return settingsObject[key].trim()
}

return ''
}

module.exports = function mdnsResponder (app) {
let config = app.config

Expand All @@ -43,11 +35,9 @@ module.exports = function mdnsResponder (app) {
// hardcoded out of master/slave, main/aux
roles: 'master, main',
self: app.selfId,
vessel_name: getConfigString(config.settings.vessel, 'name'),
vessel_brand: getConfigString(config.settings.vessel, 'brand'),
vessel_type: getConfigString(config.settings.vessel, 'type'),
vessel_mmsi: getConfigString(config.settings.vessel, 'mmsi'),
vessel_uuid: getConfigString(config.settings.vessel, 'uuid')
vessel_name: config.defaults.vessels.self.name,
vessel_mmsi: config.defaults.vessels.self.mmsi,
vessel_uuid: config.defaults.vessels.self.uuid
}

// Strip all the null or empty props in txtRecord
Expand Down
24 changes: 19 additions & 5 deletions lib/serverroutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,15 +405,18 @@ module.exports = function(app, saveSecurityConfig, getSecurityConfig) {
})

app.get('/vessel', (req, res, next) => {
if (!app.config.defaults) {
app.config.defaults = {
var defaults

try {
defaults = config.readDefaultsFile(app)
} catch (e) {
defaults = {
vessels: {
self: {}
}
}
}

var self = app.config.defaults.vessels.self
var self = defaults.vessels.self

var json = {
name: self.name,
Expand All @@ -426,6 +429,17 @@ module.exports = function(app, saveSecurityConfig, getSecurityConfig) {
gpsFromBow: _.get(self, 'sensors.gps.fromBow.value'),
gpsFromCenter: _.get(self, 'sensors.gps.fromCenter.value')
}

if (app.config.settings.vessel) {
if (!json.name) {
json.name = app.config.settings.vessel.name
}
if (!json.uuid && !json.mmsi) {
json.mmsi = app.config.settings.vessel.mmsi
json.uuid = app.config.settings.vessel.uuid
}
}

res.json(json)
})

Expand All @@ -443,7 +457,7 @@ module.exports = function(app, saveSecurityConfig, getSecurityConfig) {
}
}

var self = _.get(data, 'data.vessels.self')
var self = _.get(data, 'vessels.self')

if (_.isUndefined(self)) {
self = _.set(data, 'vessels.self', {})
Expand Down

0 comments on commit 1401c24

Please sign in to comment.