Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. var CTRL_CHAN, DOOR_STATES, HOME_CHAN, LIFTMASTER_PASSWORD, LIFTMASTER_USERNAME,
  2. MyQ, PUB_KEY, PubNub, SUB_KEY, garage, initPubNub, makeHandler, pubnubConns,
  3. refreshStatus, _;
  4.  
  5. _ = require('underscore');
  6. MyQ = require('liftmaster');
  7. PubNub = require('pubnub');
  8.  
  9. LIFTMASTER_USERNAME = 'YOUR_LIFTMASTER_USERNAME';
  10. LIFTMASTER_PASSWORD = 'YOUR_LIFTMASTER_PASSWORD';
  11.  
  12. PUB_KEY = 'YOUR_PUB_KEY';
  13. SUB_KEY = 'YOUR_SUB_KEY';
  14.  
  15. HOME_CHAN = 'MyHome';
  16. CTRL_CHAN = 'MyHome_Ctrl';
  17.  
  18. DOOR_STATES = {
  19. '0': 'closed',
  20. '1': 'open',
  21. '2': 'closed',
  22. '4': 'opening',
  23. '5': 'closing'
  24. };
  25.  
  26. pubnubConns = {};
  27.  
  28. garage = new MyQ(LIFTMASTER_USERNAME, LIFTMASTER_PASSWORD);
  29.  
  30. makeHandler = function(uuid) {
  31. return function(pnMessage) {
  32. var command, newState;
  33. if (!((pnMessage.subscribedChannel === CTRL_CHAN) && (pnMessage.message.target === uuid))) {
  34. return;
  35. }
  36. command = pnMessage.message;
  37.  
  38. if (!pubnubConns[uuid]) {
  39. return;
  40. }
  41.  
  42. newState = (command.newState === "open") ? "1" : "0";
  43.  
  44. garage.setDoorState(uuid, newState, function() {
  45. console.log("SETTING DOOR STATE to: " + JSON.stringify({ uuid: uuid, newState: newState }));
  46. });
  47. };
  48. };
  49.  
  50. initPubNub = function() {
  51. garage.getDevices(function(err, devices) {
  52. _(devices).forEach(function(door) {
  53. var handler, pn, uuid;
  54. uuid = door.id;
  55. door.type = 'Garage Door';
  56.  
  57. pn = new PubNub({
  58. publishKey: PUB_KEY,
  59. subscribeKey: SUB_KEY,
  60. uuid: uuid,
  61. ssl: true
  62. });
  63.  
  64. pn.setState({
  65. channels: [HOME_CHAN, CTRL_CHAN],
  66. state: door
  67. });
  68.  
  69. pn.publish({
  70. channel: HOME_CHAN,
  71. message: door
  72. });
  73.  
  74. pn.subscribe({
  75. channels: [HOME_CHAN, CTRL_CHAN],
  76. withPresence: true
  77. });
  78.  
  79. handler = makeHandler(uuid);
  80.  
  81. pn.addListener({
  82. status: handler,
  83. message: handler,
  84. presence: handler
  85. });
  86.  
  87. pubnubConns[uuid] = pn;
  88. });
  89. });
  90. };
  91.  
  92. refreshStatus = function() {
  93. garage.getDevices(function(err, devices) {
  94. if (err) {
  95. throw err;
  96. }
  97.  
  98. _(devices).forEach(function(door) {
  99. var pn, uuid;
  100. door.type = 'Garage Door';
  101. uuid = door.id;
  102. pn = pubnubConns[uuid];
  103.  
  104. pn.setState({
  105. channels: [HOME_CHAN, CTRL_CHAN],
  106. state: door
  107. });
  108.  
  109. pn.publish({
  110. channel: HOME_CHAN,
  111. message: door
  112. });
  113. });
  114. });
  115. };
  116.  
  117. garage.login(function(err, res) {
  118. if (err) {
  119. throw err;
  120. }
  121. console.log('logged in', JSON.stringify(res));
  122. initPubNub();
  123. setInterval(refreshStatus, 3000);
  124. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement