var Accessory = require('../').Accessory;
var Service = require('../').Service;
var Characteristic = require('../').Characteristic;
var uuid = require('../').uuid;

// here's a fake hardware device that we'll expose to HomeKit
var FAKE_LIGHT = {
  powerOn: false,
  brightness: 100, // percentage
  
  setPowerOn: function(on) { 
    console.log("Turning the light %s!", on ? "on" : "off");
    FAKE_LIGHT.powerOn = true;
  },
  setBrightness: function(brightness) {
    console.log("Setting light brightness to %s", brightness);
    FAKE_LIGHT.brightness = brightness;
  },
  identify: function() {
    console.log("Identify the light!");
  }
}

// Generate a consistent UUID for our light Accessory that will remain the same even when
// restarting our server. We use the `uuid.generate` helper function to create a deterministic
// UUID based on an arbitrary "namespace" and the word "light".
var lightUUID = uuid.generate('hap-nodejs:accessories:light');

// This is the Accessory that we'll return to HAP-NodeJS that represents our fake light.
var light = exports.accessory = new Accessory('Light', lightUUID);

// Add properties for publishing (in case we're using Core.js and not BridgedCore.js)
light.username = "1A:2B:3C:4D:5E:FF";
light.pincode = "031-45-154";

// set some basic properties (these values are arbitrary and setting them is optional)
light
  .getService(Service.AccessoryInformation)
  .setCharacteristic(Characteristic.Manufacturer, "Oltica")
  .setCharacteristic(Characteristic.Model, "Rev-1")
  .setCharacteristic(Characteristic.SerialNumber, "A1S2NASF88EW");

// listen for the "identify" event for this Accessory
light.on('identify', function(paired, callback) {
  FAKE_LIGHT.identify();
  callback(); // success
});

// Add the actual Lightbulb Service and listen for change events from iOS.
// We can see the complete list of Services and Characteristics in `lib/gen/HomeKitTypes.js`
light
  .addService(Service.Lightbulb, "Fake Light") // services exposed to the user should have "names" like "Fake Light" for us
  .getCharacteristic(Characteristic.On)
  .on('set', function(value, callback) {
    FAKE_LIGHT.setPowerOn(value);
    callback(); // Our fake Light is synchronous - this value has been successfully set
  });

// also add an "optional" Characteristic for Brightness
light
  .getService(Service.Lightbulb)
  .addCharacteristic(Characteristic.Brightness)
  .on('set', function(value, callback) {
    FAKE_LIGHT.setBrightness(value);
    callback();
  })

// We want to intercept requests for our current power state so we can query the hardware itself instead of
// allowing HAP-NodeJS to return the cached Characteristic.value.
light
  .getService(Service.Lightbulb)
  .getCharacteristic(Characteristic.On)
  .on('get', function(callback) {
    
    // this event is emitted when you ask Siri directly whether your light is on or not. you might query
    // the light hardware itself to find this out, then call the callback. But if you take longer than a
    // few seconds to respond, Siri will give up.
    
    var err = null; // in case there were any problems
    
    if (FAKE_LIGHT.on) {
      console.log("Are we on? Yes.");
      callback(err, true);
    }
    else {
      console.log("Are we on? No.");
      callback(err, false);
    }
  });

@@ -409,20 +409,18 @@
 werOn = 
-true
+on
 ;%0A  %7D,%0A 
@@ -2251,251 +2251,8 @@
 );%0A%0A
-// also add an %22optional%22 Characteristic for Brightness%0Alight%0A  .getService(Service.Lightbulb)%0A  .addCharacteristic(Characteristic.Brightness)%0A  .on('set', function(value, callback) %7B%0A    FAKE_LIGHT.setBrightness(value);%0A    callback();%0A  %7D)%0A%0A
 // W
@@ -2883,17 +2883,22 @@
 E_LIGHT.
-o
+powerO
 n) %7B%0A   
@@ -3047,14 +3047,339 @@
 ;%0A    %7D%0A
-
   %7D);%0A
+%0A// also add an %22optional%22 Characteristic for Brightness%0Alight%0A  .getService(Service.Lightbulb)%0A  .addCharacteristic(Characteristic.Brightness)%0A  .on('get', function(callback) %7B%0A    callback(null, FAKE_LIGHT.brightness);%0A  %7D)%0A  .on('set', function(value, callback) %7B%0A    FAKE_LIGHT.setBrightness(value);%0A    callback();%0A  %7D)%0A

Fix for sample Light accessory