// Dependencies
var net = require('net')
var config = require('../../config')

function DoorRemoteControl () {
  var self = this

  this.keymaster = null
  this.server = net.createServer(function (c) {
    c.on('data', function (data) {
      // Check if this client is
      // really the keymaster
      if (config.secureKey === data.toString().replace(/\r?\n/, '')) {
        self.keymaster = c
      }
    })
  })
}

DoorRemoteControl.open = function (cb) {
  if (!this.keymaster) {
    return cb(new Error('Keymaster wasn\'t found'))
  }

  this.keymaster.write('open\r\n')
  return cb()
}

DoorRemoteControl.status = function (cb) {
  this.keymaster.write('status\r\n')

  var ttl = setTimeout(function () {
    cb(new Error('off'))

    // Just to be sure
    cb = function () {}
  })

  this.keymaster.once('data', function (data) {
    if (data.toString().replace(/\r?\n/, '') === 'ok') {
      // Clear timeout
      clearTimeout(ttl)

      return cb()
    }
  })
}

module.exports = DoorRemoteControl

@@ -59,19 +59,16 @@
 ire('../
-../
 config')
@@ -429,16 +429,26 @@
 Control.
+prototype.
 open = f
@@ -614,16 +614,26 @@
 Control.
+prototype.
 status =

fix door remote control