1

How to get a group of units through javascript SDK?

Topic: How to get a group of units through javascript SDK?

Hi good day,

Someone can help me know how to get groups of units wialon account for javascript?
Suggest, please.

2

How to get a group of units through javascript SDK?

Re: How to get a group of units through javascript SDK?

Denisse, to get all unit groups You have access to - just try to use:

wialon.core.Session.getInstance().getItems("avl_unit_group")

or

wialon.core.Session.getInstance().searchItems({itemsType:"avl_unit_group",propName:"",propValueMask:"",sortType:""}, 1, 0xFFFFFFFF, 0, 0xFFFFFFFF, function() {console.log(arguments);});

if You want to get unit groups of some specific account use such request:

wialon.core.Session.getInstance().searchItems({itemsType:"avl_unit_group",propName:"sys_billing_account_guid",propValueMask:"X",sortType:""}, 1, 0xFFFFFFFF, 0, 0xFFFFFFFF, function() {console.log(arguments);});

replace X with account id You are interested in

Head of Wialon Local Department
Gurtam
3

How to get a group of units through javascript SDK?

Re: How to get a group of units through javascript SDK?

Thank you very much for de answer.

And one questions more, If I get theunit group also I get units that belong to the selected group?

4

How to get a group of units through javascript SDK?

Re: How to get a group of units through javascript SDK?

Denisse, to get units of each group that belong to some account You can do the following:

wialon.core.Session.getInstance().searchItems({itemsType:"avl_unit_group",propName:"sys_billing_account_guid",propValueMask:"X",sortType:""}, 1, 0xFFFFFFFF, 0, 0xFFFFFFFF, function(code, data) {
     if (code || !data || !data.items)
          return;
     var units = [];
     for (var i = 0; i < data.items.length; i++) {
          var group = data.items[i];
          var groupUnits = group.getUnits();
          for (var j = 0; j < groupUnits.length; j++) {
               units.push(groupUnits[j]);
          }
     }
     console.log(units);
     if (units.length)
          wialon.core.Session.getInstance().searchItems({itemsType:"avl_unit",propName:"sys_id",propValueMask:units.join(","),sortType:""}, 1, 0xFFFFFFFF, 0, 0xFFFFFFFF, function() {console.log(arguments);});
});
Head of Wialon Local Department
Gurtam
5

How to get a group of units through javascript SDK?

Re: How to get a group of units through javascript SDK?

Thank you very much for the answer.

6

How to get a group of units through javascript SDK?

Re: How to get a group of units through javascript SDK?

deal

Hi!

A question, the reports, can I execute for unit group with same library avl_unit_group?

Suggest please?

7

How to get a group of units through javascript SDK?

Re: How to get a group of units through javascript SDK?

Denisse, try to modify this example for unit groups instead of units:
https://sdk.wialon.com/playground/demo/execute_report

Head of Wialon Local Department
Gurtam
8

How to get a group of units through javascript SDK?

Re: How to get a group of units through javascript SDK?

Thank you deal

9

How to get a group of units through javascript SDK?

(edited by Denisse 09/05/2017 02:39:25)

Re: How to get a group of units through javascript SDK?

Hi deal good day,

The detail of my query is that I am getting the units that belong to a group but wanting to get their position or names I can not. I put the code that modifies according to Playground. Some suggest or idea please, I will be very important, thanks

// Print message to log
function msg(text) { $("#log").prepend(text + "<br/>"); }

