Toggle navigation
Wialon Playground
Add library
jQuery latest
Bootstrap 3.3.1
Wialon Javascript SDK
Run
Get code
Fork
Save
Examples
Login
Get units
Change unit icon
Get messages
Get sensors
Edit sensors
Commands
Monitoring notification
Track layer
Get resources
Resources and accounts
Account parameters
Do payment
Management Driver
Create notification
Get geofences
Geofence parameters
Create geofence
Gurtam map
Units on map
Unit trace
Create report template
Execute report
Execute custom report
Create driver
Bind driver to unit
Unit edit fields
Import fillings
Token login for site
Advanced authorization form
Token usage in app
Account hierarchy
Nearest units
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Система контроля за подвижными элементами</title> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> <script type="text/javascript" src="https://hst-api.wialon.com/wsdk/script/wialon.js"></script> <script language="Javascript" type="text/javascript" src="jquery.js"></script> <link type="text/css" href="/calendar/latest.css" rel="Stylesheet" /> <script type="text/javascript" src="/calendar/latest.js"></script> </head> <body> <td>Гос № ТС: </td> <td><select id="units"></select></td> <br> Ресурс:<select id="res"><option></option></select> Геозона:<select id="zones"></select> <br> <tr> <td>От: (дата)</td> <td><input type="date" id = "from"/></td> <td>(время)</td> <td><input type="time" id = "fromT"/></td> <br> </tr> <td>До: (дата)</td> <td><input type="date" id = "to"/></td> <td>(время)</td> <td><input type="time" id = "toT"/></td> <br> <tr> <form> <td><input type="button" value="Выполнить отчет" id=exec_btn></td> </form> <br> </tr><tr> <td> <div id="log"></div></td> </body> </html> </body> </html>
HTML
CSS
// 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 | wialon.item.Item.dataFlag.customFields; sess.updateDataFlags( // load items to current session [{type: "type", data: "avl_unit", flags: flags, mode: 0}], // Items specification function (code) { // updateDataFlags callback console.log(code); 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"); console.log(units); 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 var u = units[i]; // current unit in cycle var customFields = u.getCustomFields(); var firstCustomField = ""; for (var id in customFields) { firstCustomField = customFields[id].n + ": " + customFields[id].v; break; } // append option to select $("#units").append("<option value='"+ u.getId() +"'>" + u.getName() + (firstCustomField.length ? " (" + firstCustomField + ")" : "") + "</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 if(!val) return; // exit if no unit selected var unit = wialon.core.Session.getInstance().getItem(val); // get unit by id if(!unit){ msg("Unit not found");return; } // exit if unit not found // construct message with unit information var text = "<div>'"+unit.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 = unit.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 () { var sess = wialon.core.Session.getInstance(); sess.initSession("https://hst-api.wialon.com"); // init session sess.loadLibrary("itemIcon"); // load Icon Library sess.loadLibrary("itemCustomFields"); // For more info about how to generate token check // [url]http://sdk.wialon.com/playground/demo/app_auth_token[/url] wialon.core.Session.getInstance().loginToken("5dce19710a5e26ab8b7b8986cb3c49e58C291791B7F0A7AEB8AFBFCEED7DC03BC48FF5F8", "", // 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 }); });
JS
Result
Source code of example
Close ✕
×
Source code