Minor restructoring

This commit is contained in:
Andrew Stewart 2013-10-21 10:27:47 -07:00
parent e7a628b9eb
commit 21e225a4d3
1 changed files with 24 additions and 31 deletions

View File

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