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 - Commands</title> <script type="text/javascript" src="//code.jquery.com/jquery-latest.min.js"></script> <script type="text/javascript" src="//hst-api.wialon.host/wsdk/script/wialon.js"></script> </head> <body> Select unit: <select id="units"></select> available commands <select id="cmds"></select> <table> <tr> <td>Name</td> <td><input type="text" id="cmd_n"/></td> <td rowspan="2"> <input type="button" value="Create command" id="create_btn"/><br/> <input type="button" value="Update command" id="update_btn"/><br/> <input type="button" value="Delete command" id="delete_btn"/><br/> <input type="button" value="Execute command" id="exec_btn"/> </td> </tr><tr> <td>Command</td> <td> <select id="cmd_c"><option></option> <option value="block_engine">Block engine</option> <option value="unblock_engine">Unblock engine</option> <option value="custom_msg">Custom message</option> <option value="driver_msg">Message to driver</option> <option value="download_msgs">Download messages</option> <option value="query_pos">Locate device</option> <option value="output_on">Activate output</option> <option value="output_off">Deactivate output</option> <option value="send_position">Send position</option> <option value="set_report_interval">Set online report period</option> <option value="upload_cfg">Upload configuration</option> <option value="upload_sw">Upload firmware</option> </select> </td> </tr> </table> <div id="log"></div> </body> </html>
HTML
#log, td{ border: 1px solid #c6c6c6; padding:5px; } table select, table input { width:100%; } #exec_btn{ margin-top:20px; }
CSS
var cur_cmd; // global variable // Print message to log function msg(text) { $("#log").prepend(text + "<br/>"); } function init() { // Execute after login succeed var sess = wialon.core.Session.getInstance(); // get instance of current Session // flags to specify what kind of data should be returned var flags = wialon.item.Item.dataFlag.base | wialon.item.Unit.dataFlag.commands | wialon.item.Unit.dataFlag.commandAliases; sess.loadLibrary("unitCommandDefinitions"); // load Command Definitions Library sess.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 with edit commands access var units = wialon.util.Helper.filterItems( sess.getItems("avl_unit"), wialon.item.Unit.accessFlag.editCmdAliases); if (!units || !units.length){ msg("No units found"); return; } // check if units found for (var i=0; i<units.length; i++) // construct Select list using found units $("#units").append("<option value='"+ units[i].getId() +"'>"+ units[i].getName() +"</option>"); getCmds(); // get available commands for currently selected unit // bind actions to selects $("#units").change( function(){ getCmds(); } ); $("#cmds").change( function(){ getCmdDefinition(); } ); }); } function getCmds(){ // construct commands Select list for selected unit if(!$("#units").val()){ msg("Select unit"); return;} // exit if no unit selected clearForm(); // clear fields $("#cmds").html("<option></option>"); // add first empty element var sess = wialon.core.Session.getInstance(); // get instance of current Session var unit = sess.getItem( $("#units").val() ); // get unit by id var cmds = unit.getCommandDefinitions(); // get unit's commands for(var i in cmds) // construct select list $("#cmds").append("<option value='" + cmds[i].id + "'>" + cmds[i].n + "</option>"); } function clearForm(){ // clear fields function cur_cmd = null; $("#cmd_n").val(""); $("#cmd_c").val(""); $("#exec_btn").prop("disabled",false); } function getCmdDefinition(){ // get and show information about selected Command if(!$("#units").val()){ msg("Select unit"); return; } // exit if no unit selected if(!$("#cmds").val()){ clearForm(); return; } // clear fields if empty element selected var sess = wialon.core.Session.getInstance(); // get instance of current Session var unit = sess.getItem( $("#units").val() ); // get unit by id var cmd = unit.getCommandDefinition( $("#cmds").val() ); // get command by id var can_exec = false, can_access_cmd = false; // variables for access check var cmmds = unit.getCommands(); // get commands if (cmmds && cmmds.length && // if commands available commands exists && (unit.getUserAccess() & wialon.item.Unit.accessFlag.executeCommands)) // user access check can_exec = true; //check user access for selected command if (wialon.util.Number.and(unit.getUserAccess(), cmd.a) == cmd.a) can_access_cmd = true; can_exec = can_exec && can_access_cmd; // result access flag = true only when both flags TRUE if(!can_exec){ // if not enought right msg("Not enought rights to execute <b>"+ cmd.n +"</b> command" ); // display message $("#exec_btn").prop("disabled",true); // disable execute button } cur_cmd = cmd; // save selected command to glabal variable // put command information to corresponding fields $("#cmd_n").val(cmd.n); $("#cmd_c").val(cmd.c); } function createCmd(){ // create Command for selected unit using entered data // get command information from corresponding fields var name = $("#cmd_n").val(); var cmd = $("#cmd_c").val(); // simple validation and exit if invalid if(!name){ msg("Enter command name"); return; } if(!cmd){ msg("Select command"); return; } // construct Comamnd object with defaults and entered data var obj = { n:name, c:cmd, l:"", p:"", a:1 }; var sess = wialon.core.Session.getInstance(); // get instance of current Session // get Unit by id and create Command from obj sess.getItem( $("#units").val() ).createCommandDefinition(obj, function(code, data){ // create command callback if (code) msg(wialon.core.Errors.getErrorText(code)); // print error if error code else { // print message about creation succeed and refresh command list msg("<b>'"+ data.n +"'</b> command created successfully"); getCmds(); } }); } function updateCmd(){ // update selected Command using entered data if(!cur_cmd){ msg("Select command"); return; } // exit if no command selected // get command data from corresponding fields var name = $("#cmd_n").val(); var cmd = $("#cmd_c").val(); // validate command data if(!name){ msg("Enter command name"); return; } if(!cmd){ msg("Select command"); return; } // update command object var obj = cur_cmd; obj.n = name; obj.c = cmd; var sess = wialon.core.Session.getInstance(); // get instance of current Session // get Unit by id and update selected command sess.getItem( $("#units").val() ).updateCommandDefinition(obj, function(code, data){ // update command callback if (code) msg(wialon.core.Errors.getErrorText(code)); // print error if error code else{ // print message about update succeed and refresh command list msg("<b>'"+ data.n +"'</b> command updated successfully "); getCmds(); } }); } function deleteCmd(){ // delete selected Command if(!cur_cmd){ msg("Select command"); return; } // exit if no command selected if( confirm("Delete '" + cur_cmd.n +"' command?") ){ // ask confirmation var sess = wialon.core.Session.getInstance(); // get instance of current Session // get Unit by id and delete selected command sess.getItem( $("#units").val() ).deleteCommandDefinition(cur_cmd.id, function(code){ // delete command callback if (code) msg(wialon.core.Errors.getErrorText(code)); // print error if error code else{ // print message about delete succeed and refresh command list msg("Command deleted successfully "); getCmds(); } }); } } function execCmd(){ // execute selected Command if(!cur_cmd){ msg("Select command"); return; } // exit if no command selected var sess = wialon.core.Session.getInstance(); // get instance of current Session var unit = sess.getItem( $("#units").val() ); // get Unit by id var cmd = unit.getCommandDefinition( $("#cmds").val() ); // get command by id // shedule command to execute after 0 sec unit.remoteCommand(cmd.n, cmd.l, cmd.p, 0, function(code){ // execute command callback if (code) msg(wialon.core.Errors.getErrorText(code)); // print error if error code else msg("Command executed successfully"); // print message about execute succeed }); } // execute when DOM ready $(document).ready(function () { // bind actions to button clicks $("#create_btn").click( createCmd ); $("#update_btn").click( updateCmd ); $("#delete_btn").click( deleteCmd ); $("#exec_btn").click( execCmd ); wialon.core.Session.getInstance().initSession("https://hst-api.wialon.host"); // init session // For more info about how to generate token check // http://sdk.wialon.host/playground/demo/app_auth_token wialon.core.Session.getInstance().loginToken("5dce19710a5e26ab8b7b8986cb3c49e58C291791B7F0A7AEB8AFBFCEED7DC03BC48FF5F8", "", // try to login function (code) { // login callback if (code){ msg(wialon.core.Errors.getErrorText(code)); return; } // exit if error code msg("Logged successfully"); init(); // when login suceed then run init() function }); });
JS
Result
Source code of example
Close ✕
×
Source code