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

[WIP]: Self identity fixes #1448

Open
wants to merge 5 commits into
base: master
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
11 changes: 2 additions & 9 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,6 @@ function setBaseDeltas(app: ConfigApp) {
return true
}

export function sendBaseDeltas(app: ConfigApp) {
const copy = JSON.parse(JSON.stringify(app.config.baseDeltaEditor.deltas))
copy.forEach((delta: any) => {
app.handleMessage('defaults', delta)
})
}

function writeDefaultsFile(app: ConfigApp, defaults: any, cb: any) {
fs.writeFile(getDefaultsPath(app), JSON.stringify(defaults, null, 2), cb)
}
Expand All @@ -336,7 +329,7 @@ function writeBaseDeltasFile(app: ConfigApp) {
return app.config.baseDeltaEditor.save(getBaseDeltasPath(app))
}

function setSelfSettings(app: ConfigApp) {
async function setSelfSettings(app: ConfigApp) {
const name = app.config.baseDeltaEditor.getSelfValue('name')
const mmsi = app.config.baseDeltaEditor.getSelfValue('mmsi')
let uuid = app.config.baseDeltaEditor.getSelfValue('uuid')
Expand All @@ -352,6 +345,7 @@ function setSelfSettings(app: ConfigApp) {
if (mmsi === null && uuid === null) {
uuid = 'urn:mrn:signalk:uuid:' + uuidv4()
app.config.baseDeltaEditor.setSelfValue('uuid', uuid)
await writeBaseDeltasFile(app)
}

app.config.vesselName = name
Expand Down Expand Up @@ -488,6 +482,5 @@ module.exports = {
writeSettingsFile,
writeDefaultsFile,
readDefaultsFile,
sendBaseDeltas,
writeBaseDeltasFile
}
12 changes: 11 additions & 1 deletion src/deltaeditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,22 @@ const META = 'meta'
const SELF_VESSEL = 'vessels.self'

class DeltaEditor {
deltas: any[]
private deltas: any[]

constructor() {
this.deltas = []
}

getDeltas() {
return JSON.parse(JSON.stringify(this.deltas))
}

sendDeltas(handleMessage: (id: string, delta: any) => void) {
this.getDeltas().forEach((delta: any) => {
handleMessage('defaults', delta)
})
}

load(filename: string) {
const data = fs.readFileSync(filename, 'utf8')
const deltas = JSON.parse(data)
Expand Down
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import https from 'https'
import _ from 'lodash'
import path from 'path'
import { SelfIdentity, ServerApp, SignalKMessageHub, WithConfig } from './app'
import { ConfigApp, load, sendBaseDeltas } from './config/config'
import { load } from './config/config'
import { createDebug } from './debug'
import DeltaCache from './deltacache'
import DeltaChain, { DeltaInputHandler } from './deltachain'
Expand Down Expand Up @@ -245,6 +245,9 @@ class Server {
app.on('nmea2000OutAvailable', () => {
app.isNmea2000OutAvailable = true
})

app.sendBaseDeltas = () =>
app.config.baseDeltaEditor.sendDeltas(app.handleMessage)
}

start() {
Expand Down Expand Up @@ -360,7 +363,7 @@ class Server {
debug('ID type: ' + app.selfType)
debug('ID: ' + app.selfId)

sendBaseDeltas((app as unknown) as ConfigApp)
app.config.baseDeltaEditor.sendDeltas(app.handleMessage)

startInterfaces(app)
startMdns(app)
Expand Down
3 changes: 1 addition & 2 deletions src/put.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const _ = require('lodash')
import { createDebug } from './debug'
const debug = createDebug('signalk-server:put')
const { v4: uuidv4 } = require('uuid')
const { createRequest, updateRequest } = require('./requestResponse')
const skConfig = require('./config/config')

Expand Down Expand Up @@ -81,7 +80,7 @@ module.exports = {
}

app.config.baseDeltaEditor.setMeta(context, metaPath, metaValue)
skConfig.sendBaseDeltas(app)
app.sendBaseDeltas()

if (app.config.hasOldDefaults) {
let data
Expand Down
25 changes: 22 additions & 3 deletions src/serverroutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
const fs = require('fs')
const os = require('os')
const readdir = require('util').promisify(fs.readdir)
const page = require('./page')
import { createDebug, listKnownDebugs } from './debug'
const debug = createDebug('signalk-server:serverroutes')
const path = require('path')
Expand All @@ -40,6 +39,9 @@ const defaultSecurityStrategy = './tokensecurity'
const skPrefix = '/signalk/v1'
import { SERVERROUTESPREFIX } from './constants'

const MMSIPATTERN = /^\d{9}$/
const UUIDPATTERN = /^urn:mrn:signalk:uuid:[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-4[0-9A-Fa-f]{3}-[89ABab][0-9A-Fa-f]{3}-[0-9A-Fa-f]{12}$/

module.exports = function(app, saveSecurityConfig, getSecurityConfig) {
let securityWasEnabled
let restoreFilePath
Expand Down Expand Up @@ -638,10 +640,27 @@ module.exports = function(app, saveSecurityConfig, getSecurityConfig) {
})
}

app.put(`${SERVERROUTESPREFIX}/vessel`, (req, res, next) => {
app.put(`${SERVERROUTESPREFIX}/vessel`, (req, res) => {
console.log(JSON.stringify(req.body, null, 2))
const de = app.config.baseDeltaEditor
let vessel = req.body

if (!vessel.mmsi && !vessel.uuid) {
res.status(400)
res.send(`You must provide either mmsi or uuid`)
return
}
if (vessel.mmsi && !vessel.mmsi.match(MMSIPATTERN)) {
res.status(400)
res.send(`MMSI must be a number with 9 digits`)
return
}
if (vessel.uuid && !vessel.uuid.match(UUIDPATTERN)) {
res.status(400)
res.send(`Uuid must be a v4 UUID with prefix urn:mrn:signalk:uuid:`)
return
}

de.setSelfValue('name', vessel.name)
app.config.vesselName = vessel.name
de.setSelfValue('mmsi', vessel.mmsi)
Expand Down Expand Up @@ -701,7 +720,7 @@ module.exports = function(app, saveSecurityConfig, getSecurityConfig) {
}
})

skConfig.sendBaseDeltas(app)
app.sendBaseDeltas()

if (app.config.hasOldDefaults) {
writeOldDefaults(req, res)
Expand Down