Skip to content

Commit

Permalink
feature: add ability to send NMEA 2000 out with the Data Fiddler
Browse files Browse the repository at this point in the history
  • Loading branch information
sbender9 committed Jun 13, 2024
1 parent 97d4b72 commit 8607e91
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
35 changes: 29 additions & 6 deletions packages/server-admin-ui/src/views/Playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ class Playground extends Component {
input,
inputIsJson: isJson(input),
sending: false,
sendingN2K: false,
activeTab: DELTAS_TAB_ID,
}

this.handleExecute = this.handleExecute.bind(this)
this.handleSendN2K = this.handleSendN2K.bind(this)
this.handleInput = this.handleInput.bind(this)
this.send = this.send.bind(this)
this.beautify = this.beautify.bind(this)
Expand All @@ -77,6 +79,10 @@ class Playground extends Component {
this.send(true)
}

handleSendN2K() {
this.send(false, true)
}

componentDidMount() {
if (this.state.input && this.state.input.length > 0) {
this.send(false)
Expand All @@ -102,7 +108,7 @@ class Playground extends Component {
}
}

send(sendToServer) {
send(sendToServer, sendToN2K) {
let start = this.state.input.trim().charAt(0)
if (start === '{' || start === '[') {
try {
Expand All @@ -125,11 +131,14 @@ class Playground extends Component {
}
}

const body = { value: this.state.input, sendToServer }
const body = { value: this.state.input, sendToServer, sendToN2K }
localStorage.setItem(inputStorageKey, this.state.input)
if (sendToServer) {
this.setState({ ...this.state, sending: true })
}
if (sendToN2K) {
this.setState({ ...this.state, sendingN2K: true })
}
fetch(`${window.serverRoutesPrefix}/inputTest`, {
method: 'POST',
credentials: 'include',
Expand All @@ -140,9 +149,9 @@ class Playground extends Component {
})
.then((response) => response.json())
.then((data) => {
if (sendToServer) {
if (sendToServer || sendToN2K) {
setTimeout(() => {
this.setState({ ...this.state, sending: false })
this.setState({ ...this.state, sending: false, sendingN2K: false })
}, 1000)
}
if (data.error) {
Expand Down Expand Up @@ -214,8 +223,8 @@ class Playground extends Component {
error: error.message,
jsonError: null,
})
if (sendToServer) {
this.setState({ ...this.state, sending: false })
if (sendToServer || sendToN2K) {
this.setState({ ...this.state, sending: false, sendingN2K: false })
}
})
}
Expand Down Expand Up @@ -291,6 +300,20 @@ class Playground extends Component {
}
/>{' '}
Send To Server
</Button>{' '}
<Button
size="sm"
color="primary"
onClick={this.handleSendN2K}
>
<i
className={
this.state.sendingN2K
? 'fa fa-spinner fa-spin'
: 'fa fa-dot-circle-o'
}
/>{' '}
Send To N2K Out
</Button>
</CardFooter>
</Card>
Expand Down
26 changes: 25 additions & 1 deletion src/interfaces/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ module.exports = function (app) {
const n2kMapper = new N2kMapper({ app }, app.propertyValues)
const pgnParser = new FromPgn({}, app.propertyValues)

app.on('nmea2000JsonOut', (msg) => {
console.log('nmea2000JsonOut: ' + msg)
})

app.on('nmea2000out', (msg) => {
console.log('nmea2000out: ' + msg)
})

const processors = {
n2k: (msgs, sendToServer) => {
const n2kJson = []
Expand Down Expand Up @@ -97,9 +105,10 @@ module.exports = function (app) {

app.post(`${serverRoutesPrefix}/inputTest`, (req, res) => {
const sendToServer = req.body.sendToServer
const sendToN2K = req.body.sendToN2K

if (
sendToServer &&
(sendToServer || sendToN2K) &&
!app.securityStrategy.isDummy() &&
!app.securityStrategy.allowConfigure(req)
) {
Expand All @@ -114,6 +123,15 @@ module.exports = function (app) {
return
}

if (sendToN2K && type != 'n2k-json' && type != 'n2k') {
res
.status(400)
.json({
error: 'PLease enter NMEA 2000 json format or Actisense format'
})
return
}

if (type === 'signalk') {
let puts = []
if (sendToServer) {
Expand Down Expand Up @@ -171,6 +189,12 @@ module.exports = function (app) {
} else {
res.json({ deltas: msgs })
}
} else if (sendToN2K) {
const event = type == 'n2k' ? 'nmea2000out' : 'nmea2000JsonOut'
msgs.forEach((msg) => {
app.emit(event, msg)
})
res.json({ deltas: [] })
} else {
try {
const data = processors[type](msgs, sendToServer)
Expand Down

0 comments on commit 8607e91

Please sign in to comment.