refactord, tidied up, added icon for storage and a new node for writing buffers to storage files

This commit is contained in:
Peter Svensson
2019-07-02 20:43:04 +02:00
parent ae90a7c225
commit df5b5c8e82
26 changed files with 120 additions and 16 deletions

62
rtdb/flow-to-rtdb.html Normal file
View File

@ -0,0 +1,62 @@
<script type="text/javascript">
RED.nodes.registerType('flow-to-rtdb',{
category: 'firebase-admin',
color: '#a6bbcf',
defaults: {
name: {value:""},
flowId: {value:""},
path: {value:""},
cred: {value: "", type: 'firebase-config'}
},
inputs:1,
outputs:1,
icon: "firebase-admin-icon.png",
label: function() {
return this.name||"flow-to-rtdb";
},
oneditsave: function() {
let type = $('#apitype-select').val()
console.log('type is set to '+type)
},
oneditprepare: function() {
$('#apitype-select').change(function () {
$("#node-input-apitype").val($(this).find('option:selected').val())
});
$("#apitype-select").val($("#node-input-apitype").val())
$('#apitype-select').trigger('change');
}
});
</script>
<script type="text/x-red" data-template-name="flow-to-rtdb">
<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>
<div class="form-row">
<label for="node-input-flowId"><i class="icon-tag"></i> Flow id</label>
<input type="text" id="node-input-flowId" placeholder="FlowId">
</div>
<div class="form-row">
<label for="node-input-path"><i class="icon-tag"></i> Path</label>
<input type="text" id="node-input-path" placeholder="Path">
</div>
<div class="form-row">
<label for="node-input-cred"><i class="icon-tag"></i> Credentials</label>
<input type="text" id="node-input-cred" placeholder="Credentials">
</div>
</script>
<script type="text/x-red" data-help-name="flow-to-rtdb">
<p>A node that wraps the flow-to-rtdb SDK.</p>
</script>

42
rtdb/flow-to-rtdb.js Normal file
View File

@ -0,0 +1,42 @@
const runtime = require('node-red').runtime
module.exports = function(RED) {
function FirebaseAdmin(config) {
RED.nodes.createNode(this, config);
var node = this;
if(config.cred){
let c = RED.nodes.getNode(config.cred)
this.admin = c.admin
}
node.on('input', function(msg) {
if(msg && msg.payload){
console.log('flow-to-rtdb got input')
console.dir(msg)
this.path = msg.payload.path || config.path
this.flowId = msg.payload.flowId || config.flowId
console.log('flow-to-rtdb path='+this.path+', flowId='+this.flowId)
runtime.flows.getFlow({id: this.flowId}).then((flow)=>{
flow.updated_at = Date.now()
this.admin.database().ref(this.path).set(flow).then((res)=>{
console.log('firebase set result '+res)
console.dir(res)
node.send({payload: res})
},(err)=>{
console.log('rtdb saving error')
console.dir(err)
}).catch((ex)=>{
console.log('rtdb saving exception')
console.dir(ex)
})
})
}
}.bind(this));
}
RED.nodes.registerType("flow-to-rtdb", FirebaseAdmin);
}

56
rtdb/rtdb-get.html Normal file
View File

@ -0,0 +1,56 @@
<script type="text/javascript">
RED.nodes.registerType('rtdb-get',{
category: 'firebase-admin',
color: '#a6bbcf',
defaults: {
name: {value:""},
path: {value:""},
cred: {value: "", type: 'firebase-config'}
},
inputs:1,
outputs:1,
icon: "firebase-admin-icon.png",
label: function() {
return this.name||"rtdb-get";
},
oneditsave: function() {
let type = $('#apitype-select').val()
console.log('type is set to '+type)
},
oneditprepare: function() {
$('#apitype-select').change(function () {
$("#node-input-apitype").val($(this).find('option:selected').val())
});
$("#apitype-select").val($("#node-input-apitype").val())
$('#apitype-select').trigger('change');
}
});
</script>
<script type="text/x-red" data-template-name="rtdb-get">
<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>
<div class="form-row">
<label for="node-input-path"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-path" placeholder="Path">
</div>
<div class="form-row">
<label for="node-input-cred"><i class="icon-tag"></i> Credentials</label>
<input type="text" id="node-input-cred" placeholder="Name">
</div>
</script>
<script type="text/x-red" data-help-name="rtdb-get">
<p>A node that wraps the rtdb-get SDK</p>
</script>