function init() { // Execute after login succeed
    var sess = wialon.core.Session.getInstance(); // get instance of current Session
    // flags to specify what kind of data should be returned
    var flags = wialon.item.Item.dataFlag.base | wialon.item.Unit.dataFlag.lastMessage;

    sess.loadLibrary("itemIcon"); // load Icon Library    
    sess.updateDataFlags( // load items to current session
    [{type: "type", data: "avl_unit_group", flags: flags, mode: 0}], // Items specification
        function (code) { // updateDataFlags callback
            if (code) { msg(wialon.core.Errors.getErrorText(code)); return; } // exit if error code

            // get loaded 'avl_unit's items  
            var units = sess.getItems("avl_unit_group");
            if (!units || !units.length){ msg("Units not found"); return; } // check if units found

            for (var i = 0; i< units.length; i++){ // construct Select object using found units
                //msg("Que imprime: " + units[i]);
                var u = units[i]; // current unit in cycle
                var grupoU = u.getUnits();
                msg("Hola: " + grupoU.length);
                //for(var j=0;j<grupoU.length;j++){
                    //msg(grupoU[j]);
                //}
                //msg(grupoU);
                // append option to select
                $("#units").append("<option value='"+ u.getId() +"'>"+ u.getName()+ "</option>");
            }
            // bind action to select change event
            $("#units").change( getSelectedUnitInfo );
        }
    );
}

function getSelectedUnitInfo(){ // print information about selected Unit

    var val = $("#units").val(); // get selected unit id
    msg("Valor del grupo: " + val);
    if(!val) return; // exit if no unit selected
    
    var unit = wialon.core.Session.getInstance().getItem(val); // get unit by id
    msg(unit.getUnits());
    var unidades = unit.getUnits();
    
    msg(unidades.length);
    for(var j=0;j<inidades.length;j++){
       msg(unidades.getPosition()); //THIS PROBLEM NO INFORMATION
    }
    
    
    
    if(!unit){ 
        msg("Unit not found");
        return; 
    } // exit if unit not found
    
    // construct message with unit information
    var text = "<div>'"+unidades.getName()+"' selected. "; // get unit name
    var icon = unit.getIconUrl(32); // get unit Icon url
    if(icon) text = "<img class='icon' src='"+ icon +"' alt='icon'/>"+ text; // add icon to message
    var pos = unidades.getPosition(); // get unit position
    if(pos){ // check if position data exists
        var time = wialon.util.DateTime.formatTime(pos.t);
        text += "<b>Last message</b> "+ time +"<br/>"+ // add last message time
            "<b>Position</b> "+ pos.x+", "+pos.y +"<br/>"+ // add info about unit position
            "<b>Speed</b> "+ pos.s; // add info about unit speed
        // try to find unit location using coordinates 
        wialon.util.Gis.getLocations([{lon:pos.x, lat:pos.y}], function(code, address){ 
            if (code) { msg(wialon.core.Errors.getErrorText(code)); return; } // exit if error code
            msg(text + "<br/><b>Location of unit</b>: "+ address+"</div>"); // print message to log
        });
    } else // position data not exists, print message
        msg(text + "<br/><b>Location of unit</b>: Unknown</div>");
}

// execute when DOM ready
$(document).ready(function () {
    wialon.core.Session.getInstance().initSession("https://hst-api.wialon.com"); // init session
    // For more info about how to generate token check
    // http://sdk.wialon.com/playground/demo/app_auth_token
    wialon.core.Session.getInstance().loginToken("52e46546b26c686a90a987643f056cfcE3F6CFF45C55B3E221520C9E0FBA29D05615679D", "", // try to login
        function (code) { // login callback
            // if error code - print error message
            if (code){ msg(wialon.core.Errors.getErrorText(code)); return; }
            msg("Logged successfully"); init(); // when login suceed then run init() function
    });
});
10

How to get a group of units through javascript SDK?

Re: How to get a group of units through javascript SDK?

Denisse, you don't get units because only unit_groups were added to session.
Just add units to updateDataFlags:

[{type: "type", data: "avl_unit_group", flags: flags, mode: 0}, {type: "type", data: "avl_unit", flags: flags, mode: 0}]

Add lastPosition data flag: wialon.item.Unit.dataFlag.lastPosition

Also change code of getting units' positions:

    msg(unidades.length);
    for(var j=0;j<inidades.length;j++){
       var u = wialon.core.Session.getInstance().getItem(inidades[j]);
       msg(u.getPosition()); //THIS PROBLEM NO INFORMATION
    }
Head of Wialon Local Department
Gurtam
11

How to get a group of units through javascript SDK?

Re: How to get a group of units through javascript SDK?

Good day deal

Thank you for this information, I am grateful.