2013-10-31 10:52:15 +01:00
|
|
|
/**
|
2014-04-15 16:22:06 +02:00
|
|
|
* Copyright 2014 IBM Corp.
|
2013-10-31 10:52:15 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
**/
|
|
|
|
|
2015-01-04 22:14:00 +01:00
|
|
|
// If you use this as a template, update the copyright with your own name.
|
2013-10-31 10:52:15 +01:00
|
|
|
|
|
|
|
// Sample Node-RED node file
|
|
|
|
|
2015-01-04 22:14:00 +01:00
|
|
|
|
2014-06-29 00:37:19 +02:00
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
2014-09-21 12:17:49 +02:00
|
|
|
// require any external libraries we may need....
|
|
|
|
//var foo = require("foo-library");
|
2014-06-29 00:37:19 +02:00
|
|
|
|
|
|
|
// The main node definition - most things happen in here
|
|
|
|
function SampleNode(n) {
|
|
|
|
// Create a RED node
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
|
|
|
|
// Store local copies of the node configuration (as defined in the .html)
|
|
|
|
this.topic = n.topic;
|
2015-01-12 20:14:04 +01:00
|
|
|
|
|
|
|
// copy "this" object in case we need it in context of callbacks of other functions.
|
2015-01-04 22:14:00 +01:00
|
|
|
var node = this;
|
2014-06-29 00:37:19 +02:00
|
|
|
|
|
|
|
// Do whatever you need to do in here - declare callbacks etc
|
|
|
|
// Note: this sample doesn't do anything much - it will only send
|
|
|
|
// this message once at startup...
|
|
|
|
// Look at other real nodes for some better ideas of what to do....
|
|
|
|
var msg = {};
|
|
|
|
msg.topic = this.topic;
|
|
|
|
msg.payload = "Hello world !"
|
|
|
|
|
|
|
|
// send out the message to the rest of the workspace.
|
2015-01-04 22:14:00 +01:00
|
|
|
// ... this message will get sent at startup so you may not see it in a debug node.
|
2015-01-12 20:14:04 +01:00
|
|
|
this.send(msg);
|
2014-06-29 00:37:19 +02:00
|
|
|
|
2014-09-21 12:17:49 +02:00
|
|
|
// respond to inputs....
|
2015-01-12 20:14:04 +01:00
|
|
|
this.on('input', function (msg) {
|
2014-09-21 12:17:49 +02:00
|
|
|
node.warn("I saw a payload: "+msg.payload);
|
|
|
|
// in this example just send it straight on... should process it here really
|
2015-01-04 22:14:00 +01:00
|
|
|
node.send(msg);
|
|
|
|
});
|
2014-09-21 12:17:49 +02:00
|
|
|
|
2015-01-12 20:14:04 +01:00
|
|
|
this.on("close", function() {
|
2014-06-29 00:37:19 +02:00
|
|
|
// Called when the node is shutdown - eg on redeploy.
|
|
|
|
// Allows ports to be closed, connections dropped etc.
|
2015-01-04 22:14:00 +01:00
|
|
|
// eg: node.client.disconnect();
|
2014-06-29 00:37:19 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the node by name. This must be called before overriding any of the
|
|
|
|
// Node functions.
|
|
|
|
RED.nodes.registerType("sample",SampleNode);
|
2015-01-04 22:14:00 +01:00
|
|
|
|
2013-10-31 10:52:15 +01:00
|
|
|
}
|