AddStreamEntries snippet service

    The AddStreamEntries snippet does not offer too much information, except that it needs an InfoTable as input.

    It is however based on the InfoTable for the AddStreamEntity service.

     

     

    To use the AddStreamEntries table, an InfoTable based on sourceType, values, location, source, timestamp and tags must be used.

     

    In this example, I started with a new Thing based on a Stream template and the following DataShape:

     

     

    This DataShape must be converted into an InfoTable with is used as the values parameter.

    It's important that the timestamp parameter has distinct values! Otherwise values matching the same timestamp will be overwritten!

     

    We don't really need the sourceType as ThingWorx will automatically determine the type by knowing the source and which kind of Entity Type it is.

     

    I created a new MyStreamThing with a new service, filling the InfoTable and the Stream.

    The result is the following code which will add 5 rows to the Stream:

     

     

    // *** SET UP META DATA FOR INFO TABLE ***
    
    // create a new InfoTable based on AddStreamEntries parameters (timestamp, location, source, sourceType, tags, values)
    
    var myInfoTable = { dataShape: { fieldDefinitions : {} }, rows: [] };
    
    myInfoTable.dataShape.fieldDefinitions['timestamp']  = { name: 'timestamp', baseType: 'DATETIME' };
    myInfoTable.dataShape.fieldDefinitions['location']  = { name: 'location', baseType: 'LOCATION' };
    myInfoTable.dataShape.fieldDefinitions['source']    = { name: 'source', baseType: 'STRING' };
    myInfoTable.dataShape.fieldDefinitions['sourceType'] = { name: 'sourceType', baseType: 'STRING' };
    myInfoTable.dataShape.fieldDefinitions['tags']      = { name: 'tags', baseType: 'TAGS' };
    myInfoTable.dataShape.fieldDefinitions['values']    = { name: 'values', baseType: 'INFOTABLE' };
    
    // *** SET UP ACTUAL VALUES FOR INFO TABLE ***
    
    // create new meta data
    
    var tags = new Array();
    var timestamp = new Date();
    var location = new Object();
    location.latitude = 0;
    location.longitude = 0;
    location.elevation = 0;
    location.units = "WGS84";
    
    // add rows to InfoTable (~5 times)
    
    for (i=0; i<5; i++) {
    
        // create new values based on Stream DataShape
    
        var params = {
              infoTableName : "InfoTable",
              dataShapeName : "Cxx-DS"
        };
    
        var values = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);
    
        // add something to the values to make them unique
    
        // create and add new row based on Stream DataShape
        // only a single line allowed!
    
        var newValues = new Object();
        newValues.a = "aaa" + i; // STRING - isPrimaryKey = true
        newValues.b = "bbb" + i; // STRING
        newValues.c = "ccc" + i; // STRING
    
        values.AddRow(newValues);
    
        // create new InfoTable row based on meta data & values
        // add 10 ms to each object, to make it's timestamp unique
        // otherwise entries with the same timestamp will be overwritten
    
        var newEntry = new Object();
        newEntry.timestamp = new Date(Date.now() + (i * 10));
        newEntry.location = location;
        newEntry.source = me.name;
        newEntry.tags = tags;
        newEntry.values = values;
    
        // add new Info Table row to Info Table
    
            myInfoTable.rows[i] = newEntry;
    
        }
    
        // *** ADD myInfoTable (HOLDING MULITPLE STREAM ENTRIES) TO STREAM
    
        // add stream entries in the InfoTable
    
        var params = {
              values: myInfoTable /* INFOTABLE */
        };
    
        // no return
    
        Things["MyStreamThing"].AddStreamEntries(params);
     

     

    To verify the values have been added correctly, call the GetStreamEntriesWithData service on the MyStreamThing