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 - Import resource props</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> Append resource props to: <select id="resource" name="resource"><option value="">-- select resource --</option></select><br/> From file: <input class="btn" type="file" name="import_file" id="import_file"><br/> <input class="btn" id="btn_import_wlp" type="submit" value="Upload & Import"> </div> Data to import: <div id="prop_types"></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 importWLP = { // generate needed res props system data prop_types: { poi: { library: "resourcePois", dataFlag: wialon.item.Resource.dataFlag.poi, editFlag: wialon.item.Resource.accessFlag.editPoi, methodCreateItem: "createPoi", printName: "POIs" }, zones: { library: "resourceZones", dataFlag: wialon.item.Resource.dataFlag.zones, editFlag: wialon.item.Resource.accessFlag.editZones, methodCreateItem: "createZone", printName: "Geofences" }, jobs: { library: "resourceJobs", dataFlag: wialon.item.Resource.dataFlag.jobs, editFlag: wialon.item.Resource.accessFlag.editJobs, methodCreateItem: "createJob", printName: "Jobs" }, notifications: { library: "resourceNotifications", dataFlag: wialon.item.Resource.dataFlag.notifications, editFlag: wialon.item.Resource.accessFlag.editNotifications, methodCreateItem: "createNotification", printName: "Notifications" }, drivers: { library: "resourceDrivers", dataFlag: wialon.item.Resource.dataFlag.drivers, editFlag: wialon.item.Resource.accessFlag.editDrivers, methodCreateItem: "createDriver", printName: "Drivers" }, trailers: { library: "resourceTrailers", dataFlag: wialon.item.Resource.dataFlag.trailers, editFlag: wialon.item.Resource.accessFlag.editTrailers, methodCreateItem: "createTrailer", printName: "Trailers" }, reports: { library: "resourceReports", dataFlag: wialon.item.Resource.dataFlag.reports, editFlag: wialon.item.Resource.accessFlag.editReports, methodCreateItem: "createReport", printName: "Reports" } }, init: function () { var sess = wialon.core.Session.getInstance(); // flags to specify what kind of data should be returned var flags = wialon.item.Item.dataFlag.base; for (var i in this.prop_types) { sess.loadLibrary( this.prop_types[i].library ); flags = wialon.util.Number.or(flags, this.prop_types[i].dataFlag); // 64 bit OR $('#prop_types').append('<input type="checkbox" checked="checked" name="'+i+'"> '+this.prop_types[i].printName+'<br>'); // construct prop types checkboxes } // 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) { importWLP.log("Error: "+wialon.core.Errors.getErrorText(code)); return; // exit if error code } importWLP.res = sess.getItems("avl_resource"); // get loaded 'avl_resource's items for (var i = 0; i < importWLP.res.length; i++) { $('#resource').append('<option value="'+ importWLP.res[i].getId() +'">'+ importWLP.res[i].getName() +'</option>'); } }); $('#btn_import_wlp').click( function(){ importWLP.importFromFile.apply( $('#import_file')[0] ) } ); }, importFromFile: function (event) { var res_id = $('#resource').val(), filter = []; if ( !res_id ) { alert('Select resource to import to'); return false; } $('#prop_types input[type="checkbox"]:checked').each(function(){ filter.push( $(this).attr('name') ); }); if ( !filter.length ) { alert('Select prop types to import'); return false; } importWLP.log('Import start...'); // this - is input type="file" wialon.exchange.Exchange.importJson([this], function(code, fileData){ if (code) { importWLP.log('Error('+code+')'); return; } var res = wialon.core.Session.getInstance().getItem(res_id); // get resource by id var user_access = res.getUserAccess(); // iterate through uploaded files wialon.core.Remote.getInstance().startBatch(); for (var f in fileData.files) { // iterate through props for (var res_type in importWLP.prop_types) { if ( filter.indexOf(res_type) <= -1 ) { importWLP.log('Skipped '+res_type); continue; } // check resProp exists and items are in array if ( typeof fileData.files[f][res_type] != 'undefined' && $.isArray( fileData.files[f][res_type] ) && fileData.files[f][res_type].length ) { if ( !wialon.util.Number.and(user_access, importWLP.prop_types[res_type].editFlag) ) { importWLP.log('You don\'t have access to edit '+ res_type +' in ' + res.getName()); continue; } // iterate through prop items for (var i in fileData.files[f][res_type]) { // create needed prop res[ importWLP.prop_types[res_type].methodCreateItem ]( fileData.files[f][res_type][i], function(code, data) { importWLP.log('Created ' + ( (data && typeof data.n != 'undefined') ? "'" + data.n + "'" : 'prop') + ' ' + (code ? 'Error('+code+')' : 'Ok')); }); } } } } wialon.core.Remote.getInstance().finishBatch(function(){ importWLP.log('Import completed successfully'); $('#resource').val(''); }); }); }, 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) { importWLP.log(wialon.core.Errors.getErrorText(code)); return; // exit if error code } importWLP.init(); // when login suceed then run init() function }); });
JS
Result
Source code of example
Close ✕
×
Source code