cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X

Creating a steam programatically

hpulkkinen
1-Newbie

Creating a steam programatically

I have been creating new stream with the following service:

// Example: For Node1 the stream will be named Node1Stream

var streamName = nodeName + "Stream";

var newStreamParams = {

    name: streamName,

    description: undefined,

    thingTemplateName: "NodeStreamTemplate",

    tags: undefined

};

var dataShapeParams = {

    name: "NodeDataShape" /* DATASHAPENAME */

};

try{

    // Create new Stream

    Resources["EntityServices"].CreateThing(newStreamParams);

 

    // Set datashape

    Things[streamName].SetDataShape(dataShapeParams);

    Things[streamName].EnableThing();

    Things[streamName].RestartThing();

}

catch(e){

    logger.error("Unable to create Thing:" + streamName);

    var params = {

        name: streamName

    };

    Resources["EntityServices"].DeleteThing(params);

}

This seems to work fine, but it gives an error to the log:

2016-01-28 10:14:36

ERROR

Invalid DataShape [] assigned to stream Node1Stream

<my email>

c.t.s.StreamThing

Is there a better way to do this and avoid the error message? Is it possible to define the data shape directly when the new stream thing is created?

1 ACCEPTED SOLUTION

Accepted Solutions

Instead of creating a new stream from scratch, I have an existing prototype stream that I clone.

var streamName    = nodeName+"Stream";

try

{

  Resources.EntityServices.CloneThing({

    sourceThingName : "_Prototype_NodeDataStream",

    name : streamName 

  });

  var thing = Things[streamName];

  thing.EnableThing();

  thing.RestartThing();

}

catch (error)

{

  logger.error("Unabled to create stream: "+streamName);

}

View solution in original post

4 REPLIES 4

Instead of creating a new stream from scratch, I have an existing prototype stream that I clone.

var streamName    = nodeName+"Stream";

try

{

  Resources.EntityServices.CloneThing({

    sourceThingName : "_Prototype_NodeDataStream",

    name : streamName 

  });

  var thing = Things[streamName];

  thing.EnableThing();

  thing.RestartThing();

}

catch (error)

{

  logger.error("Unabled to create stream: "+streamName);

}

That solved it! Thanks!

Good answer, but what was wrong with the original javascript code? This seems to work as long as the Template and DataShape names are valid.

Since the data shape is set after the creation of the stream, that code gave me the error message every time it was ran. But you are right that it was working otherwise fine.

Top Tags