Тема: 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.
Вы не вошли. Пожалуйста, войдите или зарегистрируйтесь.
Hi good day,
Someone can help me know how to get groups of units wialon account for javascript?
Suggest, please.
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
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?
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);});
});
Thank you very much for the answer.
deal
Hi!
A question, the reports, can I execute for unit group with same library avl_unit_group?
Suggest please?
Denisse, try to modify this example for unit groups instead of units:
https://sdk.wialon.com/playground/demo/execute_report
Thank you deal
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
});
});
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
}
Good day deal
Thank you for this information, I am grateful.
Hi,
If I have a list of groups, how do I get the list of selected group units?
mshamsan hello,
You can try to use search items by properties request. This request allows to get internal IDs of the items and other properties. You can use it to get all available unit groups names and IDs and array of units' IDs included in each group.
Example of the request: https://hst-api.wialon.com/wialon/ajax.html?svc=core/search_items¶ms={"spec":{"itemsType":"avl_unit_group","propName":"sys_name","propValueMask":"*","sortType":"sys_name"},"force":1,"flags":1,"from":0,"to":0}&sid=ACTIVE_SID
The units IDs will be available in the "u" parameter array.
Then you can use IDs of the units t search specific unit details with search item by ID request.