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 notification</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> Resource:<select id="res"></select><br/> <table><tr> <td>Select geofences:<br/><select id="zones" multiple="multiple"></select></td> <td>Select units:<br/><select id="units" multiple="multiple"></select></td> </tr><tr> <td>Control type</td> <td> <select id="type"> <option value="0">Entries to a geofence</option> <option value="1">Exits from a geofence</option> </select> </td> </tr><tr> <td>Notification name</td> <td><input type="text" id="notif_name"/></td> </tr></table> <input type="button" id="create_btn" value="Create notification"/> <div id="log"></div> </body> </html>
HTML
#log, td { border: 1px solid #c6c6c6; }
CSS
// 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 //specify what kind of data should be returned (diffrent flags for 'avl_unit' and 'avl_resource') var flags_res = wialon.item.Item.dataFlag.base | wialon.item.Resource.dataFlag.zones; var flags_units = wialon.item.Item.dataFlag.base; sess.loadLibrary("resourceZones"); // load Geofences Library sess.loadLibrary("resourceNotifications"); // load Notification Library sess.updateDataFlags( // load items to current session [{type: "type", data: "avl_resource", flags: flags_res, mode: 0}, // Items (avl_resource) specification {type: "type", data: "avl_unit", flags: flags_units, mode: 0}], // Items (avl_unit) specification function (code) { // updateDataFlags callback if (code) { msg(wialon.core.Errors.getErrorText(code)); return; } // exit if error code // get loaded 'avl_resource's items with edit notification access var res = wialon.util.Helper.filterItems(sess.getItems("avl_resource"), wialon.item.Resource.accessFlag.editNotifications); for (var i = 0; i< res.length; i++) // construct Select list using found resources $("#res").append("<option value='"+ res[i].getId() +"'>"+ res[i].getName() +"</option>"); $("#res").change( function(){ getZones(this.value); } ); // bind acion to select change getZones($("#res").val()); // get geofences for selected resource var units = sess.getItems("avl_unit"); // get loaded 'avl_unit's items 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>"); }); } function getZones( res_id ){ // get geofences by resource id $("#zones").empty(); // clean 'zones' select list var res = wialon.core.Session.getInstance().getItem(res_id); // get resource by id var zones = res.getZones(); // get resource's zones for (var i in zones) // construct Select list using found zones $("#zones").append("<option value='" + zones[i].id + "'>" + zones[i].n + "</option>"); } function createNotification(){ //create notification var res = wialon.core.Session.getInstance().getItem($("#res").val()); //get resource by id if(!res){ msg("Unknown resource"); return; }; // check if resource found var ids = $("#zones").val(); // get selected zones ids var un = $("#units").val(); // get selected units ids var name = $("#notif_name").val(); // get Notification name var type = $("#type").val(); // get Notification type var from = wialon.core.Session.getInstance().getServerTime(); // get server time (activation time) var to = from + 3600*24*7; // calculate deactivation time (activation + 1 week) // simple validation and exit if invalid if(!un){ msg("Select units"); return; } // units validation if(!ids){ msg("Select geofences"); return;} // geozones validation else{ ids = ids.join(); } // convert geozones ids to right format if(!name){ msg("Enter name of notification"); return; } // name validation // construct Notifiacation object using delault and entered data var obj = { ma:0, fl:1, tz:10800, la:"en", act: [{t:"message", p:{}}], // default values sch: { f1:0, f2: 0, t1: 0, t2: 0, m: 0, y: 0, w: 0}, // shedule default value txt: "Test Notification Text", mmtd: 3600, cdt:10, mast:0, mpst:0, cp:3600, // default values n: name, un: un, ta:from, td:to, // set name, units, activation and deactivation time trg: {// Notification trigger t:"geozone", // geofences control p: { geozone_ids: ids, type: type } // trigger parameters ( geozones ids, control type) } }; res.createNotification(obj, // create Notification using created object function(code){ // create Notification callback if(code){ msg(wialon.core.Errors.getErrorText(code)); return;} // exit if error code msg("Notification created successfully"); // print create succeed message // clean notification parameters fields $("#notif_name").val(""); $("#zones").val(""); $("#units").val(""); }); } // execute when DOM ready $(document).ready(function () { $("#create_btn").click( createNotification ); // bind action to button click 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){ 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