1

JS API: How to subscribe to Add/Remove Unit message

Тема: JS API: How to subscribe to Add/Remove Unit message

Hello! Session object has getItems() method, but how do I know that there was a change. I see that there is addListerner() but can't figure out where and how I should subscribe. Thanks!

Sergei Shirokov / GoGPS Service
Senior Software Engineer
www.gogps.eu
Skype: sergei.shirokov
2

JS API: How to subscribe to Add/Remove Unit message

Re: JS API: How to subscribe to Add/Remove Unit message

Hi

Check Session.updateDataFlags function (its SDK JS wrapper around Remote API's core/update_data_flags request)

3

JS API: How to subscribe to Add/Remove Unit message

Re: JS API: How to subscribe to Add/Remove Unit message

Could You please be more specific, which flags exactly I need? Here is my current code:

    session = wialon.core.Session.getInstance();
    session.updateDataFlags([
      {
        type: "type",
        data: "avl_unit",
        flags: wialon.item.Item.dataFlag.base | wialon.item.Unit.dataFlag.lastMessage,
        mode: 0
      }
    ], function(update_code) {
      var items;
      if (update_code) {
        return;
      }
      items = session.getItems('avl_unit');
      return console.log(items.length);
    });

The problem is that the callback (where I call getItems()) is called only once. What I need is a notification when there was add/remove of an unit (for example: another admin just added a new unit for this user).

Sergei Shirokov / GoGPS Service
Senior Software Engineer
www.gogps.eu
Skype: sergei.shirokov
4

JS API: How to subscribe to Add/Remove Unit message

Re: JS API: How to subscribe to Add/Remove Unit message

Could You please be more specific, which flags exactly I need?

For create/delete base flag (0x1) is enought

What does Session.updateDataFlags do:
1. load items into Session (so you can get it session.getItems(...))
2. update items automaticaly (every 2 seconds SDK JS perform avl_events request, parse results and update correspondence items)

If you want to listen any item event (for example, itemDeleted) you should addListener for every item. You can find some items events examples on sdk.wialon.com (one of them sdk.wialon.com/playground)

If you're interested in itemCreated event you must addListener to your Session as follows

wialon.core.Session.getInstance().addListener("itemCreated", function () {
  // itemCreated handler
}
5

JS API: How to subscribe to Add/Remove Unit message

Re: JS API: How to subscribe to Add/Remove Unit message

wialon.core.Session.getInstance().addListener("itemCreated", function () {
  // itemCreated handler
}

Sorry, that doesn't work - function never called. Could you please try this approach by yourself on Wialon Local ?

I even added a button with click handler which calls session.getItems('avl_unit') and then traces to console number of items. Here is what I see:
a) if I delete an unit then clicking the button I see immediately decreased number - that correct!
b) but if I add a new unit then clicking the button shows no change, i.e. the same number of items as before unit addon - so my session don't see such kind "itemCreate" of event from server (which probably explains why there is no callback for the addListener).

For me it seems that  "wialon.item.Item.dataFlag.base | wialon.item.Unit.dataFlag.lastMessage" is NOT enough to receive "item created" notifications from server.

Sergei Shirokov / GoGPS Service
Senior Software Engineer
www.gogps.eu
Skype: sergei.shirokov
6

JS API: How to subscribe to Add/Remove Unit message

Re: JS API: How to subscribe to Add/Remove Unit message

Sorry for confusing, but there is one more thing - by default, ItemsCreationChecking (method that trigger itemCreated event) run once in 10 minute, so i beleive you just havent wait enough to see it.

If you want to run check more often, you can do it manually calling following command

wialon.core.Session.getInstance().startItemsCreationChecking('avl_unit');

We understand the problem with this event and will try to fix or refactor it soon.

7

JS API: How to subscribe to Add/Remove Unit message

Re: JS API: How to subscribe to Add/Remove Unit message

Tried to wait more than 10 min - still no callback. On attempt to call startItemsCreationChecking (exactly as you suggested) I see following call stack in console:

Uncaught TypeError: undefined is not a function
  MainViewModel.refresh    mainViewModel.coffee:39
  __bind    mainViewModel.coffee:1
  (anonymous function)    mainViewModel.coffee:30
  qx.Class.define.members.__dq    wialon.js:9543
  qx.Bootstrap.define.statics.create    wialon.js:5325
  qx.Class.define.members.__dC    wialon.js:10040
  qx.Class.define.members.__dy    wialon.js:10005
  qx.Bootstrap.define.statics.create    wialon.js:5325
  qx.Class.define.members.__dD    wialon.js:10134
  qx.Bootstrap.define.statics.create    wialon.js:5325
Sergei Shirokov / GoGPS Service
Senior Software Engineer
www.gogps.eu
Skype: sergei.shirokov
8

JS API: How to subscribe to Add/Remove Unit message

Re: JS API: How to subscribe to Add/Remove Unit message

Sorry again, full example below (for  Wialon Hosting, but should work on your Wialon Local too).
FYI we're refactoring "itemCreated" event system, so this code (with manual findNewItems calling) will be outdated soon. But old-style "itemCreated" listeners would work in any case.

Thanks for this topic, it helped to make SDK better wink

<!DOCTYPE html>
<html>
<head>
    <title>itemCreated event</title>
</head>
<body>
    <!-- Button for manual item creation check -->
    <input type="button" value="Run check" id="check"/>

    <script type='text/javascript' src='https://hst-api.wialon.com/wsdk/script/wialon.js'></script>
    <script type='text/javascript'>
        // init session
        wialon.core.Session.getInstance().initSession("https://hst-api.wialon.com");
        // try to login
        wialon.core.Session.getInstance().login("wialon_test", "test", "", function (code) {
            if (code) {
                console.log("Login failed");
            } else {
                console.log("Login success");
                // Include mixin
                qx.Class.include(wialon.core.Session, wialon.util.MDataFlagsHelper);
                // Init creation checking procedure
                wialon.core.Session.getInstance().startItemsCreationChecking("avl_unit");

                // Add itemCreated event listener
                wialon.core.Session.getInstance().addListener("itemCreated", function () {
                    // itemCreated handler
                    console.log("Created! ", arguments);
                });

                // Run checkItemCreation onclick
                // use setTimeout here
                document.getElementById("check").onclick = function () {
                    console.log("Trying to find new units..");
                    wialon.core.Session.getInstance().findNewItems("avl_unit");
                };
            }
        });
    </script>
</body>
</html>
9

JS API: How to subscribe to Add/Remove Unit message

(17/09/2014 17:48:16 отредактировано sergej.shirokov)

Re: JS API: How to subscribe to Add/Remove Unit message

Hallelujah, I just has received "itemCreated" callback!!! The key elements were:

1) qx.Class.include(wialon.core.Session, wialon.util.MDataFlagsHelper);
2) wialon.core.Session.getInstance().startItemsCreationChecking("avl_unit");
3) wialon.core.Session.getInstance().addListener("itemCreated", function () {

Thank You for cooperation!

Sergei Shirokov / GoGPS Service
Senior Software Engineer
www.gogps.eu
Skype: sergei.shirokov
10

JS API: How to subscribe to Add/Remove Unit message

Re: JS API: How to subscribe to Add/Remove Unit message

Just quick follow up: Is it really necessary to do all the time the "wialon.core.Session.getInstance()", or it's quite legitimate to just store the instance in a class member (at constructor for instance) and then just use the member in all the code, callbacks and so on?

Sergei Shirokov / GoGPS Service
Senior Software Engineer
www.gogps.eu
Skype: sergei.shirokov
11

JS API: How to subscribe to Add/Remove Unit message

Re: JS API: How to subscribe to Add/Remove Unit message

sergej.shirokov пишет:

Just quick follow up: Is it really necessary to do all the time the "wialon.core.Session.getInstance()", or it's quite legitimate to just store the instance in a class member (at constructor for instance) and then just use the member in all the code, callbacks and so on?

I left full name for better example visibility, sure you can store instance in a variable and than use it where needed