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

Quick tidy up of pusher node - remove final key from flow,

fix in/out order on palette.
This commit is contained in:
Dave C-J 2014-05-24 16:38:35 +01:00
parent 6d7ecea8cd
commit fe0222d498
2 changed files with 85 additions and 127 deletions

View File

@ -1,4 +1,5 @@
<!-- <!--
Copyright 2014 Charalampos Doukas, @BuildingIoT Copyright 2014 Charalampos Doukas, @BuildingIoT
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
@ -13,26 +14,21 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
--> -->
<script type="text/x-red" data-template-name="pusher in"> <script type="text/x-red" data-template-name="pusher in">
<div class="form-row"> <div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i>App Key:</label> <label for="node-input-topic"><i class="icon-tasks"></i>App Key</label>
<input type="text" id="node-input-appkey_sub" placeholder="apikey"> <input type="text" id="node-input-appkey_sub" placeholder="apikey">
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i>Channel:</label> <label for="node-input-topic"><i class="icon-tasks"></i>Channel</label>
<input type="text" id="node-input-channel" placeholder="channel"> <input type="text" id="node-input-channel" placeholder="channel">
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i>Even Name:</label> <label for="node-input-topic"><i class="icon-tasks"></i>Event Name</label>
<input type="text" id="node-input-eventname" placeholder="eventname"> <input type="text" id="node-input-eventname" placeholder="eventname">
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label> <label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
@ -47,7 +43,7 @@
<!-- Finally, the node type is registered along with all of its properties --> <!-- Finally, the node type is registered along with all of its properties -->
<script type="text/javascript"> <script type="text/javascript">
RED.nodes.registerType('pusher in',{ RED.nodes.registerType('pusher in',{
category: 'social-output', // the palette category category: 'social-input', // the palette category
color:"#A9D0F5", color:"#A9D0F5",
defaults: { // defines the editable properties of the node defaults: { // defines the editable properties of the node
name: {value:""}, // along with default values. name: {value:""}, // along with default values.
@ -58,7 +54,7 @@
outputs:1, // set the number of outputs - 0 to n outputs:1, // set the number of outputs - 0 to n
icon: "pusher.png", // set the icon (held in public/icons) icon: "pusher.png", // set the icon (held in public/icons)
label: function() { // sets the default label contents label: function() { // sets the default label contents
return this.name||this.topic||"pusher"; return this.name||"pusher";
}, },
labelStyle: function() { // sets the class to apply to the label labelStyle: function() { // sets the class to apply to the label
return this.name?"node_label_italic":""; return this.name?"node_label_italic":"";
@ -67,8 +63,6 @@
var credentials = {}; var credentials = {};
var newAppKey_sub = $('#node-input-appkey_sub').val(); var newAppKey_sub = $('#node-input-appkey_sub').val();
credentials.pusherappkey_sub = newAppKey_sub; credentials.pusherappkey_sub = newAppKey_sub;
$.ajax({ $.ajax({
url: 'pusher/'+this.id, url: 'pusher/'+this.id,
type: 'POST', type: 'POST',
@ -119,7 +113,6 @@
<input type="text" id="node-input-eventname" placeholder="eventname"> <input type="text" id="node-input-eventname" placeholder="eventname">
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label> <label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
@ -134,20 +127,19 @@
<!-- Finally, the node type is registered along with all of its properties --> <!-- Finally, the node type is registered along with all of its properties -->
<script type="text/javascript"> <script type="text/javascript">
RED.nodes.registerType('pusher out',{ RED.nodes.registerType('pusher out',{
category: 'social-input', // the palette category category: 'social-output', // the palette category
color:"#A9D0F5", color:"#A9D0F5",
defaults: { // defines the editable properties of the node defaults: { // defines the editable properties of the node
name: {value:""}, // along with default values. name: {value:""}, // along with default values.
channel: {value:"", required:true}, channel: {value:"", required:true},
eventname: {value:"", required:true}, eventname: {value:"", required:true}
appid: {value:"", required:true}
}, },
inputs:1, // set the number of inputs - only 0 or 1 inputs:1, // set the number of inputs - only 0 or 1
outputs:0, // set the number of outputs - 0 to n outputs:0, // set the number of outputs - 0 to n
icon: "pusher.png", // set the icon (held in public/icons) icon: "pusher.png", // set the icon (held in public/icons)
align: "right", align: "right",
label: function() { // sets the default label contents label: function() { // sets the default label contents
return this.name||this.topic||"pusher"; return this.name||"pusher";
}, },
labelStyle: function() { // sets the class to apply to the label labelStyle: function() { // sets the class to apply to the label
return this.name?"node_label_italic":""; return this.name?"node_label_italic":"";
@ -160,8 +152,6 @@
credentials.pusherappid = newAppID; credentials.pusherappid = newAppID;
credentials.pusherappkey = newAppKey; credentials.pusherappkey = newAppKey;
credentials.pusherappsecret = newAppSecret; credentials.pusherappsecret = newAppSecret;
$.ajax({ $.ajax({
url: 'pusher/'+this.id, url: 'pusher/'+this.id,
type: 'POST', type: 'POST',
@ -177,11 +167,9 @@
if (data.pusherappkey) { if (data.pusherappkey) {
$('#node-input-appkey').val(data.pusherappkey); $('#node-input-appkey').val(data.pusherappkey);
} }
if (data.pusherappsecret) { if (data.pusherappsecret) {
$('#node-input-appsecret').val(data.pusherappsecret); $('#node-input-appsecret').val(data.pusherappsecret);
} }
}); });
}, },
ondelete: function() { ondelete: function() {

View File

@ -1,8 +1,9 @@
/** /**
* pusher_send.js * pusher.js
* Subscription module for the Pusher service (www.pusher.com) * Subscription module for the Pusher service (www.pusher.com)
* Requires 'pusher' module * Requires 'pusher' and 'pusher-client' modules.
* Copyright 2014 Charalampos Doukas - @BuildingIoT *
* Copyright 2014 Charalampos Doukas, @BuildingIoT
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,60 +18,12 @@
* limitations under the License. * limitations under the License.
**/ **/
var Pusher = require('pusher'); var Pusher = require('pusher');
var PusherClient = require('pusher-client'); var PusherClient = require('pusher-client');
// Require main module // Require main module
var RED = require(process.env.NODE_RED_HOME+"/red/red"); 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 //node for subscribing to an event/channel
function PusherNode(n) { function PusherNode(n) {
// Create a RED node // Create a RED node
@ -86,50 +39,69 @@ function PusherNodeSend(n) {
this.channel = n.channel; this.channel = n.channel;
this.eventname = n.eventname; this.eventname = n.eventname;
//create a subscription to the channel and event defined by user //create a subscription to the channel and event defined by user
var socket = new PusherClient(''+this.appkey); var socket = new PusherClient(''+this.appkey);
var my_channel = socket.subscribe(''+this.channel); var my_channel = socket.subscribe(''+this.channel);
socket.bind(''+this.eventname, socket.bind(''+this.eventname,
function(data) { function(data) {
var msg = {}; var msg = {};
msg.payload = data; msg.payload = data;
node.send(msg); node.send(msg);
} }
); );
this.on("input", function(msg){
});
this.on("close", function() { 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(); socket.disconnect();
}); });
}
//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() {
});
} }
//debugging on the output: //debugging on the output:
var displayResult = function(result) { var displayResult = function(result) {
console.log(result); node.log(result);
}; };
var displayError = function(err) { var displayError = function(err) {
console.error(err); node.log("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); RED.nodes.registerType("pusher in",PusherNode);
RED.nodes.registerType("pusher out",PusherNodeSend);
var querystring = require('querystring'); var querystring = require('querystring');
@ -179,9 +151,7 @@ RED.httpAdmin.post('/pusher/:id',function(req,res) {
credentials.pusherappkey_sub = newCreds.pusherappkey_sub||credentials.pusherappkey_sub; credentials.pusherappkey_sub = newCreds.pusherappkey_sub||credentials.pusherappkey_sub;
} }
RED.nodes.addCredentials(req.params.id,credentials); RED.nodes.addCredentials(req.params.id,credentials);
res.send(200); res.send(200);
}); });
}); });