sphero-pwn
Version:
Driver for Sphero robots
63 lines (51 loc) • 1.5 kB
text/coffeescript
fs = require 'fs-promise'
Recording = require './recording.coffee'
# Records all the bytes going to a channel.
class ChannelRecorder
# {SerialChannel} the channel being recorded
# {String} recordingPath the file where the recording will be saved
constructor: (channel, recordingPath) ->
= channel.sourceId
= channel
.onData = .bind(@)
= recordingPath
= null
= null
# {SerialChannel#onError}
onError: (error) ->
return
# {SerialChannel#onData}
onData: (data) ->
return
# {SerialChannel#sourceId}
sourceId: null
# {SerialChannel#open}
open: ->
||= fs.open(, 'w')
.then (fd) =>
= fd
.open()
# {SerialChannel#write}
write: (data) ->
.then =>
lineString = "> " + Recording.bufferToHex(data) + "\n"
fs.writeSync , lineString, 'utf8'
fs.fsyncSync
.write data
# {SerialChannel#close}
close: ->
.then =>
.close()
.then =>
fs.closeSync unless is null
= null
_onChannelData: (data) ->
.then =>
lineString = "< " + Recording.bufferToHex(data) + "\n"
fs.writeSync , lineString, 'utf8'
fs.fsyncSync
data
module.exports = ChannelRecorder