1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

Merge pull request #56 from hdoukas/master

added pusher node
This commit is contained in:
Dave Conway-Jones 2014-05-24 16:32:23 +01:00
commit d503c12792
3 changed files with 382 additions and 0 deletions

View File

@ -0,0 +1,195 @@
<!--
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.
-->
<script type="text/x-red" data-template-name="pusher in">
<div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i>App Key:</label>
<input type="text" id="node-input-appkey_sub" placeholder="apikey">
</div>
<div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i>Channel:</label>
<input type="text" id="node-input-channel" placeholder="channel">
</div>
<div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i>Even Name:</label>
<input type="text" id="node-input-eventname" placeholder="eventname">
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
</script>
<!-- Next, some simple help text is provided for the node. -->
<script type="text/x-red" data-help-name="pusher in">
<p>Pusher input mode. Use this node to subscribe to a Pusher channel/event. You need a valid Pusher App key.</p>
</script>
<!-- Finally, the node type is registered along with all of its properties -->
<script type="text/javascript">
RED.nodes.registerType('pusher in',{
category: 'social-output', // the palette category
color:"#A9D0F5",
defaults: { // defines the editable properties of the node
name: {value:""}, // along with default values.
channel: {value:"", required:true},
eventname: {value:"", required:true}
},
inputs:0, // set the number of inputs - only 0 or 1
outputs:1, // set the number of outputs - 0 to n
icon: "pusher.png", // set the icon (held in public/icons)
label: function() { // sets the default label contents
return this.name||this.topic||"pusher";
},
labelStyle: function() { // sets the class to apply to the label
return this.name?"node_label_italic":"";
},
oneditsave: function() {
var credentials = {};
var newAppKey_sub = $('#node-input-appkey_sub').val();
credentials.pusherappkey_sub = newAppKey_sub;
$.ajax({
url: 'pusher/'+this.id,
type: 'POST',
data: credentials,
success: function(result){}
});
},
oneditprepare: function() {
$.getJSON('pusher/'+this.id,function(data) {
if (data.pusherappkey_sub) {
$('#node-input-appkey_sub').val(data.pusherappkey_sub);
}
});
},
ondelete: function() {
$.ajax({
url: 'pusher/'+this.id,
type: 'DELETE',
success: function(result) {}
});
}
});
</script>
<script type="text/x-red" data-template-name="pusher out">
<div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i>App ID:</label>
<input type="text" id="node-input-appid" placeholder="appid">
</div>
<div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i>App Key:</label>
<input type="text" id="node-input-appkey" placeholder="appkey">
</div>
<div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i>App Secret:</label>
<input type="text" id="node-input-appsecret" placeholder="appsecret">
</div>
<div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i>Channel:</label>
<input type="text" id="node-input-channel" placeholder="channel">
</div>
<div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i>Even Name:</label>
<input type="text" id="node-input-eventname" placeholder="eventname">
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
</script>
<!-- Next, some simple help text is provided for the node. -->
<script type="text/x-red" data-help-name="pusher out">
<p>Pusher output node for sending messages to a specific channel/event. You need an App key, secret and ID of a Pusher app. The node will send the payload of the incoming message.</p>
</script>
<!-- Finally, the node type is registered along with all of its properties -->
<script type="text/javascript">
RED.nodes.registerType('pusher out',{
category: 'social-input', // the palette category
color:"#A9D0F5",
defaults: { // defines the editable properties of the node
name: {value:""}, // along with default values.
channel: {value:"", required:true},
eventname: {value:"", required:true},
appid: {value:"", required:true}
},
inputs:1, // set the number of inputs - only 0 or 1
outputs:0, // set the number of outputs - 0 to n
icon: "pusher.png", // set the icon (held in public/icons)
align: "right",
label: function() { // sets the default label contents
return this.name||this.topic||"pusher";
},
labelStyle: function() { // sets the class to apply to the label
return this.name?"node_label_italic":"";
},
oneditsave: function() {
var credentials = {};
var newAppID = $('#node-input-appid').val();
var newAppKey = $('#node-input-appkey').val();
var newAppSecret = $('#node-input-appsecret').val();
credentials.pusherappid = newAppID;
credentials.pusherappkey = newAppKey;
credentials.pusherappsecret = newAppSecret;
$.ajax({
url: 'pusher/'+this.id,
type: 'POST',
data: credentials,
success: function(result){}
});
},
oneditprepare: function() {
$.getJSON('pusher/'+this.id,function(data) {
if (data.pusherappid) {
$('#node-input-appid').val(data.pusherappid);
}
if (data.pusherappkey) {
$('#node-input-appkey').val(data.pusherappkey);
}
if (data.pusherappsecret) {
$('#node-input-appsecret').val(data.pusherappsecret);
}
});
},
ondelete: function() {
$.ajax({
url: 'pusher/'+this.id,
type: 'DELETE',
success: function(result) {}
});
}
});
</script>

187
social/pusher/114-pusher.js Normal file
View File

@ -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 in",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);
});
});

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB