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>Wialon Playground - Get sensors</title> <script type="text/javascript" src="//code.jquery.com/jquery-latest.min.js"></script> <script type="text/javascript" src="//hst-api.wialon.com/wsdk/script/wialon.js"></script> </head> <body> Select unit: <select id="units"></select> available sensors <select id="sensors"></select> <div id="log"></div> </body> </html>
HTML
#log { border: 1px solid #c6c6c6; } .icon {float:left; margin:10px;}
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.sensors | wialon.item.Unit.dataFlag.lastMessage; sess.loadLibrary("unitSensors"); // load Sensor Library sess.updateDataFlags( // load items to current session [{type: "type", data: "avl_unit", flags: flags, mode: 0}], // Items specification function (code) { // updateDataFlags callback if (code) { msg(wialon.core.Errors.getErrorText(code)); return; } // exit if error code var units = sess.getItems("avl_unit"); // get loaded 'avl_unit's items if (!units || !units.length){ msg("No units found"); return; } // check if units found for(var i=0; i<units.length; i++) // construct Select list using found units $("#units").append("<option value='"+ units[i].getId() +"'>"+ units[i].getName() +"</option>"); getSensors(); // get Sensors for currently selected unit // bind actions to selects $("#units").change( getSensors ); $("#sensors").change( getSensorInfo ); }); } function getSensors(){ // construct sensors Select list for selected unit if(!$("#units").val()){ msg("Select unit"); return;} // exit if no unit selected $("#sensors").html("<option></option>"); // add first empty element var sess = wialon.core.Session.getInstance(); // get instance of current Session var unit = sess.getItem( $("#units").val() ); // get unit by id var sens = unit.getSensors(); // get unit's sensors for(var i in sens) // construct select list $("#sensors").append("<option value='" + sens[i].id + "'>" + sens[i].n + "</option>"); } function getSensorInfo(){ // get and show information about selected Sensor if(!$("#units").val()){ msg("Select unit"); return;} // exit if no unit selected if(!$("#sensors").val()) return; // exit if no unit selected var sess = wialon.core.Session.getInstance(); // get instance of current Session var unit = sess.getItem( $("#units").val() ); // get unit by id var sens = unit.getSensor( $("#sensors").val() ); // get sensor by id // calculate sensor value var result = unit.calculateSensorValue(sens, unit.getLastMessage()); if(result == -348201.3876) result = "N/A"; // compare result with invalid sensor value constant // print result message msg("Value of "+ unit.getName() +" <b>'"+ sens.n +"'</b> sensor ("+ sens.t +"): "+ result + " ("+ sens.m +")"); } // execute when DOM ready $(document).ready(function () { msg("Trying to login"); 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("5dce19710a5e26ab8b7b8986cb3c49e58C291791B7F0A7AEB8AFBFCEED7DC03BC48FF5F8", "", // try to login function (code) { // login callback if (code){ msg(wialon.core.Errors.getErrorText(code)); return; } // exit if error code msg("Logged successfully"); init(); // when login suceed then run init() function }); });
JS
Result
Source code of example
Close ✕
×
Source code