Skip to main content
1-Visitor
June 17, 2016
Solved

Change properties values any undefined amount of time

  • June 17, 2016
  • 7 replies
  • 3695 views

Hi, I have this current situation:

ThingTemplate named "MyTemplate" with three properties: Prop1 (Number), Prop2 (number), Prop3 (number).

25 Things named "TH-1"..."TH-25".


Those properties will change every x seconds (where x is a random number) and I can see on a mashup the new value on three gauges each one binded to a property.


Now I have the following questions:


  1. What I should write in the service to change the thing any random time? Because if it is a fixed time, I can make a Timer Thing but what about a random time? Would you make me a simple example of code?
  2. I know the widget "Autorefresh" which refreshes the page automatically every T seconds where T is set in the widget properties but I have random time. What can I do to autorefresh when it triggers?

Thank you

Best answer by CarlesColl

1. With a normal timer, which every time it's executed "rolls a dice" to decide it has to be executed the corresponding service or not. How to roll a dice? with for instance (Math.random()>0.5)

2. Right now TW doesn't supports to send events to the browser from the server. Then you need to do a Poll every n-seconds, if you don't want to refresh the whole gauges every fixed n-seconds, you can set a property called "anyUpdates" and every n-seconds you query for that property, if that property it's true then you call the real data pull and you set again anyUpdates=false. If you have multiple users looking each one from their browser, then you will need to set this anyUpdates on the user session.

7 replies

1-Visitor
June 17, 2016

1. With a normal timer, which every time it's executed "rolls a dice" to decide it has to be executed the corresponding service or not. How to roll a dice? with for instance (Math.random()>0.5)

2. Right now TW doesn't supports to send events to the browser from the server. Then you need to do a Poll every n-seconds, if you don't want to refresh the whole gauges every fixed n-seconds, you can set a property called "anyUpdates" and every n-seconds you query for that property, if that property it's true then you call the real data pull and you set again anyUpdates=false. If you have multiple users looking each one from their browser, then you will need to set this anyUpdates on the user session.

fmanniti1-VisitorAuthor
1-Visitor
June 20, 2016

I am sorry for my question but, since I'm quite new in "ThingWorx's world", I still didn't understand very well the services situation, so I didn't understand your answer.

From what I got the service is a function while the subscription is an action that "happens" when an event occurs.

If I see it in this way, I guess that what I need is not a service but a subscription and that my event should occur randomly.

So, that's what I've done. May you please tell me if there is something wrong?

  1. Created a Thing called: TH-1 which is a Generic Thing and which as a number property called Temp
  2. Created a Stream called TestStream with a Data Shape called TestDataFeed with a field called T (number)
  3. Now, you said that I must use a normal time so I created a Timer Thing called RandTime; the problem is that this TimerThing already has a configuration with a time set at 60 seconds.
  4. Anyways, I go to TH-1 subscriptions and Add a new subscription where the source is RandTime and the Event is Timer.
  5. Then I wrote this code:
    // values:INFOTABLE(Datashape: TestDataFeed)
    var values = Things["TestStream"].CreateValues();
    me.Temp=Math.floor(Math.random()*100)+1;
    values.T = me.Temp; //NUMBER
    var params = {
      values : values
    };
    // AddStreamEntry(tags:TAGS, timestamp:DATETIME, source:STRING("me.name"), values:INFOTABLE(TestDataFeed), location:LOCATION):NOTHING
    Things["TestStream"].AddStreamEntry(params);
  6. This works, but it changes my value not randomly.
  7. If I would write a random value changing in a random time in a normal HTML page I would do this
    var myVar;
    function doRandom() {
         myVar=Math.floor((Math.random()*100)+1);
    }
    (function loop() {
        var rand = Math.round(Math.random() * (3000 - 500)) + 500;
        setTimeout(function() {
    doRandom();
                loop(); 
        }, rand);
    }());

I would like to know: According to what are you saying, am I right in part or I'm totally wrong?
Thank you

1-Visitor
June 20, 2016

7. You don't have a setTimeout on ThingWorx, if you want it, you will have to implement with a "queue" an a timer which consumes the pending timeout services.