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 - Fuel by mail</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>Resource:</legend> <select id="resource" name="resource" > <option value="">-- select resource --</option> </select> </fieldset> <fieldset> <legend>General</legend> <table> <tr><th>Event type:</th></tr> <tr><td><input type="checkbox" class="input" id="available"/> Fuel level</td></tr> <tr><td><input type="checkbox" class="input" id="filled"/> Filling</td></tr> <tr><td><input type="checkbox" class="input" id="thefted"/> Theft</td></tr> <tr><th>Message form:</th></tr> <tr><td><input type="radio" class="input" id="one_unit"/> Separate message for each unit</td></tr> <tr><td><input type="radio" class="input" id="all_units"/> All units in one message</td></tr> <tr><td>Time offset: <input type="text" class="input" value="0" id="time_offset"/> minutes</td></tr> </table> </fieldset> <fieldset> <legend>Recipient:</legend> <table> <tr> <th>E-mail:</th> </tr> <tr> <td> <input type="text" class="input" id="email" value=""/> </td> </tr> </table> </fieldset> <fieldset> <legend>Job :</legend> <table> <tr> <td>Name :</td> <td> <input type="text" class="input" id="job_name"/> </td> </tr> <tr> <td>Description :</td> <td> <input type="text" class="input" id="job_desc"/> </td> </tr> </table> </fieldset> <fieldset> <legend>Units :</legend> <table> <tbody id="units_table_data"> </tbody> </table> </fieldset> <input class="button input" type="button" id="send" value="Create Job"/><br/> <fieldset> <legend>Log :</legend> <div id="log_cont"> <table> <tbody id="log"></tbody> </table> </div> </fieldset> </body> </html>
HTML
#time_offset { width: 2em; text-align: center; } #log_cont { background-color: #EEEEEE; height: 150px; overflow-y: scroll; font-size:75%; }
CSS
/// Check email is correct function is_valid_email(email) { return (/^([a-z0-9а-я_\-]+\.)*[a-z0-9а-я_\-]+@([a-z0-9а-я]*[a-z0-9а-я_\-]+\.)+[a-zа-я]{2,4}$/i).test(email); } var fuelConsumption = { id: "fuelConsumption", type: "fuelConsumption", selected_units: [], user: {}, /** DEFINE */ /// Send one unit data in one message AVL_REPORT_JOB_ONE_UNIT_ONE_MSG: 0x01, /// Send all units data in one message AVL_REPORT_JOB_ALL_UNITS_ONE_MSG: 0x02, // Type event fuel (in send email job) /// Event filled AVL_REPORT_JOB_EVENT_FILLED: 0x04, /// Event thefted AVL_REPORT_JOB_EVENT_THEFHED: 0x08, /// Event available AVL_REPORT_JOB_EVENT_AVAILABLE: 0x10, // Type send mail (in send email job) /// Send msg to e-mail AVL_REPORT_JOB_SEND_MSG_EMAIL: 0x20, create_table_body: function (units) { $('#units_table_data').html(""); /** * @param {object} row data * @param {boolean} cols one or two column(s) of table */ var add_row = function (row) { if (!row || !row.item_id || !row.item_name) { return ""; } var html = ""; html += "<tr><td style='width:31px' align='center'><input type='checkbox' class='input unit' id=" + row.item_id + "></td>"; html += "<td><label for='unit_label_" + row.item_id + "'>" + row.item_name + "</label></td></tr>"; return html; }; var str = ""; for (var i = 0; i < units.length; i++) { if (!wialon.util.Number.and(units[i].getUserAccess(), wialon.item.Unit.accessFlag.monitorState) || !wialon.util.Number.and(units[i].getUserAccess(), wialon.item.Item.accessFlag.execReports)) { fuelConsumption.log("Not enought rights for: " + units[i].getName()); continue; } var unit = units[i]; // current unit in cycle // append row to table var checked = false; str += add_row({ item_id: unit.getId(), item_name: unit.getName(), checked: checked }); } if (str != "") { $('#units_table_data').html(str); } }, onSend: function () { var res_id = $('#resource').val(); fuelConsumption.selected_units = []; if (!res_id) { alert('Select resource where you want to create job'); return false; } var res = wialon.core.Session.getInstance().getItem(res_id); $('input.unit').each(function () { var checked = $(this).prop("checked"); if (checked) { fuelConsumption.selected_units.push( $(this).attr("id") ); } }); if (fuelConsumption.selected_units.length == 0) { fuelConsumption.log("Error no selected units "); return; } var r_ok = is_valid_email($("#email").val()); if (r_ok == false) { fuelConsumption.log("Please check email "); $('#email').css("color", r_ok ? "black" : "red"); return; } if ($('#job_name').val() == "") { fuelConsumption.log("Please check job name "); return; } // add flags for action var flags = 0; flags += fuelConsumption.AVL_REPORT_JOB_SEND_MSG_EMAIL; flags += $('#one_unit').prop("checked") ? fuelConsumption.AVL_REPORT_JOB_ONE_UNIT_ONE_MSG : 0x00; flags += $('#all_units').prop("checked") ? fuelConsumption.AVL_REPORT_JOB_ALL_UNITS_ONE_MSG : 0x00; flags += $('#filled').prop("checked") ? fuelConsumption.AVL_REPORT_JOB_EVENT_FILLED : 0x00; flags += $('#thefted').prop("checked") ? fuelConsumption.AVL_REPORT_JOB_EVENT_THEFHED : 0x00; flags += $('#available').prop("checked") ? fuelConsumption.AVL_REPORT_JOB_EVENT_AVAILABLE : 0x00; var job = {}; job.n = $('#job_name').val().trim(); job.d = $('#job_desc').val() == "" ? "" : $("#job_desc").val().trim(); job.r = "2 60"; // sending by every minutes job.at = wialon.core.Session.getInstance().getServerTime() + 30; job.tz = +fuelConsumption.user.getCustomProperty("tz"); job.l = "en"; job.e = 1; job.m = 1; // run job once job.st = { e: 1, c: 0, l: 0 }; // default values job.sch = { f1: 0, f2: 0, t1: 0, t2: 0, m: 0, y: 0, w: 0 }; // default values // add action job.act = { t: "send_email_sms_fuel", p: { email_to: $('#email').val(), flags: flags, phone_to: "", time_offset: parseInt($("#time_offset").val()), units: fuelConsumption.selected_units.toString() } }; job.id = 0; // finally create the job res.createJob(job, function (code, res) { if (code) { fuelConsumption.log("Error creating job: " + job.n); } else { wialon.core.Session.getInstance().logout(function (code) { if (code) { fuelConsumption.log(wialon.core.Errors.getErrorText(code)); return; } else { $('input.input').prop("disabled", true); fuelConsumption.log("Logout successfull"); } }); fuelConsumption.log("Job \"" + job.n + "\" created."); } }); }, 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; sess.updateDataFlags( // load items to current session [{type: "type", data: "avl_unit", flags: flags, mode: 0}], // Items specification function (code) { // updateDataFlags callback START if (code) { fuelConsumption.log(wialon.core.Errors.getErrorText(code)); return; } // exit if error code // get loaded 'avl_unit's items var units = sess.getItems("avl_unit"); if (!units || !units.length) { fuelConsumption.log("Units not found"); return; } else { fuelConsumption.create_table_body(units); $('#resource').change(function () { if ($(this).val() != 0) { $('input.input').attr("disabled", false); $('#send').click(fuelConsumption.onSend); } else { $('input.input').prop("disabled", true); } }).trigger("change"); } }); // updateDataFlags callback END // get current user fuelConsumption.user = wialon.core.Session.getInstance().getCurrUser(); sess.loadLibrary("resourceJobs"); var flags_res = wialon.util.Number.or(wialon.item.Item.dataFlag.base, wialon.item.Resource.dataFlag.jobs); // Subscribe on resource data sess.updateDataFlags( // load items to current session [{type: "type", data: "avl_resource", flags: flags_res, mode: 0}], // Items specification function (code) { // updateDataFlags callback if (code) { fuelConsumption.log("Error: " + wialon.core.Errors.getErrorText(code)); return; // exit if error code } fuelConsumption.res = sess.getItems("avl_resource"); // get loaded 'avl_resource's items for (var i = 0; i < fuelConsumption.res.length; i++) { if (!wialon.util.Number.and( fuelConsumption.res[i].getUserAccess(), wialon.item.Resource.accessFlag.editJobs)) { fuelConsumption.log("Not enought rights for: " + fuelConsumption.res[i].getName()); continue; } $('#resource').append('<option value="' + fuelConsumption.res[i].getId() + '">' + fuelConsumption.res[i].getName() + '</option>'); } }); // Event handlers $('#one_unit').prop("checked", true); $('#one_unit').css({cursor: "pointer"}).click(function () { $('#all_units').prop("checked", false); }); $('#all_units').css({cursor: "pointer"}).click(function () { $('#one_unit').prop("checked", false); }); $('#email').keyup(function () { /// Check email is correct var r_ok = is_valid_email($(this).val()); $(this).css("color", r_ok ? "black" : "red"); }); }, 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) { fuelConsumption.log(wialon.core.Errors.getErrorText(code)); return; // exit if error code } else { fuelConsumption.log("Logined successfully"); fuelConsumption.init(); // when login suceed then run init() function } }); });
JS
Result
Source code of example
Close ✕
×
Source code