Skip to content

Commit

Permalink
feature: provide connection status to the server (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbender9 authored Aug 15, 2018
1 parent c907039 commit 949d75f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
23 changes: 20 additions & 3 deletions lib/canbus.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,24 @@ function CanbusStream (options) {
this.options = options
this.start()

const setProviderStatus = options.app && options.app.setProviderStatus
? (msg) => {
options.app.setProviderStatus(options.providerId, msg)
}
: () => {}
const setProviderError = options.app && options.app.setProviderError
? (msg) => {
options.app.setProviderError(options.providerId, msg)
}
: () => {}

var socketcan;

try {
socketcan = require('socketcan')
} catch ( err ) {
console.log('WARNING: canboatjs: unable to load native socketcan interface')
var msg = 'WARNING unable to load native socketcan interface'
console.error(msg)
}

var that = this
Expand All @@ -80,14 +92,17 @@ function CanbusStream (options) {
hasWriter.on('close', code => {
if ( code == 0 ) {
debug('found socketcan-writer, starting...')
setProviderStatus('Starting')
this.socketCanWriter = spawn('sh',
['-c', `socketcan-writer ${canDevice}`])

setProviderStatus(`Connected to ${canDevice}`)
this.socketCanWriter.stderr.on('data', function (data) {
console.error(data.toString())
})
this.socketCanWriter.on('close', function (code) {
console.error('socketcan-writer process exited with code ' + code)
const msg = 'socketcan-writer process exited with code ' + code
setProviderError(msg)
console.error(msg)
this.socketCanWriter = null
})
setTimeout(() => {
Expand All @@ -112,7 +127,9 @@ function CanbusStream (options) {
this.channel.start()
this.candevice = new CanDevice(this, options)
this.candevice.start()
setProviderStatus('Connected')
} catch (e) {
setProviderError(e.message)
console.error(`unable to open canbus ${canDevice}: ${e}`)
}
}
Expand Down
18 changes: 17 additions & 1 deletion lib/serial.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,27 @@ SerialStream.prototype.start = function () {
baudRate: this.options.baudrate || 115200
})

const setProviderStatus = this.options.app && this.options.app.setProviderStatus
? (msg) => {
this.options.app.setProviderStatus(this.options.providerId, msg)
}
: () => {}
const setProviderError = this.options.app && this.options.app.setProviderError
? (msg) => {
this.options.app.setProviderError(this.options.providerId, msg)
}
: () => {}
var that = this
this.serial.on(
'open',
function () {
try {
setProviderStatus('Connected')
var buf = composeMessage(NGT_MSG_SEND, new Buffer(NGT_STARTUP_MSG), NGT_STARTUP_MSG.length)
that.serial.write(buf)
debug('sent startup message')
} catch ( err ) {
setProviderError(err.message)
console.error(err)
console.error(err.stack)
}
Expand Down Expand Up @@ -129,9 +141,13 @@ SerialStream.prototype.start = function () {
}

this.serial.on('error', function (x) {
setProviderError(x.message)
console.log(x)
})
this.serial.on('close', this.start.bind(this))
this.serial.on('close', () => {
setProviderError('Closed, reconnecting...')
this.start.bind(this)
})
}
}

Expand Down

0 comments on commit 949d75f

Please sign in to comment.