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 fillings</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> <fieldset> <legend>Import fillings:</legend> <input type="file" name="import_file" id="import_file"/> <input id="import_button" type="submit" value="Upload & Import"/> </fieldset> <br/> <fieldset> <legend>Log :</legend> <div id="log_cont"> <table> <tbody id="log"></tbody> </table> </div> </fieldset> </body> </html>
HTML
#log_cont { background-color: #EEEEEE; overflow-y: scroll; font-size: 75%; height: 150px; }
CSS
var importFillings = { constructor: null, id: "importFillings", type: "importFillings", user: {}, files: "", units: {}, arrSearch: [], arrData: [], session: {}, unitsToImport: [], doImport: function () { importFillings.arrSearch.sort(); for (var i = 0; i < importFillings.arrData.length; i++) { for (var j = 0; j < importFillings.arrSearch.length; j++) { // searching unit by imei if (importFillings.arrSearch[j][1] == importFillings.arrData[i].imei) { var unit = importFillings.session.getItem(importFillings.arrSearch[j][0]); var unitData = importFillings.arrData[i]; importFillings.unitsToImport.push([unit,unitData]); break; } } } // checking if find anything if (importFillings.unitsToImport) { // start colllecting requests wialon.core.Remote.getInstance().startBatch("PGImportFillings"); for (var i = 0; i < importFillings.unitsToImport.length; i++) { var unit = importFillings.unitsToImport[i][0]; var unitData = importFillings.unitsToImport[i][1]; /** * Try to registry filling event * @param unitData.date {Long} * @param unitData.description {String} * @param unitData.x {Double} longtitude * @param unitData.y {Double} latitude * @param unitData.location {String} * @param unitData.volume {Double} * @param unitData.cost {Double} * @param unitData.deviation {Double} * @return callback that get result of server operation in form callback(code) */ unit.registryFuelFillingEvent(unitData.date, unitData.description, unitData.x, unitData.y, unitData.location, unitData.volume, unitData.cost, unitData.deviation, function (code) { if (code) { importFillings.log(wialon.core.Errors.getErrorText(code)); } }); } // stop collect requests and send then logout wialon.core.Remote.getInstance().finishBatch(function (code) { wialon.core.Session.getInstance().logout(function (code) { if (code) { importFillings.log(wialon.core.Errors.getErrorText(code)); return; } else { importFillings.log('Import completed successfully'); $('#import_button').attr("disabled", true); importFillings.log("Logout successfull"); } }); },"PGImportFillings"); } else { importFillings.log("No data to import"); wialon.core.Session.getInstance().logout(); } }, handleFileSelect: function (evt) { importFillings.files = evt.target.files; if (!importFillings.files.length) { alert('Please select a file!'); return; } if (importFillings.files[0].type.match('import.txt')) { importFillings.log("Wrong file!"); return; } $('#import_button').attr("disabled", false); }, onImport: function () { var file = importFillings.files[0]; var fileData = ""; var fileArray = []; // creating HTML5 file reader object var reader = new FileReader(); /** * reading file as text * @param file {Object} current file * @return {String} */ reader.readAsText(file); reader.onloadend = function (evt) { if (evt.target.readyState == FileReader.DONE) { importFillings.log(['Read bytes: ', file.size].join('')); } //geting result of reading fileData = evt.target.result; // parse result into array fileArray = fileData.split(/([\d;.]+[&\S\d\x20]+)\n/); var tmpArr = []; // reading fileArray which contains strings of imported file for (i = 0; i < fileArray.length; i++) { var str = fileArray[i]; if (str == "") { continue; } tmpArr = str.split(";"); // 0 unit (IMEI) unique ID if (isNaN(parseInt(tmpArr[0])) == true || tmpArr[0].length < 12 || tmpArr[0].length > 17 || tmpArr[0].indexOf("+") != -1) { importFillings.log("err 02: invalid imei"); return; } // 1 DATE of filling if (isNaN(parseInt(tmpArr[1])) == true || tmpArr[1].length != 10) { importFillings.log("err 03: invalid date"); return; } // 2 VOLUME of filling if (isNaN(parseInt(tmpArr[2])) == true || (parseInt(tmpArr[2]) <= 0 && parseInt(tmpArr[2]) > 999999)) { importFillings.log("err 04: invalid volume"); return; } if (fileArray.length >= 6) { // 4-5 X Y if (tmpArr[4].indexOf(",") != -1 || tmpArr[5].indexOf(",") != -1) { importFillings.log("err 05: invalid longtitude or latitude"); return; } } //create object wich will be sended to server var tmpObj = { imei: parseInt(tmpArr[0]), date: parseInt(tmpArr[1]), volume: parseFloat(tmpArr[2]), cost: isNaN(parseInt(tmpArr[3])) == true ? 0 : parseInt(tmpArr[3]), //cost filling location: "", // by default deviation: 30, // by default x: isNaN(parseFloat(tmpArr[4])) == true ? 0 : parseFloat(tmpArr[4]), //lon filling y: isNaN(parseFloat(tmpArr[5])) == true ? 0 : parseFloat(tmpArr[5]), //lat filling description: typeof tmpArr[6] == "undefened" ? "" : tmpArr[6] }; // collect this objects importFillings.arrData.push(tmpObj); } if (importFillings.arrData.length == 0 || importFillings.arrSearch.length == 0) { importFillings.log("unknown error"); return; } if (!importFillings.arrData.length) { importFillings.log("No data in file"); return; } importFillings.doImport(); }; }, init: function () { importFillings.session = wialon.core.Session.getInstance(); importFillings.session.loadLibrary("unitEventRegistrar"); var sess = importFillings.session; // flags to specify what kind of data should be returned var flags = wialon.util.Number.or(wialon.item.Item.dataFlag.base, wialon.item.Unit.dataFlag.restricted); sess.updateDataFlags( // START 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 // get loaded 'avl_unit's items var units = wialon.util.Helper.sortItems(sess.getItems("avl_unit")); if (!units || !units.length) { importFillings.log("Units not found"); return; } else { for (var i = 0; i < units.length; i++) { var unit = units[i]; if (!wialon.util.Number.and(unit.getUserAccess(), wialon.item.Unit.accessFlag.editEvents)) { importFillings.log("Not enought rights for: " + unit.getName()); } else { if (unit.getUniqueId() != "") { // creating array pairs for search unit by unique Id importFillings.arrSearch.push([unit.getId(), parseInt(unit.getUniqueId())]); } } } } }); // END updateDataFlags }, 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 () { $('#import_button').attr("disabled", true).click(importFillings.onImport); 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) { importFillings.log(wialon.core.Errors.getErrorText(code)); return; // exit if error code } else { importFillings.log("Logined successfully"); importFillings.init(); // when login suceed then run init() function } }); $('#import_file').change(importFillings.handleFileSelect); });
JS
Result
Source code of example
Close ✕
×
Source code