diff --git a/coffeelint.json b/coffeelint.json new file mode 100644 index 0000000..08db2df --- /dev/null +++ b/coffeelint.json @@ -0,0 +1,160 @@ +{ + "arrow_spacing": { + "name": "arrow_spacing", + "level": "warn" + }, + "braces_spacing": { + "name": "braces_spacing", + "level": "warn", + "empty_object_spaces": 0, + "spaces": 0 + }, + "camel_case_classes": { + "name": "camel_case_classes", + "level": "error" + }, + "coffeescript_error": { + "level": "error" + }, + "colon_assignment_spacing": { + "name": "colon_assignment_spacing", + "level": "warn", + "spacing": { + "left": 0, + "right": 1 + } + }, + "cyclomatic_complexity": { + "name": "cyclomatic_complexity", + "level": "ignore", + "value": 10 + }, + "duplicate_key": { + "name": "duplicate_key", + "level": "error" + }, + "empty_constructor_needs_parens": { + "name": "empty_constructor_needs_parens", + "level": "warn" + }, + "ensure_comprehensions": { + "name": "ensure_comprehensions", + "level": "warn" + }, + "eol_last": { + "name": "eol_last", + "level": "warn" + }, + "indentation": { + "name": "indentation", + "level": "error", + "value": 2 + }, + "line_endings": { + "name": "line_endings", + "level": "warn", + "value": "unix" + }, + "max_line_length": { + "name": "max_line_length", + "value": 120, + "level": "warn", + "limitComments": false + }, + "missing_fat_arrows": { + "name": "missing_fat_arrows", + "level": "ignore" + }, + "newlines_after_classes": { + "name": "newlines_after_classes", + "level": "warn", + "value": 2 + }, + "no_backticks": { + "name": "no_backticks", + "level": "error" + }, + "no_debugger": { + "name": "no_debugger", + "level": "error" + }, + "no_empty_functions": { + "name": "no_empty_functions", + "level": "warn" + }, + "no_empty_param_list": { + "name": "no_empty_param_list", + "level": "warn" + }, + "no_implicit_braces": { + "name": "no_implicit_braces", + "level": "ignore", + "strict": true + }, + "no_implicit_parens": { + "name": "no_implicit_parens", + "level": "ignore" + }, + "no_interpolation_in_single_quotes": { + "name": "no_interpolation_in_single_quotes", + "level": "error" + }, + "no_plusplus": { + "name": "no_plusplus", + "level": "ignore" + }, + "no_stand_alone_at": { + "name": "no_stand_alone_at", + "level": "warn" + }, + "no_tabs": { + "name": "no_tabs", + "level": "error" + }, + "no_this": { + "name": "no_this", + "level": "warn" + }, + "no_throwing_strings": { + "name": "no_throwing_strings", + "level": "error" + }, + "no_trailing_semicolons": { + "name": "no_trailing_semicolons", + "level": "error" + }, + "no_trailing_whitespace": { + "name": "no_trailing_whitespace", + "level": "warn", + "allowed_in_comments": false, + "allowed_in_empty_lines": true + }, + "no_unnecessary_double_quotes": { + "name": "no_unnecessary_double_quotes", + "level": "warn" + }, + "no_unnecessary_fat_arrows": { + "name": "no_unnecessary_fat_arrows", + "level": "warn" + }, + "non_empty_constructor_needs_parens": { + "name": "non_empty_constructor_needs_parens", + "level": "ignore" + }, + "prefer_english_operator": { + "name": "prefer_english_operator", + "level": "warn" + }, + "space_operators": { + "name": "space_operators", + "level": "warn" + }, + "spacing_after_comma": { + "name": "spacing_after_comma", + "level": "warn" + }, + "transform_messes_up_line_numbers": { + "name": "transform_messes_up_line_numbers", + "level": "ignore" + } +} diff --git a/gulpfile.coffee b/gulpfile.coffee index 9dc288e..2fa6ef9 100644 --- a/gulpfile.coffee +++ b/gulpfile.coffee @@ -16,7 +16,7 @@ gulp.task 'browserify', -> extensions: ['.coffee'] debug: true .transform 'coffeeify' - .bundle (error)-> + .bundle (error) -> if error console.error error b.end() diff --git a/lib/connection.coffee b/lib/connection.coffee index a540bbc..e4c6bf8 100644 --- a/lib/connection.coffee +++ b/lib/connection.coffee @@ -10,7 +10,7 @@ class Connection extends EventEmitter allowReconnect: true reconnectionInterval: 500 - constructor: (config = {})-> + constructor: (config = {}) -> config = _.defaults config, defaultConfig @host = config.host @port = config.port @@ -19,36 +19,37 @@ class Connection extends EventEmitter connect: -> unless @socket - connection = @ + connection = this @socket = new WebSocket "ws://#{@host}:#{@port}" - @socket.onopen = ()-> + @socket.onopen = -> + console.info 'Socket open' # nop - @socket.onclose = (data)-> + @socket.onclose = (data) -> handleClose.call connection, data.code, data.reason - @socket.onmessage = (message)-> - if message.data == 'OK' + @socket.onmessage = (message) -> + if message.data is 'OK' handleOpen.call connection else handleResponse.call connection, message.data - @socket.onerror = (error)-> + @socket.onerror = (error) -> console.log 'onerror', error - @ + this handleOpen = -> @stopReconnection() @connected = true @emit 'connect' - handleClose = (code, reason)-> + handleClose = (code, reason) -> delete @socket if @connected @connected = false @emit 'disconnect', code, reason - if code != WS_CLOSE_NORMAL && @allowReconnect + if code isnt WS_CLOSE_NORMAL and @allowReconnect @reconnect @reconnectionInterval - handleResponse = (data)-> - for json in data.split "\n" + handleResponse = (data) -> + for json in data.split '\n' if json.length > 0 response = JSON.parse json @emit response.category, response @@ -57,8 +58,8 @@ class Connection extends EventEmitter @stopReconnection() @socket?.close WS_CLOSE_NORMAL - reconnect: (intervalMillis = @reconnectionInterval)-> - connection = @ + reconnect: (intervalMillis = @reconnectionInterval) -> + connection = this @reconnecting = setTimeout -> connection.connect() , intervalMillis @@ -66,7 +67,7 @@ class Connection extends EventEmitter stopReconnection: -> @reconnecting = clearTimeout @reconnecting - send: (request)-> + send: (request) -> if @socket if request instanceof Object request = JSON.stringify request diff --git a/lib/gazedata.coffee b/lib/gazedata.coffee index 5e44f83..38920e2 100644 --- a/lib/gazedata.coffee +++ b/lib/gazedata.coffee @@ -7,7 +7,7 @@ class Frame @STATE_TRACKING_FAIL = 1 << 3 @STATE_TRACKING_LOST = 1 << 4 - constructor: (@data)-> + constructor: (@data) -> @timestampString = data.timestamp @timestamp = data.time @state = data.state @@ -40,7 +40,7 @@ class Eye @LEFT = 0 @RIGHT = 1 - constructor: (@type, @data)-> + constructor: (@type, @data) -> @raw = @rawCoordinates = new Point2D(data.raw) @average = @smoothedCoordinates = new Point2D(data.avg) @pupilCenter = @pupilCenterCoordinates = new Point2D(data.pcenter) @@ -58,6 +58,7 @@ class Eye ' | pupilCenter:' + @pupilCenter.toString() + ' ]' + class GazeData extends Frame @Eye = Eye diff --git a/lib/gazeutils.coffee b/lib/gazeutils.coffee index 19bd378..c8d2731 100644 --- a/lib/gazeutils.coffee +++ b/lib/gazeutils.coffee @@ -4,11 +4,11 @@ Point2D = require './point2d' class GazeUtils # Find average pupil center of two eyes. - @getEyesCenterNormalized: (leftEye, rightEye)-> + @getEyesCenterNormalized: (leftEye, rightEye) -> if leftEye instanceof GazeData {leftEye, rightEye} = leftEye - if leftEye? && rightEye? + if leftEye? and rightEye? leftEye.pupilCenter.add rightEye.pupilCenter .divide 2 else if leftEye? @@ -17,7 +17,7 @@ class GazeUtils rightEye.pupilCenter # Find average pupil center of two eyes. - @getEyesCenterPixels: (leftEye, rightEye, screenWidth, screenHeight)-> + @getEyesCenterPixels: (leftEye, rightEye, screenWidth, screenHeight) -> if leftEye instanceof GazeData [leftEye, screenWidth, screenHeight] = arguments center = @getEyesCenterNormalized leftEye, rightEye @@ -29,7 +29,7 @@ class GazeUtils # Calculates distance between pupil centers based on previously # recorded min and max values. - @getEyesDistanceNormalized: (leftEye, rightEye)-> + @getEyesDistanceNormalized: (leftEye, rightEye) -> if leftEye instanceof GazeData {leftEye, rightEye} = leftEye @@ -43,21 +43,21 @@ class GazeUtils dist / (maxinumEyesDistance - minimamEyesDistance) # Calculates distance between two points. - @getDistancePoint2D: (p1, p2)-> + @getDistancePoint2D: (p1, p2) -> Math.sqrt Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2) # Converts a relative point to screen point in pixels. - @getRelativeToScreenSpace: (point, screenWidth, screenHeight)-> + @getRelativeToScreenSpace: (point, screenWidth, screenHeight) -> if point? new Point2D Math.round(point.x * screenWidth), Math.round(point.y * screenHeight) # Normalizes a point on screen in screen dims - @getNormalizedCoords: (point, screenWidth, screenHeight)-> + @getNormalizedCoords: (point, screenWidth, screenHeight) -> if point? new Point2D point.x / screenWidth, point.y / screenHeight # Maps eye position of gaze coords in pixels within normalized space [x: -1:1 , y: -1:1] - @getNormalizedMapping: (point, screenWidth, screenHeight)-> + @getNormalizedMapping: (point, screenWidth, screenHeight) -> point = @getNormalizedCoords point, screenHeight, screenHeight if point? new Point2D point.x * 2 - 1, point.y * 2 - 1 diff --git a/lib/heartbeat.coffee b/lib/heartbeat.coffee index 490da3d..954fd3c 100644 --- a/lib/heartbeat.coffee +++ b/lib/heartbeat.coffee @@ -1,11 +1,11 @@ class Heartbeat - constructor: (@tracker)-> - heartbeat = @ - tracker.on 'heartbeatinterval', (intervalMillis)-> + constructor: (@tracker) -> + heartbeat = this + tracker.on 'heartbeatinterval', (intervalMillis) -> heartbeat.restart intervalMillis - start: (@intervalMillis)-> - heartbeat = @ + start: (@intervalMillis) -> + heartbeat = this connection = @tracker.connection if intervalMillis? @@ -17,14 +17,15 @@ class Heartbeat heartbeat.stop() else @tracker.get 'heartbeatinterval' - @ + this - restart: (intervalMillis)-> - if @intervalMillis != intervalMillis + restart: (intervalMillis) -> + if @intervalMillis isnt intervalMillis @stop() @start intervalMillis stop: -> @intervalId = clearInterval @intervalId + module.exports = Heartbeat diff --git a/lib/index.coffee b/lib/index.coffee index c7afc9d..644d834 100644 --- a/lib/index.coffee +++ b/lib/index.coffee @@ -7,8 +7,8 @@ class EyeTribe @Point2D = require('./point2d') @_ = require('underscore') - @loop = (config, callback)-> - if typeof config == 'function' + @loop = (config, callback) -> + if typeof config is 'function' [callback, config] = [config, {}] @loopTracker = new @Tracker config .loop callback diff --git a/lib/point2d.coffee b/lib/point2d.coffee index ca87a63..1234bd7 100644 --- a/lib/point2d.coffee +++ b/lib/point2d.coffee @@ -1,25 +1,25 @@ class Point2D - constructor: (x, y)-> - if x? && !y? + constructor: (x, y) -> + if x? and not y? if x instanceof Array [x, y] = x else if x.x? {x, y} = x else if x.left? {left: x, top: y} = x - @x = x || 0 - @y = y || 0 + @x = x or 0 + @y = y or 0 - add: (p2)-> + add: (p2) -> new Point2D(@x + p2.x, @y + p2.y) - subtract: (p2)-> + subtract: (p2) -> new Point2D(@x - p2.x, @y - p2.y) - multiply: (k)-> + multiply: (k) -> new Point2D(@x * k, @y * k) - divide: (k)-> + divide: (k) -> new Point2D(@x / k, @y / k) average: -> @@ -28,4 +28,5 @@ class Point2D toString: -> "#{@x}, #{@y}" + module.exports = Point2D diff --git a/lib/protocol.coffee b/lib/protocol.coffee index fe8292e..f36edab 100644 --- a/lib/protocol.coffee +++ b/lib/protocol.coffee @@ -36,13 +36,13 @@ class Protocol @TRACKER_CONNECTED_NOSTREAM = 4 - request: (category, request, values)-> + request: (category, request, values) -> JSON.stringify category: category request: request values: values - response: (json)-> + response: (json) -> category: category request: request values: values diff --git a/lib/tracker.coffee b/lib/tracker.coffee index c2465b5..3a29c88 100644 --- a/lib/tracker.coffee +++ b/lib/tracker.coffee @@ -10,12 +10,13 @@ class Tracker extends EventEmitter version: 1 push: true - constructor: (config = {})-> + + constructor: (config = {}) -> @config = _.defaults config, defaultConfig - @heartbeat = new Heartbeat @ + @heartbeat = new Heartbeat this - connect: (config = @config)-> - tracker = @ + connect: (config = @config) -> + tracker = this @connection ?= new Connection config .connect() .on 'tracker', -> @@ -24,16 +25,16 @@ class Tracker extends EventEmitter handleConnect.apply tracker, arguments .on 'disconnect', -> handleDisconnect.apply tracker, arguments - @ + this handleConnect = -> @set @config - @get Protocol.CONFIG_KEYS, (values)-> + @get Protocol.CONFIG_KEYS, (values) -> _.extend @config, values @heartbeat.start() @emit 'connect' - handleDisconnect = (code, reason)-> + handleDisconnect = (code, reason) -> @emit 'disconnect', code, reason @removeAllListeners '__values__' @@ -42,33 +43,33 @@ class Tracker extends EventEmitter delete @connection - handleData = (data)-> - if data.request == 'get' && data.values? + handleData = (data) -> + if data.request is 'get' and data.values? frame = data.values.frame @frame = if frame then new Frame(frame) else undefined @emit '__values__', data.values for key, value of data.values - @emit key, value unless key == 'frame' + @emit key, value unless key is 'frame' @emit 'frame', @frame if @frame - set: (values)-> + set: (values) -> values = _.pick values, Protocol.MUTABLE_CONFIG_KEYS @connection.send category: 'tracker' request: 'set' values: values - @ + this - get: (keys, callback)-> + get: (keys, callback) -> if _.isString keys - do (key = keys)-> + do (key = keys) -> (keys = {})[key] = callback callback = undefined if _.isArray keys if callback? - valuesCallback = (values)-> - @removeListener '__values__', valuesCallback if callback.call @, values + valuesCallback = (values) -> + @removeListener '__values__', valuesCallback if callback.call this, values @on '__values__', valuesCallback else for key, callback of keys @@ -79,9 +80,9 @@ class Tracker extends EventEmitter category: 'tracker' request: 'get' values: keys - @ + this - loop: (callback)-> + loop: (callback) -> @on 'frame', callback .connect() diff --git a/server.coffee b/server.coffee index 93c05f6..a4f75a2 100755 --- a/server.coffee +++ b/server.coffee @@ -24,10 +24,10 @@ TRACKER_PORT = 6555 server = http.createServer() .listen SERVER_PORT, SERVER_HOST, -> console.log 'LISTEN: ' + SERVER_PORT - .on 'request', (request, response)-> - if request.url == '/eyetribe.js' + .on 'request', (request, response) -> + if request.url is '/eyetribe.js' code = readScript "#{__dirname}/eyetribe-#{version}.js" - else if request.url == '/eyetribe.min.js' + else if request.url is '/eyetribe.min.js' code = readScript "#{__dirname}/eyetribe-#{version}.min.js" if code response.writeHead 200, @@ -49,30 +49,30 @@ readFile = (file) -> .toString() ws.attach server - .on 'connection', (client)-> + .on 'connection', (client) -> console.log 'CONNECTED: WS' client - .on 'message', (data)-> + .on 'message', (data) -> console.log 'REQUEST: ' + data tracker.write data - .on 'close', (data)-> + .on 'close', (data) -> console.log 'DISCONNECTED: WS' tracker.end() - .on 'error', (error)-> + .on 'error', (error) -> console.error 'ERROR: WS: ' + error tracker = new net.Socket() - .on 'data', (data)-> + .on 'data', (data) -> #console.log 'RESPONSE: ' + data try client.write data catch e console.error 'ERROR: WS: ' + e - .on 'close', (data)-> + .on 'close', (data) -> console.log 'CLOSED: TRACKER SERVER' client.end() - .on 'error', (error)-> + .on 'error', (error) -> console.error 'ERROR: TRACKER SERVER: ' + error .connect TRACKER_PORT, TRACKER_HOST, -> console.log 'OPENED: TRACKER SERVER'