53
rtdb/rtdb-get.js Normal file
View File

@ -0,0 +1,53 @@
let oldpath
module.exports = function(RED) {
function FirebaseAdmin(config) {
RED.nodes.createNode(this, config);
var node = this;
const cb = (res)=>{
//console.log('firebase get result '+res)
//console.dir(res)
let val = res.val()
//console.log('val='+val)
node.send({payload:val})
}
let setUpListener = (path)=>{
//console.log('rtdb-get setUpListener for path '+path)
if(oldpath){
this.admin.database().ref(oldpath).off('value', cb)
}
this.admin.database().ref(path).on('value', cb)
oldpath = path
}
if(config.cred){
let c = RED.nodes.getNode(config.cred)
this.admin = c.admin
}
//console.log('------------------------------- rtdg-get config')
//console.dir(config)
this.path = config.path
if(this.path){
setUpListener(this.path)
}
//console.log('configuring rtdb-get to listen for messages')
node.on('input', function(msg) {
let path = this.path
if(msg && msg.payload){
path = msg.payload.path
setUpListener(path)
}
}.bind(this));
}
RED.nodes.registerType("rtdb-get", FirebaseAdmin);
}

50
rtdb/rtdb-push.html Normal file
View File

@ -0,0 +1,50 @@
<script type="text/javascript">
RED.nodes.registerType('rtdb-push',{
category: 'firebase-admin',
color: '#a6bbcf',
defaults: {
name: {value:""},
cred: {value: "", type: 'firebase-config'}
},
inputs:1,
outputs:1,
icon: "firebase-admin-icon.png",
label: function() {
return this.name||"rtdb-push";
},
oneditsave: function() {
let type = $('#apitype-select').val()
console.log('type is set to '+type)
},
oneditprepare: function() {
$('#apitype-select').change(function () {
$("#node-input-apitype").val($(this).find('option:selected').val())
});
$("#apitype-select").val($("#node-input-apitype").val())
$('#apitype-select').trigger('change');
}
});
</script>
<script type="text/x-red" data-template-name="rtdb-push">
<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>
<div class="form-row">
<label for="node-input-cred"><i class="icon-tag"></i> Credentials</label>
<input type="text" id="node-input-cred" placeholder="Name">
</div>
</script>
<script type="text/x-red" data-help-name="rtdb-push">
<p>A node that wraps the rtdb-push SDK.</p>
</script>

31
rtdb/rtdb-push.js Normal file
View File

@ -0,0 +1,31 @@
module.exports = function(RED) {
function FirebaseAdmin(config) {
RED.nodes.createNode(this, config);
var node = this;
if(config.cred){
let c = RED.nodes.getNode(config.cred)
this.admin = c.admin
}
node.on('input', function(msg) {
if(msg && msg.payload){
console.log('rtdb-push got input')
console.dir(msg)
const path = msg.payload.path
const obj = msg.payload.obj
console.log('storing '+obj+' at rtdb path '+path)
this.admin.database().ref(path).push(obj).then((res)=>{
console.log('firebase set result '+res)
console.dir(res)
node.send({payload: res})
})
}
}.bind(this));
}
RED.nodes.registerType("rtdb-push", FirebaseAdmin);
}

50
rtdb/rtdb-query.html Normal file
View File

@ -0,0 +1,50 @@
<script type="text/javascript">
RED.nodes.registerType('rtdb-query',{
category: 'firebase-admin',
color: '#a6bbcf',
defaults: {
name: {value:""},
cred: {value: "", type: 'firebase-config'}
},
inputs:1,
outputs:1,
icon: "firebase-admin-icon.png",
label: function() {
return this.name||"rtdb-query";
},
oneditsave: function() {
let type = $('#apitype-select').val()
console.log('type is set to '+type)
},
oneditprepare: function() {
$('#apitype-select').change(function () {
$("#node-input-apitype").val($(this).find('option:selected').val())
});
$("#apitype-select").val($("#node-input-apitype").val())
$('#apitype-select').trigger('change');
}
});
</script>
<script type="text/x-red" data-template-name="rtdb-query">
<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>
<div class="form-row">
<label for="node-input-cred"><i class="icon-tag"></i> Credentials</label>
<input type="text" id="node-input-cred" placeholder="Name">
</div>
</script>
<script type="text/x-red" data-help-name="rtdb-query">
<p>A node that wraps the rtdb-query SDK</p>
</script>

