Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. var Accessory = require('../').Accessory;
  2. var Service = require('../').Service;
  3. var Characteristic = require('../').Characteristic;
  4. var uuid = require('../').uuid;
  5. var mqtt = require('mqtt');
  6. var options = {
  7. port: 1883,
  8. host: '192.168.1.99',
  9. clientId: 'Bedroom plug accessory'
  10. };
  11. var client = mqtt.connect(options);
  12. console.log("Bedroom plug Connected to MQTT broker");
  13.  
  14.  
  15. // here's a fake hardware device that we'll expose to HomeKit
  16. var FAKELIGHT = {
  17. powerOn: false,
  18. brightness: 100, // percentage
  19.  
  20. setPowerOn: function(on) {
  21. console.log("Turning the light %s!", on ? "on" : "off");
  22. FAKELIGHT.powerOn = on;
  23. console.log(on);
  24. if(on){
  25. client.publish('bedroomplug', 'on');
  26. }
  27. else{
  28. client.publish('bedroomplug', 'off');
  29. }
  30. },
  31. setBrightness: function(brightness) {
  32. console.log("Setting light brightness to %s", brightness);
  33. FAKELIGHT.brightness = brightness;
  34. },
  35. identify: function() {
  36. console.log("Identify the light!");
  37. }
  38. }
  39.  
  40. // Generate a consistent UUID for our light Accessory that will remain the same even when
  41. // restarting our server. We use the `uuid.generate` helper function to create a deterministic
  42. // UUID based on an arbitrary "namespace" and the word "light".
  43. var lightUUID = uuid.generate('hap-nodejs:accessories:bedroomplug');
  44.  
  45. // This is the Accessory that we'll return to HAP-NodeJS that represents our fake light.
  46. var light = exports.accessory = new Accessory('Bedroom plug', lightUUID);
  47.  
  48. // Add properties for publishing (in case we're using Core.js and not BridgedCore.js)
  49. light.username = "FF:FF:FF:FF:FF:FF:A6";
  50. light.pincode = "031-45-154";
  51.  
  52. // set some basic properties (these values are arbitrary and setting them is optional)
  53. light
  54. .getService(Service.AccessoryInformation)
  55. .setCharacteristic(Characteristic.Manufacturer, "Oltica")
  56. .setCharacteristic(Characteristic.Model, "Rev-1")
  57. .setCharacteristic(Characteristic.SerialNumber, "A1S2NASF88EW");
  58.  
  59. // listen for the "identify" event for this Accessory
  60. light.on('identify', function(paired, callback) {
  61. FAKELIGHT.identify();
  62. callback(); // success
  63. });
  64.  
  65. // Add the actual Lightbulb Service and listen for change events from iOS.
  66. // We can see the complete list of Services and Characteristics in `lib/gen/HomeKitTypes.js`
  67. light
  68. .addService(Service.Lightbulb, "Bedroom plug") // services exposed to the user should have "names" like "Fake Light" for us
  69. .getCharacteristic(Characteristic.On)
  70. .on('set', function(value, callback) {
  71. FAKELIGHT.setPowerOn(value);
  72. callback(); // Our fake Light is synchronous - this value has been successfully set
  73. });
  74.  
  75. // We want to intercept requests for our current power state so we can query the hardware itself instead of
  76. // allowing HAP-NodeJS to return the cached Characteristic.value.
  77. light
  78. .getService(Service.Lightbulb)
  79. .getCharacteristic(Characteristic.On)
  80. .on('get', function(callback) {
  81.  
  82. // this event is emitted when you ask Siri directly whether your light is on or not. you might query
  83. // the light hardware itself to find this out, then call the callback. But if you take longer than a
  84. // few seconds to respond, Siri will give up.
  85.  
  86. var err = null; // in case there were any problems
  87.  
  88. if (FAKELIGHT.powerOn) {
  89. console.log("Are we on? Yes.");
  90. callback(err, true);
  91. }
  92. else {
  93. console.log("Are we on? No.");
  94. callback(err, false);
  95. }
  96. });
  97.  
  98. // also add an "optional" Characteristic for Brightness
  99. light
  100. .getService(Service.Lightbulb)
  101. .addCharacteristic(Characteristic.Brightness)
  102. .on('get', function(callback) {
  103. callback(null, FAKELIGHT.brightness);
  104. })
  105. .on('set', function(value, callback) {
  106. FAKELIGHT.setBrightness(value);
  107. callback();
  108. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement