From 863ff7f18b4d1fb349d25f4c19accfac7491a77f Mon Sep 17 00:00:00 2001 From: hdoukas Date: Sun, 18 May 2014 23:10:00 +0200 Subject: [PATCH] merged html and js merged into a single html and single js file fixed copyright typo added credentials proper handling --- social/pusher/114-pusher.html | 176 +++++++++++++++++++++++++++ social/pusher/114-pusher.js | 187 +++++++++++++++++++++++++++++ social/pusher/114-pusher_send.html | 81 ------------- social/pusher/114-pusher_send.js | 80 ------------ social/pusher/114-pusher_sub.html | 68 ----------- social/pusher/114-pusher_sub.js | 80 ------------ 6 files changed, 363 insertions(+), 309 deletions(-) create mode 100644 social/pusher/114-pusher.html create mode 100644 social/pusher/114-pusher.js delete mode 100644 social/pusher/114-pusher_send.html delete mode 100644 social/pusher/114-pusher_send.js delete mode 100644 social/pusher/114-pusher_sub.html delete mode 100644 social/pusher/114-pusher_sub.js diff --git a/social/pusher/114-pusher.html b/social/pusher/114-pusher.html new file mode 100644 index 00000000..a4596f87 --- /dev/null +++ b/social/pusher/114-pusher.html @@ -0,0 +1,176 @@ + + + + + + + + + + + + + + + + + diff --git a/social/pusher/114-pusher.js b/social/pusher/114-pusher.js new file mode 100644 index 00000000..90ecf0c7 --- /dev/null +++ b/social/pusher/114-pusher.js @@ -0,0 +1,187 @@ +/** + * pusher_send.js + * Subscription module for the Pusher service (www.pusher.com) + * Requires 'pusher' module + * Copyright 2014 Charalampos Doukas - @BuildingIoT + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ + + +var Pusher = require('pusher'); +var PusherClient = require('pusher-client'); + +// Require main module +var RED = require(process.env.NODE_RED_HOME+"/red/red"); + + +// The main node definition - most things happen in here +//Node for sending Pusher events +function PusherNodeSend(n) { + // Create a RED node + RED.nodes.createNode(this,n); + + var node = this; + + var credentials = RED.nodes.getCredentials(n.id); + + if ((credentials) && (credentials.hasOwnProperty("pusherappid"))) { this.appid = credentials.pusherappid; } + else { this.error("No Pusher api token set"); } + if ((credentials) && (credentials.hasOwnProperty("pusherappsecret"))) { this.appsecret = credentials.pusherappsecret; } + else { this.error("No Pusher user secret set"); } + + if ((credentials) && (credentials.hasOwnProperty("pusherappkey"))) { this.appkey = credentials.pusherappkey; } + else { this.error("No Pusher user key set"); } + + //get parameters from user + this.channel = n.channel; + this.eventname = n.eventname; + + var pusher = new Pusher({ + appId: this.appid, + key: this.appkey, + secret: this.appsecret + }); + + + this.on("input", function(msg){ + + pusher.trigger(this.channel, this.eventname, { + "message": ""+msg.payload + }); + + }); + + + this.on("close", function() { + // Called when the node is shutdown - eg on redeploy. + // Allows ports to be closed, connections dropped etc. + // eg: this.client.disconnect(); + }); + + } + +//node for subscribing to an event/channel + function PusherNode(n) { + // Create a RED node + RED.nodes.createNode(this,n); + + var node = this; + var credentials = RED.nodes.getCredentials(n.id); + + if ((credentials) && (credentials.hasOwnProperty("pusherappkey_sub"))) { this.appkey = credentials.pusherappkey_sub; } + else { this.error("No Pusher app key set for input node"); } + + //get parameters from user + this.channel = n.channel; + this.eventname = n.eventname; + + + + //create a subscription to the channel and event defined by user + var socket = new PusherClient(''+this.appkey); + var my_channel = socket.subscribe(''+this.channel); + socket.bind(''+this.eventname, + function(data) { + + var msg = {}; + msg.payload = data; + node.send(msg); + } + ); + + this.on("input", function(msg){ + + }); + + + this.on("close", function() { + // Called when the node is shutdown - eg on redeploy. + // Allows ports to be closed, connections dropped etc. + // eg: this.client.disconnect(); + socket.disconnect(); + }); + + } + + //debugging on the output: + var displayResult = function(result) { + console.log(result); +}; + +var displayError = function(err) { + console.error(err); +}; + + + + +// Register the node by name. This must be called before overriding any of the +// Node functions. +RED.nodes.registerType("Pusher out",PusherNodeSend); +RED.nodes.registerType("Pusher",PusherNode); + +var querystring = require('querystring'); + +RED.httpAdmin.get('/pusher/:id',function(req,res) { + var credentials = RED.nodes.getCredentials(req.params.id); + if (credentials) { + res.send(JSON.stringify({pusherappid:credentials.pusherappid,pusherappsecret:credentials.pusherappsecret, pusherappkey:credentials.pusherappkey, pusherappkey_sub:credentials.pusherappkey_sub})); + } else { + res.send(JSON.stringify({})); + } +}); + +RED.httpAdmin.delete('/pusher/:id',function(req,res) { + RED.nodes.deleteCredentials(req.params.id); + res.send(200); +}); + +RED.httpAdmin.post('/pusher/:id',function(req,res) { + var body = ""; + req.on('data', function(chunk) { + body+=chunk; + }); + req.on('end', function(){ + var newCreds = querystring.parse(body); + var credentials = RED.nodes.getCredentials(req.params.id)||{}; + + if (newCreds.pusherappid == null || newCreds.pusherappid == "") { + delete credentials.pusherappid; + } else { + credentials.pusherappid = newCreds.pusherappid; + } + if (newCreds.pusherappkey == "") { + delete credentials.pusherappkey; + } else { + credentials.pusherappkey = newCreds.pusherappkey||credentials.pusherappkey; + } + + if (newCreds.pusherappsecret == "") { + delete credentials.pusherappsecret; + } else { + credentials.pusherappsecret = newCreds.pusherappsecret||credentials.pusherappsecret; + } + + if (newCreds.pusherappkey_sub == "") { + delete credentials.pusherappkey_sub; + } else { + credentials.pusherappkey_sub = newCreds.pusherappkey_sub||credentials.pusherappkey_sub; + } + + + RED.nodes.addCredentials(req.params.id,credentials); + res.send(200); + }); +}); + diff --git a/social/pusher/114-pusher_send.html b/social/pusher/114-pusher_send.html deleted file mode 100644 index cb3d00d0..00000000 --- a/social/pusher/114-pusher_send.html +++ /dev/null @@ -1,81 +0,0 @@ - - - - - - - - - diff --git a/social/pusher/114-pusher_send.js b/social/pusher/114-pusher_send.js deleted file mode 100644 index ab2c5fd5..00000000 --- a/social/pusher/114-pusher_send.js +++ /dev/null @@ -1,80 +0,0 @@ -/** - * pusher_send.js - * Subscription module for the Pusher service (www.pusher.com) - * Requires 'pusher' module - * Copyright 2014 Charalampos Doukas - @BuildingIoT - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - **/ - - -var Pusher = require('pusher'); - -// Require main module -var RED = require(process.env.NODE_RED_HOME+"/red/red"); - - -// The main node definition - most things happen in here -function PusherNodeSend(n) { - // Create a RED node - RED.nodes.createNode(this,n); - - var node = this; - - //get parameters from user - this.appid = n.appid; - this.appkey = n.appkey; - this.appsecret = n.appsecret; - this.channel = n.channel; - this.eventname = n.eventname; - - - var pusher = new Pusher({ - appId: this.appid, - key: this.appkey, - secret: this.appsecret - }); - - - this.on("input", function(msg){ - - pusher.trigger(this.channel, this.eventname, { - "message": ""+msg.payload - }); - - }); - - - this.on("close", function() { - // Called when the node is shutdown - eg on redeploy. - // Allows ports to be closed, connections dropped etc. - // eg: this.client.disconnect(); - }); - - } - - //hue debugging on the output: - var displayResult = function(result) { - console.log(result); -}; - -var displayError = function(err) { - console.error(err); -}; - - - - -// Register the node by name. This must be called before overriding any of the -// Node functions. -RED.nodes.registerType("Pusher out",PusherNodeSend); diff --git a/social/pusher/114-pusher_sub.html b/social/pusher/114-pusher_sub.html deleted file mode 100644 index 270eb20b..00000000 --- a/social/pusher/114-pusher_sub.html +++ /dev/null @@ -1,68 +0,0 @@ - - - - - - - - - diff --git a/social/pusher/114-pusher_sub.js b/social/pusher/114-pusher_sub.js deleted file mode 100644 index 96012ae5..00000000 --- a/social/pusher/114-pusher_sub.js +++ /dev/null @@ -1,80 +0,0 @@ -/** - * pusher_sub.js - * Subscription module for the Pusher service (www.pusher.com) - * Requires 'pusher-client' module - * Copyright 2014 Charalampos Doukas - @BuildingIoT - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - **/ - - -var Pusher = require('pusher-client'); - -// Require main module -var RED = require(process.env.NODE_RED_HOME+"/red/red"); - - -// The main node definition - most things happen in here -function PusherNode(n) { - // Create a RED node - RED.nodes.createNode(this,n); - - var node = this; - - //get parameters from user - this.apikey = n.apikey; - this.channel = n.channel; - this.eventname = n.eventname; - - - - //create a subscription to the channel and event defined by user - var socket = new Pusher(''+this.apikey); - var my_channel = socket.subscribe(''+this.channel); - socket.bind(''+this.eventname, - function(data) { - - var msg = {}; - msg.payload = data; - node.send(msg); - } - ); - - this.on("input", function(msg){ - - }); - - - this.on("close", function() { - // Called when the node is shutdown - eg on redeploy. - // Allows ports to be closed, connections dropped etc. - // eg: this.client.disconnect(); - }); - - } - - //hue debugging on the output: - var displayResult = function(result) { - console.log(result); -}; - -var displayError = function(err) { - console.error(err); -}; - - - - -// Register the node by name. This must be called before overriding any of the -// Node functions. -RED.nodes.registerType("Pusher",PusherNode);