89
rtdb/rtdb-query.js Normal file
View File

@ -0,0 +1,89 @@
let oldpath
let oldeventtype
module.exports = function(RED) {
function FirebaseAdmin(config) {
RED.nodes.createNode(this, config);
var node = this;
if(config.cred){
let c = RED.nodes.getNode(config.cred)
this.admin = c.admin
}
const cb = (res)=>{
console.log('firebase get result '+res)
console.dir(res)
let val = res.val()
console.log('val='+val)
node.send({payload:val})
}
node.on('input', function(msg) {
if(msg && msg.payload){
const path = msg.payload.path
const eventtype = msg.payload.on || 'value'
if(oldpath){
this.admin.database().ref(oldpath).off(oldeventtype, cb)
}
let ref = this.admin.database().ref(path)
// Decorate with queries
if(msg.payload.queries && msg.payload.queries.length > 0){
console.log('found queries')
let ordered = false
msg.payload.queries.forEach((query)=>{
console.dir(query)
if(query.orderBy){
ordered = true
console.log('setting explicit orderBy')
if(query.orderBy === 'value'){
ref = ref.orderByValue()
} else if(query.orderBy === 'key'){
ref = ref.orderByKey()
} else if(query.orderBy === 'child'){
ref = ref.orderByChild(query.value)
}
}
if(query.startAt){
console.log('startAt '+query.startAt)
ref = ref.startAt(query.startAt)
}
if(query.endAt){
console.log('endAt '+query.endAt)
ref = ref.endAt(query.endAt)
}
if(query.equalTo){
console.log('equalTo '+query.equalTo)
ref = ref.equalTo(query.equalTo)
}
if(query.limitTo){
console.log('limitTo '+query.limitTo+' -> '+query.value)
if(query.limitTo === 'first'){
ref = ref.limitToFirst(query.value)
} else if(query.limitTo === 'last'){
ref = ref.limitToLast(query.value)
}
}
})
if(!ordered) {
console.log('setting implicit orderBy')
ref = ref.orderByValue()
}
}
console.log('finished rtdb query is')
console.dir(ref.queryParams_)
ref.on(eventtype, cb)
oldpath = path
oldeventtype = eventtype
}
}.bind(this));
}
RED.nodes.registerType("rtdb-query", FirebaseAdmin);
}

50
rtdb/rtdb-set.html Normal file
View File

@ -0,0 +1,50 @@
<script type="text/javascript">
RED.nodes.registerType('rtdb-set',{
category: 'firebase-admin',
color: '#a6bbcf',
defaults: {
name: {value:""},
cred: {value: "", type: 'firebase-config'}
},
inputs:1,
outputs:0,
icon: "firebase-admin-icon.png",
label: function() {
return this.name||"rtdb-set";
},
oneditsave: function() {
let type = $('#apitype-select').val()
console.log('type is set to '+type)
},
oneditprepare: function() {
$('#apitype-select').change(function () {
$("#node-input-apitype").val($(this).find('option:selected').val())
});
$("#apitype-select").val($("#node-input-apitype").val())
$('#apitype-select').trigger('change');
}
});
</script>
<script type="text/x-red" data-template-name="rtdb-set">
<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>
<div class="form-row">
<label for="node-input-cred"><i class="icon-tag"></i> Credentials</label>
<input type="text" id="node-input-cred" placeholder="Name">
</div>
</script>
<script type="text/x-red" data-help-name="rtdb-set">
<p>A node that wraps the rtdb-set SDK</p>
</script>

33
rtdb/rtdb-set.js Normal file
View File

