Skip to content
This repository has been archived by the owner on Jun 2, 2023. It is now read-only.

Subscriptions #33

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 66 additions & 1 deletion Helpers/OpenT2T-Wink-Helper/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
var https = require('https');
var q = require('q');




//this is our base, we refactor these options in each method

var protocolVal = 'https:';
Expand Down Expand Up @@ -191,7 +194,69 @@ module.exports =
//make our Promise and give it back to the caller
return deferred.promise;

}
} ,

getSubscription:function()
{

var deferred = q.defer(); // q will help us with returning a promise

var options =
{
protocol: protocolVal,
host: hostVal,
path: apiPath
};

//the headers to make our call
options.headers =
{
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json',
'Accept': 'application/json',
}

//our HTTPS call
var req = https.get(options, (res) => {
var body = ' '; //holds the chunks of data
res.on('data', (chunk) =>
{
body += chunk;
});
res.on('end', () =>
{
try
{
//parse the JSON response and give back the pubnub details
var results = JSON.parse(body.toString());
deferred.resolve(results.data.subscription);

} catch(err)
{
deferred.reject(err);
}

});
res.on('error', (e) => {
deferred.reject(e);
}); });

req.on('error', (e) => {
console.log('problem with request:');
deferred.reject(e);
});

req.end();

//make our Promise and give it back to the caller
return deferred.promise;






}


}
Expand Down
1 change: 1 addition & 0 deletions Helpers/OpenT2T-Wink-Helper/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"dependencies": {
"request":"2.72.0",
"q":"1.4.1"

},
"devDependencies": {
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"devDependencies": {
"optimist": "0.6.1",
"opent2t-onboarding-winkhub": "*",
"q": "1.4.1"
"q": "1.4.1",
"pubnub":"3.15.2"
},
"author": "",
"license": "MIT"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,32 @@ module.exports = {

isButtonPressed: function() {
console.log('getting if doorbell Pressed');


wh.getSubscription().then(result => {
var PubNubkey = result.pubnub.subscribe_key;
var PubNubchannel = result.pubnub.channel;

console.log("key is " + PubNubkey);
var pubnub = require("pubnub")({
ssl : true, // <- enable TLS Tunneling over TCP
subscribe_key : PubNubkey
});

pubnub.subscribe({
channel : PubNubchannel,
message : function(message) {
console.log( " > doorbell");
console.log( " > ", message);
}
});
}).catch(error => {
console.log(error.message);
throw error;
});;



return logPromise(wh.getLastReading('button_pressed'));
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"async": "~1.5.2",
"optimist": "0.6.1",
"opent2t-onboarding-winkhub": "*",
"q": "1.4.1"
"q": "1.4.1",
"pubnub":"3.15.2"
},
"author": "",
"license": "MIT"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,29 @@ module.exports = {

getSmokeDetected: function() {
console.log('checking for smoke:');

wh.getSubscription().then(result => {
//get details
var PubNubkey = result.pubnub.subscribe_key;
var PubNubchannel = result.pubnub.channel;

console.log("key is " + PubNubkey);
var pubnub = require("pubnub")({
ssl : true, // <- enable TLS Tunneling over TCP
subscribe_key : PubNubkey
});
//call this whenever a change happens
pubnub.subscribe({
channel : PubNubchannel,
message : function(message) {
console.log( " > Smoke State Changed!");
console.log( " > ", message);
}
});
}).catch(error => {
console.log(error.message);
throw error;
});;

return logPromise(wh.getLastReading('smoke_detected'));
},
Expand Down