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
Get POIs
Create POI
Create notification
Get geofences
Geofence parameters
Create geofence
Export geofences
Gurtam map
Units on map
Unit trace
Create report template
Execute report
Execute custom report
Remaining SMS
Create driver
Bind driver to unit
Export resource props
Import resource props
Unit edit fields
Import fillings
Fuel by mail
Token login for site
Advanced authorization form
Token usage in app
Get location's geofences
Account hierarchy
Nearest units
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Wialon Playground - Create driver</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> <div id="base"> <div> Resource: <select id="resource" name="resource"><option value="">-- select resource --</option></select> <table id="driver_data"> <tbody> <tr> <td>Driver name</td> <td><input type="text" name="n" value="Ken Block"></td> </tr> <tr> <td>Driver description</td> <td><input type="text" name="ds" value="Cool drifter"></td> </tr> <tr> <td>Driver phone</td> <td><input type="text" name="p" value=""></td> </tr> </tbody> </table> <input class="btn" type="submit" id="btn_create_driver" value="Create driver"> </div> Log: <div id="log_cont"> <table> <tbody id="log"></tbody> </table> </div> </div> </body> </html>
HTML
.btn { cursor:pointer; } div { margin-bottom:20px; } #log_cont { background-color: #EEEEEE; height: 150px; overflow-y: scroll; font-size:75%; } #log_cont table { width:100%; }
CSS
var createDriver = { init: function () { var sess = wialon.core.Session.getInstance(); sess.loadLibrary( "resourceDrivers" ); // flags to specify what kind of data should be returned var flags = wialon.util.Number.or(wialon.item.Item.dataFlag.base, wialon.item.Resource.dataFlag.drivers); // 64 bit OR // Subscribe on resource data sess.updateDataFlags( // load items to current session [{type: "type", data: "avl_resource", flags: flags, mode: 0}], // Items specification function (code){ // updateDataFlags callback if (code) { createDriver.log("Error: "+wialon.core.Errors.getErrorText(code)); return; // exit if error code } createDriver.res = sess.getItems("avl_resource"); // get loaded 'avl_resource's items for (var i = 0; i < createDriver.res.length; i++) { $('#resource').append('<option value="'+ createDriver.res[i].getId() +'">'+ createDriver.res[i].getName() +'</option>'); } }); $('#btn_create_driver').click( createDriver.create ); }, create: function (event) { var res_id = $('#resource').val(); if ( !res_id ) { alert('Select resource where you want to create driver'); return false; } var res = wialon.core.Session.getInstance().getItem(res_id); // get resource by id if ( !wialon.util.Number.and(res.getUserAccess(), wialon.item.Resource.accessFlag.editDrivers) ) { createDriver.log('You don\'t have access to edit drivers in ' + res.getName()); return; } var $t, driver = { "itemId":res_id, // resourceId "id":0, // item_id "callMode":"create", "c":"", // driver code "ck":0, // image checksum "ds":"", // description "n":"", // name "p":"", // phone "r":1, // image aspect ratio "f":0, // flags "jp":{} // additional fields } $('#driver_data input').each(function(){ $t = $(this); driver[ $t.attr('name') ] = $t.val(); }); res.createDriver( driver, function(code, data) { createDriver.log('Driver ' + ( (data && typeof data.n != 'undefined') ? "'" + data.n + "'" : '') + ' create result: ' + (code ? 'Error('+code+')' : 'Ok')); }); }, log: function(msg) { var $log = $('#log'), $log_cont = $('#log_cont'); $log.append('<tr><td>' + msg + '</td></tr>'); $log_cont.animate({ scrollTop: $log_cont[0].scrollHeight }, 300); } }; // 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("5dce19710a5e26ab8b7b8986cb3c49e58C291791B7F0A7AEB8AFBFCEED7DC03BC48FF5F8", "", // try to login function (code) { // login callback if (code){ createDriver.log(wialon.core.Errors.getErrorText(code)); return; // exit if error code } createDriver.init(); // when login suceed then run init() function }); });
JS
Result
Source code of example
Close ✕
×
Source code