@ -0,0 +1,33 @@
module.exports = function(RED) {
function FirebaseAdmin(config) {
RED.nodes.createNode(this, config);
var node = this;
if(config.cred){
let c = RED.nodes.getNode(config.cred)
this.admin = c.admin
}
node.on('input', function(msg) {
if(msg && msg.payload){
console.log('rtdb-set got input')
console.dir(msg)
const path = msg.payload.path
const obj = msg.payload.obj
console.log('storing '+obj+' at rtdb path '+path)
this.admin.database().ref(path).set(obj).then((res)=>{
console.log('firebase set result '+res)
console.dir(res)
}).catch((err)=>{
console.log('------ ERROR ------')
console.dir(err)
})
}
}.bind(this));
}
RED.nodes.registerType("rtdb-set", FirebaseAdmin);
}

63
rtdb/rtdb-to-flow.html Normal file
View File

@ -0,0 +1,63 @@
<script type="text/javascript">
RED.nodes.registerType('rtdb-to-flow',{
category: 'firebase-admin',
color: '#a6bbcf',
defaults: {
name: {value:""},
flowId: {value:""},
path: {value:""},
env_var: {value:""},
cred: {value: "", type: 'firebase-config'}
},
inputs:1,
outputs:1,
icon: "firebase-admin-icon.png",
label: function() {
return this.name||"rtdb-to-flow";
},
oneditsave: function() {
let type = $('#apitype-select').val()
console.log('type is set to '+type)
},
oneditprepare: function() {
$('#apitype-select').change(function () {
$("#node-input-apitype").val($(this).find('option:selected').val())
});
$("#apitype-select").val($("#node-input-apitype").val())
$('#apitype-select').trigger('change');
}
});
</script>
<script type="text/x-red" data-template-name="rtdb-to-flow">
<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>
<div class="form-row">
<label for="node-input-path"><i class="icon-tag"></i> Path</label>
<input type="text" id="node-input-path" placeholder="Path">
</div>
<div class="form-row">
<label for="node-input-env_var"><i class="icon-tag"></i> Environment variable to allow update</label>
<input type="text" id="node-input-env_var" placeholder="Env var">
</div>
<div class="form-row">
<label for="node-input-cred"><i class="icon-tag"></i> Credentials</label>
<input type="text" id="node-input-cred" placeholder="Credentials">
</div>
</script>
<script type="text/x-red" data-help-name="rtdb-to-flow">
<p>A node that wraps the rtdb-to-flow SDK.</p>
</script>

63
rtdb/rtdb-to-flow.js Normal file
View File

@ -0,0 +1,63 @@
const runtime = require('node-red').runtime
let oldpath
module.exports = function(RED) {
function FirebaseAdmin(config) {
RED.nodes.createNode(this, config);
var node = this;
const cb = (res)=>{
//console.log('firebase get result '+res)
//console.dir(res)
if(config.env_var && process.env[config.env_var]){
let val = res.val()
runtime.flows.getFlow({id: val.id}).then((flow)=>{
console.log('rtdb-to-flow old flow updated_at = '+flow.updated_at+' new flow updated_at = '+val.updated_at)
if(!flow.updated_at || val.updated_at > flow.updated_at){
console.log('updating flow...')
runtime.flows.updateFlow({id: val.id, flow: val})
}
//console.log('val='+val)
node.send({payload:val})
})
} else {
console.log('skipping flow update because environment variable '+config.env_var+' was either not defined or not set to true')
}
}
let setUpListener = (path)=>{
//console.log('rtdb-get setUpListener for path '+path)
if(oldpath){
this.admin.database().ref(oldpath).off('value', cb)
}
this.admin.database().ref(path).on('value', cb)
oldpath = path
}
if(config.cred){
let c = RED.nodes.getNode(config.cred)
this.admin = c.admin
}
//console.log('------------------------------- rtdg-get config')
//console.dir(config)
this.path = config.path
if(this.path){
setUpListener(this.path)
}
//console.log('configuring rtdb-get to listen for messages')
node.on('input', function(msg) {
let path = this.path
if(msg && msg.payload){
path = msg.payload.path
setUpListener(path)
}
}.bind(this));
}
RED.nodes.registerType("rtdb-to-flow", FirebaseAdmin);
}