diff --git a/src/robot.coffee b/src/robot.coffee index 57926cb..bdac3f3 100644 --- a/src/robot.coffee +++ b/src/robot.coffee @@ -12,50 +12,43 @@ Connection = source("connection") Device = source("device") module.exports = class Robot + @connectionTypes = {} + @deviceTypes = {} + constructor: (opts = {}) -> - @connectionTypes = {} - @deviceTypes = {} @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) -> - console.log "Starting connection '#{ connection.name }'..." - connection.connect() + 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) -> - console.log "Starting device '#{ device.name }'..." - device.start() - - @randomName: -> - "Robot #{ Math.floor(Math.random() * 100000) }" + for n, device of @deviceTypes + console.log "Starting device '#{ device.name }'..." + device.start()