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 - 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 units:<br/><select id="units" multiple="multiple"></select></td> </tr><tr> </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 var flags_units = wialon.item.Item.dataFlag.base | wialon.item.Unit.dataFlag.sensors; sess.loadLibrary("unitSensors"); 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 //tZones($("#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 createNotification(){ //create notification var res = wialon.core.Session.getInstance().getItem($("#res").val()); //get resource by id console.log(res); if(!res){ msg("Unknown resource"); return; }; // check if resource found var un = $("#units").val(); // get selected units ids var name = $("#notif_name").val(); // get Notification name var from = wialon.core.Session.getInstance().getServerTime(); // get server time (activation time) // simple validation and exit if invalid if(!un){ msg("Select units"); return; } // units validation if(!name){ msg("Enter name of notification"); return; } // name validation //params required for sensor controlling, time zone UTC+3("tz":134217728) var obj = {"n":name,"ta": from,"td":0,"tz":134217728,"la":"en","ma":0, "sch":{"f1":0,"f2":0,"t1":0,"t2":0,"m":0,"y":0,"w":0}, "ctrl_sch":{"f1":0,"f2":0,"t1":0,"t2":0,"m":0,"y":0,"w":0}, "un": un, //selected unit, it's possible also to pass massiv "trg":{ "t":"sensor_value", "p": {"sensor_type":"","sensor_name_mask":"*Ves*","lower_bound":4.5,"upper_bound":8.5,"prev_msg_diff":0,"merge":1,"type":0} }, "d":"", "act":[{"t":"message","p":{"name": name,"url":"","color":"","blink":0}}], //online-window trigger "txt":"Any notification text.","fl":0,"mast":0,"mpst":0,"cdt":0,"mmtd":3600,"cp":3600 } 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(""); $("#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("e9c61881b400f40edd4d0766191f4aa960CBA8FBC1A77CA086E3283F1ABC1DFA31B2AD47", "", // 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