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 - Login</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> Token: <input type="text" id="token"/> <input type="button" value="login" id="login_btn" /> <div id="unit_select_container" style="display:none;">Unit: <select></select> <button>Get last messages</button></div> <div id="log"></div> </body> </html>
HTML
#log { border: 1px solid #c6c6c6; }
CSS
// Print message to log function msg(text) { $("#log").prepend(text + "<br/>"); } var session, remote; function onLogin() { msg("Logged successfully"); // login succeed $("#login_btn").hide(); remote = wialon.core.Remote.getInstance(); var unitSelectContainerEl = document.getElementById('unit_select_container'), unitsSelectEl = unitSelectContainerEl.querySelector('select'), executeButtonEl = unitSelectContainerEl.querySelector('button'); // Load all units to the session var dataFlags = wialon.util.Number.or(wialon.item.Item.dataFlag.base, wialon.item.Unit.dataFlag.sensors); session.updateDataFlags([{ type: 'type', data: 'avl_unit', flags: dataFlags , mode: 0 }], function(error) { if (error) { msg(wialon.core.Errors.getErrorText(error)); return; } var units = session.getItems('avl_unit'); if (!units || !units.length) { msg('No units available'); return; } wialon.util.Helper.sortItems(units); units.forEach(function(unit) { var optionEl = document.createElement('option'); optionEl.value = unit.getId(); optionEl.innerHTML = unit.getName(); unitsSelectEl.appendChild(optionEl); }); unitSelectContainerEl.style.display = ''; }); executeButtonEl.addEventListener('click', function() { executeButtonEl.disabled = true; var unit = session.getItem(unitsSelectEl.value); if (!unit) { executeButtonEl.disabled = false; msg('No such unit'); return; } remote.startBatch(); var timeTo = session.getServerTime(), timeFrom = timeTo - 24 * 60 * 60; // 1 day var allMessages = null, allSensorValues = null; remote.remoteCall('messages/load_interval', { itemId: unit.getId(), timeFrom: timeFrom, timeTo: timeTo, flags: 0, flagsMask: 0xff00, loadCount: 999999 }, function(error, result) { if (error) { executeButtonEl.disabled = false; msg(wialon.core.Errors.getErrorText(error)); return; } allMessages = result.messages; }); remote.remoteCall('unit/calc_sensors', { source: '', unitId: unit.getId(), indexFrom: 0, indexTo: 999999, sensorId: 0 // all available }, function(error, result) { if (error) { executeButtonEl.disabled = false; msg(wialon.core.Errors.getErrorText(error)); return; } allSensorValues = result; }); remote.finishBatch(function() { if (!allMessages || !allSensorValues) { msg('fail'); return; } var unitSensors = unit.getSensors(); allSensorValues.forEach(function(messageSensors, messageIndex) { var sensorsData = []; for (var key in messageSensors) if (Object.prototype.hasOwnProperty.call(messageSensors, key)) { var sensorName = unitSensors[key].n, sensorValue = messageSensors[key]; sensorsData.push(sensorName + ': ' + sensorValue); } //rows.push(sensorsData.join(', '); msg('<b>' + allMessages[messageIndex].t + ':</b> ' + sensorsData.join(', ') + '<br>'); }); }); }); } function loadAllMessages(options, callback) { var count = options.count; var MAX_MESSAGES = 3000; var messages = []; loadPart(0); function loadPart(offset) { if (offset >= count) { callback(null, messages); return; } remote.remoteCall('messages/get_messages', { indexFrom: offset, indexTo: offset + MAX_MESSAGES }, function(error, result) { if (error) { callback(error); return; } result.forEach(function(msg) { messages.push(msg); }); if (!result.length) { callback(null, messages); return; } loadPart(offset + result.length); }); } } // Login to server using entered username and password function login() { var sess = wialon.core.Session.getInstance(); // get instance of current Session sess.loadLibrary("unitSensors"); session = sess; var user = sess.getCurrUser(); // get current User if( user ) { // if user exists - you are already logged, print username to log msg("You are logged as '" + user.getName()+"', click logout button first"); return; } // if not logged var token = $("#token").val(); // get token from input if (!token) { // if token is empty - print message to log msg("Enter token"); return; } msg("Trying to login with token '"+ token +"'"); sess.initSession("https://hst-api.wialon.com"); // initialize Wialon session sess.loginToken(token, "", // trying login function (code) { // login callback if (code) msg(wialon.core.Errors.getErrorText(code)); // login failed, print error else onLogin(); } ); } // execute when DOM ready $(document).ready(function(){ // For more info about how to generate token check // http://sdk.wialon.com/playground/demo/app_auth_token $("#token").val("5dce19710a5e26ab8b7b8986cb3c49e58C291791B7F0A7AEB8AFBFCEED7DC03BC48FF5F8"); // bind actions to buttons click $("#login_btn").click( login ); });
JS
Result
Source code of example
Close ✕
×
Source code