Merge pull request #1 from hybridgroup/robot-reorganization

Robot.coffee - minor reorganization
This commit is contained in:
Ron Evans 2013-10-21 10:43:51 -07:00
commit eb984aa447
1 changed files with 24 additions and 31 deletions

View File

@ -12,50 +12,43 @@ Connection = source("connection")
Device = source("device")
module.exports = class Robot
constructor: (opts = {}) ->
@connectionTypes = {}
@deviceTypes = {}
constructor: (opts = {}) ->
@name = opts.name or @constructor.randomName()
@connections = initConnections(opts.connection or opts.connections or {})
@devices = initDevices(opts.device or opts.devices or {})
@work = opts.work or -> (console.log "No work yet")
@randomName: ->
"Robot #{ Math.floor(Math.random() * 100000) }"
initConnections = (connections) ->
console.log "Initializing connections..."
for connection in connections
console.log "Initializing connection '#{ connection.name }'..."
@connectionTypes[connection.name] = new Connection(connection)
initDevices = (devices) ->
console.log "Initializing devices..."
for device in devices
console.log "Initializing device '#{ device.name }'..."
@deviceTypes[device.name] = new Device(device)
start: ->
@startConnections()
@startDevices()
(@work)()
initConnections = (connections) ->
console.log "Initializing connections..."
initConnection connection for connection in connections
initConnection = (connection) ->
console.log "Initializing connection '#{ connection.name }'..."
@connectionTypes[connection.name] = new Connection(connection)
initDevices = (devices) ->
console.log "Initializing devices..."
initDevice device for device in devices
initDevice = (device) ->
console.log "Initializing device '#{ device.name }'..."
@deviceTypes[device.name] = new Device(device)
startConnections: ->
console.log "Starting connections..."
startConnection connection for n, connection of @connectionTypes
startConnection: (connection) ->
for n, connection of @connectionTypes
console.log "Starting connection '#{ connection.name }'..."
connection.connect()
startDevices: ->
console.log "Starting devices..."
startDevice device for n, device of @deviceTypes
startDevice: (device) ->
for n, device of @deviceTypes
console.log "Starting device '#{ device.name }'..."
device.start()
@randomName: ->
"Robot #{ Math.floor(Math.random() * 100000) }"