Skip to content

Commit

Permalink
Add manual tracking of datasource nodes
Browse files Browse the repository at this point in the history
Also make sure the internal unique id of nodes in subflows is used when
available
Fixes #11
  • Loading branch information
Cristian Traistaru committed Jun 2, 2016
1 parent de501ab commit 2fb037e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
16 changes: 11 additions & 5 deletions datasource.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

var util = require( "util" );
var dashServer = require( "./server" );
var datasourceManager = require( "./datasources" );

module.exports = function(RED)
{
Expand All @@ -20,6 +21,9 @@ module.exports = function(RED)
this.clients = [];
this.currentHistoryRequest = null;
this.historyRequests = {};

this.nodeID = this._alias || this.id;
datasourceManager.addNode(this.nodeID, this);

this.on( "input" , function( msg ) {

Expand All @@ -30,7 +34,7 @@ module.exports = function(RED)
self.dataComponents = undefined;
self.sendToAll( JSON.stringify( {
type : "config",
id : self.id,
id : self.nodeID,
config : self.getDatasourceConfig()
} ) );
return;
Expand All @@ -57,7 +61,7 @@ module.exports = function(RED)

var configMsg = {
type : "config",
id : self.id,
id : self.nodeID,
config : self.getDatasourceConfig()
};

Expand All @@ -77,7 +81,7 @@ module.exports = function(RED)
{
newData = {
type : "history",
id : self.id,
id : self.nodeID,
cid : self.currentHistoryRequest.cid,
data : msg.payload
};
Expand All @@ -88,7 +92,7 @@ module.exports = function(RED)
{
newData = {
type : "live",
id : self.id,
id : self.nodeID,
data : msg.payload
};
newData = JSON.stringify( newData );
Expand All @@ -104,6 +108,8 @@ module.exports = function(RED)
{
self.clients[i].ws.close();
}

datasourceManager.removeNode(self.nodeID);
} );

// Finds the index of a data point inside an array of data points sorted by unique timestamp
Expand Down Expand Up @@ -153,7 +159,7 @@ module.exports = function(RED)
this.clients.push( client );
var configMsg = {
type : "config",
id : this.id,
id : this.nodeID,
config : this.getDatasourceConfig()
};

Expand Down
41 changes: 25 additions & 16 deletions datasources.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,18 @@ var wsServer = null;

var app = express();

var datasourceNodes = {};

function init( _RED )
{
RED = _RED;

app.get( "/" , function( request , response ) {

var data = {};

RED.nodes.eachNode( function( nodeObj ) {
if( nodeObj.type == "iot-datasource" )
{
var node = RED.nodes.getNode( nodeObj.id );
if( !node ) return;

data[ nodeObj.id ] = node.getDatasourceConfig();
}
} );
for( var key in datasourceNodes ) {
data[ key ] = datasourceNodes[ key ].getDatasourceConfig();
}

response.setHeader( "Content-Type" , "application/json" );
response.end( JSON.stringify( data ) );
Expand All @@ -48,7 +43,7 @@ function init( _RED )
var end = parseInt( request.query.end );
if( isNaN( start ) || isNaN( end ) ) throw 1;

var node = RED.nodes.getNode( request.query.id );
var node = getNode( request.query.id );
if( !node ) throw 1;

node.handleHistoryRequest( response , start , end );
Expand Down Expand Up @@ -94,7 +89,7 @@ function handleWSConnection( ws )
if( !util.isArray( msg.id ) ) msg.id = [ msg.id ];
for( i = 0; i < msg.id.length; i++ )
{
node = RED.nodes.getNode( msg.id[i] );
node = getNode( msg.id[i] );
if( node )
{
node.addClient( { ws : ws } );
Expand All @@ -106,7 +101,7 @@ function handleWSConnection( ws )
if( !util.isArray( msg.id ) ) msg.id = [ msg.id ];
for( i = 0; i < msg.id.length; i++ )
{
node = RED.nodes.getNode( msg.id[i] );
node = getNode( msg.id[i] );
if( node )
{
node.removeClient( ws );
Expand All @@ -115,7 +110,7 @@ function handleWSConnection( ws )
}
else if( msg.m == "hist" )
{
node = RED.nodes.getNode( msg.dsid );
node = getNode( msg.dsid );
if( node )
{
node.handleHistoryRequest( ws , msg.cid , msg.start , msg.end );
Expand All @@ -136,8 +131,22 @@ function handleWSConnection( ws )
} );
}

function addNode( id, node ) {
datasourceNodes[ id ] = node;
}

function removeNode( id ) {
delete datasourcesNodes[ id ];
}

function getNode( id ) {
return datasourceNodes[ id ];
}

module.exports = {
app : app,

init : init
init : init,
addNode : addNode,
removeNode : removeNode
};

0 comments on commit 2fb037e

Please sign in to comment.