           callback error, result
       )
 
-  getDeviceOperations: (newDevices=[], oldDevices=[], callback=->) =>
-    debug 'getDeviceOperations'
+  getDevicesByOperation: (newDevices=[], callback=->) =>
+    oldDevices = _.clone @runningDevices
+    debug 'getDevicesByOperation'
     debug 'newDevices', _.pluck(newDevices, 'name')
     debug 'oldDevices', _.pluck(oldDevices, 'name')
 
     devicesToProcess = _.clone newDevices
     async.map devicesToProcess, @deviceExists, (error, remainingDevices) =>
       return callback error if error?
+
       remainingDevices = _.compact remainingDevices
       debug 'devices that exist', _.pluck(remainingDevices, 'name')
 

      remainingDevices = _.difference remainingDevices, devicesToStop

      devicesToStart = _.reject remainingDevices, (device) =>
        _.findWhere oldDevices, uuid: device.uuid

      debug 'devicesToStart:', _.pluck(devicesToStart, 'name')



class DeviceManager extends EventEmitter
  constructor: (@config) ->
    @deviceProcesses = {}
    @runningDevices = []

  refreshDevices: (devices, callback) =>
    debug 'refreshDevices', _.pluck(devices, 'uuid')

    @getDevicesByOperation devices, (devicesToStart, devicesToStop, devicesToRestart, unchangedDevices) =>
      connectorsToInstall = _.uniq _.pluck devicesToStart, 'connector'

      async.series(
        [
          (callback) => async.each connectorsToInstall, @installConnector, callback
          (callback) => async.each devicesToStop, @stopDevice, callback
          (callback) => async.each devicesToStart, @setupDevice, callback
          (callback) => async.each devicesToStart, @startDevice, callback
          (callback) => async.each devicesToRestart, @restartDevice, callback
        ]
        (error, result)=>
