10 Replies Latest reply on Dec 30, 2015 2:29 AM by petersch RSS
    petersch Apprentice

    Triggering a Property 'DataChange' subscription when using AddNumberValueStreamEntry

    In order to load some historic data into a logged property I've written a data loader service that uses Things[].AddNumberValueStreamEntry() and its variants (depending on the data type of the property). This lets me set the timestamp to a value in the past, and so far it seems to work well.

     

    However I find that my subscriptions to 'DataChange' for these properties are not fired when I do this. Is that expected behavior when using entity services like AddNumberValueStreamEntry?

     

    The same 'DataChange' subscription works just fine if I merely 'set' the property value of course, but that can only be used to set the property value entry for the current time.

     

    I'd like to be able to load some property value stream data and also generate other updates triggered off of data changes to those properties. Is that possible?

      • Re: Triggering a Property 'DataChange' subscription when using AddNumberValueStreamEntry
        carlesc Ninja

        You have to trigger by yourself, instead of using AddNumberValueStreamEntry wrap it inside a custom service ( for instance AddNumberValueStreamEntryCustom ) and once you added the value trigger the DataChangeEvent:

         

          me.DataChange({

                            newValue: "infotable-with-new-value-inserted-on-the-stream' ,

                            oldValue: undefined

                        });

          • Re: Triggering a Property 'DataChange' subscription when using AddNumberValueStreamEntry
            petersch Apprentice

            Thanks for the pointer Carles - it hadn't occurred to me that I might trigger my own datachange event. Follow up question though - I must not quite be using this correctly as it is throwing a warning in the application log. Specifically the log says: Unable to publish event in MyThingName : Event DataChange not found

             

            Any explanation for why my attempt to call a DataChange is rebuffed as not found?

             

            Perhaps the way I call DataChange is at fault. The newValue infotable parameter I'm using employs the namedVTQ datashape (seemed reasonable choice and I don't see any errors arising from that, but it wasn't clear if a particular datashape is needed). A snipped of my test service for this is below (I pass in a 'thing' as an input parameter, and then create a value stream entry not shown below for brevity):

             

            Things[thing].AddNumberValueStreamEntry(aparams); // This works fine. I get my value stream entry for the date I want etc.

             

            var params = {

              infoTableName : "InfoTable",

              dataShapeName : "NamedVTQ"

            };

            var namedVTQ = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

             

            // NamedVTQ entry object - used this since I wasn't entirely sure what shape the DataChange expects.

            var newEntry = new Object();

            newEntry.name = "TEMP"; // STRING - isPrimaryKey = true

            newEntry.time = now; // DATETIME

            newEntry.value = "98.6"; // VARIANT

            newEntry.quality = "GOOD"; // STRING

             

            namedVTQ.AddRow(newEntry); // Logging this out shows a valid data row in the infotable

             

            var params = {

              newValue: namedVTQ /* INFOTABLE */,

              oldValue: undefined /* INFOTABLE */

            };

             

            Things[thing].DataChange(params); // This would appear to be throwing that Warning in the application log.

             

            TIA for any other suggestions you may have. As a workaround, I've augmented my service that creates the historical entry to the value stream to also write the additional values I was otherwise going to derive by my subscription to the datachange event. But it ends up being duplicate code so I'd rather not do that. And mostly I just want to get a handle on correctly using the DataChange event in a service for my own future reference!