Skip to main content
1-Visitor
January 28, 2016
Solved

Creating a steam programatically

  • January 28, 2016
  • 1 reply
  • 2472 views

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?

Best answer by ptc-6292103

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);

}

1 reply

1-Visitor
January 29, 2016

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);

}

1-Visitor
February 1, 2016

That solved it! Thanks!