diff --git a/lib/canbus.js b/lib/canbus.js index 6d8ad3a..19b460c 100644 --- a/lib/canbus.js +++ b/lib/canbus.js @@ -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 @@ -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(() => { @@ -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}`) } } diff --git a/lib/serial.js b/lib/serial.js index 8a576ff..6895e28 100644 --- a/lib/serial.js +++ b/lib/serial.js @@ -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) } @@ -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) + }) } }