node-red-firebase = function (RED) { 'use strict'; function sendMessageFromSnapshot(msg, snapshot) { msg.href = snapshot.ref().toString(); msg.payload = snapshot.val(); this.send(msg); } function FirebaseQuery(n) { var Firebase = require('firebase'), firebaseStatus = require('./utility/status'); RED.nodes.createNode(this, n); this.child = n.child; this.credentials = RED.nodes.getNode(n.firebaselogin).credentials; this.firebasepath = n.firebasepath; // Status firebaseStatus.connecting(this); // Check credentials if (!this.credentials.appid) { firebaseStatus.error(this, 'Check credentials! (Query)'); } else { this.firebaseurl = 'https://' + this.credentials.appid + '.firebaseio.com/' + this.firebasepath; this.firebase = new Firebase(this.firebaseurl); // Status firebaseStatus.checkStatus(this); // Add listener this.on('input', function (msg) { var childpath = (this.child) ? String(msg[this.child]) : ''; // get path from msg or default to childpath = (childpath.indexOf('/') == 0) ? childpath : '/' + childpath; // make sure the path starts with this.firebase.child(childpath).once('value', sendMessageFromSnapshot.bind(this, msg)); }); } } RED.nodes.registerType('firebase query', FirebaseQuery); }
n/a
addListener = function (node) { var self = this; global.refFirebase.onAuth(function(authData) { if (authData) { self.connected(node); } else { self.error(node); } }); global.refFirebase.offAuth(function(authData) { if (authData) { self.connected(node); } else { self.offline(node); } }); }
n/a
checkStatus = function (node) { var authData, self = this; node.firebase.onAuth(function(authData) { if (authData) { self.connected(node); } else { self.error(node); } }); node.firebase.offAuth(function(authData) { if (authData) { self.connected(node); } else { self.offline(node); } }); authData = node.firebase.getAuth(); if (authData) { self.connected(node); } else { self.offline(node); } }
...
if (!this.credentials.appid) {
firebaseStatus.error(this, 'Check credentials! (Query)');
} else {
this.firebaseurl = 'https://' + this.credentials.appid + '.firebaseio.com/' + this.firebasepath;
this.firebase = new Firebase(this.firebaseurl);
// Status
firebaseStatus.checkStatus(this);
// Add listener
this.on('input', function (msg) {
var childpath = (this.child) ? String(msg[this.child]) : ''; // get path from msg or default to
childpath = (childpath.indexOf('/') == 0) ? childpath : '/' + childpath; // make sure the path starts with
this.firebase.child(childpath).once('value', sendMessageFromSnapshot.bind(this, msg));
});
...
connected = function (node) { node.status({ fill: 'green', shape: 'dot', text: 'connected' } ); }
...
*/
addListener: function (node) {
var self = this;
global.refFirebase.onAuth(function(authData) {
if (authData) {
self.connected(node);
} else {
self.error(node);
}
});
global.refFirebase.offAuth(function(authData) {
if (authData) {
...
connecting = function (node) { node.status({ fill: 'grey', shape: 'ring', text: 'connecting' } ); }
...
RED.nodes.createNode(this, n);
this.child = n.child;
this.credentials = RED.nodes.getNode(n.firebaselogin).credentials;
this.firebasepath = n.firebasepath;
// Status
firebaseStatus.connecting(this);
// Check credentials
if (!this.credentials.appid) {
firebaseStatus.error(this, 'Check credentials! (Query)');
} else {
this.firebaseurl = 'https://' + this.credentials.appid + '.firebaseio.com/' + this.firebasepath;
this.firebase = new Firebase(this.firebaseurl);
...
error = function (node, msg) { node.status({ fill: 'red', shape: 'ring', text: msg || 'connection failed' } ); }
...
this.firebasepath = n.firebasepath;
// Status
firebaseStatus.connecting(this);
// Check credentials
if (!this.credentials.appid) {
firebaseStatus.error(this, 'Check credentials! (Query)');
} else {
this.firebaseurl = 'https://' + this.credentials.appid + '.firebaseio.com/' + this.firebasepath;
this.firebase = new Firebase(this.firebaseurl);
// Status
firebaseStatus.checkStatus(this);
...
offline = function (node) { node.status({ fill: 'gray', shape: 'ring', text: 'disconnected' } ); }
...
}
});
global.refFirebase.offAuth(function(authData) {
if (authData) {
self.connected(node);
} else {
self.offline(node);
}
});
},
/**
* AddEventListener
*
...